Skip to content
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

fix(overlay): remove global keydown listener when there are no open overlays #8389

Merged
merged 1 commit into from
Nov 20, 2017
Merged
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
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 @@ -26,10 +26,7 @@ export class OverlayKeyboardDispatcher implements OnDestroy {
private _keydownEventSubscription: Subscription | null;

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 @@ -45,9 +42,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 @@ -65,6 +68,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