Menubar

A row of menus, like an application's File and View.

Put a Menu inside for each one. Menubar re-exports the menu parts, so one import covers both. Once a menu is open, the arrow keys move between menus.

Preview
basic.tsx
import {
  Menu,
  MenuCheckboxItem,
  MenuContent,
  MenuItem,
  MenuSeparator,
  Menubar,
  MenuTrigger,
} from "~/components/ui/menubar"

export default function Example() {
  return (
    <Menubar aria-label="Edition">
      <Menu>
        <MenuTrigger>Edition</MenuTrigger>
        <MenuContent className="w-48">
          <MenuItem value="new" shortcut="mod+n">
            New edition
          </MenuItem>
          <MenuItem value="open" shortcut="mod+o">
            Open…
          </MenuItem>
          <MenuSeparator />
          <MenuItem value="export">Export</MenuItem>
        </MenuContent>
      </Menu>
      <Menu>
        <MenuTrigger>View</MenuTrigger>
        <MenuContent className="w-48">
          <MenuCheckboxItem value="guides" defaultChecked>
            Guides
          </MenuCheckboxItem>
          <MenuCheckboxItem value="baselines">Baselines</MenuCheckboxItem>
          <MenuCheckboxItem value="ads" defaultChecked>
            Ad spaces
          </MenuCheckboxItem>
        </MenuContent>
      </Menu>
    </Menubar>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesMenuTextprimitive
Used byNothing yet.
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/menubar.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/menubar.tsx
import { Menubar as MenubarPrimitive } from "@ui-sdk/react/menubar"
import type * as React from "react"

import { textVariants } from "~/components/ui/text"
import { cnState } from "~/lib/utils"

/**
 * A row of menus, like an application's File and Edit. Put a `Menu` from
 * `~/components/ui/menu` inside for each one; their triggers take the bar's style.
 */
function Menubar({ className, ...props }: React.ComponentProps<typeof MenubarPrimitive>) {
  return (
    <MenubarPrimitive
      className={cnState(
        [
          textVariants({ size: "sm", weight: "medium" }),
          "flex h-9 w-fit items-center gap-3xs rounded-lg border border-border bg-background p-3xs",
          "*:data-[slot=menu-trigger]:flex *:data-[slot=menu-trigger]:h-full *:data-[slot=menu-trigger]:cursor-default *:data-[slot=menu-trigger]:items-center *:data-[slot=menu-trigger]:rounded-md *:data-[slot=menu-trigger]:px-sm *:data-[slot=menu-trigger]:outline-none *:data-[slot=menu-trigger]:select-none",
          "*:data-[slot=menu-trigger]:hover:bg-muted *:data-[slot=menu-trigger]:data-open:bg-muted *:data-[slot=menu-trigger]:focus-visible:ring-3 *:data-[slot=menu-trigger]:focus-visible:ring-ring/30",
        ].join(" "),
        className,
      )}
      {...props}
    />
  )
}

export { Menubar }

// Menubar is built from menus; re-exported so one import covers both.
export * from "~/components/ui/menu"

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Menubar

A row of menus, like an application's File and Edit. Put a Menu from ~/components/ui/menu inside for each one; their triggers take the bar's style.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: MenubarState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: MenubarState) => string)

controller

Opens, closes and focuses the menubar from anywhere. Create one with createMenubarController().

Type { focus: () => void; isOpen: () => boolean; close: (options?: ChangeOptions<MenuOpenChangeReason>) => boolean; }

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

disabled

Stops every menu from opening. Triggers stay focusable with aria-disabled, so the menubar keeps its tab stop and stays discoverable.

Type boolean · Default false

loop

Whether arrow keys wrap from the last menu to the first, between triggers and between open menus.

Type boolean · Default true

modal

The modal setting of every menu in the menubar that doesn't set its own. "pointer": an outside press only closes the open menu. true: also locks scroll, traps focus and makes the rest of the page inert, except the menubar. false: outside presses reach what they hit.

Type boolean | "pointer" · Default "pointer"

orientation

Which arrow keys move between menus: left and right, or up and down. A vertical menubar opens its menus to the side.

Type MenubarOrientation · Default "horizontal"

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<MenubarState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: MenubarState) => CSSProperties)

Menu

A list of actions and options, opened from a button or a right-click. Put its trigger and content inside.

container

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type Element | DocumentFragment | null

controller

Opens, closes and inspects the menu from anywhere, and connects triggers outside it. Create one with createMenuController().

