Menubar
A horizontal bar of menus, as in a desktop application, with one tab stop and arrow keys that move between the menus.
import { Menu, MenuContent, MenuItem, MenuSeparator, MenuTrigger } from '@slotted/react/menu';import { Menubar } from '@slotted/react/menubar';export default function MenubarBasic() { return ( <Menubar aria-label="Operations"> <Menu value="orders"> <MenuTrigger>Orders</MenuTrigger> <MenuContent> <MenuItem shortcut="Mod+N">New order</MenuItem> <MenuItem shortcut="Mod+O">Open order…</MenuItem> <MenuSeparator /> <MenuItem shortcut="Mod+P">Print packing slip</MenuItem> </MenuContent> </Menu> <Menu value="inventory"> <MenuTrigger>Inventory</MenuTrigger> <MenuContent> <MenuItem>Receive stock</MenuItem> <MenuItem>Count stock</MenuItem> <MenuItem>Transfer stock</MenuItem> </MenuContent> </Menu> <Menu value="reports"> <MenuTrigger>Reports</MenuTrigger> <MenuContent> <MenuItem>Sales by region</MenuItem> <MenuItem>Stock aging</MenuItem> <MenuItem>Late shipments</MenuItem> </MenuContent> </Menu> </Menubar> );}Installation
The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.
pnpm add @slotted/react @slotted/theme @slotted/themesimport { Menubar, MenubarDefaultsProvider } from '@slotted/react';Menus on a bar
Menubar holds which menu is open and nothing else. Each menu on it is the Menu of the menu family, so submenus, shortcuts and every other menu capability work unchanged; in Angular the trigger is a button with slMenuTriggerFor placed inside sl-menubar, next to its sl-menu. Give each menu a value: the bar identifies the open menu by that name. Press a trigger to open its menu; with one menu open, moving the pointer along the bar switches menus without another press, and moving it along a closed bar opens nothing. Name the bar with aria-label, because the menubar role has no name of its own.
import { Menu, MenuContent, MenuItem, MenuSeparator, MenuTrigger } from '@slotted/react/menu';import { Menubar } from '@slotted/react/menubar';export default function MenubarSubmenus() { return ( <Menubar aria-label="Operations"> <Menu value="orders"> <MenuTrigger>Orders</MenuTrigger> <MenuContent> <MenuItem>New order</MenuItem> <Menu> <MenuTrigger>Export as</MenuTrigger> <MenuContent> <MenuItem>CSV</MenuItem> <MenuItem>PDF</MenuItem> <MenuItem>Excel workbook</MenuItem> </MenuContent> </Menu> <MenuSeparator /> <MenuItem>Print packing slip</MenuItem> </MenuContent> </Menu> <Menu value="inventory"> <MenuTrigger>Inventory</MenuTrigger> <MenuContent> <MenuItem>Receive stock</MenuItem> <Menu> <MenuTrigger>Move to warehouse</MenuTrigger> <MenuContent> <MenuItem>Rotterdam</MenuItem> <MenuItem>Leipzig</MenuItem> <MenuItem>Lyon</MenuItem> <MenuItem>Gdańsk</MenuItem> </MenuContent> </Menu> </MenuContent> </Menu> </Menubar> );}Options in a menu
Checkbox items and a radio group keep the menu open while the reader changes them, so a View menu takes several choices in one visit. The bar keeps the same menu open until the reader moves to another menu or leaves the bar.
import { Menu, MenuCheckboxItem, MenuContent, MenuItem, MenuRadioGroup, MenuRadioItem, MenuSeparator, MenuTrigger,} from '@slotted/react/menu';import { Menubar } from '@slotted/react/menubar';export default function MenubarOptions() { return ( <Menubar aria-label="Operations"> <Menu value="orders"> <MenuTrigger>Orders</MenuTrigger> <MenuContent> <MenuItem>New order</MenuItem> <MenuItem>Open order…</MenuItem> </MenuContent> </Menu> <Menu value="view"> <MenuTrigger>View</MenuTrigger> <MenuContent> <MenuCheckboxItem defaultChecked>Cancelled orders</MenuCheckboxItem> <MenuCheckboxItem>Archived orders</MenuCheckboxItem> <MenuCheckboxItem>Test orders</MenuCheckboxItem> <MenuSeparator /> <MenuRadioGroup aria-label="Group orders by" defaultValue="status"> <MenuRadioItem value="status">By status</MenuRadioItem> <MenuRadioItem value="customer">By customer</MenuRadioItem> <MenuRadioItem value="warehouse">By warehouse</MenuRadioItem> </MenuRadioGroup> </MenuContent> </Menu> </Menubar> );}Orientation
orientation="vertical" turns the bar into a column of triggers, as in a sidebar of commands. The bar sets aria-orientation="vertical", the menus open beside the bar instead of under it, and the two arrow pairs exchange places: ArrowDown and ArrowUp move between triggers, ArrowRight opens the menu, and ArrowLeft closes it. In a right-to-left document the horizontal arrows mirror.
import { Menu, MenuContent, MenuItem, MenuTrigger } from '@slotted/react/menu';import { Menubar } from '@slotted/react/menubar';export default function MenubarOrientation() { return ( <Menubar aria-label="Workspace" orientation="vertical"> <Menu value="orders"> <MenuTrigger>Orders</MenuTrigger> <MenuContent> <MenuItem>New order</MenuItem> <MenuItem>Open order…</MenuItem> </MenuContent> </Menu> <Menu value="inventory"> <MenuTrigger>Inventory</MenuTrigger> <MenuContent> <MenuItem>Receive stock</MenuItem> <MenuItem>Count stock</MenuItem> </MenuContent> </Menu> <Menu value="reports"> <MenuTrigger>Reports</MenuTrigger> <MenuContent> <MenuItem>Sales by region</MenuItem> <MenuItem>Late shipments</MenuItem> </MenuContent> </Menu> </Menubar> );}Sizes
size sets the step of every trigger and every menu on the bar: sm, md (the default) or lg. A menu on the bar takes the bar’s size, so the size is stated once.
import { Menu, MenuContent, MenuItem, MenuTrigger } from '@slotted/react/menu';import { Menubar } from '@slotted/react/menubar';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';export default function MenubarSizes() { return ( <Stack gap={4}> <Text size="sm" tone="muted"> sm </Text> <Menubar aria-label="Operations, small" size="sm"> <Menu value="orders"> <MenuTrigger>Orders</MenuTrigger> <MenuContent> <MenuItem>New order</MenuItem> <MenuItem>Open order…</MenuItem> </MenuContent> </Menu> <Menu value="inventory"> <MenuTrigger>Inventory</MenuTrigger> <MenuContent> <MenuItem>Receive stock</MenuItem> <MenuItem>Count stock</MenuItem> </MenuContent> </Menu> </Menubar> <Text size="sm" tone="muted"> md </Text> <Menubar aria-label="Operations, medium" size="md"> <Menu value="orders"> <MenuTrigger>Orders</MenuTrigger> <MenuContent> <MenuItem>New order</MenuItem> <MenuItem>Open order…</MenuItem> </MenuContent> </Menu> <Menu value="inventory"> <MenuTrigger>Inventory</MenuTrigger> <MenuContent> <MenuItem>Receive stock</MenuItem> <MenuItem>Count stock</MenuItem> </MenuContent> </Menu> </Menubar> <Text size="sm" tone="muted"> lg </Text> <Menubar aria-label="Operations, large" size="lg"> <Menu value="orders"> <MenuTrigger>Orders</MenuTrigger> <MenuContent> <MenuItem>New order</MenuItem> <MenuItem>Open order…</MenuItem> </MenuContent> </Menu> <Menu value="inventory"> <MenuTrigger>Inventory</MenuTrigger> <MenuContent> <MenuItem>Receive stock</MenuItem> <MenuItem>Count stock</MenuItem> </MenuContent> </Menu> </Menubar> </Stack> );}Disabled menu
disabled on a menu keeps its trigger in the bar and in the keyboard order, sets aria-disabled on it and opens nothing. The trigger stays focusable so that a reader using assistive technology finds a command that is unavailable instead of passing over a gap. The disabled prop of the bar disables every menu on it at once, as on the second bar here, whose accounting period is closed.
import { Menu, MenuContent, MenuItem, MenuTrigger } from '@slotted/react/menu';import { Menubar } from '@slotted/react/menubar';import { Stack } from '@slotted/react/stack';export default function MenubarStates() { return ( <Stack gap={4}> <Menubar aria-label="Operations"> <Menu value="orders"> <MenuTrigger>Orders</MenuTrigger> <MenuContent> <MenuItem>New order</MenuItem> <MenuItem>Open order…</MenuItem> </MenuContent> </Menu> <Menu value="inventory"> <MenuTrigger>Inventory</MenuTrigger> <MenuContent> <MenuItem>Receive stock</MenuItem> <MenuItem>Count stock</MenuItem> </MenuContent> </Menu> <Menu disabled value="archive"> <MenuTrigger>Archive</MenuTrigger> <MenuContent> <MenuItem>Restore order</MenuItem> </MenuContent> </Menu> </Menubar> <Menubar aria-label="Closed period" disabled> <Menu value="journal"> <MenuTrigger>Journal</MenuTrigger> <MenuContent> <MenuItem>Post entry</MenuItem> <MenuItem>Reverse entry</MenuItem> </MenuContent> </Menu> <Menu value="ledger"> <MenuTrigger>Ledger</MenuTrigger> <MenuContent> <MenuItem>Reconcile account</MenuItem> </MenuContent> </Menu> </Menubar> </Stack> );}Controlled
Pass value to own which menu is open. In React, onValueChange receives the next value and a request carrying the reason (pointer, keyboard, escape, outsidePress, select, dismiss or programmatic) and the originating event; the request can be cancelled. In Angular, [(value)] binds the open menu and valueChangeRequest emits the same request. A closed bar reports an empty string in the request. Here the buttons open and close the bar from code, and the line beside them reports the last change.
import { useState } from 'react';import { Button } from '@slotted/react/button';import { Inline } from '@slotted/react/inline';import { Menu, MenuContent, MenuItem, MenuTrigger } from '@slotted/react/menu';import { Menubar } from '@slotted/react/menubar';import type { MenubarChangeRequest } from '@slotted/react/menubar';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';export default function MenubarControlled() { const [open, setOpen] = useState<string | undefined>(undefined); const [last, setLast] = useState('No menu open'); function handleValueChange(next: string, request: MenubarChangeRequest) { setOpen(next === '' ? undefined : next); setLast(next === '' ? `Closed by ${request.reason}` : `${next} opened by ${request.reason}`); } return ( <Stack gap={4}> <Menubar aria-label="Operations" onValueChange={handleValueChange} value={open}> <Menu value="orders"> <MenuTrigger>Orders</MenuTrigger> <MenuContent> <MenuItem>New order</MenuItem> <MenuItem>Open order…</MenuItem> </MenuContent> </Menu> <Menu value="inventory"> <MenuTrigger>Inventory</MenuTrigger> <MenuContent> <MenuItem>Receive stock</MenuItem> <MenuItem>Count stock</MenuItem> </MenuContent> </Menu> <Menu value="reports"> <MenuTrigger>Reports</MenuTrigger> <MenuContent> <MenuItem>Sales by region</MenuItem> <MenuItem>Late shipments</MenuItem> </MenuContent> </Menu> </Menubar> <Inline align="center" gap={3} wrap="wrap"> <Button onClick={() => setOpen('inventory')} variant="ghost"> Open Inventory </Button> <Button onClick={() => setOpen(undefined)} variant="ghost"> Close </Button> <Text size="sm" tone="muted"> {last} </Text> </Inline> </Stack> );}Keyboard
The bar is one tab stop: Tab enters it on the trigger the reader last left, and the next Tab leaves it and closes any open menu. ArrowLeft and ArrowRight move between triggers and, while a menu is open, switch to the neighbouring menu with focus left on its trigger. Home and End jump to the first and last trigger. Typing a letter moves to the next trigger whose label begins with it. Escape closes the open menu and leaves focus on its trigger. loop={false} in React and [loop]="false" in Angular stop the arrows at the ends of the bar instead of wrapping; typeahead set to false turns typing off.
import { Menu, MenuContent, MenuItem, MenuTrigger } from '@slotted/react/menu';import { Menubar } from '@slotted/react/menubar';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';export default function MenubarKeyboard() { return ( <Stack gap={4}> <Text size="sm" tone="muted"> loop off: the arrows stop at the first and last trigger </Text> <Menubar aria-label="Operations, no wrapping" loop={false}> <Menu value="orders"> <MenuTrigger>Orders</MenuTrigger> <MenuContent> <MenuItem>New order</MenuItem> <MenuItem>Open order…</MenuItem> </MenuContent> </Menu> <Menu value="inventory"> <MenuTrigger>Inventory</MenuTrigger> <MenuContent> <MenuItem>Receive stock</MenuItem> <MenuItem>Count stock</MenuItem> </MenuContent> </Menu> <Menu value="reports"> <MenuTrigger>Reports</MenuTrigger> <MenuContent> <MenuItem>Sales by region</MenuItem> <MenuItem>Late shipments</MenuItem> </MenuContent> </Menu> </Menubar> <Text size="sm" tone="muted"> typeahead off: typing a letter moves nothing </Text> <Menubar aria-label="Operations, no typeahead" typeahead={false}> <Menu value="orders"> <MenuTrigger>Orders</MenuTrigger> <MenuContent> <MenuItem>New order</MenuItem> <MenuItem>Open order…</MenuItem> </MenuContent> </Menu> <Menu value="inventory"> <MenuTrigger>Inventory</MenuTrigger> <MenuContent> <MenuItem>Receive stock</MenuItem> <MenuItem>Count stock</MenuItem> </MenuContent> </Menu> <Menu value="reports"> <MenuTrigger>Reports</MenuTrigger> <MenuContent> <MenuItem>Sales by region</MenuItem> <MenuItem>Late shipments</MenuItem> </MenuContent> </Menu> </Menubar> </Stack> );}Defaults for every bar
MenubarDefaultsProvider in React and provideMenubarDefaults in Angular set orientation, size, loop, typeahead and disabled once for every bar under them. A bar that states a value of its own keeps it, as the second bar does here.
import { Inline } from '@slotted/react/inline';import { Menu, MenuContent, MenuItem, MenuTrigger } from '@slotted/react/menu';import { Menubar, MenubarDefaultsProvider } from '@slotted/react/menubar';import { Stack } from '@slotted/react/stack';import { Heading, Text } from '@slotted/react/typography';export default function MenubarDefaults() { return ( <MenubarDefaultsProvider size="sm"> <Stack gap={4}> <Inline align="center" gap={4}> <Heading level={3} size="md"> Northwind </Heading> <Menubar aria-label="Operations"> <Menu value="orders"> <MenuTrigger>Orders</MenuTrigger> <MenuContent> <MenuItem>New order</MenuItem> <MenuItem>Open order…</MenuItem> </MenuContent> </Menu> <Menu value="inventory"> <MenuTrigger>Inventory</MenuTrigger> <MenuContent> <MenuItem>Receive stock</MenuItem> <MenuItem>Count stock</MenuItem> </MenuContent> </Menu> </Menubar> </Inline> <Text size="sm" tone="muted"> A bar that states its own size keeps it </Text> <Menubar aria-label="Operations, large" size="lg"> <Menu value="orders"> <MenuTrigger>Orders</MenuTrigger> <MenuContent> <MenuItem>New order</MenuItem> <MenuItem>Open order…</MenuItem> </MenuContent> </Menu> <Menu value="inventory"> <MenuTrigger>Inventory</MenuTrigger> <MenuContent> <MenuItem>Receive stock</MenuItem> <MenuItem>Count stock</MenuItem> </MenuContent> </Menu> </Menubar> </Stack> </MenubarDefaultsProvider> );}Accessibility
| Key | Effect | Focus |
|---|---|---|
| Enter | opens the trigger's menu | the menu's first item |
| Space | opens the trigger's menu | the menu's first item |
| ArrowDown | on a horizontal bar, opens the trigger's menu; on a vertical bar, moves to the next trigger | the menu's first item, or the next trigger |
| ArrowUp | on a horizontal bar, opens the trigger's menu onto its last item, which is the pattern's optional behavior; on a vertical bar, moves to the previous trigger | the menu's last item, or the previous trigger |
| ArrowRight | the inline-end direction, mirrored in a right-to-left document: on a horizontal bar moves to the next trigger, wrapping when loop is true; on a vertical bar opens the trigger's menu | the next trigger, whose menu opens without focus entering it when a menu was already open |
| ArrowLeft | the inline-start direction, mirrored in a right-to-left document: the previous trigger on a horizontal bar, and on a vertical bar the key that closes the open menu back to its trigger | the previous trigger, or the trigger of the menu that closed |
| Home | moves to the first trigger | the first trigger |
| End | moves to the last trigger | the last trigger |
| Printable character | moves to the next trigger whose label begins with the sequence typed, which the pattern asks of a menubar as well as of a menu | the matched trigger |
| Escape | closes the open menu and leaves the bar focused | the trigger whose menu closed |
| Tab | moves focus out of the bar entirely and closes every open menu; the bar is one tab stop | the next element in the page after the bar |
| Shift+Tab | the same, backwards | the element before the bar |
- The platform supplies each trigger: it is a native button, so the press and the two keys are the browser's own. role="menuitem" replaces role="button", because on a bar the control is an item of a composite widget rather than a button in a row.
- Menubar supplies the keyboard, as the WAI-ARIA menu and menubar pattern describes it: one tab stop for the whole bar, arrows along its own axis with wrapping, Home and End at the ends, typeahead by name, and — from inside an open menu — the inline arrows closing it and opening the neighbouring one with focus left on the trigger.
- The bar has no name of its own to invent, so the consumer gives it one: aria-label on the bar, which is what a reader hears before the first trigger.
- A disabled trigger stays focusable and takes aria-disabled rather than the native attribute, so a reader can find a menu that is currently unavailable instead of walking past a gap in the bar.
API
Menubar
Props
valuestring | undefined- The menu now open, named by its own value. A bar has at most one, so the state is that name rather than a boolean on each menu.
defaultValuestring- The menu an uncontrolled bar starts with open, read once.
onValueChange(value, request) => void- Asked before the bar moves to another menu, with the reason and the event, and refusable.
orientation'horizontal' | 'vertical'Defaulthorizontal- Which way the bar runs. A vertical bar swaps the two arrow pairs and writes aria-orientation.
size'sm' | 'md' | 'lg'Defaultmd- The step every trigger and every menu on the bar takes.
loopbooleanDefaulttrue- Whether the arrows wrap past the ends of the bar.
typeaheadbooleanDefaulttrue- Whether typing moves to the trigger whose label begins with it.
disabledbooleanDefaultfalse- A disabled bar opens no menu, and every menu on it reads the same answer.
render(props) => ReactNode- Renders the consumer's own element as the bar, with the role, the state and the keyboard on it.
Events
| Name | Payload | When |
|---|---|---|
onValueChange | (value: string, request: MenubarChangeRequest) | Before the bar opens another menu or closes the one it has. The request carries pointer, keyboard, escape, outsidePress, select, dismiss or programmatic, the originating event, and cancel(). |
Composition
MenubarDefaultsProvidercomponent- On ApplicationSets the orientation, the size, the wrapping, the typeahead and the disabled state once for a subtree. A bar that states one itself still wins.
Menucomponent- On Inside a MenubarThe menus on a bar are the menu family unchanged: every capability it has — submenus, options, shortcuts, tone — reaches the bar without a second API.
valueprop- On MenuThe name a menu answers to on the bar. A consumer controlling the bar names its menus; on its own a menu needs none.
refprop- A handle carrying focus() and close(), for an application that moves focus to the bar or closes its menu from a shortcut.
native propsattributes- Anything a div takes reaches the bar, and a consumer's own onKeyDown runs before the family's rather than instead of it.
Parts
| Part | Element | Required | Purpose |
|---|---|---|---|
root | div, or the element render returns | Yes | The bar. It carries data-ui="menubar", role="menubar", its orientation and its size, holds one tab stop for every trigger on it, and paints no ground of its own — the header it sits in owns that. |
trigger | button | Yes | One menu's control, as role="menuitem". It is rendered by MenuTrigger inside the bar: the menus on a bar are the menu family unchanged, so the bar itself declares no other part. |
Theming
Each token is a CSS custom property: set it in a theme, or on any element to retune every menubar inside it.
Menubar tokens
| Token | Default |
|---|---|
--slotted-menubar-gap | var(--slotted-space-1, 0.125rem) |
--slotted-menubar-highlight | var(--slotted-tone-secondary-subtle-hover) |
--slotted-menubar-highlight-active | var(--slotted-tone-secondary-subtle-active) |
--slotted-menubar-text | var(--slotted-text, #0f172a) |
--slotted-menubar-trigger-font-weight | var(--slotted-font-weight-medium, 500) |
--slotted-menubar-trigger-radius | var(--slotted-control-radius) |
Theme tokens it reads
| Token | Default |
|---|---|
--slotted-control-font-size-lg | comfortable 14px · compact 13px · dense 12px |
--slotted-control-font-size-md | comfortable 13px · compact 12px · dense 12px |
--slotted-control-font-size-sm | comfortable 12px · compact 12px · dense 12px |
--slotted-control-height-lg | comfortable 38px · compact 32px · dense 30px |
--slotted-control-height-md | comfortable 34px · compact 28px · dense 26px |
--slotted-control-height-sm | comfortable 30px · compact 26px · dense 24px |
--slotted-control-padding-inline-lg | comfortable 16px · compact 12px · dense 10px |
--slotted-control-padding-inline-md | comfortable 13px · compact 10px · dense 8px |
--slotted-control-padding-inline-sm | comfortable 10px · compact 8px · dense 6px |
--slotted-control-radius | var(--slotted-radius-sm) |
--slotted-control-transition-duration | var(--slotted-duration-short) |
--slotted-control-transition-easing | var(--slotted-easing-standard) |
--slotted-disabled-background | Set per scheme by the theme |
--slotted-disabled-foreground | Set per scheme by the theme |
--slotted-duration-short | 120ms |
--slotted-easing-standard | cubic-bezier(0.2, 0, 0, 1) |
--slotted-focus-ring-color | Set per scheme by the theme |
--slotted-focus-ring-width | var(--slotted-border-width-thick) |
--slotted-font-family-sans | 'Geist Variable', ui-sans-serif, system-ui, sans-serif |
--slotted-font-weight-medium | 500 |
--slotted-space-1 | 0.125rem |
--slotted-text | Set per scheme by the theme |
--slotted-tone-secondary-subtle-active | Set per scheme by the theme |
--slotted-tone-secondary-subtle-hover | Set per scheme by the theme |