From f56feefd5f61c27af549331a0477c04d6c557ab3 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Tue, 1 Jun 2021 17:53:51 +0200 Subject: [PATCH] fix(material/menu): not interrupting keyboard events to other overlays For historical reasons, `mat-menu` doesn't use the same keyboard event dispatcher as the other overlays. To work around it, previously we added a dummy subscription so that the menu would still show up in the overlay keyboard stack. This works for most events, but it breaks down for the escape key, because closing the menu removes it from the stack immediately, allowing the event to bubble up to the document and be dispatched to the next overlay in the stack. These changes resolve the issue by adding a `stopPropagation` call. Fixes #22694. --- src/material-experimental/mdc-menu/menu.spec.ts | 2 ++ src/material/menu/menu.spec.ts | 2 ++ src/material/menu/menu.ts | 5 +++++ 3 files changed, 9 insertions(+) diff --git a/src/material-experimental/mdc-menu/menu.spec.ts b/src/material-experimental/mdc-menu/menu.spec.ts index ac003751e194..5673468bcafd 100644 --- a/src/material-experimental/mdc-menu/menu.spec.ts +++ b/src/material-experimental/mdc-menu/menu.spec.ts @@ -437,12 +437,14 @@ describe('MDC-based MatMenu', () => { const panel = overlayContainerElement.querySelector('.mat-mdc-menu-panel')!; const event = createKeyboardEvent('keydown', ESCAPE); + spyOn(event, 'stopPropagation').and.callThrough(); dispatchEvent(panel, event); fixture.detectChanges(); tick(500); expect(overlayContainerElement.textContent).toBe(''); expect(event.defaultPrevented).toBe(true); + expect(event.stopPropagation).toHaveBeenCalled(); })); it('should not close the menu when pressing ESCAPE with a modifier', fakeAsync(() => { diff --git a/src/material/menu/menu.spec.ts b/src/material/menu/menu.spec.ts index b368ccbcb984..21c77c9aa6cb 100644 --- a/src/material/menu/menu.spec.ts +++ b/src/material/menu/menu.spec.ts @@ -436,6 +436,7 @@ describe('MatMenu', () => { const panel = overlayContainerElement.querySelector('.mat-menu-panel')!; const event = createKeyboardEvent('keydown', ESCAPE); + spyOn(event, 'stopPropagation').and.callThrough(); dispatchEvent(panel, event); fixture.detectChanges(); @@ -443,6 +444,7 @@ describe('MatMenu', () => { expect(overlayContainerElement.textContent).toBe(''); expect(event.defaultPrevented).toBe(true); + expect(event.stopPropagation).toHaveBeenCalled(); })); it('should not close the menu when pressing ESCAPE with a modifier', fakeAsync(() => { diff --git a/src/material/menu/menu.ts b/src/material/menu/menu.ts index bacd6b63f634..e9966aba4656 100644 --- a/src/material/menu/menu.ts +++ b/src/material/menu/menu.ts @@ -337,7 +337,12 @@ export class _MatMenuBase implements AfterContentInit, MatMenuPanel } manager.onKeydown(event); + return; } + + // Don't allow the event to propagate if we've already handled it, or it may + // end up reaching other overlays that were opened earlier (see #22694). + event.stopPropagation(); } /**