Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Menubar for UI SDK

File, Edit and View menus in one row, as in a desktop app, with submenus, checkbox and radio items and shortcut hints. A menubar for React on UI SDK.

On this page9 sections

A menubar is the row of menus across the top of a desktop app, File, Edit and View, each opening a list of commands for the whole window. It suits a web app that works like one: a document editor, a design tool, a code playground, where people expect every command to sit in the same place each time.

For one button that opens a few actions, use a Dropdown Menu. Links that move around a site belong in a Navigation Menu.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Menubar anatomy: trigger, root, item, separator, sub trigger, content

Usage

import {
  Menubar, MenubarContent, MenubarItem, MenubarMenu, MenubarSeparator,
  MenubarShortcut, MenubarTrigger,
} from "@/components/ui/menubar";
<Menubar>
  <MenubarMenu>
    <MenubarTrigger>File</MenubarTrigger>
    <MenubarContent>
      <MenubarItem value="new-tab">
        New Tab <MenubarShortcut>⌘T</MenubarShortcut>
      </MenubarItem>
      <MenubarItem value="new-window">New Window</MenubarItem>
      <MenubarSeparator />
      <MenubarItem value="print">Print</MenubarItem>
    </MenubarContent>
  </MenubarMenu>
  <MenubarMenu>
    <MenubarTrigger>Edit</MenubarTrigger>
    <MenubarContent>
      <MenubarItem value="undo">
        Undo <MenubarShortcut>⌘Z</MenubarShortcut>
      </MenubarItem>
      <MenubarItem value="redo">Redo</MenubarItem>
    </MenubarContent>
  </MenubarMenu>
</Menubar>

Accessibility

Menubar has role="menubar" and its triggers role="menuitem", so the bar is one tab stop and the arrow keys move between triggers, wrapping from the last to the first. Once a menu is open, ArrowLeft and ArrowRight open the neighboring menu, and so does hovering another trigger. Typing a letter finds a trigger or an item by its name, and Tab from an open menu closes it and leaves the bar. The bar's aria-label is Menu bar unless you pass one that says whose menus these are.

Tab
Moves focus into the bar, on the trigger used last, and out of it, since the bar is one tab stop. In an open menu, closes it and leaves the bar.
Arrow Right
On a trigger, moves focus to the next trigger, and from the last back to the first. In an open menu, opens the next menu, or the submenu when focus is on a submenu trigger. Right to left, the two arrows swap.
Arrow Left
On a trigger, moves focus to the previous trigger, and from the first to the last. In an open menu, opens the previous menu, or closes the submenu that has focus.
Arrow Down
On a trigger, opens its menu on the first item. In a menu, moves focus to the next item, and from the last one back to the first.
Arrow Up
On a trigger, opens its menu on the last item. In a menu, moves focus to the previous item, and from the first one to the last.
Enter
On a trigger, opens its menu on the first item. On an item, chooses it and closes the menu, while a checkbox or radio item toggles and the menu stays open. On a submenu trigger, opens the submenu.
Space
On a trigger, opens its menu on the first item. On an item, chooses it and closes the menu, while a checkbox or radio item toggles and the menu stays open. On a submenu trigger, opens the submenu.
Home
Moves focus to the first trigger, or to the first item in an open menu.
End
Moves focus to the last trigger, or to the last item in an open menu.
Escape
Closes the menu, or only the submenu that has focus, and moves focus back to the trigger that opened it.

Demos

Checkbox

View holds Always Show Bookmarks Bar and Always Show Full URLs above Reload and a disabled Force Reload, and Format holds Strikethrough, Code and Superscript. Each MenubarCheckboxItem takes checked and onCheckedChange from state, and the menu stays open while people toggle rows. The plain items carry inset, which indents their labels to line up with the checked rows.

Preview width

Radio

Profiles picks one of Andy, Benoit and Luis, with Edit and Add Profile under a separator, and Theme picks Light, Dark or System. Each MenubarRadioGroup holds its value in state, so one choice never clears the other, and the menu stays open after a pick. Keep commands such as Edit outside the group, with inset so they line up.

Preview width

In File, a Share submenu with Email link, Messages and Notes sits above Print. In Edit, a Find submenu with Find, Find Next and Find Previous sits between Undo and Redo and the clipboard commands. A submenu holds a cluster that doesn't earn a menu of its own in the bar. Hovering its trigger opens it, and so does ArrowRight.

Preview width

Icons

File puts a page, a folder and a disk in front of New File, Open Folder and Save, with ⌘N and ⌘S still at the end of their rows, and More holds Settings, Help and a destructive Delete. Every icon has aria-hidden, since the label already names the command. Icons help in a menu people open many times a day.

Preview width

RTL

The whole bar in Arabic, File to Profiles, inside a right-to-left Direction provider and a div with dir set to rtl, so the triggers run from the right and ArrowLeft moves to the next one. The menus render outside that div, so each MenubarContent and MenubarSubContent sets dir too: their text and chevrons turn around, and submenus open to the left, where ArrowLeft opens them.

Preview width

