Skip to content

Commit f64a857

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 255f9d8 commit f64a857

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/lib/sidenav/drawer.spec.ts

Lines changed: 24 additions & 1 deletion
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 {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';
@@ -520,6 +521,27 @@ describe('MatDrawerContainer', () => {
520521
expect(parseInt(contentElement.style.marginLeft)).toBeLessThan(initialMargin);
521522
}));
522523

524+
it('should recalculate the margin if the direction has changed', fakeAsync(() => {
525+
const fixture = TestBed.createComponent(DrawerContainerStateChangesTestApp);
526+
527+
fixture.detectChanges();
528+
fixture.componentInstance.drawer.open();
529+
fixture.detectChanges();
530+
tick();
531+
fixture.detectChanges();
532+
533+
const contentElement = fixture.debugElement.nativeElement.querySelector('.mat-drawer-content');
534+
const margin = parseInt(contentElement.style.marginLeft);
535+
536+
expect(margin).toBeGreaterThan(0);
537+
538+
fixture.componentInstance.direction = 'rtl';
539+
fixture.detectChanges();
540+
541+
expect(parseInt(contentElement.style.marginLeft)).toBe(0);
542+
expect(parseInt(contentElement.style.marginRight)).toBe(margin);
543+
}));
544+
523545
it('should not animate when the sidenav is open on load ', fakeAsync(() => {
524546
const fixture = TestBed.createComponent(DrawerSetToOpenedTrue);
525547

@@ -696,14 +718,15 @@ class DrawerDelayed {
696718

697719
@Component({
698720
template: `
699-
<mat-drawer-container>
721+
<mat-drawer-container [dir]="direction">
700722
<mat-drawer *ngIf="renderDrawer" [mode]="mode" style="width:100px"></mat-drawer>
701723
</mat-drawer-container>`,
702724
})
703725
class DrawerContainerStateChangesTestApp {
704726
@ViewChild(MatDrawer) drawer: MatDrawer;
705727
@ViewChild(MatDrawerContainer) drawerContainer: MatDrawerContainer;
706728

729+
direction: Direction = 'ltr';
707730
mode = 'side';
708731
renderDrawer = true;
709732
}

src/lib/sidenav/drawer.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,14 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
474474
private _ngZone: NgZone,
475475
private _changeDetectorRef: ChangeDetectorRef,
476476
@Inject(MAT_DRAWER_DEFAULT_AUTOSIZE) defaultAutosize = false) {
477-
// If a `Dir` directive exists up the tree, listen direction changes and update the left/right
478-
// properties to point to the proper start/end.
479-
if (_dir != null) {
480-
_dir.change.pipe(takeUntil(this._destroyed)).subscribe(() => this._validateDrawers());
477+
478+
// If a `Dir` directive exists up the tree, listen direction changes
479+
// and update the left/right properties to point to the proper start/end.
480+
if (_dir) {
481+
_dir.change.pipe(takeUntil(this._destroyed)).subscribe(() => {
482+
this._validateDrawers();
483+
this._updateContentMargins();
484+
});
481485
}
482486

483487
this._autosize = defaultAutosize;
@@ -618,7 +622,7 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
618622
this._right = this._left = null;
619623

620624
// Detect if we're LTR or RTL.
621-
if (this._dir == null || this._dir.value == 'ltr') {
625+
if (!this._dir || this._dir.value == 'ltr') {
622626
this._left = this._start;
623627
this._right = this._end;
624628
} else {

0 commit comments

Comments
 (0)