Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Using mat-menu as context menu: How to close opened mat-menu, and open it in another location #655

@brucebrit

Description

@brucebrit

Angular: v8.2.8
Angular material: 8.2.2

I manage to use mat-menu (angular material) to emulate the context menu. Right clicking anyway on my page, the menu appears at my mouse location.

What I want to achieve: when I move my mouse to another location on the page, I right click again. I was expecting the menu at the original location to close, and reopen at the new location.

But In reality: Nothing happen and the menu at the original location remain open, and no menu open at the new location.

In fact, I have to left click to close the opened menu at original location, followed by right click to open menu at the new mouse location.

Question: Any idea how to achieve what I want?

@Component({
  selector: 'fl-home',
  template:`
 <mat-menu #contextmenu>
      <div >
           <button mat-menu-item>Clear all Paths</button>
       </div>
  </mat-menu>
  <div [matMenuTriggerFor]="contextmenu" [style.position]="'absolute'" [style.left.px]="menuX" [style.top.px]="menuY" ></div>
  
  <div class="container" (contextmenu)="onTriggerContextMenu($event);"> ....</div>
`})
export class HomeComponent  {

    menuX:number=0
    menuY:number=0

    @ViewChild(MatMenuTrigger,{static:false}) menu: MatMenuTrigger; 
    
   
    onTriggerContextMenu(event){
        
        event.preventDefault();
        this.menuX = event.x - 10;
        this.menuY = event.y - 10;
        this.menu.closeMenu() // putting this does not work.
        this.menu.openMenu()

    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions