Skip to content

Commit b9c6ed0

Browse files
committed
feat(overlay): expose keydown events on the opened overlay
Since the consumer doesn't have direct access to the overlay that is being opened, there's no way for them to listen for keyboard events. These changes expose the `OverlayRef.keydownEvents` via an output.
1 parent ee2732a commit b9c6ed0

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {By} from '@angular/platform-browser';
33
import {ComponentFixture, TestBed, async, inject} from '@angular/core/testing';
44
import {Directionality} from '@angular/cdk/bidi';
55
import {dispatchKeyboardEvent} from '@angular/cdk/testing';
6-
import {ESCAPE} from '@angular/cdk/keycodes';
6+
import {ESCAPE, A} from '@angular/cdk/keycodes';
77
import {CdkConnectedOverlay, OverlayModule, CdkOverlayOrigin} from './index';
88
import {OverlayContainer} from './overlay-container';
99
import {
@@ -447,6 +447,18 @@ describe('Overlay directives', () => {
447447
expect(fixture.componentInstance.detachHandler).toHaveBeenCalled();
448448
});
449449

450+
it('should emit the keydown events from the overlay', () => {
451+
expect(fixture.componentInstance.keydownHandler).not.toHaveBeenCalled();
452+
453+
fixture.componentInstance.isOpen = true;
454+
fixture.detectChanges();
455+
456+
const event = dispatchKeyboardEvent(document.body, 'keydown', A);
457+
fixture.detectChanges();
458+
459+
expect(fixture.componentInstance.keydownHandler).toHaveBeenCalledWith(event);
460+
});
461+
450462
});
451463

452464
});
@@ -474,6 +486,7 @@ describe('Overlay directives', () => {
474486
(positionChange)="positionChangeHandler($event)"
475487
(attach)="attachHandler()"
476488
(detach)="detachHandler()"
489+
(keydownEvents)="keydownHandler($event)"
477490
[cdkConnectedOverlayMinWidth]="minWidth"
478491
[cdkConnectedOverlayMinHeight]="minHeight"
479492
[cdkConnectedOverlayPositions]="positionOverrides">
@@ -499,7 +512,8 @@ class ConnectedOverlayDirectiveTest {
499512
growAfterOpen: boolean;
500513
push: boolean;
501514
backdropClickHandler = jasmine.createSpy('backdropClick handler');
502-
positionChangeHandler = jasmine.createSpy('positionChangeHandler');
515+
positionChangeHandler = jasmine.createSpy('positionChange handler');
516+
keydownHandler = jasmine.createSpy('keydown handler');
503517
positionOverrides: ConnectionPositionPair[];
504518
attachHandler = jasmine.createSpy('attachHandler').and.callFake(() => {
505519
this.attachResult =

src/cdk/overlay/overlay-directives.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
205205
/** Event emitted when the overlay has been detached. */
206206
@Output() detach = new EventEmitter<void>();
207207

208+
/** Emits when there are keyboard events that are targeted at the overlay. */
209+
@Output() keydownEvents = new EventEmitter<KeyboardEvent>();
210+
208211
// TODO(jelbourn): inputs for size, scroll behavior, animation, etc.
209212

210213
constructor(
@@ -335,6 +338,8 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
335338
this._createOverlay();
336339

337340
this._overlayRef!.keydownEvents().subscribe((event: KeyboardEvent) => {
341+
this.keydownEvents.next(event);
342+
338343
if (event.keyCode === ESCAPE) {
339344
this._detachOverlay();
340345
}

0 commit comments

Comments
 (0)