Menubar
A row of menus that stays on screen, such as File, Edit and View across the top of an editor: the WAI-ARIA menubar pattern. A menubar is for an application's commands; for links to a site's pages, use Navigation Menu, as WAI-ARIA advises. The bar is one tab stop, and the arrow keys, Home, End and typed characters move along it, wrapping at the ends. Each menu is a Menu, opened from an item of the bar, so its items, submenus, keys and typeahead are Menu's, and an app with both components pays for them once. A press, Down Arrow, Enter or Space opens a menu with focus on its first item. Once one menu is open, moving along the bar, by a key or the pointer, opens the next one with focus left on its item in the bar, and Right or Left Arrow inside a menu does the same. Escape closes the open menu and returns focus to its item in the bar; Tab and Shift+Tab close it and leave the bar. Name the bar with aria-label.
- Size
- 12.4 kB
Example
import { Menubar } from "menhir/menubar";
const trigger =
"cursor-default rounded px-3 py-1 text-sm hover:bg-stone-100 data-open:bg-stone-100 dark:hover:bg-stone-800 dark:data-open:bg-stone-800";
const popup =
"m-0 min-w-40 rounded-lg border border-stone-200 bg-white p-1 text-sm text-inherit shadow-lg transition-opacity duration-100 data-closed:opacity-0 dark:border-stone-700 dark:bg-stone-900 starting:data-open:opacity-0";
const item =
"flex cursor-default items-center gap-2 rounded py-1.5 pr-2 pl-7 outline-none select-none data-active:bg-stone-100 data-disabled:opacity-50 dark:data-active:bg-stone-800";
const separator = "my-1 border-stone-200 dark:border-stone-700";
const indicator = "-ml-5 w-3 data-unchecked:invisible";
export default function MenubarExample() {
return (
<Menubar.Root
aria-label="Editor"
class="flex gap-0.5 rounded-lg border border-stone-200 p-1 dark:border-stone-700"
>
<Menubar.Menu>
<Menubar.Trigger class={trigger}>File</Menubar.Trigger>
<Menubar.Popup class={popup} sideOffset={4}>
<Menubar.Item class={item}>New</Menubar.Item>
<Menubar.Item class={item}>Open…</Menubar.Item>
<hr class={separator} />
<Menubar.Submenu>
<Menubar.SubmenuTrigger
class={`${item} after:ml-auto after:content-['›']`}
>
Share
</Menubar.SubmenuTrigger>
<Menubar.Popup class={popup}>
<Menubar.Item class={item}>Email</Menubar.Item>
<Menubar.Item class={item}>Messages</Menubar.Item>
</Menubar.Popup>
</Menubar.Submenu>
</Menubar.Popup>
</Menubar.Menu>
<Menubar.Menu>
<Menubar.Trigger class={trigger}>Edit</Menubar.Trigger>
<Menubar.Popup class={popup} sideOffset={4}>
<Menubar.Item class={item}>Undo</Menubar.Item>
<Menubar.Item class={item} disabled>
Redo
</Menubar.Item>
<hr class={separator} />
<Menubar.Item class={item}>Copy</Menubar.Item>
<Menubar.Item class={item}>Paste</Menubar.Item>
</Menubar.Popup>
</Menubar.Menu>
<Menubar.Menu>
<Menubar.Trigger class={trigger}>View</Menubar.Trigger>
<Menubar.Popup class={popup} sideOffset={4}>
<Menubar.CheckboxItem class={item} defaultChecked>
<Menubar.ItemIndicator class={indicator}>✓</Menubar.ItemIndicator>
Word wrap
</Menubar.CheckboxItem>
<Menubar.CheckboxItem class={item}>
<Menubar.ItemIndicator class={indicator}>✓</Menubar.ItemIndicator>
Minimap
</Menubar.CheckboxItem>
</Menubar.Popup>
</Menubar.Menu>
</Menubar.Root>
);
}
Anatomy
<Menubar.Root>
<Menubar.Menu>
<Menubar.Trigger />
<Menubar.Popup>
<Menubar.Item />
<Menubar.CheckboxItem>
<Menubar.ItemIndicator />
</Menubar.CheckboxItem>
<Menubar.Group>
<Menubar.GroupLabel />
</Menubar.Group>
<Menubar.RadioGroup>
<Menubar.RadioItem>
<Menubar.ItemIndicator />
</Menubar.RadioItem>
</Menubar.RadioGroup>
<hr />
<Menubar.Submenu>
<Menubar.SubmenuTrigger />
<Menubar.Popup />
</Menubar.Submenu>
</Menubar.Popup>
</Menubar.Menu>
</Menubar.Root>API reference
Root
<div role="menubar">, the bar, around its Menus. Name it with aria-label or aria-labelledby. It is horizontal.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the bar with your own component. Read once. |
Menu
One menu of the bar: holds its open state and renders no element. Its Trigger and Popup have data-open or data-closed. Opening another menu of the bar closes this one.
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | — | Whether the menu is open, when controlled. Pair it with onOpenChange. |
defaultOpen | boolean | false | Whether the menu is open at first, when uncontrolled. |
onOpenChange | (open: boolean, details: { reason, event }) => void | — | Called with the new value when the user opens or closes the menu, and why: "trigger-press", "trigger-hover" (the pointer moved onto another item of the bar while a menu was open), "arrow-key" (a key moved along the bar), "item-press" (an item was activated), "escape-key", "outside-press", "focus-out" (Tab or Shift + Tab moved focus out) or "close-request". A controlled menu changes only if open does. |
Trigger
<button type="button" role="menuitem"> in the bar, with aria-haspopup="menu" and aria-expanded, that opens and closes its menu. The item that last had focus is the bar's tab stop, else the first. Render it inside a Menu, right before its Popup.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the item with your own button component. Read once. |
Popup
<div popover="auto" role="menu">, placed against its trigger and labelled by it; without a Trigger, give it an aria-label. Its id is generated, for the trigger to name. Render it right after the Trigger, or right after a submenu's SubmenuTrigger.
| Prop | Type | Default | Description |
|---|---|---|---|
side | "top" | "right" | "bottom" | "left" | "bottom", in a submenu the inline end | The side of its trigger it is placed against. It moves to the other side when it does not fit, and data-side says where it went. |
align | "start" | "center" | "end" | "start" | Where along that side it sits. start and end follow the writing direction. |
sideOffset | number | 0 | The gap between its trigger and the Popup, in pixels. |
alignOffset | number | 0 | The shift along its trigger's side, in pixels. |
collisionPadding | number | 5 | The space it keeps from the viewport's edges, in pixels. |
anchor | Element | VirtualElement | (() => Element | VirtualElement | undefined) | — | What it is placed against instead of its trigger: an element, a virtual element with a getBoundingClientRect(), such as a point, or a function returning one, for an element you hold in a ref. |
returnFocus | HTMLElement | ((focused) => HTMLElement | null | undefined) | — | Where focus goes once it has closed with focus inside, instead of its trigger. A function gets the element the browser focused, or null. |
render | (props) => JSX.Element | — | Renders the popup with your own component. Read once. |
Item
<div role="menuitem">. A click, Enter or Space calls its onClick and closes every open menu. Render a link with render; Enter then follows it.
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | — | Whether it is disabled: the keys and the pointer still reach it, as the menu pattern asks, but it is not activated and its onClick is not called. |
label | string | — | The text typing a character matches, when the item's content is not its label, such as an icon and a shortcut beside the text. |
render | (props) => JSX.Element | — | Renders the item with your own component, such as a link. Read once. |
CheckboxItem
<div role="menuitemcheckbox"> with aria-checked and data-checked or data-unchecked. A click and Enter change it and close the menu; Space changes it and keeps the menu open.
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | — | Whether it is checked, when controlled. Pair it with onCheckedChange. |
defaultChecked | boolean | false | Whether it is checked at first, when uncontrolled. |
onCheckedChange | (checked: boolean) => void | — | Called with the new value when the user changes it. |
disabled | boolean | — | Whether it is disabled: the keys and the pointer still reach it, as the menu pattern asks, but it is not activated and its onClick is not called. |
label | string | — | The text typing a character matches, when the item's content is not its label, such as an icon and a shortcut beside the text. |
render | (props) => JSX.Element | — | Renders the item with your own component. Read once. |
RadioGroup
<div role="group"> that holds which of its RadioItems is checked, named by its GroupLabel.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | null | — | The checked item's value, when controlled; null checks none. Pair it with onValueChange. |
defaultValue | string | null | null | The checked item's value at first, when uncontrolled. |
onValueChange | (value: string) => void | — | Called with the value of the item the user checks. |
render | (props) => JSX.Element | — | Renders the group with your own component. Read once. |
RadioItem
<div role="menuitemradio"> with aria-checked and data-checked or data-unchecked. A click and Enter check it and close the menu; Space checks it and keeps the menu open.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | — | The value its group holds while it is checked. |
disabled | boolean | — | Whether it is disabled: the keys and the pointer still reach it, as the menu pattern asks, but it is not activated and its onClick is not called. |
label | string | — | The text typing a character matches, when the item's content is not its label, such as an icon and a shortcut beside the text. |
render | (props) => JSX.Element | — | Renders the item with your own component. Read once. |
ItemIndicator
aria-hidden <span> with its item's data-checked or data-unchecked, always rendered: show a check mark by state with CSS.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the indicator with your own component. Read once. |
Group
<div role="group"> for items that belong together, named by its GroupLabel.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the group with your own component. Read once. |
GroupLabel
<div> that names its Group or RadioGroup. The keys do not reach it. Takes your id, else a generated one.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the label with your own component. Read once. |
Submenu
Holds a submenu's open state, as the Root does, and renders no element. Place it inside a Popup, around a SubmenuTrigger and the submenu's Popup.
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | — | Whether the submenu is open, when controlled. Pair it with onOpenChange. |
defaultOpen | boolean | false | Whether the submenu is open at first, when uncontrolled. |
onOpenChange | (open: boolean, details: { reason, event }) => void | — | Called with the new value, and why: "trigger-hover", "trigger-press", "arrow-key" (Right or Left Arrow, or Up, Down, Home, End or typing moving through the menu around it), "item-press", "escape-key" or "outside-press". |
delay | number | 100 | How long the pointer rests on the SubmenuTrigger before the submenu opens, in milliseconds. |
closeDelay | number | 0 | How long after the pointer has left the SubmenuTrigger and the submenu it closes, in milliseconds. The pointer may cross to the submenu diagonally without a delay. |
SubmenuTrigger
<div role="menuitem" aria-haspopup="menu"> with aria-expanded and data-open or data-closed: an item of the menu around it that opens its submenu, which it labels.
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | — | Whether it is disabled: the keys and the pointer still reach it, as the menu pattern asks, but it is not activated and its onClick is not called. |
label | string | — | The text typing a character matches, when the item's content is not its label, such as an icon and a shortcut beside the text. |
render | (props) => JSX.Element | — | Renders the item with your own component. Read once. |
Keyboard
| Keys | Action |
|---|---|
| Right Arrow, Left Arrow | In the bar, moves to the next or previous item, wrapping; with a menu open, opens that item's menu, focus staying on the item. In a menu, on an item with no submenu, closes the menus and does the same; Left Arrow in a submenu closes it instead, and Right Arrow on a SubmenuTrigger opens its submenu. Mirrored right to left. |
| Home, End | Moves to the first or last item, of the bar or of the menu that has focus. |
| A character | Moves to the next item, of the bar or of the menu that has focus, whose label starts with it; characters typed within a second make one string. |
| Down Arrow | On an item of the bar, opens its menu with focus on its first item. In a menu, moves to the next item, from the last to the first. |
| Up Arrow | On an item of the bar, opens its menu with focus on its last item. In a menu, moves to the previous item, from the first to the last. |
| Enter, Space | On an item of the bar, opens its menu, or moves into it if open, with focus on its first item. On an item, activates it and closes the menu; Space on a checkbox or radio item changes it and keeps the menu open. On a SubmenuTrigger, opens the submenu with focus on its first item. |
| Escape | Closes the menu that has focus, or the open menu of the focused item of the bar, and returns focus to the item it opened from. |
| Tab, Shift + Tab | Closes every menu and moves focus out of the bar. |