How to make Material Design Menu (mat-menu) hide on mouseleave

后端 未结 1 971
盖世英雄少女心
盖世英雄少女心 2020-12-20 01:30

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 ide

相关标签:
1条回答
  • 2020-12-20 01:32

    You can do this by wrapping the menu buttons in a <span> element:

    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();
      }  
    }
    


    Demo (V5):

    StackBlitz

    Material V6:

    StackBlitz

    0 讨论(0)
提交回复
热议问题