isaacplmann / ngx-contextmenu

An Angular component to show a context menu on an arbitrary component
MIT License
248 stars 91 forks source link

Manually closing contextmenu #122

Closed Jamlearner closed 6 years ago

Jamlearner commented 6 years ago

There is a case where I am using a table-renderer package with scroll bars. In that element, I am unable to close contextmenu when attempt to scroll (Using mousewheel or dragging scrollbar). Due to the limitation on not having any way to put cdkScrollable on the element.

Does ngx-contextmenu provides API to do a manual closing?

isaacplmann commented 6 years ago

I think it used to, but there isn't an API for doing it now.

The best workaround is to programmatically trigger a click event on the document.

document.dispatchEvent(new Event('click'))
Jamlearner commented 6 years ago

That's a good workaround. Thanks @isaacplmann

vincent-cm commented 6 years ago

@isaacplmann Just curious that why I am unable to call the methods directly inside this package?

// import { ContextMenuService } from 'ngx-contextmenu/public_api';

Then in constructor

constructor(
    // public contextMenuService: ContextMenuService,
  )

Explicitly call the method:

public onClearContextMenu() {
    // const keyEvent = new KeyboardEvent('keydown', { key: 'Escape' });
    // if (this.contextMenuService) {
    //   this.contextMenuService.closeAllContextMenus({
    //     eventType: 'cancel',
    //     event: keyEvent
    //   });
    // }
  }

There is no error in IDE but an error when try to build or serve: Module not found: Error: Can't resolve 'ngx-contextmenu/public_api' in 'my component.ts'

isaacplmann commented 6 years ago

Try

import { ContextMenuService } from 'ngx-contextmenu';
Thorski commented 5 years ago

I think it used to, but there isn't an API for doing it now.

The best workaround is to programmatically trigger a click event on the document.

document.dispatchEvent(new Event('click'))

There used to be a triggerClose Subject on the ContextMenuService but that no longer works. In the code I find no references to triggerClose other than the definition. See #38. Instead I now use closeAllContextMenus(...).