A desktop-style menu bar. Every menu is a native popover opened by a popovertarget button, so opening, closing on Escape or a click outside and the top layer come from the browser. CSS anchor positioning puts each menu under its trigger and flips it when there is no room. A small client script adds the keyboard model of a menubar: Left and Right move between menus, Up and Down between items, Home and End jump to the ends, typing a letter jumps to a matching item, and hovering another trigger while a menu is open switches to it.
Browser Support
Limited availability4 featuresDetails
Opens and closes every menu in the top layer, with light dismiss and Escape.
Places each menu under its trigger and flips it. Without it a small script computes the position.
Fades the menu in when it opens. Without it the menu appears at once.
Fades the menu out when it closes. Without it the menu disappears at once.
Preview
Installation
Usage
import {
Menubar,
MenubarContent,
MenubarItem,
MenubarMenu,
MenubarSeparator,
MenubarShortcut,
MenubarTrigger,
} from "@/components/ui/menubar"
export default function Example() {
return (
<Menubar>
<MenubarMenu>
<MenubarTrigger>File</MenubarTrigger>
<MenubarContent>
<MenubarItem>
New file <MenubarShortcut>⌘N</MenubarShortcut>
</MenubarItem>
<MenubarItem>Open</MenubarItem>
<MenubarSeparator />
<MenubarItem disabled>Print</MenubarItem>
</MenubarContent>
</MenubarMenu>
</Menubar>
)
}Examples
Keyboard
Tab enters the bar once. Left and Right switch menus, Down or Enter opens a menu and focuses its first item, Up opens it on the last item, Escape closes it and returns to the trigger.
- ← →Move between menus (opens the neighbour if a menu is open)
- ↓ ↑Open the menu / move between items
- Home EndFirst and last menu or item
- A–ZJump to the item that starts with the typed letters
- EscClose the menu and return focus to its trigger
Links and plain items
MenubarItem with asChild turns a link into a menu item. MenubarCheckboxItem and MenubarRadioItem keep their state in aria-checked; keepOpen leaves the menu open after a choice.
<MenubarItem asChild>
<a href="/docs">Docs</a>
</MenubarItem>
<MenubarCheckboxItem checked keepOpen>Show toolbar</MenubarCheckboxItem>
<MenubarRadioGroup>
<MenubarRadioItem checked>Normal</MenubarRadioItem>
<MenubarRadioItem>Large</MenubarRadioItem>
</MenubarRadioGroup>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| asChild | boolean | false | Render the only child, for example an <a>, instead of a button |
| inset | boolean | - | Indent the label to line up with items that have a check indicator |
| variant | "default" | "destructive" | "default" | Visual variant of the item |
| keepOpen | boolean | false | Keep the menu open after the item is chosen |
| checked | boolean | false | Initial state of a checkbox or radio item. The client script toggles it on click |
| id | string | - | id of the content element (MenubarMenu). Generated when omitted |
| className | string | - | Additional CSS classes |
Features
- Menus open, close on Escape and on a click outside without any JavaScript: popover="auto" with a popovertarget trigger.
- role="menubar", role="menu" and role="menuitem" / "menuitemcheckbox" / "menuitemradio", aria-haspopup and aria-expanded on triggers.
- Roving tab stop: Tab enters and leaves the bar in one step, arrows move inside.
- Trigger and content are linked without React context, so the parts stay server components. Keep MenubarTrigger and MenubarContent as direct children of MenubarMenu.
- No submenus yet: use a separate menu or a Popover for nested choices.