Skip to content

Commit 883e184

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

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 {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

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

484488
this._autosize = defaultAutosize;
@@ -619,7 +623,7 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
619623
this._right = this._left = null;
620624

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

0 commit comments

Comments
 (0)