Skip to content

refactor(overlay): use key event dispatcher for CdkConnectedOverlay #8531

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
3 changes: 1 addition & 2 deletions src/cdk/overlay/overlay-directives.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('Overlay directives', () => {
fixture.componentInstance.isOpen = true;
fixture.detectChanges();

dispatchKeyboardEvent(document, 'keydown', ESCAPE);
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
fixture.detectChanges();

expect(overlayContainerElement.textContent!.trim()).toBe('',
Expand Down Expand Up @@ -342,4 +342,3 @@ class ConnectedOverlayPropertyInitOrder {
@ViewChild(CdkConnectedOverlay) connectedOverlayDirective: CdkConnectedOverlay;
@ViewChild('trigger') trigger: CdkOverlayOrigin;
}

20 changes: 7 additions & 13 deletions src/cdk/overlay/overlay-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
} from './position/connected-position';
import {ConnectedPositionStrategy} from './position/connected-position-strategy';
import {RepositionScrollStrategy, ScrollStrategy} from './scroll/index';
import {DOCUMENT} from '@angular/common';


/** Default set of positions for the overlay. Follows the behavior of a dropdown. */
Expand Down Expand Up @@ -238,8 +237,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
templateRef: TemplateRef<any>,
viewContainerRef: ViewContainerRef,
@Inject(CDK_CONNECTED_OVERLAY_SCROLL_STRATEGY) private _scrollStrategy,
@Optional() private _dir: Directionality,
@Optional() @Inject(DOCUMENT) private _document: any) {
@Optional() private _dir: Directionality) {
this._templatePortal = new TemplatePortal(templateRef, viewContainerRef);
}

Expand Down Expand Up @@ -336,11 +334,16 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
private _attachOverlay() {
if (!this._overlayRef) {
this._createOverlay();

this._overlayRef!.keydownEvents().subscribe((event: KeyboardEvent) => {
if (event.keyCode === ESCAPE) {
this._detachOverlay();
}
});
}

this._position.withDirection(this.dir);
this._overlayRef.setDirection(this.dir);
this._document.addEventListener('keydown', this._escapeListener);

if (!this._overlayRef.hasAttached()) {
this._overlayRef.attach(this._templatePortal);
Expand All @@ -362,7 +365,6 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
}

this._backdropSubscription.unsubscribe();
this._document.removeEventListener('keydown', this._escapeListener);
}

/** Destroys the overlay created by this directive. */
Expand All @@ -373,13 +375,5 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {

this._backdropSubscription.unsubscribe();
this._positionSubscription.unsubscribe();
this._document.removeEventListener('keydown', this._escapeListener);
}

/** Event listener that will close the overlay when the user presses escape. */
private _escapeListener = (event: KeyboardEvent) => {
if (event.keyCode === ESCAPE) {
this._detachOverlay();
}
}
}