Skip to content

fix(drawer): infinite loop when two-way opened binding is toggled mid-animation #8872

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
Dec 13, 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
35 changes: 34 additions & 1 deletion src/lib/sidenav/drawer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@angular/core/testing';
import {Component, ElementRef, ViewChild} from '@angular/core';
import {By} from '@angular/platform-browser';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
import {MatDrawer, MatSidenavModule, MatDrawerContainer} from './index';
import {A11yModule} from '@angular/cdk/a11y';
import {PlatformModule} from '@angular/cdk/platform';
Expand Down Expand Up @@ -138,6 +138,7 @@ describe('MatDrawer', () => {
expect(testComponent.openCount).toBe(0);
expect(testComponent.closeCount).toBe(0);

tick();
fixture.debugElement.query(By.css('.close')).nativeElement.click();
fixture.detectChanges();

Expand Down Expand Up @@ -367,6 +368,37 @@ describe('MatDrawer', () => {

expect(fixture.componentInstance.isOpen).toBe(true);
}));

it('should not throw when a two-way binding is toggled quickly while animating',
fakeAsync(() => {
TestBed
.resetTestingModule()
.configureTestingModule({
imports: [MatSidenavModule, BrowserAnimationsModule],
declarations: [DrawerOpenBinding],
})
.compileComponents();

const fixture = TestBed.createComponent(DrawerOpenBinding);
fixture.detectChanges();

// Note that we need actual timeouts and the `BrowserAnimationsModule`
// in order to test it correctly.
setTimeout(() => {
fixture.componentInstance.isOpen = !fixture.componentInstance.isOpen;
expect(() => fixture.detectChanges()).not.toThrow();

setTimeout(() => {
fixture.componentInstance.isOpen = !fixture.componentInstance.isOpen;
expect(() => fixture.detectChanges()).not.toThrow();
}, 1);

tick(1);
}, 1);

tick(1);
}));

});

describe('focus trapping behavior', () => {
Expand Down Expand Up @@ -496,6 +528,7 @@ describe('MatDrawerContainer', () => {

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

expect(parseInt(contentElement.style.marginLeft)).toBeLessThan(initialMargin);
}));
Expand Down
11 changes: 6 additions & 5 deletions src/lib/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
_animationState: 'open-instant' | 'open' | 'void' = 'void';

/** Event emitted when the drawer open state is changed. */
@Output() openedChange: EventEmitter<boolean> = new EventEmitter<boolean>();
@Output() openedChange: EventEmitter<boolean> =
// Note this has to be async in order to avoid some issues with two-bindings (see #8872).
new EventEmitter<boolean>(/* isAsync */true);

/** Event emitted when the drawer has been opened. */
@Output('opened')
Expand Down Expand Up @@ -390,10 +392,9 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
_onAnimationEnd(event: AnimationEvent) {
const {fromState, toState} = event;

if (toState.indexOf('open') === 0 && fromState === 'void') {
this.openedChange.emit(true);
} else if (toState === 'void' && fromState.indexOf('open') === 0) {
this.openedChange.emit(false);
if ((toState.indexOf('open') === 0 && fromState === 'void') ||
(toState === 'void' && fromState.indexOf('open') === 0)) {
this.openedChange.emit(this._opened);
}
}

Expand Down