> Markdown version of [Menu Bar](https://vaadin.com/docs/v23/components/menu-bar). Section index: [llms.txt](https://vaadin.com/docs/v23/components/llms.txt)

# Menu Bar

Menu Bar is a horizontal button bar with hierarchical drop-down menus. Menu items can trigger an action, open a menu, or work as a toggle.

**TypeScript** — `menu-bar-basic.ts`

```typescript
@state()
private items = [
  { text: 'View' },
  { text: 'Edit' },
  {
    text: 'Share',
    children: [
      {
        text: 'On social media',
        children: [{ text: 'Facebook' }, { text: 'Twitter' }, { text: 'Instagram' }],
      },
      { text: 'By email' },
      { text: 'Get link' },
    ],
  },
  {
    text: 'Move',
    children: [{ text: 'To folder' }, { text: 'To trash' }],
  },
  { text: 'Duplicate' },
];

@state()
private selectedItem?: MenuBarItem;
    <vaadin-menu-bar
      .items="${this.items}"
      @item-selected="${this.itemSelected}"
    ></vaadin-menu-bar>

    <div>Clicked item: ${this.selectedItem?.text}</div>
```

**Java** — `MenuBarBasic.java`

```java
MenuBar menuBar = new MenuBar();
Text selected = new Text("");
ComponentEventListener<ClickEvent<MenuItem>> listener = e -> selected
        .setText(e.getSource().getText());
Div message = new Div(new Text("Clicked item: "), selected);

menuBar.addItem("View", listener);
menuBar.addItem("Edit", listener);

MenuItem share = menuBar.addItem("Share");
SubMenu shareSubMenu = share.getSubMenu();
MenuItem onSocialMedia = shareSubMenu.addItem("On social media");
SubMenu socialMediaSubMenu = onSocialMedia.getSubMenu();
socialMediaSubMenu.addItem("Facebook", listener);
socialMediaSubMenu.addItem("Twitter", listener);
socialMediaSubMenu.addItem("Instagram", listener);
shareSubMenu.addItem("By email", listener);
shareSubMenu.addItem("Get Link", listener);

MenuItem move = menuBar.addItem("Move");
SubMenu moveSubMenu = move.getSubMenu();
moveSubMenu.addItem("To folder", listener);
moveSubMenu.addItem("To trash", listener);

menuBar.addItem("Duplicate", listener);
```

## <a id="styles"></a>Styles

### <a id="default-variants"></a>Default Variants

The following variants are available to adjust the appearance of the component:

**TypeScript** — `menu-bar-styles.ts`

```typescript
<vaadin-menu-bar
  .items="${[{ text: 'Default', children: [{ text: 'Item' }] }]}"
></vaadin-menu-bar>
<vaadin-menu-bar
  theme="tertiary"
  .items="${[{ text: 'Tertiary', children: [{ text: 'Item' }] }]}"
></vaadin-menu-bar>
<vaadin-menu-bar
  theme="primary"
  .items="${[{ text: 'Primary', children: [{ text: 'Item' }] }]}"
></vaadin-menu-bar>
<vaadin-menu-bar
  theme="small"
  .items="${[{ text: 'Small', children: [{ text: 'Item' }] }]}"
></vaadin-menu-bar>
```

**Java** — `MenuBarStyles.java`

```java
MenuBar menuWithDefaultTheme = new MenuBar();
addItem(menuWithDefaultTheme, "Default");

MenuBar menuWithTertiaryTheme = new MenuBar();
menuWithTertiaryTheme.addThemeVariants(MenuBarVariant.LUMO_TERTIARY);
addItem(menuWithTertiaryTheme, "Tertiary");

MenuBar menuWithPrimaryTheme = new MenuBar();
menuWithPrimaryTheme.addThemeVariants(MenuBarVariant.LUMO_PRIMARY);
addItem(menuWithPrimaryTheme, "Primary");

MenuBar menuWithSmallTheme = new MenuBar();
menuWithSmallTheme.addThemeVariants(MenuBarVariant.LUMO_SMALL);
addItem(menuWithSmallTheme, "Small");
```

| Variant  | Usage recommendations                                                                                                                                                                               |
| -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Tertiary | Corresponds to the [tertiary button variant](https://vaadin.com/docs/v23/components/button.md#styles), omitting the background color.                                                               |
| Primary  | Corresponds to the primary button variant. As only one primary action should be presented in the same part of the UI, this should be used only for [drop-down button use cases](#dropdown-buttons). |
| Small    | Compact variant. Can be combined with Tertiary and Primary.                                                                                                                                         |

> **Tip: Default menu button styles can be customized**
>
> The standard Menu Button styles can be adjusted using [theme features](https://vaadin.com/docs/v23/styling/lumo/design-tokens/color.md). These variants should be used only to differentiate special instances of the component.

### <a id="alignment"></a>Alignment

Top-level items are aligned to the start of the Menu Bar, by default. Use the "end-aligned" theme variant to align them to the end, instead.

**TypeScript** — `menu-bar-right-aligned.ts`

```html
<vaadin-menu-bar theme="end-aligned" .items="${this.items}"></vaadin-menu-bar>
```

**Java** — `MenuBarRightAligned.java`

```java
MenuBar menuBar = new MenuBar();
menuBar.addThemeVariants(MenuBarVariant.LUMO_END_ALIGNED);
```

### <a id="styling-menu-items"></a>Styling Menu Items

An individual menu item can be styled using a [custom variant](https://vaadin.com/docs/v23/styling/custom-theme/custom-component-variants.md).

To style a root-level item, create a custom variant for the `vaadin-menu-bar-button` component.

To style a sub-menu item, create a custom variant for the `vaadin-context-menu-item` component.

See [Vaadin Component Variants](https://vaadin.com/docs/v23/styling/custom-theme/creating-custom-theme.md#vaadin-component-styles) for details of how to add style sheets for these components.

**TypeScript** — `menu-bar-custom-theme.ts`

```typescript
@state()
private items = [
  { text: 'View', theme: 'custom-theme' },
  { text: 'Edit' },
  {
    text: 'Share',
    children: [{ text: 'By email', theme: 'custom-theme' }, { text: 'Get link' }],
  },
];

protected override render() {
  return html`<vaadin-menu-bar .items="${this.items}"></vaadin-menu-bar>`;
}
```

**Java** — `MenuBarCustomTheme.java`

```java
MenuItem view = menuBar.addItem("View");
view.addThemeNames("custom-theme");

MenuItem edit = menuBar.addItem("Edit");

MenuItem share = menuBar.addItem("Share");
SubMenu shareSubMenu = share.getSubMenu();
shareSubMenu.addItem("By email").addThemeNames("custom-theme");
shareSubMenu.addItem("Get Link");
```

`vaadin-context-menu-item.css`

```css
:host([theme~='custom-theme']) {
    text-transform: uppercase;
}
```

`vaadin-menu-bar-button.css`

```css
:host([theme~='custom-theme']) {
    text-transform: uppercase;
}
```

## <a id="overflow"></a>Overflow

Items that don’t fit into the current width of the menu bar collapse automatically into an overflow menu at the end:

**TypeScript** — `menu-bar-overflow.ts`

```typescript
@state()
private items = [
  { text: 'View' },
  { text: 'Edit' },
  {
    text: 'Share',
    children: [
      {
        text: 'On social media',
        children: [{ text: 'Facebook' }, { text: 'Twitter' }, { text: 'Instagram' }],
      },
      { text: 'By email' },
      { text: 'Get link' },
    ],
  },
  {
    text: 'Move',
    children: [{ text: 'To folder' }, { text: 'To trash' }],
  },
  { text: 'Duplicate' },
];
    <vaadin-split-layout>
      <vaadin-menu-bar .items="${this.items}"></vaadin-menu-bar>
      <div>Move the splitter to see overflow feature</div>
    </vaadin-split-layout>
```

**Java** — `MenuBarOverflow.java`

```java
MenuBar menuBar = new MenuBar();
addItems(menuBar);
Div div = new Div();
div.setText("Move the splitter to see overflow feature");

SplitLayout splitLayout = new SplitLayout(menuBar, div);
```

## <a id="menu-item-features"></a>Menu Item Features

### <a id="icons"></a>Icons

Menu items can have icons in addition to, or instead of text.

**TypeScript** — `menu-bar-icons.ts`

```typescript
@state()
private items = [
  {
    component: this.createItem('share', 'Share'),
    children: [
      { component: this.createItem('share', 'By email', true) },
      { component: this.createItem('link', 'Get link', true) },
    ],
  },
  {
    component: this.createItem('copy', ''),
  },
];
    <vaadin-menu-bar theme="icon" .items="${this.items}"></vaadin-menu-bar>
```

**Java** — `MenuBarIcons.java`

```java
MenuBar menuBar = new MenuBar();
menuBar.addThemeVariants(MenuBarVariant.LUMO_ICON);
MenuItem share = createIconItem(menuBar, VaadinIcon.SHARE, "Share",
        null);
SubMenu shareSubMenu = share.getSubMenu();
createIconItem(shareSubMenu, VaadinIcon.SHARE, "By Email", null, true);
createIconItem(shareSubMenu, VaadinIcon.LINK, "Get link", null, true);
createIconItem(menuBar, VaadinIcon.COPY, null, "duplicate");

...

private MenuItem createIconItem(HasMenuItems menu, VaadinIcon iconName,
        String label, String ariaLabel) {
    return createIconItem(menu, iconName, label, ariaLabel, false);
}

private MenuItem createIconItem(HasMenuItems menu, VaadinIcon iconName,
        String label, String ariaLabel, boolean isChild) {
    Icon icon = new Icon(iconName);

    if (isChild) {
        icon.getStyle().set("width", "var(--lumo-icon-size-s)");
        icon.getStyle().set("height", "var(--lumo-icon-size-s)");
        icon.getStyle().set("marginRight", "var(--lumo-space-s)");
    }

    MenuItem item = menu.addItem(icon, e -> {
    });

    if (ariaLabel != null) {
        item.getElement().setAttribute("aria-label", ariaLabel);
    }

    if (label != null) {
        item.add(new Text(label));
    }

    return item;
}
```

Usage recommendations:

- **Use icons sparingly**. Most actions are difficult to represent reliably with icons. The benefit of icons in addition to text should be weighed against the visual distractions it may create.

- Menu items in drop-down menus should always have text labels.

- **Icon-only** menu buttons should be used primarily for common recurring actions with highly standardized, universally understood icons, such a cross for "close".

- **Icon-only** menu buttons should provide a textual alternative for screen readers using the `aria-label` attribute or [tooltips](#tooltips).

Menu Bars with icon-only top-level items can use the **Tertiary Inline** style variant to reduce the horizontal padding around the icons.

**TypeScript** — `menu-bar-icon-only.ts`

```typescript
@state()
private items = [
  { component: this.createItem('eye', 'View') },
  { component: this.createItem('pencil', 'Edit') },
  {
    component: this.createItem('share', 'Share'),
    children: [
      {
        text: 'On social media',
        children: [{ text: 'Facebook' }, { text: 'Twitter' }, { text: 'Instagram' }],
      },
      { text: 'By email' },
      { text: 'Get link' },
    ],
  },
  {
    component: this.createItem('folder', 'Move'),
    children: [{ text: 'To folder' }, { text: 'To trash' }],
  },
  { component: this.createItem('copy', 'Duplicate') },
];
    <vaadin-menu-bar theme="tertiary-inline" .items="${this.items}"></vaadin-menu-bar>
```

**Java** — `MenuBarIconOnly.java`

```java
MenuBar menuBar = new MenuBar();
menuBar.addThemeVariants(MenuBarVariant.LUMO_TERTIARY_INLINE);

createIconItem(menuBar, VaadinIcon.EYE, "View");
createIconItem(menuBar, VaadinIcon.PENCIL, "Edit");

MenuItem share = createIconItem(menuBar, VaadinIcon.SHARE, "Share");
SubMenu shareSubMenu = share.getSubMenu();
MenuItem onSocialMedia = shareSubMenu.addItem("On social media");
SubMenu socialMediaSubMenu = onSocialMedia.getSubMenu();
socialMediaSubMenu.addItem("Facebook");
socialMediaSubMenu.addItem("Twitter");
socialMediaSubMenu.addItem("Instagram");
shareSubMenu.addItem("By email");
shareSubMenu.addItem("Get Link");

MenuItem move = createIconItem(menuBar, VaadinIcon.FOLDER, "Move");
SubMenu moveSubMenu = move.getSubMenu();
moveSubMenu.addItem("To folder");
moveSubMenu.addItem("To trash");

createIconItem(menuBar, VaadinIcon.COPY, "Duplicate");

...

private MenuItem createIconItem(MenuBar menu, VaadinIcon iconName,
        String ariaLabel) {
    Icon icon = new Icon(iconName);
    MenuItem item = menu.addItem(icon);
    item.getElement().setAttribute("aria-label", ariaLabel);

    return item;
}
```

> **Warning: Other components in menu items**
>
> While it’s technically possible to put any UI element in a menu item, this can cause problems for accessibility as it may not be possible to focus them, and they may not be interpreted correctly by assistive technologies.

### <a id="disabled-items"></a>Disabled Items

Menu items can be disabled to show that they are unavailable currently.

**TypeScript** — `menu-bar-disabled.ts`

```typescript
@state()
private items = [
  { text: 'View' },
  { text: 'Edit', disabled: true },
  {
    text: 'Share',
    children: [{ text: 'By email', disabled: true }, { text: 'Get link' }],
  },
];
```

**Java** — `MenuBarDisabled.java`

```java
MenuBar menuBar = new MenuBar();

menuBar.addItem("View");
MenuItem edit = menuBar.addItem("Edit");
edit.setEnabled(false);

MenuItem share = menuBar.addItem("Share");
SubMenu shareSubMenu = share.getSubMenu();
shareSubMenu.addItem("By email").setEnabled(false);
shareSubMenu.addItem("Get Link");
```

### <a id="checkable-menu-items"></a>Checkable Menu Items

Menu items in drop-down menus can be configured as checkable to toggle options on and off.

**TypeScript** — `menu-bar-checkable.ts`

```typescript
@state()
private items = [
  {
    text: 'Options',
    children: [{ text: 'Save automatically', checked: true }, { text: 'Notify watchers' }],
  },
];
    <vaadin-menu-bar
      .items="${this.items}"
      @item-selected="${this.itemSelected}"
    ></vaadin-menu-bar>
itemSelected(e: MenuBarItemSelectedEvent) {
  const item = e.detail.value;
  (item as SubMenuItem).checked = !(item as SubMenuItem).checked;
}
```

**Java** — `MenuBarCheckable.java`

```java
MenuBar menuBar = new MenuBar();
MenuItem options = menuBar.addItem("Options");
SubMenu subItems = options.getSubMenu();

MenuItem saveItem = subItems.addItem("Save automatically");
saveItem.setCheckable(true);
saveItem.setChecked(true);
MenuItem notifyItem = subItems.addItem("Notify watchers");
notifyItem.setCheckable(true);
notifyItem.setChecked(false);

ComponentEventListener<ClickEvent<MenuItem>> listener = event -> {
    // System.out.println(event.getSource().isChecked());
};

saveItem.addClickListener(listener);
notifyItem.addClickListener(listener);
```

> **Note: Not a replacement for radio buttons**
>
> A Menu Bar with checkable items shouldn’t be used as a replacement for [radio buttons](https://vaadin.com/docs/v23/components/radio-button.md) in a form.

## <a id="dividers"></a>Dividers

You can use dividers to separate and group related content.

**TypeScript** — `menu-bar-dividers.ts`

```typescript
@state()
private items = [
  {
    text: 'Share',
    children: [
      { text: 'Facebook' },
      { text: 'Twitter' },
      { text: 'Instagram' },
      { component: 'hr' },
      { text: 'By email' },
      { text: 'Get link' },
      { component: 'hr' },
      { text: 'Set permissions' },
    ],
  },
];
    <vaadin-menu-bar .items="${this.items}"></vaadin-menu-bar>
```

**Java** — `MenuBarDividers.java`

```java
MenuBar menuBar = new MenuBar();
MenuItem item = menuBar.addItem("Share");
SubMenu subMenu = item.getSubMenu();
subMenu.addItem("Facebook");
subMenu.addItem("Twitter");
subMenu.addItem("Instagram");
subMenu.add(new Hr());
subMenu.addItem("By email");
subMenu.addItem("Get link");
subMenu.add(new Hr());
subMenu.addItem("Set permissions");
```

Use dividers sparingly to avoid creating unnecessary visual clutter.

> **Warning: Content other than menu items not accessible**
>
> While it’s technically possible to put any UI element in a drop-down menu, including interactive components, they are not accessible by keyboard or assistive technologies.

## <a id="open-on-hover"></a>Open on Hover

The component can be configured to open drop-down menus on hover, instead of on click.

**TypeScript** — `menu-bar-open-on-hover.ts`

```typescript
@state()
private items = [
  { text: 'View' },
  { text: 'Edit' },
  {
    text: 'Share',
    children: [
      {
        text: 'On social media',
        children: [{ text: 'Facebook' }, { text: 'Twitter' }, { text: 'Instagram' }],
      },
      { text: 'By email' },
      { text: 'Get link' },
    ],
  },
  {
    text: 'Move',
    children: [{ text: 'To folder' }, { text: 'To trash' }],
  },
  { text: 'Duplicate' },
];
    <vaadin-menu-bar .items="${this.items}" open-on-hover></vaadin-menu-bar>
```

**Java** — `MenuBarOpenOnHover.java`

```java
MenuBar menuBar = new MenuBar();
menuBar.setOpenOnHover(true);
addItems(menuBar);
```

## <a id="tooltips"></a>Tooltips (since V23.3)

Tooltips can be configured on top-level items to provide additional information, especially for icon-only items. When a top-level item is disabled, the corresponding tooltip isn’t shown.

**TypeScript** — `menu-bar-tooltip.ts`

```typescript
@state()
private items = [
  {
    component: this.createItem('eye'),
    tooltip: 'View',
  },
  {
    component: this.createItem('pencil'),
    tooltip: 'Edit',
  },
  {
    component: this.createItem('folder'),
    tooltip: 'Move',
  },
  {
    component: this.createItem('copy'),
    tooltip: 'Duplicate',
  },
  {
    component: this.createItem('archive'),
    tooltip: 'Archive',
    disabled: true,
  },
];

...

<vaadin-menu-bar .items="${this.items}" theme="icon">
  <vaadin-tooltip slot="tooltip"></vaadin-tooltip>
</vaadin-menu-bar>
```

**Java** — `MenuBarTooltip.java`

```java
MenuBar menuBar = new MenuBar();
menuBar.addThemeVariants(MenuBarVariant.LUMO_ICON);

createIconItem(menuBar, VaadinIcon.EYE, "View");
createIconItem(menuBar, VaadinIcon.PENCIL, "Edit");
createIconItem(menuBar, VaadinIcon.FOLDER, "Move");
createIconItem(menuBar, VaadinIcon.COPY, "Duplicate");
MenuItem archive = createIconItem(menuBar, VaadinIcon.ARCHIVE,
        "Archive");
archive.setEnabled(false);

...

private MenuItem createIconItem(MenuBar menu, VaadinIcon iconName,
        String tooltipText) {
    Icon icon = new Icon(iconName);
    MenuItem item = menu.addItem(icon, tooltipText);
    return item;
}
```

See [Tooltips documentation](https://vaadin.com/docs/v23/components/tooltip.md) for details on tooltip configuration.

## <a id="keyboard-usage"></a>Keyboard Usage

### <a id="top-level-items"></a>Top-Level Items

| Interaction                         | Keyboard Shortcut             |
| ----------------------------------- | ----------------------------- |
| Navigate between top-level items.   | `Left` and `Right` arrow keys |
| Open top-level menu.                | `Down` / `Space` / `Enter`    |
| Trigger top-level item with a menu. | `Space` / `Enter`             |

### <a id="menu-items"></a>Menu-Items

| Interaction                                 | Keyboard Shortcut           |
| ------------------------------------------- | --------------------------- |
| Navigate between items in a drop-down menu. | `Up` and `Down` arrow keys  |
| Open sub-menu.                              | `Right` / `Space` / `Enter` |
| Trigger menu item without a sub-menu.       | `Space` / `Enter`           |
| Return to previous menu.                    | `Left`                      |
| Close the drop-down menu.                   | `Esc`                       |

## <a id="dropdown-buttons"></a>Drop-down and "Combo" Buttons

A Menu Bar with a single top-level item is essentially a drop-down button. This solution provides a better user experience and better accessibility than a regular [Button](https://vaadin.com/docs/v23/components/button.md) paired with a [Context Menu](https://vaadin.com/docs/v23/components/context-menu.md).

**TypeScript** — `menu-bar-drop-down.ts`

```typescript
@state()
private items = [
  {
    text: 'John Smith',
    children: [
      { text: 'Profile' },
      { text: 'Account' },
      { text: 'Preferences' },
      { component: 'hr' },
      { text: 'Sign out' },
    ],
  },
];
    <vaadin-menu-bar .items="${this.items}"></vaadin-menu-bar>
```

**Java** — `MenuBarDropDown.java`

```java
MenuBar menuBar = new MenuBar();
MenuItem item = menuBar.addItem("John Smith");
SubMenu subMenu = item.getSubMenu();
subMenu.addItem("Profile");
subMenu.addItem("Account");
subMenu.addItem("Preferences");
subMenu.add(new Hr());
subMenu.addItem("Sign out");
```

So-called "combo buttons" can be created in a similar way. For example, they can be created to provide a set of variations on an action.

**TypeScript** — `menu-bar-combo-buttons.ts`

```typescript
@state()
private items = [
  { text: 'Save' },
  {
    component: this.createItem(),
    children: [{ text: 'Save as draft' }, { text: 'Save as copy' }, { text: 'Save and publish' }],
  },
];
    <vaadin-menu-bar theme="icon primary" .items="${this.items}"></vaadin-menu-bar>
```

**Java** — `MenuBarComboButtons.java`

```java
MenuBar menuBar = new MenuBar();
menuBar.addThemeVariants(MenuBarVariant.LUMO_ICON,
        MenuBarVariant.LUMO_PRIMARY);
menuBar.addItem("Save");
MenuItem item = menuBar.addItem(new Icon(VaadinIcon.CHEVRON_DOWN));
SubMenu subItems = item.getSubMenu();
subItems.addItem("Save as draft");
subItems.addItem("Save as copy");
subItems.addItem("Save and publish");
```

## <a id="internationalization-i18n"></a>Internationalization (i18n)

Menu Bar provides an API for localization. Currently, only the accessible label for the overflow menu button can be customized.

**TypeScript** — `menu-bar-internationalization.ts`

```typescript
const customI18n: MenuBarI18n = {
  // Provide accessible label for the overflow menu button
  // to screen readers
  moreOptions: 'More actions',
};

return html`<vaadin-menu-bar .i18n="${customI18n}" .items="${this.items}"></vaadin-menu-bar>`;
```

**Java** — `MenuBarInternationalization.java`

```java
MenuBar menuBar = new MenuBar();
MenuBar.MenuBarI18n customI18n = new MenuBar.MenuBarI18n()
        // Provide accessible label for the overflow menu button
        // to screen readers
        .setMoreOptions("More actions");

menuBar.setI18n(customI18n);
```

## <a id="best-practices"></a>Best Practices

- Menu Bar shouldn’t be used for navigation. Use [tabs](https://vaadin.com/docs/v23/components/tabs.md) to switch between content, or anchor elements for regular navigation.

- Menu Bar isn’t an input field. Use [Select](https://vaadin.com/docs/v23/components/select.md), [Combo Box](https://vaadin.com/docs/v23/components/combo-box.md), or [Radio Button](https://vaadin.com/docs/v23/components/radio-button.md) instead.

## <a id="related-components"></a>Related Components

| Component                                                              | Usage recommendations                                                                |
| ---------------------------------------------------------------------- | ------------------------------------------------------------------------------------ |
| [Button](https://vaadin.com/docs/v23/components/button.md)             | Regular Button component for individual actions.                                     |
| [Select](https://vaadin.com/docs/v23/components/select.md)             | Drop-down input field.                                                               |
| [Tabs](https://vaadin.com/docs/v23/components/tabs.md)                 | Tabs should be used to split content into sections that the user can switch between. |
| [Context Menu](https://vaadin.com/docs/v23/components/context-menu.md) | A generic drop-down menu that can be triggered from any component.                   |

`BCC76FD2-FB02-4F71-A6DF-7574CAC1C662`
