Skip to content
Slotted

Menubar

A horizontal bar of menus, as in a desktop application, with one tab stop and arrow keys that move between the menus.

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.

bash
pnpm add @slotted/react @slotted/theme @slotted/themes
tsx
import { Menubar, MenubarDefaultsProvider } from '@slotted/react';

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.

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.

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.

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.

sm
md
lg

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.

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.

No menu open

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.

loop off: the arrows stop at the first and last trigger
typeahead off: typing a letter moves nothing

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.

Northwind

A bar that states its own size keeps it

Accessibility

KeyEffectFocus
Enteropens the trigger's menuthe menu's first item
Spaceopens the trigger's menuthe menu's first item
ArrowDownon a horizontal bar, opens the trigger's menu; on a vertical bar, moves to the next triggerthe menu's first item, or the next trigger
ArrowUpon 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 triggerthe menu's last item, or the previous trigger
ArrowRightthe 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 menuthe next trigger, whose menu opens without focus entering it when a menu was already open
ArrowLeftthe 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 triggerthe previous trigger, or the trigger of the menu that closed
Homemoves to the first triggerthe first trigger
Endmoves to the last triggerthe last trigger
Printable charactermoves to the next trigger whose label begins with the sequence typed, which the pattern asks of a menubar as well as of a menuthe matched trigger
Escapecloses the open menu and leaves the bar focusedthe trigger whose menu closed
Tabmoves focus out of the bar entirely and closes every open menu; the bar is one tab stopthe next element in the page after the bar
Shift+Tabthe same, backwardsthe 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'Default horizontal
Which way the bar runs. A vertical bar swaps the two arrow pairs and writes aria-orientation.
size'sm' | 'md' | 'lg'Default md
The step every trigger and every menu on the bar takes.
loopbooleanDefault true
Whether the arrows wrap past the ends of the bar.
typeaheadbooleanDefault true
Whether typing moves to the trigger whose label begins with it.
disabledbooleanDefault false
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

NamePayloadWhen
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

PartElementRequiredPurpose
rootdiv, or the element render returnsYesThe 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.
triggerbuttonYesOne 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

TokenDefault
--slotted-menubar-gapvar(--slotted-space-1, 0.125rem)
--slotted-menubar-highlightvar(--slotted-tone-secondary-subtle-hover)
--slotted-menubar-highlight-activevar(--slotted-tone-secondary-subtle-active)
--slotted-menubar-textvar(--slotted-text, #0f172a)
--slotted-menubar-trigger-font-weightvar(--slotted-font-weight-medium, 500)
--slotted-menubar-trigger-radiusvar(--slotted-control-radius)

Theme tokens it reads

TokenDefault
--slotted-control-font-size-lgcomfortable 14px · compact 13px · dense 12px
--slotted-control-font-size-mdcomfortable 13px · compact 12px · dense 12px
--slotted-control-font-size-smcomfortable 12px · compact 12px · dense 12px
--slotted-control-height-lgcomfortable 38px · compact 32px · dense 30px
--slotted-control-height-mdcomfortable 34px · compact 28px · dense 26px
--slotted-control-height-smcomfortable 30px · compact 26px · dense 24px
--slotted-control-padding-inline-lgcomfortable 16px · compact 12px · dense 10px
--slotted-control-padding-inline-mdcomfortable 13px · compact 10px · dense 8px
--slotted-control-padding-inline-smcomfortable 10px · compact 8px · dense 6px
--slotted-control-radiusvar(--slotted-radius-sm)
--slotted-control-transition-durationvar(--slotted-duration-short)
--slotted-control-transition-easingvar(--slotted-easing-standard)
--slotted-disabled-backgroundSet per scheme by the theme
--slotted-disabled-foregroundSet per scheme by the theme
--slotted-duration-short120ms
--slotted-easing-standardcubic-bezier(0.2, 0, 0, 1)
--slotted-focus-ring-colorSet per scheme by the theme
--slotted-focus-ring-widthvar(--slotted-border-width-thick)
--slotted-font-family-sans'Geist Variable', ui-sans-serif, system-ui, sans-serif
--slotted-font-weight-medium500
--slotted-space-10.125rem
--slotted-textSet per scheme by the theme
--slotted-tone-secondary-subtle-activeSet per scheme by the theme
--slotted-tone-secondary-subtle-hoverSet per scheme by the theme