Skip to content

Commit

Permalink
fix(overlay): remove global keydown listener when there are no open o…
Browse files Browse the repository at this point in the history
…verlays (#8389)

Removes the global `keydown` listener from the overlay dispatcher once all overlays are closed.
  • Loading branch information
crisbeto authored and jelbourn committed Nov 20, 2017
1 parent 2bc0b41 commit 131272a
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/cdk/overlay/keyboard/overlay-keyboard-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ export class OverlayKeyboardDispatcher implements OnDestroy {
constructor(@Inject(DOCUMENT) private _document: any) {}

ngOnDestroy() {
if (this._keydownEventSubscription) {
this._keydownEventSubscription.unsubscribe();
this._keydownEventSubscription = null;
}
this._unsubscribeFromKeydownEvents();
}

/** Add a new overlay to the list of attached overlay refs. */
Expand All @@ -48,9 +45,15 @@ export class OverlayKeyboardDispatcher implements OnDestroy {
/** Remove an overlay from the list of attached overlay refs. */
remove(overlayRef: OverlayRef): void {
const index = this._attachedOverlays.indexOf(overlayRef);

if (index > -1) {
this._attachedOverlays.splice(index, 1);
}

// Remove the global listener once there are no more overlays.
if (this._attachedOverlays.length === 0) {
this._unsubscribeFromKeydownEvents();
}
}

/**
Expand All @@ -68,6 +71,14 @@ export class OverlayKeyboardDispatcher implements OnDestroy {
});
}

/** Removes the global keydown subscription. */
private _unsubscribeFromKeydownEvents(): void {
if (this._keydownEventSubscription) {
this._keydownEventSubscription.unsubscribe();
this._keydownEventSubscription = null;
}
}

/** Select the appropriate overlay from a keydown event. */
private _selectOverlayFromEvent(event: KeyboardEvent): OverlayRef {
// Check if any overlays contain the event
Expand Down

0 comments on commit 131272a

Please sign in to comment.