Skip to content

fix(drawer): drawer running inside the zone keydown event. #10092

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
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
28 changes: 16 additions & 12 deletions src/lib/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {startWith} from 'rxjs/operators/startWith';
import {takeUntil} from 'rxjs/operators/takeUntil';
import {debounceTime} from 'rxjs/operators/debounceTime';
import {map} from 'rxjs/operators/map';
import {fromEvent} from 'rxjs/observable/fromEvent';
import {Subject} from 'rxjs/Subject';
import {Observable} from 'rxjs/Observable';
import {matDrawerAnimations} from './drawer-animations';
Expand Down Expand Up @@ -119,7 +120,6 @@ export class MatDrawerContent implements AfterContentInit {
'[@transform]': '_animationState',
'(@transform.start)': '_onAnimationStart($event)',
'(@transform.done)': '_onAnimationEnd($event)',
'(keydown)': 'handleKeydown($event)',
// must prevent the browser from aligning text based on value
'[attr.align]': 'null',
'[class.mat-drawer-end]': 'position === "end"',
Expand Down Expand Up @@ -258,6 +258,7 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
private _focusTrapFactory: FocusTrapFactory,
private _focusMonitor: FocusMonitor,
private _platform: Platform,
private _ngZone: NgZone,
@Optional() @Inject(DOCUMENT) private _doc: any) {

this.openedChange.subscribe((opened: boolean) => {
Expand All @@ -273,6 +274,20 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
this._restoreFocus();
}
});

/**
* Listen to `keydown` events outside the zone so that change detection is not run every
* time a key is pressed. Instead we re-enter the zone only if the `ESC` key is pressed
* and we don't have close disabled.
*/
this._ngZone.runOutsideAngular(() => {
fromEvent(this._elementRef.nativeElement, 'keydown').pipe(
filter((event: KeyboardEvent) => event.keyCode === ESCAPE && !this.disableClose)
).subscribe((event) => this._ngZone.run(() => {
this.close();
event.stopPropagation();
}));
});
}

/** Traps focus inside the drawer. */
Expand Down Expand Up @@ -382,17 +397,6 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
});
}

/**
* Handles the keyboard events.
* @docs-private
*/
handleKeydown(event: KeyboardEvent): void {
if (event.keyCode === ESCAPE && !this.disableClose) {
this.close();
event.stopPropagation();
}
}

_onAnimationStart(event: AnimationEvent) {
this._animationStarted.emit(event);
}
Expand Down
1 change: 0 additions & 1 deletion src/lib/sidenav/sidenav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export class MatSidenavContent extends MatDrawerContent {
'[@transform]': '_animationState',
'(@transform.start)': '_onAnimationStart($event)',
'(@transform.done)': '_onAnimationEnd($event)',
'(keydown)': 'handleKeydown($event)',
// must prevent the browser from aligning text based on value
'[attr.align]': 'null',
'[class.mat-drawer-end]': 'position === "end"',
Expand Down