Skip to content

Commit ead7cfc

Browse files
committed
Revert "fix(material/menu): handle keyboard events through dispatcher (#29997)"
This reverts commit 42f6a4a.
1 parent 3c77ae3 commit ead7cfc

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

src/material/menu/menu-trigger.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,11 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
407407
config.positionStrategy as FlexibleConnectedPositionStrategy,
408408
);
409409
this._overlayRef = this._overlay.create(config);
410-
this._overlayRef.keydownEvents().subscribe(event => {
411-
if (this.menu instanceof MatMenu) {
412-
this.menu._handleKeydown(event);
413-
}
414-
});
410+
411+
// Consume the `keydownEvents` in order to prevent them from going to another overlay.
412+
// Ideally we'd also have our keyboard event logic in here, however doing so will
413+
// break anybody that may have implemented the `MatMenuPanel` themselves.
414+
this._overlayRef.keydownEvents().subscribe();
415415
}
416416

417417
return this._overlayRef;

src/material/menu/menu.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class="mat-mdc-menu-panel"
44
[id]="panelId"
55
[class]="_classList"
6+
(keydown)="_handleKeydown($event)"
67
(click)="closed.emit('click')"
78
[@transformMenu]="_panelAnimationState"
89
(@transformMenu.start)="_onAnimationStart($event)"

src/material/menu/menu.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,16 @@ describe('MatMenu', () => {
473473
fixture.componentInstance.trigger.openMenu();
474474

475475
const panel = overlayContainerElement.querySelector('.mat-mdc-menu-panel')!;
476-
const event = dispatchKeyboardEvent(panel, 'keydown', ESCAPE);
476+
const event = createKeyboardEvent('keydown', ESCAPE);
477+
spyOn(event, 'stopPropagation').and.callThrough();
477478

479+
dispatchEvent(panel, event);
478480
fixture.detectChanges();
479481
tick(500);
480482

481483
expect(overlayContainerElement.textContent).toBe('');
482484
expect(event.defaultPrevented).toBe(true);
485+
expect(event.stopPropagation).toHaveBeenCalled();
483486
}));
484487

485488
it('should not close the menu when pressing ESCAPE with a modifier', fakeAsync(() => {

src/material/menu/menu.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ export class MatMenu implements AfterContentInit, MatMenuPanel<MatMenuItem>, OnI
385385
manager.onKeydown(event);
386386
return;
387387
}
388+
389+
// Don't allow the event to propagate if we've already handled it, or it may
390+
// end up reaching other overlays that were opened earlier (see #22694).
391+
event.stopPropagation();
388392
}
389393

390394
/**

src/material/menu/testing/menu-harness.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
HarnessLoader,
1313
HarnessPredicate,
1414
TestElement,
15+
TestKey,
1516
} from '@angular/cdk/testing';
1617
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1718
import {MenuHarnessFilters, MenuItemHarnessFilters} from './menu-harness-filters';
@@ -81,7 +82,7 @@ export class MatMenuHarness extends ContentContainerComponentHarness<string> {
8182
async close(): Promise<void> {
8283
const panel = await this._getMenuPanel();
8384
if (panel) {
84-
return panel.click();
85+
return panel.sendKeys(TestKey.ESCAPE);
8586
}
8687
}
8788

0 commit comments

Comments
 (0)