Skip to content

Commit f81f5c7

Browse files
crisbetojelbourn
authored andcommitted
fix(drawer): margins not being updated on direction changes (angular#9161)
Fixes the `mat-drawer-container` margins not being updated when its directionality has changed. Fixes angular#9158.
1 parent 1a92bca commit f81f5c7

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/lib/sidenav/drawer.spec.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {Component, ElementRef, ViewChild} from '@angular/core';
1010
import {By} from '@angular/platform-browser';
1111
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
1212
import {MatDrawer, MatSidenavModule, MatDrawerContainer} from './index';
13+
import {Direction} from '@angular/cdk/bidi';
1314
import {A11yModule} from '@angular/cdk/a11y';
1415
import {PlatformModule} from '@angular/cdk/platform';
1516
import {ESCAPE} from '@angular/cdk/keycodes';
@@ -573,6 +574,27 @@ describe('MatDrawerContainer', () => {
573574
expect(parseInt(contentElement.style.marginLeft)).toBeLessThan(initialMargin);
574575
}));
575576

577+
it('should recalculate the margin if the direction has changed', fakeAsync(() => {
578+
const fixture = TestBed.createComponent(DrawerContainerStateChangesTestApp);
579+
580+
fixture.detectChanges();
581+
fixture.componentInstance.drawer.open();
582+
fixture.detectChanges();
583+
tick();
584+
fixture.detectChanges();
585+
586+
const contentElement = fixture.debugElement.nativeElement.querySelector('.mat-drawer-content');
587+
const margin = parseInt(contentElement.style.marginLeft);
588+
589+
expect(margin).toBeGreaterThan(0);
590+
591+
fixture.componentInstance.direction = 'rtl';
592+
fixture.detectChanges();
593+
594+
expect(parseInt(contentElement.style.marginLeft)).toBe(0);
595+
expect(parseInt(contentElement.style.marginRight)).toBe(margin);
596+
}));
597+
576598
it('should not animate when the sidenav is open on load ', fakeAsync(() => {
577599
const fixture = TestBed.createComponent(DrawerSetToOpenedTrue);
578600

@@ -760,14 +782,15 @@ class DrawerDelayed {
760782

761783
@Component({
762784
template: `
763-
<mat-drawer-container>
785+
<mat-drawer-container [dir]="direction">
764786
<mat-drawer *ngIf="renderDrawer" [mode]="mode" style="width:100px"></mat-drawer>
765787
</mat-drawer-container>`,
766788
})
767789
class DrawerContainerStateChangesTestApp {
768790
@ViewChild(MatDrawer) drawer: MatDrawer;
769791
@ViewChild(MatDrawerContainer) drawerContainer: MatDrawerContainer;
770792

793+
direction: Direction = 'ltr';
771794
mode = 'side';
772795
renderDrawer = true;
773796
}

src/lib/sidenav/drawer.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -487,10 +487,14 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
487487
private _ngZone: NgZone,
488488
private _changeDetectorRef: ChangeDetectorRef,
489489
@Inject(MAT_DRAWER_DEFAULT_AUTOSIZE) defaultAutosize = false) {
490-
// If a `Dir` directive exists up the tree, listen direction changes and update the left/right
491-
// properties to point to the proper start/end.
492-
if (_dir != null) {
493-
_dir.change.pipe(takeUntil(this._destroyed)).subscribe(() => this._validateDrawers());
490+
491+
// If a `Dir` directive exists up the tree, listen direction changes
492+
// and update the left/right properties to point to the proper start/end.
493+
if (_dir) {
494+
_dir.change.pipe(takeUntil(this._destroyed)).subscribe(() => {
495+
this._validateDrawers();
496+
this._updateContentMargins();
497+
});
494498
}
495499

496500
this._autosize = defaultAutosize;
@@ -631,7 +635,7 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
631635
this._right = this._left = null;
632636

633637
// Detect if we're LTR or RTL.
634-
if (this._dir == null || this._dir.value == 'ltr') {
638+
if (!this._dir || this._dir.value == 'ltr') {
635639
this._left = this._start;
636640
this._right = this._end;
637641
} else {

0 commit comments

Comments
 (0)