Skip to content

fix(overlay): always dispatch keyboard events to top overlay in OverlayKeyboardDispatcher #10807

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
22 changes: 0 additions & 22 deletions src/cdk/overlay/keyboard/overlay-keyboard-dispatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,6 @@ describe('OverlayKeyboardDispatcher', () => {
button.parentNode!.removeChild(button);
});

it('should dispatch targeted keyboard events to the overlay containing that target', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a test case with multiple open overlays?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one called should dispatch body keyboard events to the most recently attached overlay a bit above.

const overlayOne = overlay.create();
const overlayTwo = overlay.create();
const overlayOneSpy = jasmine.createSpy('overlayOne keyboard event spy');
const overlayTwoSpy = jasmine.createSpy('overlayOne keyboard event spy');

overlayOne.keydownEvents().subscribe(overlayOneSpy);
overlayTwo.keydownEvents().subscribe(overlayTwoSpy);

// Attach overlays
keyboardDispatcher.add(overlayOne);
keyboardDispatcher.add(overlayTwo);

const overlayOnePane = overlayOne.overlayElement;

dispatchKeyboardEvent(document.body, 'keydown', ESCAPE, overlayOnePane);

// Targeted overlay should receive event
expect(overlayOneSpy).toHaveBeenCalled();
expect(overlayTwoSpy).not.toHaveBeenCalled();
});

it('should complete the keydown stream on dispose', () => {
const overlayRef = overlay.create();
const completeSpy = jasmine.createSpy('keydown complete spy');
Expand Down
18 changes: 4 additions & 14 deletions src/cdk/overlay/keyboard/overlay-keyboard-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,6 @@ export class OverlayKeyboardDispatcher implements OnDestroy {
}
}

/** Select the appropriate overlay from a keydown event. */
private _selectOverlayFromEvent(event: KeyboardEvent): OverlayRef {
// Check if any overlays contain the event
const targetedOverlay = this._attachedOverlays.find(overlay => {
return overlay.overlayElement === event.target ||
overlay.overlayElement.contains(event.target as HTMLElement);
});

// Use the overlay if it exists, otherwise choose the most recently attached one
return targetedOverlay || this._attachedOverlays[this._attachedOverlays.length - 1];
}

/** Detaches the global keyboard event listener. */
private _detach() {
if (this._isAttached) {
Expand All @@ -88,8 +76,10 @@ export class OverlayKeyboardDispatcher implements OnDestroy {
/** Keyboard event listener that will be attached to the body. */
private _keydownListener = (event: KeyboardEvent) => {
if (this._attachedOverlays.length) {
// Dispatch keydown event to the correct overlay.
this._selectOverlayFromEvent(event)._keydownEvents.next(event);
// Dispatch the keydown event to the top overlay. We want to target the most recent overlay,
// rather than trying to match where the event came from, because some components might open
// an overlay, but keep focus on a trigger element (e.g. for select and autocomplete).
this._attachedOverlays[this._attachedOverlays.length - 1]._keydownEvents.next(event);
}
}
}
Expand Down