Skip to content

Commit

Permalink
fix(drawer): margins not being updated on direction changes (#9161)
Browse files Browse the repository at this point in the history
Fixes the `mat-drawer-container` margins not being updated when its directionality has changed.

Fixes #9158.
  • Loading branch information
crisbeto authored and jelbourn committed Jan 8, 2018
1 parent 255f9d8 commit f64a857
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
25 changes: 24 additions & 1 deletion src/lib/sidenav/drawer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {Component, ElementRef, ViewChild} from '@angular/core';
import {By} from '@angular/platform-browser';
import {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';
Expand Down Expand Up @@ -520,6 +521,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);

Expand Down Expand Up @@ -696,14 +718,15 @@ class DrawerDelayed {

@Component({
template: `
<mat-drawer-container>
<mat-drawer-container [dir]="direction">
<mat-drawer *ngIf="renderDrawer" [mode]="mode" style="width:100px"></mat-drawer>
</mat-drawer-container>`,
})
class DrawerContainerStateChangesTestApp {
@ViewChild(MatDrawer) drawer: MatDrawer;
@ViewChild(MatDrawerContainer) drawerContainer: MatDrawerContainer;

direction: Direction = 'ltr';
mode = 'side';
renderDrawer = true;
}
Expand Down
14 changes: 9 additions & 5 deletions src/lib/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,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;
Expand Down Expand Up @@ -618,7 +622,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 {
Expand Down

0 comments on commit f64a857

Please sign in to comment.