diff --git a/src/lib/sidenav/drawer.spec.ts b/src/lib/sidenav/drawer.spec.ts index 70cf24896b79..b84afe17ed25 100644 --- a/src/lib/sidenav/drawer.spec.ts +++ b/src/lib/sidenav/drawer.spec.ts @@ -10,6 +10,7 @@ import {Component, ElementRef, ViewChild} from '@angular/core'; import {By} from '@angular/platform-browser'; import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations'; import {MatDrawer, MatSidenavModule, MatDrawerContainer} from './index'; +import {Direction} from '@angular/cdk/bidi'; import {A11yModule} from '@angular/cdk/a11y'; import {PlatformModule} from '@angular/cdk/platform'; import {ESCAPE} from '@angular/cdk/keycodes'; @@ -573,6 +574,27 @@ describe('MatDrawerContainer', () => { expect(parseInt(contentElement.style.marginLeft)).toBeLessThan(initialMargin); })); + it('should recalculate the margin if the direction has changed', fakeAsync(() => { + const fixture = TestBed.createComponent(DrawerContainerStateChangesTestApp); + + fixture.detectChanges(); + fixture.componentInstance.drawer.open(); + fixture.detectChanges(); + tick(); + fixture.detectChanges(); + + const contentElement = fixture.debugElement.nativeElement.querySelector('.mat-drawer-content'); + const margin = parseInt(contentElement.style.marginLeft); + + expect(margin).toBeGreaterThan(0); + + fixture.componentInstance.direction = 'rtl'; + fixture.detectChanges(); + + expect(parseInt(contentElement.style.marginLeft)).toBe(0); + expect(parseInt(contentElement.style.marginRight)).toBe(margin); + })); + it('should not animate when the sidenav is open on load ', fakeAsync(() => { const fixture = TestBed.createComponent(DrawerSetToOpenedTrue); @@ -760,7 +782,7 @@ class DrawerDelayed { @Component({ template: ` - + `, }) @@ -768,6 +790,7 @@ class DrawerContainerStateChangesTestApp { @ViewChild(MatDrawer) drawer: MatDrawer; @ViewChild(MatDrawerContainer) drawerContainer: MatDrawerContainer; + direction: Direction = 'ltr'; mode = 'side'; renderDrawer = true; } diff --git a/src/lib/sidenav/drawer.ts b/src/lib/sidenav/drawer.ts index 750456315103..8926b3ea3d78 100644 --- a/src/lib/sidenav/drawer.ts +++ b/src/lib/sidenav/drawer.ts @@ -487,10 +487,14 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy { private _ngZone: NgZone, private _changeDetectorRef: ChangeDetectorRef, @Inject(MAT_DRAWER_DEFAULT_AUTOSIZE) defaultAutosize = false) { - // If a `Dir` directive exists up the tree, listen direction changes and update the left/right - // properties to point to the proper start/end. - if (_dir != null) { - _dir.change.pipe(takeUntil(this._destroyed)).subscribe(() => this._validateDrawers()); + + // If a `Dir` directive exists up the tree, listen direction changes + // and update the left/right properties to point to the proper start/end. + if (_dir) { + _dir.change.pipe(takeUntil(this._destroyed)).subscribe(() => { + this._validateDrawers(); + this._updateContentMargins(); + }); } this._autosize = defaultAutosize; @@ -631,7 +635,7 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy { this._right = this._left = null; // Detect if we're LTR or RTL. - if (this._dir == null || this._dir.value == 'ltr') { + if (!this._dir || this._dir.value == 'ltr') { this._left = this._start; this._right = this._end; } else {