Skip to content

fix(drawer): drawer container not reacting to drawer removal #7060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion src/lib/sidenav/drawer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,11 @@ describe('MatDrawerContainer', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MatSidenavModule, A11yModule, PlatformModule, NoopAnimationsModule],
declarations: [DrawerContainerTwoDrawerTestApp, DrawerDelayed],
declarations: [
DrawerContainerTwoDrawerTestApp,
DrawerDelayed,
DrawerContainerStateChangesTestApp,
],
});

TestBed.compileComponents();
Expand Down Expand Up @@ -363,6 +367,47 @@ describe('MatDrawerContainer', () => {

expect(parseInt(contentElement.style.marginLeft)).toBeGreaterThan(0);
}));

it('should recalculate the margin if a drawer is destroyed', 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 initialMargin = parseInt(contentElement.style.marginLeft);

expect(initialMargin).toBeGreaterThan(0);

fixture.componentInstance.renderDrawer = false;
fixture.detectChanges();

expect(parseInt(contentElement.style.marginLeft)).toBeLessThan(initialMargin);
}));

it('should recalculate the margin if the drawer mode is 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 initialMargin = parseInt(contentElement.style.marginLeft);

expect(initialMargin).toBeGreaterThan(0);

fixture.componentInstance.mode = 'over';
fixture.detectChanges();

expect(parseInt(contentElement.style.marginLeft)).toBeLessThan(initialMargin);
}));

});


Expand Down Expand Up @@ -474,3 +519,19 @@ class DrawerDelayed {
@ViewChild(MatDrawer) drawer: MatDrawer;
showDrawer = false;
}


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

mode = 'side';
renderDrawer = true;
}

27 changes: 19 additions & 8 deletions src/lib/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
private _left: MatDrawer | null;
private _right: MatDrawer | null;

/** Subscription to the Directionality change EventEmitter. */
private _dirChangeSubscription = Subscription.EMPTY;
/** Emits when the component is destroyed. */
private _destroyed = new Subject<void>();

_contentMargins = new Subject<{left: number, right: number}>();

Expand All @@ -409,23 +409,33 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
// 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) {
this._dirChangeSubscription = _dir.change.subscribe(() => this._validateDrawers());
takeUntil.call(_dir.change, this._destroyed).subscribe(() => this._validateDrawers());
}
}

ngAfterContentInit() {
startWith.call(this._drawers.changes, null).subscribe(() => {
this._validateDrawers();

this._drawers.forEach((drawer: MatDrawer) => {
this._watchDrawerToggle(drawer);
this._watchDrawerPosition(drawer);
this._watchDrawerMode(drawer);
});

if (!this._drawers.length ||
this._isDrawerOpen(this._start) ||
this._isDrawerOpen(this._end)) {
this._updateContentMargins();
}

this._changeDetectorRef.markForCheck();
});
}

ngOnDestroy() {
this._dirChangeSubscription.unsubscribe();
this._destroyed.next();
this._destroyed.complete();
}

/** Calls `open` of both start and end drawers */
Expand Down Expand Up @@ -478,10 +488,11 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
/** Subscribes to changes in drawer mode so we can run change detection. */
private _watchDrawerMode(drawer: MatDrawer): void {
if (drawer) {
takeUntil.call(drawer._modeChanged, this._drawers.changes).subscribe(() => {
this._updateContentMargins();
this._changeDetectorRef.markForCheck();
});
takeUntil.call(drawer._modeChanged, merge(this._drawers.changes, this._destroyed))
.subscribe(() => {
this._updateContentMargins();
this._changeDetectorRef.markForCheck();
});
}
}

Expand Down