How to make Material Design Menu (mat-menu) hide on mouseleave
How to make Material Design Menu (mat-menu) hide on mouseleave
I succeeded in making the menu appear on mouseenter. What I want to do now is make it disappear on the mouseleave event of the menu itself. Any ideas on how to make this possible?
mouseenter
mouseleave
<button mat-button [mat-menu-trigger-for]="menu"
#menuTrigger="matMenuTrigger" (mouseenter)="menuTrigger.openMenu()">
TRIGGER BUTTON
</button>
<mat-menu #menu="matMenu" [overlapTrigger]="false"
(mouseleave)="menuTrigger.closeMenu()">
<button mat-menu-item [routerLink]="['sources']">
<mat-icon>view_headline</mat-icon>
MENU CHOICE
</button>
</mat-menu>
this.menuTrigger.closeMenu()
1 Answer
1
You can do this by wrapping the menu buttons in a <span> element:
<span>
HTML:
<button mat-button
[matMenuTriggerFor]="menu"
(mouseenter)="openMyMenu()">
Trigger
</button>
<mat-menu #menu="matMenu" overlapTrigger="false">
<span (mouseleave)="closeMyMenu()">
<button mat-menu-item>Item 1</button>
<button mat-menu-item>Item 2</button>
</span>
</mat-menu>
Component:
export class MenuOverviewExample {
@ViewChild(MatMenuTrigger) trigger: MatMenuTrigger;
openMyMenu() {
this.trigger.openMenu();
}
closeMyMenu() {
this.trigger.closeMenu();
}
}
StackBlitz
Material V6:
StackBlitz
Until this is officially supported I think this is the best solution. Thank you.
– glazjoon
Dec 8 '17 at 8:58
Can you please update this with an additional functionality so that if I mouseLeave the button itself it turn off the dropdown/menu.
– Sami
Mar 26 at 14:38
This (mouseleave) event is not working for latest version of the packages. So can you make the require changes for the latest version of the packages.
– Vignesh
Jul 2 at 10:34
@Vignesh see here - stackblitz.com/edit/angular-t8qh6k - material v6
– Und3rTow
Jul 2 at 11:12
@Und3rTow on mouseout i need to close the menu can you suggest me.
– Vignesh
Jul 2 at 11:17
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Is closeMenu() being called at all? Try making a function in your component that calls
this.menuTrigger.closeMenu(), and call that function instead.– Brandon Miller
Dec 7 '17 at 14:00