Preventing MenuBar from closing when MenuBarRootItem is clicked.

Hi,
I have a simple MenuBar like :

MenuBar menu = new MenuBar(); menu.addItem(VaadinIcon.PLUS.create(), event -> increase()); menu.addItem(VaadinIcon.MINUS.create(), event -> decrease());

I would like to modify the default behavior of MenuBar so that it doesn’t close when I click on the items (to be able to click them several times). Can someone tell me how to achieve this please ?

I had it to work thanks to this post : https://github.com/vaadin/web-components/issues/1005#issuecomment-763251674
The trick was to write a KeepMenuBarOpenDecorator class :

public class KeepMenuBarOpenDecorator {

    public KeepMenuBarOpenDecorator(MenuItem menuItem) {
        menuItem.getElement().addEventListener("click", e -> {
        }).addEventData("event.preventDefault()");
    }

    public static void keepOpenOnClick(MenuItem menuItem) {
        new KeepMenuBarOpenDecorator(menuItem);
    }

    public static void keepOpenOnClick(SubMenu subMenu) {
        subMenu.getItems().forEach(KeepMenuBarOpenDecorator::keepOpenOnClick);
    }
}

And decorate the menu items with :

MenuBar menu = new MenuBar();
KeepMenuBarOpenDecorator.keepOpenOnClick(menu.addItem(VaadinIcon.PLUS.create(), event -> increase()));
KeepMenuBarOpenDecorator.keepOpenOnClick(menu.addItem(VaadinIcon.MINUS.create(), event -> decrease()));

Now the menu bar wont close when clicking on items.
Hope it helps.