Type PopupController<any, MenuOpenChangeReason, MenuOpenOptions, string, MenuPayloadChangeReason>

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

defaultOpen

Whether the menu starts open, when uncontrolled.

Type boolean · Default false

delay

Milliseconds the pointer rests on a submenu trigger before its submenu opens.

Type number · Default 100

dismissible

Whether Escape, an outside press, focus or the window leaving, Android's back gesture and a sheet's swipe can close it. Triggers, Close parts and the controller always can. Pass false while it holds unsaved work, and ask in onDismissAttempt.

Type boolean · Default true

items

The menu as data. Renders the content for you, unless you place a content part of your own, and runs the items' shortcuts.

Type MenuData

itemShortcuts

Whether a data menu runs its items' shortcuts while closed.

Type boolean · Default true

loop

Whether arrow keys wrap from the last item to the first.

Type boolean · Default true

modal

"pointer": an outside press only closes the menu. true: also locks scroll, traps focus and makes the rest of the page inert. false: outside presses reach what they hit.

Type boolean | "pointer" · Default "pointer", or true while it presents as a sheet

onAction

Runs for every plain item, including items in submenus, with the item's value. Return a Promise to mark the item pending until it settles: it gets data-pending, aria-busy and aria-disabled, the menu stays open, and choosing it again waits. It closes once the Promise resolves, and a rejection keeps it open with the reason in the item's error state until the next try.

Type (value: string, details: MenuActionDetails) => unknown

onCheckedChange

Runs when any checkbox item in the menu or its submenus toggles, with the item's value in details.value. Made for data menus.

Type (checked: boolean, details: MenuCheckedChangeDetails) => void

onDismissAttempt

Runs when dismissible refused a dismissal made on purpose: Escape, an outside press, back or a swipe. Focus or the window leaving is refused without it. Ask "Discard changes?" here.

Type (details: { reason: "outside-press"; event?: MouseEvent; } | { reason: "escape-key"; event?: KeyboardEvent; } | { reason: "back"; event?: Event; } | { reason: "swipe"; event?: PointerEvent | TouchEvent; }) => void

onOpenChange

Runs when the menu wants to open or close. details.reason says why, and details.cancel() stops it.

Type (open: boolean, details: ChangeDetails<MenuOpenChangeReason>) => void

onOpenChangeComplete

Runs once the menu has finished opening or closing, after any animation.

Type (open: boolean) => void

onPayloadChange

Runs when the payload the content shows changes: a trigger or context area with another payload opens the menu, or takes the open menu over, which fires no onOpenChange, and so does a controller's open({ payload }) on the open menu. details.cancel() keeps the previous payload.

Type (payload: unknown, details: ChangeDetails<MenuPayloadChangeReason>) => void

onValueChange

Runs when any radio group in the menu or its submenus changes, with the group in details.group. Made for data menus.

Type (value: string, details: MenuValueChangeDetails) => void

open

Whether the menu is open. Pass it to control the menu.

Type boolean

pendingAnnouncement

What screen readers hear once an item has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.

Type string · Default "Loading"

presentation

How the menu presents. "sheet" slides it up from the bottom of the screen, where it can be swiped away and submenus open in its place, and "auto" does that on small touch screens. Decided each time it opens; the server renders "popover" for "auto". A context menu and a menu in a menubar are always popovers.

Type PresentationSetting<"popover"> · Default "popover"

MenuCheckboxItem

An item that turns a setting on and off, with its check mark built in.

checked

Whether the item is checked. Pass it to control the item.

Type boolean

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: MenuCheckboxItemState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: MenuCheckboxItemState) => string)

closeOnSelect

Whether toggling closes the menu.

Type boolean · Default false

defaultChecked

Whether the item starts checked, when uncontrolled.

Type boolean · Default false

disabled

Keeps the item focusable but stops it from toggling, as the menu pattern asks.

Type boolean

label

The text typeahead matches, when the content isn't plain text. It isn't the accessible name: use aria-label for that.

Type string

onCheckedChange

Runs when the item wants to toggle. details.reason says why, and details.cancel() stops it.

Type (checked: boolean, details: ChangeDetails<MenuCheckedChangeReason>) => void

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<MenuCheckboxItemState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: MenuCheckboxItemState) => CSSProperties)

value*

Identifies the item, in the menu's onCheckedChange.

Type string

MenuContent

