flow-components icon indicating copy to clipboard operation
flow-components copied to clipboard

Disable automatic close when clicking inside the menu

Open CookedApps opened this issue 7 years ago • 8 comments

Hello. The context menu automatically closes itself when an item is clicked inside the menu. That works fine when using the provided MenuItems. But in my use case i add a ComboBox and a Button to the menu (which works fine) but i cant use it because the menu closes every time i click something inside it.

Here is an example of what i mean: Open ComboBox Is it possible to disable this behavior and only close the menu "onOutsideClick" ?

Thanks in advance.

CookedApps avatar Nov 20 '18 13:11 CookedApps

Sounds like a useful feature. We need to come up with a proper API for this, depending on if we want it to be just a boolean to toggle between two modes (close-on-click and close-on-outside-click), or if we want to provide more options.

At least one possibility for a third option comes to my mind: not closing via any event at all, giving full control for the developer by using the close() method.

Until then, you can use the low-level Element API. This should do the job:

contextMenu.getElement().setProperty("closeOn", "vaadin-overlay-outside-click");

pekam avatar Nov 20 '18 14:11 pekam

Thanks @pekam for your workaround! I just discovered the same solution 5 minutes ago. :laughing: It would be nice to have this feature properly integrated in the API.

CookedApps avatar Nov 20 '18 14:11 CookedApps

Hello, I need to have the contextmenu opened up on mouse-over. Currently, the only method to do so is through low-level element API. Would suggest to have proper API to do so.

t0920008899 avatar Dec 28 '18 07:12 t0920008899

Hi, could you please tell me where should we use the code: contextMenu.getElement().setProperty("closeOn", "vaadin-overlay-outside-click"); <context-menu #basicMenu > <div>Text</div> <div>Button</div> </context-menu> I want to close the context menu only on click of the button and not on click on the text. Any help would be of great help.

RakeshKB avatar Jul 20 '20 11:07 RakeshKB

Hi, try setting this on the menu item: menuItem.getElement().setAttribute("onclick", "event.stopPropagation()");

sirbris avatar Jul 21 '20 07:07 sirbris

While the open-on works, adding this value to close-on doesn't seem to keep the menu open after having clicked a child of the main menu.

<vaadin-context-menu id="siteSelect" .items=${this.siteTree} @item-selected=${this.handleSiteChange} open-on="click" close-on="vaadin-overlay-outside-click"></vaadin-context-menu>

mel-dejesus avatar Sep 28 '21 02:09 mel-dejesus

Found a bizarre bug, where after setAttribute("onclick", "event.stopPropagation()");, the first click is not triggered at all, even with JS

Workaround:

menuItem.getElement().setAttribute("onclick", "event.stopPropagation()");
menuItem.getElement().executeJs("this.click();");

Immediately after setting the onClick attribute, to trigger a click using javascript, the click functionality works as usually.

Alexenon avatar Sep 28 '24 20:09 Alexenon

hi , I want avoid closing menu when outSideClick, and close menu only when APPLY button (menuItem) click ? any suggestion ?

rmsnandha avatar Mar 26 '25 17:03 rmsnandha