FAQ

Should I use a Menubar or a Dropdown Menu?

Use a Menubar when an app has several groups of commands that apply to the whole window, such as File, Edit and View in an editor, and people come back to them all day. Use a Dropdown Menu for one button that opens a handful of actions, such as the ellipsis on a table row or the avatar in a header.

A menubar on a marketing site or a dashboard with three commands is more chrome than it's worth. Links that move people between pages belong in a Navigation Menu.

Can I use a Menubar from the keyboard?

Yes, the way a desktop menubar works. Tab reaches the bar as one stop, ArrowLeft and ArrowRight move between triggers, and ArrowDown or Enter opens a menu. With a menu open, ArrowLeft and ArrowRight open the neighboring menu, so people can scan every menu without closing one first.

The Accessibility section lists every key for UI SDK.

How do I control a checkbox in a Menubar in React?

Pass checked and onCheckedChange, which receives the new boolean first, to each MenubarCheckboxItem and hold the flag in state, as the Checkbox demo does. UI SDK leaves the menu open after a toggle, so people can change several settings in a row. Set closeOnSelect on an item to close the menu after it instead.

How do I keep one Menubar radio item selected?

Put the choices in one MenubarRadioGroup, hold the chosen value in state, and update it from onValueChange, which receives the new value as a string. Exactly one row is marked at a time, and commands such as Edit go outside the group as plain MenubarItem rows, as in the Radio demo.

How do I open a Menubar menu from my own code?

Pass open to that MenubarMenu and update it from onOpenChange, which receives the new boolean first and an object describing the cause second. UI SDK calls it for every change it makes itself, on a click, a hover across the bar, Escape or a click outside, so your state stays in step with what's on screen.

Why don't the keyboard shortcuts shown in my Menubar work?

MenubarShortcut is a styled span at the end of a row: it prints the keys and listens for nothing. Bind each shortcut yourself, with one keydown listener on the document that checks metaKey or ctrlKey and the key, so the command runs whether or not its menu is open.

Show ⌘ on macOS and Ctrl elsewhere, and only print a hint for a shortcut that really exists. A hint that does nothing teaches people to stop trusting the rest.

Why does the page stop scrolling when a Menubar menu is open?

It doesn't on UI SDK: the bar's modal setting defaults to "pointer", so the page keeps scrolling while a menu is open, and a click outside only closes the menu, without reaching what's under it. Set modal to true on Menubar to lock scroll and make the rest of the page inert, while the bar stays usable for switching menus. If scrolling stops without it, something else has locked it, such as a modal dialog the bar sits in.

API Reference

<div> data-slot="menubar"

PropType
controller?{ focus: () → void; isOpen: () → boolean; close: (options?: ChangeOptions<MenuOpenChangeReason>) → boolean; }

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

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

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

disabled?boolean

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

Default
false
loop?boolean

Whether arrow keys wrap from the last menu to the first, between triggers and between open menus. UI SDK's default: true.

Default
loopFocus
loopFocus?boolean

The other bases' name for loop.

modal?"pointer" | boolean

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
"pointer" | boolean
Default
"pointer"
orientation?MenubarOrientation

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

Default
"horizontal"
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when the whole menubar is disabled.

data-open?string

Present while one of its menus is open.

data-orientation"horizontal" | "vertical"

Which arrow keys move between menus.

Type
"horizontal" | "vertical"
PropType
open?boolean

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

defaultOpen?boolean

Whether the menu starts open, when uncontrolled.

Default
false
onOpenChange?(open: boolean, details: ChangeDetails<MenuOpenChangeReason>) → void

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
Parameters

open – boolean

details – ChangeDetails<MenuOpenChangeReason>

Returns
void
container?DocumentFragment | Element | null

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
DocumentFragment | Element | null
controller?MenuController

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

debug?boolean

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

delay?number

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

Default
100
dismissible?boolean

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.

Default
true
items?MenuData

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

itemShortcuts?boolean

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

Default
true
loop?boolean

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

Default
true
modal?"pointer" | boolean

"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
"pointer" | boolean
Default
"pointer", or true while it presents as a sheet
onAction?(value: string, details: MenuActionDetails) → unknown

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
Parameters

value – string

details – MenuActionDetails

Returns
unknown
onCheckedChange?(checked: boolean, details: MenuCheckedChangeDetails) → void

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
Parameters

checked – boolean

details – MenuCheckedChangeDetails

Returns
void
onDismissAttempt?(details: DismissAttemptDetails<Extract<Reason, DismissAttemptReason>>) → void

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: DismissAttemptDetails<Extract<Reason, DismissAttemptReason>>) → void
Parameters

details – DismissAttemptDetails<Extract<Reason

DismissAttemptReason>>

Returns
void
onOpenChangeComplete?(open: boolean) → void

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

Parameters

open – boolean

Returns
void
onPayloadChange?(payload: unknown, details: ChangeDetails<MenuPayloadChangeReason>) → void

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
Parameters

payload – unknown

details – ChangeDetails<MenuPayloadChangeReason>

