Skip to content

Commit 1790cf5

Browse files
committed
fix(menu): nested trigger staying highlighted after click
Prevents the sub-menu trigger from staying highlighted if the user clicks on it and moves away to another menu item. Fixes #6838.
1 parent 1b6b270 commit 1790cf5

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/lib/menu/menu-trigger.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,13 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
413413
_handleMousedown(event: MouseEvent): void {
414414
if (!isFakeMousedownFromScreenReader(event)) {
415415
this._openedByMouse = true;
416+
417+
// Since clicking on the trigger won't close the menu if it opens a sub-menu,
418+
// we should prevent focus from moving onto it via click to avoid the
419+
// highlight from lingering on the menu item.
420+
if (this.triggersSubmenu) {
421+
event.preventDefault();
422+
}
416423
}
417424
}
418425

src/lib/menu/menu.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
dispatchMouseEvent,
3030
dispatchEvent,
3131
createKeyboardEvent,
32+
dispatchFakeEvent,
3233
} from '@angular/cdk/testing';
3334

3435

@@ -996,6 +997,15 @@ describe('MdMenu', () => {
996997
expect(overlay.querySelectorAll('.mat-menu-panel').length).toBe(0, 'Expected no open menus');
997998
}));
998999

1000+
it('should prevent the default mousedown action if the menu item opens a sub-menu', () => {
1001+
compileTestComponent();
1002+
instance.rootTrigger.openMenu();
1003+
fixture.detectChanges();
1004+
1005+
const event = dispatchFakeEvent(overlay.querySelector('[md-menu-item]')!, 'mousedown');
1006+
1007+
expect(event.defaultPrevented).toBe(true);
1008+
});
9991009

10001010
});
10011011

0 commit comments

Comments
 (0)