Skip to content

Commit

Permalink
fix(drawer): drawer running inside the zone keydown event. (angular#…
Browse files Browse the repository at this point in the history
…10092)

Currently the drawer is listening to the event 'keydown' inside the zone. This makes the change detector be fired every time while whe are pressing 'any' key.
  • Loading branch information
llorenspujol authored and tinayuangao committed Mar 1, 2018
1 parent b4b76bd commit 981c9a9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
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

0 comments on commit 981c9a9

Please sign in to comment.