The menu's popup. Placed below its trigger with a 4px gap unless you set side and sideOffset.

align

How the popup lines up with the anchor along that side. start and end follow the reading direction. How the menu lines up with the trigger along that side.

Type "center" | "start" | "end" · Default "start"

alignOffset

Shift along the anchor, in pixels. Shift along the trigger, in pixels. Submenus default to lining up their first item with the trigger.

Type number

anchor

Positions the popup against this instead of its trigger: an element, a CSS selector, a point like { x, y }, or anything with getBoundingClientRect(). It's read on every check, so it can move. A controller's open({ anchor }) wins for that open.

Type string | Anchor | null

arrowPadding

Space to keep between the arrow and the popup's corners, in pixels.

Type number · Default 6

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: MenuContentState<unknown>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: MenuContentState<unknown>) => string)

collisionPadding

Space to keep from the viewport edges, in pixels.

Type number · Default 8

container

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type Element | DocumentFragment | null

finalFocus

Where focus goes when the menu closes while focus is inside it. "trigger" returns it to the trigger and "none" leaves it. A CSS selector, an element or a function picks a specific element.

Type PopupFocusTarget · Default "trigger"

keepMounted

Whether the menu stays in the page, hidden, while closed. Its content keeps its state between opens.

Type boolean · Default false

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<MenuContentState<unknown>>

side

Which side of the anchor the popup opens on. Flips to the other side when there isn't room. Which side of the trigger the menu opens on. Submenus open to the side, mirrored in right-to-left layouts.

Type "start" | "end" | "top" | "bottom" | "left" | "right" · Default "bottom"

sideOffset

Gap between the anchor and the popup, in pixels. Gap between the trigger and the menu, in pixels.

Type number · Default 4

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: MenuContentState<unknown>) => CSSProperties)

MenuGroup

A group of items, named by its MenuGroupLabel.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: {}) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: {}) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<{}>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: {}) => CSSProperties)

MenuGroupLabel

Names a group of items.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: {}) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: {}) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<{}>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: {}) => CSSProperties)

MenuItem

An action. Its shortcut shows as a hint at the end of the row.

variantours

destructive marks an action that deletes or can't be undone.

Type "default" | "destructive" · Default default

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: MenuItemState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: MenuItemState) => string)

closeOnSelect

Whether running the item closes the menu.

Type boolean · Default true

destructive

Styles the item as dangerous, like Delete. Sets data-destructive.

Type boolean

disabled

Keeps the item focusable but stops it from running, as the menu pattern asks. Sets data-disabled and aria-disabled.

Type boolean

label

The text typeahead matches, when the content isn't plain text. It isn't the accessible name: use aria-label for that.

Type string

onSelect

Runs when the item is selected, by pointer or keyboard, with the event that selected it, or a new select Event when no DOM event did. Call event.preventDefault() to keep the menu open and skip onAction. Return a Promise to mark the item pending until it settles, like onAction.

Type (event: Event) => unknown

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<MenuItemState>

shortcut

A keyboard shortcut like "mod+n". Shows ⌘N on Apple platforms and Ctrl+N elsewhere, and sets aria-keyshortcuts.

Type string

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: MenuItemState) => CSSProperties)

value

Passed to onAction when the item runs.

Type string

MenuRadioGroup

Radio items that share one value. T is inferred from value, defaultValue or onValueChange.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: MenuRadioGroupState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: MenuRadioGroupState) => string)

defaultValue

The value selected at first, when uncontrolled.

Type string | null

name

Identifies the group in the menu's onValueChange. Defaults to a generated id, and the group then keeps its choice by its place among the menu's groups. Give a group that's shown only some of the time a name, so it keeps its own.

Type string

onValueChange

Runs when the selection wants to change. details.reason says why, and details.cancel() stops it.

Type (value: T, details: ChangeDetails<MenuValueChangeReason>) => void

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<MenuRadioGroupState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: MenuRadioGroupState) => CSSProperties)

value

The selected value. Pass it to control the group. null selects nothing.

Type string | null

MenuRadioItem

One choice in a MenuRadioGroup, with its dot built in.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: MenuRadioItemState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: MenuRadioItemState) => string)

closeOnSelect

Whether choosing the item closes the menu.

Type boolean · Default false

disabled

Keeps the item focusable but stops it from being chosen, as the menu pattern asks.

Type boolean

label

The text typeahead matches, when the content isn't plain text. It isn't the accessible name: use aria-label for that.

