Skip to content

Commit a1b5e75

Browse files
crisbetojelbourn
authored andcommitted
refactor(overlay): use key event dispatcher for CdkConnectedOverlay (#8531)
Switched the `CdkConnectedOverlay` directive to use the key event dispatcher rather than listening to events on the document.
1 parent c7b1e27 commit a1b5e75

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

src/cdk/overlay/overlay-directives.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe('Overlay directives', () => {
110110
fixture.componentInstance.isOpen = true;
111111
fixture.detectChanges();
112112

113-
dispatchKeyboardEvent(document, 'keydown', ESCAPE);
113+
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
114114
fixture.detectChanges();
115115

116116
expect(overlayContainerElement.textContent!.trim()).toBe('',
@@ -342,4 +342,3 @@ class ConnectedOverlayPropertyInitOrder {
342342
@ViewChild(CdkConnectedOverlay) connectedOverlayDirective: CdkConnectedOverlay;
343343
@ViewChild('trigger') trigger: CdkOverlayOrigin;
344344
}
345-

src/cdk/overlay/overlay-directives.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import {
3535
} from './position/connected-position';
3636
import {ConnectedPositionStrategy} from './position/connected-position-strategy';
3737
import {RepositionScrollStrategy, ScrollStrategy} from './scroll/index';
38-
import {DOCUMENT} from '@angular/common';
3938

4039

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

@@ -336,11 +334,16 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
336334
private _attachOverlay() {
337335
if (!this._overlayRef) {
338336
this._createOverlay();
337+
338+
this._overlayRef!.keydownEvents().subscribe((event: KeyboardEvent) => {
339+
if (event.keyCode === ESCAPE) {
340+
this._detachOverlay();
341+
}
342+
});
339343
}
340344

341345
this._position.withDirection(this.dir);
342346
this._overlayRef.setDirection(this.dir);
343-
this._document.addEventListener('keydown', this._escapeListener);
344347

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

364367
this._backdropSubscription.unsubscribe();
365-
this._document.removeEventListener('keydown', this._escapeListener);
366368
}
367369

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

374376
this._backdropSubscription.unsubscribe();
375377
this._positionSubscription.unsubscribe();
376-
this._document.removeEventListener('keydown', this._escapeListener);
377-
}
378-
379-
/** Event listener that will close the overlay when the user presses escape. */
380-
private _escapeListener = (event: KeyboardEvent) => {
381-
if (event.keyCode === ESCAPE) {
382-
this._detachOverlay();
383-
}
384378
}
385379
}

0 commit comments

Comments
 (0)