Skip to content

Commit 04bf3d1

Browse files
crisbetommalerba
authored andcommitted
fix(menu): nested trigger staying highlighted after click (#6853)
* 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. * fix: firefox tests crashing
1 parent 881630f commit 04bf3d1

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

src/cdk/testing/dispatch-events.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export function dispatchKeyboardEvent(node: Node, type: string, keyCode: number)
2525
}
2626

2727
/** Shorthand to dispatch a mouse event on the specified coordinates. */
28-
export function dispatchMouseEvent(node: Node, type: string, x = 0, y = 0): MouseEvent {
29-
return dispatchEvent(node, createMouseEvent(type, x, y)) as MouseEvent;
28+
export function dispatchMouseEvent(node: Node, type: string, x = 0, y = 0,
29+
event = createMouseEvent(type, x, y)): MouseEvent {
30+
return dispatchEvent(node, event) as MouseEvent;
3031
}

src/cdk/testing/event-objects.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function createKeyboardEvent(type: string, keyCode: number, target?: Elem
5757

5858
/** Creates a fake event object with any desired event type. */
5959
export function createFakeEvent(type: string) {
60-
let event = document.createEvent('Event');
60+
const event = document.createEvent('Event');
6161
event.initEvent(type, true, true);
6262
return event;
6363
}

src/lib/menu/menu-trigger.ts

+7
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

+15
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
dispatchMouseEvent,
3030
dispatchEvent,
3131
createKeyboardEvent,
32+
createMouseEvent,
3233
} from '@angular/cdk/testing';
3334

3435

@@ -1014,6 +1015,20 @@ describe('MdMenu', () => {
10141015
expect(overlay.querySelectorAll('.mat-menu-panel').length).toBe(2, 'Expected two open menus');
10151016
}));
10161017

1018+
it('should prevent the default mousedown action if the menu item opens a sub-menu', () => {
1019+
compileTestComponent();
1020+
instance.rootTrigger.openMenu();
1021+
fixture.detectChanges();
1022+
1023+
const event = createMouseEvent('mousedown');
1024+
1025+
Object.defineProperty(event, 'buttons', {get: () => 1});
1026+
event.preventDefault = jasmine.createSpy('preventDefault spy');
1027+
1028+
dispatchMouseEvent(overlay.querySelector('[md-menu-item]')!, 'mousedown', 0, 0, event);
1029+
expect(event.preventDefault).toHaveBeenCalled();
1030+
});
1031+
10171032
});
10181033

10191034
});

0 commit comments

Comments
 (0)