Flow replacement for MenuBar.addItemBefore()

We are currently in the process of migrating our application from Vaadin 8 to Vaadin 24 (Flow). In Vaadin8, the built-in MenuBar component allowed to add a new item before a specific other item via addItemBefore(). This method is no longer existing. Is there any other option, besides removing all items and adding them in a custom order again?

I’m wondering what is the use case? Maybe you can create a menu item, keep its reference, and toggle its visibility down the line?

MenuBar bar = new MenuBar();
MenuItem item = bar.addItem("Temporary");
item.setVisible(false);
// more code
item.setText("Not temporary");
item.setVisible(true);

We built a dynamic menu bar, on which certain sub-layouts can add items dynamically when being navigated to. Nevertheless I want to have the “User/Logout” item at the very end, so we want to add those dynamic items at the “second-last” place, which worked perfectly fine with addItemBefore().

So your solution does not work for us unfortunately.

Is it always just before the User/Logout item?
Then I’d considering splitting it apart from the rest of the menu, either as its own component (a button for example) or another MenuBar. Obviously not ideal.

No, we have multiple nested layouts / tabs and each of these can modify the header bar with a dynamic amount of items.
I will look into splitting it completely, or otherwise wrapping the MenuBar and offering a custom addItemBefore() method which indeed removes everything and re-adds it in the correct order.

I was just hoping the functionality was still existing but I was just looking at the wrong place. Kind of a bummer that that certain, rather non-complex feature was just removed.

The underlying MenuManager supports adding based on index, so it would probably be fairly easy to create a pull request for that API if you wanna tackle it.

ContextMenu does have an addComponentAtIndex method, so presumably it should be doable in MenuBar as well by exposing a corresponding method.

(As for why it was removed: it wasn’t really. V10+ has brand new components that are not based on the V8 versions at all. The APIs in V10+ were written from scratch, and as part of that some APIs that existed in V8 were not recreated in V10. In some cases that was just an oversight, in some cases they were deemed unnecessary, and in some cases there were technical reasons why it wasn’t possible or at least not easy to re-create them. Not 100% sure which case we’re looking at here, but my guess would be oversight.)

I’d propose creating a feature request issue in https://github.com/vaadin/flow-components/issues and, if you like, contributing a PR that introduces the API.

Thank you Rolf, I will open a feature request for that.

Feature request opened: https://github.com/vaadin/flow-components/issues/6029