Returns
void
onValueChange?(value: string, details: MenuValueChangeDetails) → void

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
Parameters

value – string

details – MenuValueChangeDetails

Returns
void
pendingAnnouncement?string

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.

Default
"Loading"
presentation?PresentationSetting<"popover">

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"
PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
PropType
controller?PopupController<any, MenuOpenChangeReason, MenuOpenOptions, string, MenuPayloadChangeReason>

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?boolean

Stops the trigger from opening the menu. A native button can't be focused or pressed, unless focusableWhenDisabled is set.

Default
false
focusableWhenDisabled?boolean

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.

Default
false
payload?unknown

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

shortcut?string

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

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

PropType
align?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.

Default
"start"
alignOffset?number

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

Default
-4
anchor?string | Anchor | null

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?number

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

Default
6
collisionPadding?number

Space to keep from the viewport edges, in pixels.

Default
8
container?DocumentFragment | Element | null

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
DocumentFragment | Element | null
finalFocus?PopupFocusTarget

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.

Default
"trigger"
keepMounted?boolean

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

Default
false
side?DropdownMenuSide

The side of the trigger it opens on. It flips when there's no room, unless avoidCollisions is off.

sideOffset?number

Gap between the anchor and the popup, in pixels. Gap between the trigger and the menu, in pixels. DropdownMenuContent's default: 4.

Default
8
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

PropType
closeOnSelect?boolean

Whether running the item closes the menu.

Default
true
destructive?boolean

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

disabled?boolean

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

inset?boolean

Indents it to line up with items that have an icon or indicator.

label?string

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

onSelect?(event: Event) → unknown

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.

Parameters

event – Event

Returns
unknown
shortcut?string

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

value?string

Passed to onAction when the item runs.

variant?"default" | "destructive"

The visual style.

Type
"default" | "destructive"
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

<div> data-slot="menu-checkbox-item"

PropType
checked?boolean

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

defaultChecked?boolean

Whether the item starts checked, when uncontrolled.

Default
false
onCheckedChange?(checked: boolean, details: ChangeDetails<MenuCheckedChangeReason>) → void

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

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

checked – boolean

details – ChangeDetails<MenuCheckedChangeReason>

Returns
void
closeOnSelect?boolean

Whether toggling closes the menu.

Default
false
disabled?boolean

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

inset?boolean

Indents it to line up with items that have an icon or indicator.

label?string

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

value?string

Names the item in the menu's onCheckedChange; a generated id without one.

Default
id
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-checked?string

Present on checked checkbox items, and on the radio item that's its group's value.

data-highlighted?string

Present on the item with keyboard focus or under the pointer.

data-inset?string

Mirrors inset.

data-unchecked?string

Present on items that aren't checked.

PropType
value?T | null

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

Type
T | null
defaultValue?T | null

The value selected at first, when uncontrolled.

Type
T | null
onValueChange?(value: T, details: ChangeDetails<MenuValueChangeReason>) → void

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

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

value – T

details – ChangeDetails<MenuValueChangeReason>

Returns
void
name?string

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.

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)

<div> data-slot="menu-radio-item"

PropType
valuestring

Selected in the group when the item is chosen.

closeOnSelect?boolean

Whether choosing the item closes the menu.

Default
false
disabled?boolean

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

inset?boolean

Indents it to line up with items that have an icon or indicator.

label?string

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

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-checked?string

Present on checked checkbox items, and on the radio item that's its group's value.

data-highlighted?string

Present on the item with keyboard focus or under the pointer.

data-unchecked?string

Present on items that aren't checked.

data-slot="menubar-label"

PropType
inset?boolean

Indents it to line up with items that have an icon or indicator.

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

data-slot="menubar-shortcut"

PropType
className?string

Merged with the part's classes.

PropType
open?boolean

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

defaultOpen?boolean

Whether the submenu starts open, when uncontrolled.

onOpenChange?(open: boolean, details: ChangeDetails<MenuOpenChangeReason>) → void

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
Parameters

open – boolean

details – ChangeDetails<MenuOpenChangeReason>

Returns
void
controller?MenuController

Opens, closes and inspects the submenu. Create one with createMenuController(). Triggers outside the menu can't connect to it.

debug?boolean

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

delay?number

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

Default
100
loop?boolean

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

Default
true
onOpenChangeComplete?(open: boolean) → void

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

Parameters

open – boolean

Returns
void
PropType
disabled?boolean

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

inset?boolean

Indents it to line up with items that have an icon or indicator.

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

PropType
align?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.

Default
"start"
alignOffset?number

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

anchor?string | Anchor | null

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?number

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

Default
6
collisionPadding?number

Space to keep from the viewport edges, in pixels.

Default
8
container?DocumentFragment | Element | null

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
DocumentFragment | Element | null
finalFocus?PopupFocusTarget

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.

Default
"trigger"
keepMounted?boolean

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

Default
false
side?DropdownMenuSide

The side of the trigger it opens on. It flips when there's no room, unless avoidCollisions is off.

sideOffset?number

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

Default
4
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.