Skip to content

Commit d013491

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 d013491

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

src/demo-app/dialog/dialog-demo.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ export class DialogDemo {
9898
<input matInput #howMuch>
9999
</mat-form-field>
100100
101+
<mat-toolbar>
102+
<button mat-icon-button [matMenuTriggerFor]="menu" aria-label="Open basic menu">
103+
<mat-icon>more_vert</mat-icon>
104+
</button>
105+
</mat-toolbar>
106+
107+
<mat-menu #menu="matMenu">
108+
<button mat-menu-item>
109+
Hello
110+
</button>
111+
</mat-menu>
112+
113+
101114
<p cdkDragHandle> {{ data.message }} </p>
102115
<button type="button" (click)="dialogRef.close(howMuch.value)">Close dialog</button>
103116
<button (click)="togglePosition()">Change dimensions</button>

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)