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.
Installation
Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
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.
KeyAction
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.
EscEscape
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.
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.
Submenu
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.