Menu Bar
- Usage
- Styling
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.
new tab
const [selectedItem, setSelectedItem] = useState<MenuBarItem | undefined>(undefined);
const 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' },
];
return (
<>
<MenuBar items={items} onItemSelected={(event) => setSelectedItem(event.detail.value)} />
<div>Clicked item: {selectedItem?.text}</div>
</>
);
Styles
Default Variants
The following variants are available to adjust the appearance of the component:
new tab
<MenuBar items={[{ text: 'Default', children: [{ text: 'Item' }] }]} />
<MenuBar theme="tertiary" items={[{ text: 'Tertiary', children: [{ text: 'Item' }] }]} />
<MenuBar theme="primary" items={[{ text: 'Primary', children: [{ text: 'Item' }] }]} />
<MenuBar theme="small" items={[{ text: 'Small', children: [{ text: 'Item' }] }]} />
Variant | Usage Recommendation |
---|---|
Tertiary | Corresponds to the tertiary button variant, 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. |
Small | Compact variant. Can be combined with Tertiary and Primary. |
Tip
|
Customize Default Menu Button Styles
The standard Menu Button styles can be adjusted using <<https://vaadin.com/docs/styling/lumo/lumo-style-properties#,the Lumo style properties>>. These variants should be used only to differentiate special instances of the component.
|
Alignment
Top-level items are aligned by default to the start of the Menu Bar. Use instead the end-aligned
theme variant to align them to the end.
Styling Menu Items
An individual menu item can be styled using custom theme variants. A custom variant can be created by adding a custom theme name (e.g., custom-theme
) to an item, and then adding CSS for styling all items using that theme name.
The following example shows how to add a custom theme variant named custom-theme
to individual menu bar buttons and items, and how to style that variant with CSS:
new tab
const items = [
{ text: 'View', theme: 'custom-theme' },
{ text: 'Edit' },
{
text: 'Share',
children: [{ text: 'By email', theme: 'custom-theme' }, { text: 'Get link' }],
},
];
return <MenuBar items={items} />;
Overflow
Items that don’t fit into the current width of the menu bar collapse automatically into an overflow menu at the end:
new tab
const 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' },
];
return (
<SplitLayout>
<MenuBar items={items} />
<div>Move the splitter to see overflow feature</div>
</SplitLayout>
);
Menu Item Features
There are several features available for menu items. They’re described in the following sub-sections.
Icons
Menu items can have icons in addition to text — or instead of text.
new tab
const items = [
{
component: createItem('share', 'Share'),
children: [
{ component: createItem('share', 'By email', true) },
{ component: createItem('link', 'Get link', true) },
],
},
{
component: createItem('copy', ''),
},
];
return <MenuBar theme="icon" items={items} />;
Most actions are difficult to represent reliably with icons, so use them sparingly. The benefit of icons in addition to text should be weighed against the visual distractions they 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. Menu buttons should include a textual alternative for screen readers using the aria-label
attribute or tooltips. Menu Bars with icon-only top-level items can use the Tertiary Inline style variant to reduce the horizontal padding around the icons.
new tab
const items = [
{ component: createItem('eye', 'View') },
{ component: createItem('pencil', 'Edit') },
{
component: createItem('share', 'Share'),
children: [
{
text: 'On social media',
children: [{ text: 'Facebook' }, { text: 'Twitter' }, { text: 'Instagram' }],
},
{ text: 'By email' },
{ text: 'Get link' },
],
},
{
component: createItem('folder', 'Move'),
children: [{ text: 'To folder' }, { text: 'To trash' }],
},
{ component: createItem('copy', 'Duplicate') },
];
return <MenuBar theme="icon" items={items} />;
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.
|
Disabled Items
Menu items can be disabled to show that they are unavailable currently.
new tab
const items = [
{ text: 'View' },
{ text: 'Edit', disabled: true },
{
text: 'Share',
children: [{ text: 'By email', disabled: true }, { text: 'Get link' }],
},
];
return <MenuBar items={items} />;
Checkable Menu Items
Menu items in drop-down menus can be configured as checkable to toggle options on and off.
new tab
const [items, setItems] = useState([
{
text: 'Options',
children: [{ text: 'Save automatically', checked: true }, { text: 'Notify watchers' }],
},
]);
const itemSelected = (e: MenuBarItemSelectedEvent) => {
const item = e.detail.value;
(item as SubMenuItem).checked = !(item as SubMenuItem).checked;
setItems([...items]);
};
return <MenuBar items={items} onItemSelected={itemSelected} />;
Note
|
Not a Radio Button Replacement
A Menu Bar with checkable items shouldn’t be used as a replacement for radio buttons in a form.
|
Dividers
You can use dividers to separate and group related content. However, use dividers sparingly to avoid creating unnecessary visual clutter.
new tab
const items = [
{
text: 'Share',
children: [
{ text: 'Facebook' },
{ text: 'Twitter' },
{ text: 'Instagram' },
{ component: 'hr' },
{ text: 'By email' },
{ text: 'Get link' },
{ component: 'hr' },
{ text: 'Set permissions' },
],
},
];
return <MenuBar items={items} />;
Warning
|
Other Content Not Accessible
While it’s technically possible to put any UI element in a drop-down menu — including interactive components — they’re not accessible by keyboard or assistive technologies.
|
Open on Hover
A component can be configured to open drop-down menus on hover, instead of on click.
new tab
const 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' },
];
return <MenuBar items={items} openOnHover />;
Tooltips
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.
new tab
const items = [
{ component: createItem('eye'), tooltip: 'View' },
{ component: createItem('pencil'), tooltip: 'Edit' },
{ component: createItem('folder'), tooltip: 'Move' },
{ component: createItem('copy'), tooltip: 'Duplicate' },
{ component: createItem('archive'), tooltip: 'Archive', disabled: true },
];
return (
<MenuBar items={items} theme="icon">
<Tooltip slot="tooltip" />
</MenuBar>
);
See the Tooltips documentation page for details on tooltip configuration.
Keyboard Usage
Drop-Down & 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 paired with a Context Menu.
new tab
const items = [
{
text: 'John Smith',
children: [
{ text: 'Profile' },
{ text: 'Account' },
{ text: 'Preferences' },
{ component: 'hr' },
{ text: 'Sign out' },
],
},
];
return <MenuBar items={items} />;
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.
new tab
const items = [
{ text: 'Save' },
{
component: menuComponent(<Icon icon="vaadin:chevron-down" ariaLabel="Other save options" />),
children: [{ text: 'Save as draft' }, { text: 'Save as copy' }, { text: 'Save and publish' }],
},
];
return <MenuBar theme="icon primary" items={items} />;
Internationalization (i18n)
Menu Bar provides an API for localization. Currently, only the accessible label for the overflow menu button can be customized.
new tab
const customI18n: MenuBarI18n = {
// Provide accessible label for the overflow menu button
// to screen readers
moreOptions: 'More actions',
};
return <MenuBar i18n={customI18n} items={items} />;
Best Practices
Menu Bar shouldn’t be used for navigation. Use tabs to switch between content, or anchor elements for regular navigation. It isn’t an input field. Use instead Select, Combo Box, or Radio Button.
Related Components
Component | Usage Recommendation |
---|---|
Regular Button component for individual actions. | |
Drop-down input field. | |
Tabs should be used to split content into sections that the user can switch between. | |
A generic drop-down menu that can be triggered from any component. |