Type string

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<MenuRadioItemState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: MenuRadioItemState) => CSSProperties)

value*

Selected in the group when the item is chosen.

Type string

MenuSeparator

A line between groups of items.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: {}) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: {}) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<{}>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: {}) => CSSProperties)

MenuShortcut

A keyboard hint at the end of an item, when you render one yourself.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: {}) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: {}) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<{}>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: {}) => CSSProperties)

MenuSub

A submenu. Put its MenuSubTrigger and MenuSubContent inside, in a menu's content.

controller

Opens, closes and inspects the menu from anywhere, and connects triggers outside it. Create one with createMenuController().

Type PopupController<any, MenuOpenChangeReason, MenuOpenOptions, string, MenuPayloadChangeReason>

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

defaultOpen

Whether the menu starts open, when uncontrolled.

Type boolean · Default false

delay

Milliseconds the pointer rests on a submenu trigger before its submenu opens.

Type number · Default 100

loop

Whether arrow keys wrap from the last item to the first.

Type boolean · Default true

onOpenChange

Runs when the menu wants to open or close. details.reason says why, and details.cancel() stops it.

Type (open: boolean, details: ChangeDetails<MenuOpenChangeReason>) => void

onOpenChangeComplete

Runs once the menu has finished opening or closing, after any animation.

Type (open: boolean) => void

open

Whether the menu is open. Pass it to control the menu.

Type boolean

MenuSubContent

A submenu's popup.

align

How the popup lines up with the anchor along that side. start and end follow the reading direction. How the menu lines up with the trigger along that side.

Type "center" | "start" | "end" · Default "start"

alignOffset

Shift along the anchor, in pixels. Shift along the trigger, in pixels. Submenus default to lining up their first item with the trigger.

Type number

anchor

Positions the popup against this instead of its trigger: an element, a CSS selector, a point like { x, y }, or anything with getBoundingClientRect(). It's read on every check, so it can move. A controller's open({ anchor }) wins for that open.

Type string | Anchor | null

arrowPadding

Space to keep between the arrow and the popup's corners, in pixels.

Type number · Default 6

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: MenuContentState<unknown>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: MenuContentState<unknown>) => string)

collisionPadding

Space to keep from the viewport edges, in pixels.

Type number · Default 8

container

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type Element | DocumentFragment | null

finalFocus

Where focus goes when the menu closes while focus is inside it. "trigger" returns it to the trigger and "none" leaves it. A CSS selector, an element or a function picks a specific element.

Type PopupFocusTarget · Default "trigger"

keepMounted

Whether the menu stays in the page, hidden, while closed. Its content keeps its state between opens.

Type boolean · Default false

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<MenuContentState<unknown>>

side

Which side of the anchor the popup opens on. Flips to the other side when there isn't room. Which side of the trigger the menu opens on. Submenus open to the side, mirrored in right-to-left layouts.

Type "start" | "end" | "top" | "bottom" | "left" | "right" · Default "bottom"

sideOffset

Gap between the anchor and the popup, in pixels. Gap between the trigger and the menu, in pixels.

Type number · Default 4

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: MenuContentState<unknown>) => CSSProperties)

MenuSubTrigger

Opens a submenu, with its chevron built in.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: MenuTriggerState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: MenuTriggerState) => string)

disabled

Keeps the submenu trigger focusable but stops it from opening its submenu, as the menu pattern asks. Sets data-disabled and aria-disabled.

Type boolean

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<MenuTriggerState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: MenuTriggerState) => CSSProperties)

MenuTrigger

The button that opens the menu. A trigger outside the menu connects through controller.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: MenuTriggerState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: MenuTriggerState) => string)

controller

Connects a trigger outside the menu, like one per table row, to the menu with this controller.

Type PopupController<any, MenuOpenChangeReason, MenuOpenOptions, string, MenuPayloadChangeReason>

disabled

Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.

Type boolean · Default false

focusableWhenDisabled

Keeps a disabled trigger in the tab order with aria-disabled instead of the native attribute, so it can host a tooltip that says why. Presses still do nothing.

Type boolean · Default false

payload

Data for the content to render when this trigger opens the menu, like the row it belongs to.

Type unknown

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<MenuTriggerState>

shortcut

A keyboard shortcut that opens the menu from anywhere, like "mod+j". mod is Command on Apple platforms and Control elsewhere. Sets aria-keyshortcuts.

Type string

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: MenuTriggerState) => CSSProperties)