Skip to content

Commit df53c21

Browse files
crisbetojosephperrott
authored andcommitted
fix(menu): closing parent overlay when pressing escape (#13442)
1 parent aa69dc0 commit df53c21

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

src/lib/menu/menu-directive.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ export class MatMenu implements AfterContentInit, MatMenuPanel<MatMenuItem>, OnI
262262
switch (keyCode) {
263263
case ESCAPE:
264264
this.closed.emit('keydown');
265-
event.stopPropagation();
266265
break;
267266
case LEFT_ARROW:
268267
if (this.parentMenu && this.direction === 'ltr') {

src/lib/menu/menu-trigger.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
350350
const config = this._getOverlayConfig();
351351
this._subscribeToPositions(config.positionStrategy as FlexibleConnectedPositionStrategy);
352352
this._overlayRef = this._overlay.create(config);
353+
354+
// Consume the `keydownEvents` in order to prevent them from going to another overlay.
355+
// Ideally we'd also have our keyboard event logic in here, however doing so will
356+
// break anybody that may have implemented the `MatMenuPanel` themselves.
357+
this._overlayRef.keydownEvents().subscribe();
353358
}
354359

355360
return this._overlayRef;

src/lib/menu/menu.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,12 @@ describe('MatMenu', () => {
254254

255255
const panel = overlayContainerElement.querySelector('.mat-menu-panel')!;
256256
const event = createKeyboardEvent('keydown', ESCAPE);
257-
const stopPropagationSpy = spyOn(event, 'stopPropagation').and.callThrough();
258257

259258
dispatchEvent(panel, event);
260259
fixture.detectChanges();
261260
tick(500);
262261

263262
expect(overlayContainerElement.textContent).toBe('');
264-
expect(stopPropagationSpy).toHaveBeenCalled();
265263
}));
266264

267265
it('should open a custom menu', () => {

0 commit comments

Comments
 (0)