Skip to content

Menubar

A horizontal bar of menus, such as an application’s File, Edit, and View. Render a Menubar.Menu for each menu inside it. Left and Right Arrow move focus between the triggers, and while one menu is open they open the neighboring menu. The ends wrap around.

Built on bits-ui Menubar.

<Menubar>
<Menubar.Menu>
<Menubar.Trigger>File</Menubar.Trigger>
<Menubar.Content>
<Menubar.Item onclick={open}>Open</Menubar.Item>
</Menubar.Content>
</Menubar.Menu>
</Menubar>
  • Each menu is a Menubar.Menu holding a Menubar.Trigger and a Menubar.Content.
  • Shares every item part with Menu, so the item kinds, submenus, shortcuts, and selection are documented there.
  • With one menu open, moving to a neighbouring trigger opens that menu, and the ends wrap around.
  • Give the bar an aria-label so assistive technology can name it.

Every part is a property on Menubar. Menubar renders the role="menubar" element, Menubar.Menu renders no element, and Menubar.Content portals itself to the body.

<Menubar>
<Menubar.Menu>
<Menubar.Trigger />
<Menubar.Content>
<Menubar.Item />
<Menubar.Separator />
</Menubar.Content>
</Menubar.Menu>
</Menubar>

Project, Edit, and View menus with shortcut hints, a toggle, a submenu, a separator, and a destructive item.

Code
Basic.svelte
<script lang="ts">
import { Menubar } from "@viamrobotics/prime-ui";
let showGrid = $state(true);
</script>
<div class="not-content">
<Menubar aria-label="Application">
<Menubar.Menu>
<Menubar.Trigger>Project</Menubar.Trigger>
<Menubar.Content aria-label="Project">
<Menubar.Item>
New
<Menubar.Shortcut>⌘N</Menubar.Shortcut>
</Menubar.Item>
<Menubar.Item>
Open
<Menubar.Shortcut>⌘O</Menubar.Shortcut>
</Menubar.Item>
<Menubar.Sub>
<Menubar.SubTrigger>Export as</Menubar.SubTrigger>
<Menubar.SubContent>
<Menubar.Item>PNG</Menubar.Item>
<Menubar.Item>SVG</Menubar.Item>
<Menubar.Item>PDF</Menubar.Item>
</Menubar.SubContent>
</Menubar.Sub>
<Menubar.Separator />
<Menubar.Item variant="danger">Delete project</Menubar.Item>
</Menubar.Content>
</Menubar.Menu>
<Menubar.Menu>
<Menubar.Trigger>Edit</Menubar.Trigger>
<Menubar.Content aria-label="Edit">
<Menubar.Item>
Undo
<Menubar.Shortcut>⌘Z</Menubar.Shortcut>
</Menubar.Item>
<Menubar.Item>
Redo
<Menubar.Shortcut>⇧⌘Z</Menubar.Shortcut>
</Menubar.Item>
</Menubar.Content>
</Menubar.Menu>
<Menubar.Menu>
<Menubar.Trigger>View</Menubar.Trigger>
<Menubar.Content aria-label="View">
<Menubar.CheckboxItem bind:checked={showGrid}>
Show grid
</Menubar.CheckboxItem>
<Menubar.Item>Zoom to fit</Menubar.Item>
</Menubar.Content>
</Menubar.Menu>
</Menubar>
</div>

Disabled trigger

Disabled and danger items

Every part that renders an element takes class on that element. Menubar lays its triggers out in a row, and Menubar.Trigger is a text button that highlights on hover, on focus, and while its menu is open. Menubar.Content wears z-menu; see z layers. The data attributes on the item parts are the same as on Menu.

KeyAction
ArrowRight / ArrowLeftOn a trigger, moves focus to the next or previous trigger, wrapping.
ArrowDown / Enter / SpaceOn a trigger, opens its menu and focuses the first item.
ArrowRight / ArrowLeftIn an open menu, opens the next or previous menu, or opens and closes a submenu.
ArrowDown / ArrowUpMoves focus to the next or previous item.
Home / EndMoves focus to the first or last item.
Typed charactersFocuses the next item whose text starts with them.
Enter / SpaceOn an item, activates it and closes the menu.
EscapeCloses the menu and returns focus to its trigger.

Takes extraClasses, children, ...restProps }: Props as its props.

A toggle in a Menu.Content. Bind checked two-way, or as a getter and setter to veto or transform a change. A check icon leads the label while checked. The menu closes after a toggle.

<Menu.CheckboxItem bind:checked={showHidden}>Show hidden</Menu.CheckboxItem>
PropTypeDefaultDescription
idstringElement id. null is not accepted.
bind:checkedbooleanfalseChecked state. Two-way bindable.
disabledbooleanfalsePrevent the item from being toggled.
childrenSnippet<[{ checked: boolean }]>The item's content. Receives { checked }.

Plus everything in Omit<HTMLAttributes<HTMLDivElement>, 'children'>, spread onto the element.

