Skip to content

Commit 0da21c5

Browse files
committed
fix(menu): closing parent overlay when pressing escape
Along the same lines as #13413. Since `MatMenu` doesn't consume the `OverlayRef.keydownEvents`, it means that they'll be passed along to the next overlay in the stack.
1 parent 15a1ab7 commit 0da21c5

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
@@ -347,6 +347,11 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
347347
const config = this._getOverlayConfig();
348348
this._subscribeToPositions(config.positionStrategy as FlexibleConnectedPositionStrategy);
349349
this._overlayRef = this._overlay.create(config);
350+
351+
// Consume the `keydownEvents` in order to prevent them from going to another overlay.
352+
// Ideally we'd also have our keyboard event logic in here, however doing so will
353+
// break anybody that may have implemented the `MatMenuPanel` themselves.
354+
this._overlayRef.keydownEvents().subscribe();
350355
}
351356

352357
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)