Skip to content

Commit 66b1ff5

Browse files
crisbetoandrewseguin
authored andcommitted
fix(menu): close child menus when parent is closed programmatically (#6329)
Fixes the child menus not closing themselves when a parent menu is closed via `closeMenu`.
1 parent 9bdd228 commit 66b1ff5

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/lib/menu/menu-trigger.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,10 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
214214
/** Closes the menu. */
215215
closeMenu(): void {
216216
if (this._overlayRef && this.menuOpen) {
217+
this._resetMenu();
217218
this._overlayRef.detach();
218219
this._closeSubscription.unsubscribe();
219-
this._resetMenu();
220+
this.menu.close.emit();
220221

221222
if (this.menu instanceof MdMenu) {
222223
this.menu._resetAnimation();

src/lib/menu/menu.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,28 @@ describe('MdMenu', () => {
961961
.not.toContain('mat-elevation-z3', 'Expected no stacked elevation.');
962962
});
963963

964+
it('should close all of the menus when the root is closed programmatically', () => {
965+
compileTestComponent();
966+
instance.rootTrigger.openMenu();
967+
fixture.detectChanges();
968+
969+
instance.levelOneTrigger.openMenu();
970+
fixture.detectChanges();
971+
972+
instance.levelTwoTrigger.openMenu();
973+
fixture.detectChanges();
974+
975+
const menus = overlay.querySelectorAll('.mat-menu-panel');
976+
977+
expect(menus.length).toBe(3, 'Expected three open menus');
978+
979+
instance.rootTrigger.closeMenu();
980+
fixture.detectChanges();
981+
982+
expect(overlay.querySelectorAll('.mat-menu-panel').length).toBe(0, 'Expected no open menus');
983+
});
984+
985+
964986
});
965987

966988
});

0 commit comments

Comments
 (0)