The floating panel of a Menubar, portaled to the body. It holds Menubar.Item parts and the other menu parts. Escape closes it and returns focus to the trigger. A press outside closes it and leaves focus on the control it pressed.

PropTypeDefaultDescription
idstringElement id. null is not accepted.
dir'ltr' | 'rtl'Text direction of the panel. auto is not accepted, because positioning needs a concrete direction.
side'top' | 'right' | 'bottom' | 'left''bottom'Which side of the trigger the panel opens on.
align'start' | 'center' | 'end''start'Alignment of the panel along the trigger's edge.
sideOffsetnumber4Gap in pixels between the trigger and the panel.
alignOffsetnumber0Shift in pixels along the alignment axis.
portalPortalTargettrueWhere the panel renders: true at the end of <body>, false in place, or inside an element or the first match of a CSS selector.

Plus everything in HTMLAttributes<HTMLDivElement>, spread onto the element.

A set of related items in a Menu.Content, labeled by its Menu.GroupHeading.

PropTypeDefaultDescription
idstringElement id. null is not accepted.

Plus everything in HTMLAttributes<HTMLDivElement>, spread onto the element.

A non-interactive heading that labels the Menu.Group or Menu.RadioGroup it sits in. It must be a child of one of those, and rendering it directly in the content throws when the menu opens.

Takes extraClasses, children, ...restProps }: Props as its props.

An action in a Menu.Content. Activation is the native onclick, which fires for a click, Enter, and Space. The menu closes after the item is activated. A leading icon is part of the children.

<Menu.Item onclick={rename}>
<Icon icon={mdiPencil} size="sm" />
Rename
</Menu.Item>
PropTypeDefaultDescription
idstringElement id. null is not accepted.
variant'default' | 'danger''default'Visual treatment. danger marks a destructive action.
disabledbooleanfalsePrevent the item from being activated.

Plus everything in HTMLAttributes<HTMLDivElement>, spread onto the element.

One menu of a Menubar. It renders no element. It pairs a Menubar.Trigger with a Menubar.Content.

PropTypeDefaultDescription
childrenSnippetThe Menubar.Trigger and Menubar.Content of this menu.

A set of Menu.RadioItems sharing one value. Bind value two-way, or as a getter and setter to veto or transform a change.

<Menu.RadioGroup bind:value={size}>
<Menu.RadioItem value="small">Small</Menu.RadioItem>
<Menu.RadioItem value="large">Large</Menu.RadioItem>
</Menu.RadioGroup>
PropTypeDefaultDescription
idstringElement id. null is not accepted.
bind:valuestringThe selected Menu.RadioItem's value. Two-way bindable.

Plus everything in HTMLAttributes<HTMLDivElement>, spread onto the element.

A choice in a Menu.RadioGroup. Selecting it sets the group’s value to this item’s value. A check icon leads the label while selected. The menu closes after a selection.

<Menu.RadioItem value="small">Small</Menu.RadioItem>

Takes extraClasses, children: content, ...restProps }: Props as its props.

A horizontal rule between groups of items in a Menu.Content.

Takes extraClasses, children, ...restProps }: Props as its props.

A keyboard shortcut hint at the end of a Menu.Item. It is decorative and hidden from assistive technology, so the item’s accessible name is its text alone. Binding the key is the app’s job.

<Menu.Item onclick={save}>
Save
<Menu.Shortcut>⌘S</Menu.Shortcut>
</Menu.Item>

Takes extraClasses, children, ...restProps }: Props as its props.

A nested menu inside a Menu.Content. It holds one Menu.SubTrigger and one Menu.SubContent.

<Menu.Sub bind:open>
<Menu.SubTrigger>Export</Menu.SubTrigger>
<Menu.SubContent>
<Menu.Item onclick={exportPdf}>PDF</Menu.Item>
</Menu.SubContent>
</Menu.Sub>
PropTypeDefaultDescription
bind:openbooleanWhether the submenu is open. Two-way bindable.
childrenSnippetA Menu.SubTrigger and a Menu.SubContent.

The floating panel of a Menu.Sub. It opens beside its Menu.SubTrigger, closes on ArrowLeft, and returns focus to the trigger.

PropTypeDefaultDescription
idstringElement id. null is not accepted.
dir'ltr' | 'rtl'Text direction of the panel. auto is not accepted, because positioning needs a concrete direction.
sideOffsetnumber4Gap in pixels between the trigger and the panel.
alignOffsetnumber-4Shift in pixels along the trigger's edge, so the first item lines up with the trigger.

Plus everything in HTMLAttributes<HTMLDivElement>, spread onto the element.

The item in a Menu.Content that opens a Menu.Sub. It opens on hover, Enter, Space, and ArrowRight. A trailing chevron marks it as a submenu.

Takes extraClasses, children, ...restProps }: Props as its props.

The text button that opens a menu of a Menubar. It renders a real <button> with its own look. Arrow Down on the focused trigger opens the menu and focuses the first item.

Takes extraClasses, children, ...restProps }: Props as its props.