Menubar
A persistent bar of menus for an application that has many commands, in the style of a desktop app.
Base UI, not React Aria
React Aria has no menubar primitive, so this component is the official Base UI version, made self-contained. Its parts follow the Base UI API: items take disabled, not isDisabled, checkbox and radio items take checked and value, and there is no onAction. These parts cannot be mixed with the React Aria parts of Dropdown Menu or Context Menu.
Installation
pnpm dlx shadcn@latest add @silkware/menubarTo copy the component by hand, install its dependencies:
pnpm add @base-ui/react cn lucide-reactThen copy the source into components/ui/menubar.tsx:
"use client"
import * as React from "react"import { Menu as MenuPrimitive } from "@animate-ui/react/primitives/base-ui/menu"import { Menubar as MenubarPrimitive } from "@animate-ui/react/primitives/base-ui/menubar"import { cn } from "cn"import { CheckIcon, ChevronRightIcon } from "lucide-react"
function DropdownMenu({ ...props }: MenuPrimitive.Root.Props) { return <MenuPrimitive.Root data-slot="dropdown-menu" {...props} />}
function DropdownMenuPortal({ ...props }: MenuPrimitive.Portal.Props) { return <MenuPrimitive.Portal data-slot="dropdown-menu-portal" {...props} />}
function DropdownMenuTrigger({ ...props }: MenuPrimitive.Trigger.Props) { return <MenuPrimitive.Trigger data-slot="dropdown-menu-trigger" {...props} />}
function DropdownMenuContent({ align = "start", alignOffset = 0, side = "bottom", sideOffset = 4, className, ...props}: MenuPrimitive.Popup.Props & Pick< MenuPrimitive.Positioner.Props, "align" | "alignOffset" | "side" | "sideOffset" >) { return ( <MenuPrimitive.Portal> <MenuPrimitive.Positioner className="isolate z-50 outline-none" align={align} alignOffset={alignOffset} side={side} sideOffset={sideOffset} > <MenuPrimitive.Popup data-slot="dropdown-menu-content" className={cn( "z-50 max-h-(--available-height) w-(--anchor-width) min-w-32 origin-(--transform-origin) overflow-x-hidden overflow-y-auto rounded-md bg-popover p-1 text-popover-foreground shadow-md ring-1 ring-foreground/10 outline-none data-closed:overflow-hidden", className )} {...props} /> </MenuPrimitive.Positioner> </MenuPrimitive.Portal> )}
function DropdownMenuGroup({ ...props }: MenuPrimitive.Group.Props) { return <MenuPrimitive.Group data-slot="dropdown-menu-group" {...props} />}
function DropdownMenuLabel({ className, inset, ...props}: MenuPrimitive.GroupLabel.Props & { inset?: boolean}) { return ( <MenuPrimitive.GroupLabel data-slot="dropdown-menu-label" data-inset={inset} className={cn( "px-2 py-1.5 text-xs font-medium text-muted-foreground data-inset:pl-8", className )} {...props} /> )}
function DropdownMenuItem({ className, inset, variant = "default", ...props}: MenuPrimitive.Item.Props & { inset?: boolean variant?: "default" | "destructive"}) { return ( <MenuPrimitive.Item data-slot="dropdown-menu-item" data-inset={inset} data-variant={variant} className={cn( "group/dropdown-menu-item relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground data-inset:pl-8 data-[variant=destructive]:text-destructive data-[variant=destructive]:focus:bg-destructive/10 data-[variant=destructive]:focus:text-destructive dark:data-[variant=destructive]:focus:bg-destructive/20 data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 data-[variant=destructive]:*:[svg]:text-destructive", className )} {...props} /> )}
function DropdownMenuSub({ ...props }: MenuPrimitive.SubmenuRoot.Props) { return <MenuPrimitive.SubmenuRoot data-slot="dropdown-menu-sub" {...props} />}
function DropdownMenuSubTrigger({ className, inset, children, ...props}: MenuPrimitive.SubmenuTrigger.Props & { inset?: boolean}) { return ( <MenuPrimitive.SubmenuTrigger data-slot="dropdown-menu-sub-trigger" data-inset={inset} className={cn( "flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground data-inset:pl-8 data-popup-open:bg-accent data-popup-open:text-accent-foreground data-open:bg-accent data-open:text-accent-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", className )} {...props} > {children} <ChevronRightIcon className="ml-auto" /> </MenuPrimitive.SubmenuTrigger> )}
function DropdownMenuSubContent({ align = "start", alignOffset = -3, side = "right", sideOffset = 0, className, ...props}: React.ComponentProps<typeof DropdownMenuContent>) { return ( <DropdownMenuContent data-slot="dropdown-menu-sub-content" className={cn( "w-auto min-w-[96px] rounded-md bg-popover p-1 text-popover-foreground shadow-lg ring-1 ring-foreground/10", className )} align={align} alignOffset={alignOffset} side={side} sideOffset={sideOffset} {...props} /> )}
function DropdownMenuRadioGroup({ ...props }: MenuPrimitive.RadioGroup.Props) { return ( <MenuPrimitive.RadioGroup data-slot="dropdown-menu-radio-group" {...props} /> )}
function DropdownMenuSeparator({ className, ...props}: MenuPrimitive.Separator.Props) { return ( <MenuPrimitive.Separator data-slot="dropdown-menu-separator" className={cn("-mx-1 my-1 h-px bg-border", className)} {...props} /> )}
function DropdownMenuShortcut({ className, ...props}: React.ComponentProps<"span">) { return ( <span data-slot="dropdown-menu-shortcut" className={cn( "ml-auto text-xs tracking-widest text-muted-foreground group-focus/dropdown-menu-item:text-accent-foreground", className )} {...props} /> )}
function Menubar({ className, ...props }: MenubarPrimitive.Props) { return ( <MenubarPrimitive data-slot="menubar" className={cn( "flex h-9 items-center gap-1 rounded-md border p-1 shadow-xs", className )} {...props} /> )}
function MenubarMenu({ ...props }: React.ComponentProps<typeof DropdownMenu>) { return <DropdownMenu data-slot="menubar-menu" {...props} />}
function MenubarGroup({ ...props}: React.ComponentProps<typeof DropdownMenuGroup>) { return <DropdownMenuGroup data-slot="menubar-group" {...props} />}
function MenubarPortal({ ...props}: React.ComponentProps<typeof DropdownMenuPortal>) { return <DropdownMenuPortal data-slot="menubar-portal" {...props} />}
function MenubarTrigger({ className, ...props}: React.ComponentProps<typeof DropdownMenuTrigger>) { return ( <DropdownMenuTrigger data-slot="menubar-trigger" className={cn( "flex items-center rounded-sm px-2 py-1 text-sm font-medium outline-hidden select-none hover:bg-muted aria-expanded:bg-muted", className )} {...props} /> )}
function MenubarContent({ className, align = "start", alignOffset = -4, sideOffset = 8, ...props}: React.ComponentProps<typeof DropdownMenuContent>) { return ( <DropdownMenuContent data-slot="menubar-content" align={align} alignOffset={alignOffset} sideOffset={sideOffset} className={cn( "min-w-36 rounded-md bg-popover p-1 text-popover-foreground shadow-md ring-1 ring-foreground/10", className )} {...props} /> )}
function MenubarItem({ className, inset, variant = "default", ...props}: React.ComponentProps<typeof DropdownMenuItem>) { return ( <DropdownMenuItem data-slot="menubar-item" data-inset={inset} data-variant={variant} className={cn( "group/menubar-item gap-2 rounded-sm px-2 py-1.5 text-sm focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground data-inset:pl-8 data-[variant=destructive]:text-destructive data-[variant=destructive]:focus:bg-destructive/10 data-[variant=destructive]:focus:text-destructive dark:data-[variant=destructive]:focus:bg-destructive/20 data-disabled:opacity-50 [&_svg:not([class*='size-'])]:size-4 data-[variant=destructive]:*:[svg]:text-destructive!", className )} {...props} /> )}
function MenubarCheckboxItem({ className, children, checked, inset, ...props}: MenuPrimitive.CheckboxItem.Props & { inset?: boolean}) { return ( <MenuPrimitive.CheckboxItem data-slot="menubar-checkbox-item" data-inset={inset} className={cn( "relative flex cursor-default items-center gap-2 rounded-md py-1.5 pr-2 pl-8 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground focus:**:text-accent-foreground data-inset:pl-8 data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0", className )} checked={checked} {...props} > <span className="pointer-events-none absolute left-2 flex size-4 items-center justify-center [&_svg:not([class*='size-'])]:size-4"> <MenuPrimitive.CheckboxItemIndicator> <CheckIcon /> </MenuPrimitive.CheckboxItemIndicator> </span> {children} </MenuPrimitive.CheckboxItem> )}
function MenubarRadioGroup({ ...props}: React.ComponentProps<typeof DropdownMenuRadioGroup>) { return <DropdownMenuRadioGroup data-slot="menubar-radio-group" {...props} />}
function MenubarRadioItem({ className, children, inset, ...props}: MenuPrimitive.RadioItem.Props & { inset?: boolean}) { return ( <MenuPrimitive.RadioItem data-slot="menubar-radio-item" data-inset={inset} className={cn( "relative flex cursor-default items-center gap-2 rounded-md py-1.5 pr-2 pl-8 text-sm outline-hidden select-none focus:bg-accent focus:text-accent-foreground focus:**:text-accent-foreground data-inset:pl-8 data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", className )} {...props} > <span className="pointer-events-none absolute left-2 flex size-4 items-center justify-center [&_svg:not([class*='size-'])]:size-4"> <MenuPrimitive.RadioItemIndicator> <CheckIcon /> </MenuPrimitive.RadioItemIndicator> </span> {children} </MenuPrimitive.RadioItem> )}
function MenubarLabel({ className, inset, ...props}: React.ComponentProps<typeof DropdownMenuLabel> & { inset?: boolean}) { return ( <DropdownMenuLabel data-slot="menubar-label" data-inset={inset} className={cn( "px-2 py-1.5 text-sm font-medium data-inset:pl-8", className )} {...props} /> )}
function MenubarSeparator({ className, ...props}: React.ComponentProps<typeof DropdownMenuSeparator>) { return ( <DropdownMenuSeparator data-slot="menubar-separator" className={cn("-mx-1 my-1 h-px bg-border", className)} {...props} /> )}
function MenubarShortcut({ className, ...props}: React.ComponentProps<typeof DropdownMenuShortcut>) { return ( <DropdownMenuShortcut data-slot="menubar-shortcut" className={cn( "ml-auto text-xs tracking-widest text-muted-foreground group-focus/menubar-item:text-accent-foreground", className )} {...props} /> )}
function MenubarSub({ ...props}: React.ComponentProps<typeof DropdownMenuSub>) { return <DropdownMenuSub data-slot="menubar-sub" {...props} />}
function MenubarSubTrigger({ className, inset, ...props}: React.ComponentProps<typeof DropdownMenuSubTrigger> & { inset?: boolean}) { return ( <DropdownMenuSubTrigger data-slot="menubar-sub-trigger" data-inset={inset} className={cn( "gap-2 rounded-sm px-2 py-1.5 text-sm focus:bg-accent focus:text-accent-foreground data-inset:pl-8 data-open:bg-accent data-open:text-accent-foreground [&_svg:not([class*='size-'])]:size-4", className )} {...props} /> )}
function MenubarSubContent({ className, ...props}: React.ComponentProps<typeof DropdownMenuSubContent>) { return ( <DropdownMenuSubContent data-slot="menubar-sub-content" className={cn( "min-w-32 rounded-md bg-popover p-1 text-popover-foreground shadow-lg ring-1 ring-foreground/10", className )} {...props} /> )}
export { Menubar, MenubarPortal, MenubarMenu, MenubarTrigger, MenubarContent, MenubarGroup, MenubarSeparator, MenubarLabel, MenubarItem, MenubarShortcut, MenubarCheckboxItem, MenubarRadioGroup, MenubarRadioItem, MenubarSub, MenubarSubTrigger, MenubarSubContent,}Usage
import { Menubar, MenubarContent, MenubarGroup, MenubarItem, MenubarMenu, MenubarSeparator, MenubarShortcut, MenubarTrigger,} from "@/components/ui/menubar"<Menubar> <MenubarMenu> <MenubarTrigger>Orders</MenubarTrigger> <MenubarContent> <MenubarGroup> <MenubarItem onClick={() => createOrder()}> New order <MenubarShortcut>⌘N</MenubarShortcut> </MenubarItem> <MenubarItem>Duplicate order</MenubarItem> </MenubarGroup> <MenubarSeparator /> <MenubarGroup> <MenubarItem variant="destructive">Cancel order</MenubarItem> </MenubarGroup> </MenubarContent> </MenubarMenu></Menubar>Each MenubarMenu holds one trigger and its content. Run an item's work in onClick, as in Base UI. The shortcut is a hint only: register the key yourself.
Examples
Checkboxes and radio items
MenubarCheckboxItem takes checked and onCheckedChange. MenubarRadioGroup takes value and onValueChange, and each MenubarRadioItem a value. Use defaultChecked and defaultValue for state that you do not need to read.
Closed orders: hidden. Site: brescia.
Submenu
MenubarSub holds a MenubarSubTrigger and a MenubarSubContent. Keep a submenu to one level.
The first example at the top of this page shows the full set: shortcuts, disabled items, nested submenus, checkbox items, a radio group, and inset to line items up with those that have a check mark.
Accessibility
| Key | Action |
|---|---|
| Tab | Moves focus into the menubar, to the active trigger. The bar is one tab stop, so the next Tab leaves it. |
| → | Moves focus to the next trigger, or opens the submenu of the focused item. |
| ← | Moves focus to the previous trigger, or closes the submenu. |
| ↓ | Opens the menu of the focused trigger, or moves focus to the next item. |
| ↑ | Moves focus to the previous item. |
| Enter | Opens the menu of the focused trigger, or chooses the focused item. |
| Esc | Closes the open menu and returns focus to its trigger. |
Base UI provides:
- A container with the
menubarrole, and triggers that report whether their menu is open. - Arrow key navigation between triggers and between items.
loopFocus, on by default, wraps focus from the last to the first. - Opening a neighbouring menu by moving across the triggers while a menu is open.
- Menu items with the
menu,menuitem,menuitemcheckboxandmenuitemradioroles. modal, on by default, which keeps the page behind an open menu inert, andorientationanddisabledon the whole bar.
You provide:
- Trigger text that names the menu: "Orders", "View".
- Text on every item, and a label for a group when it needs one.
- Every command in the bar reachable another way, such as from a shortcut or a button, for the commands people use most.
Guidelines
When to use
- For an application with many commands across several areas, where people work for long periods and learn the menus: an ERP, an editor, a back office.
- When the commands are stable and the same menus are on every screen.
When not to use
- For a few actions on one object. Use a Dropdown Menu.
- For actions on an element under the pointer. Use a Context Menu.
- For moving between sections of a site. Use a navigation menu.
- On a phone. A menubar needs width, and a long press on touch is not an alternative. Use a Drawer or a Sheet.
- For choosing a value in a form. Use a Select.
Content
- Name each menu after what it holds: "Orders", "Suppliers", "View". Use one word where you can.
- Keep a bar to about six menus, and every menu to at least two items.
- Start items with a verb, in sentence case, with no full stop. Show the shortcut at the end.
- Group related items with a separator, and put destructive items last.
Grouping
Group commands by what they act on, so each menu holds several related items.
Don’t make a menu for one command. The bar fills with triggers that could be buttons.
API reference
Menubar
The Base UI Menubar container. It renders a div styled as a bar. It accepts every Base UI Menubar prop.
| Prop | Type | Default | Description |
|---|---|---|---|
loopFocus? | boolean | true | Wraps keyboard focus from the last item to the first. |
modal? | boolean | true | Whether the menubar blocks the page while a menu is open. |
disabled? | boolean | false | Disables the whole menubar. |
orientation? | "horizontal" | "vertical" | "horizontal" | The direction of the bar. |
className? | string | None | Classes merged onto the bar. Use it to set the width. |
MenubarMenu
The Base UI Menu.Root. It holds one MenubarTrigger and one MenubarContent. It accepts every Base UI Menu root prop, such as open, defaultOpen and onOpenChange.
MenubarTrigger
The Base UI Menu.Trigger, styled as a bar item. It renders a button. It accepts every Base UI Menu trigger prop.
MenubarContent and MenubarPortal
Renders the portal, the positioner and the popup of the menu. It accepts every Base UI Menu popup prop and the positioning props below. MenubarPortal is the portal on its own.
| Prop | Type | Default | Description |
|---|---|---|---|
side? | "top" | "right" | "bottom" | "left" | "inline-start" | "inline-end" | "bottom" | The side of the trigger where the menu opens. |
align? | "start" | "center" | "end" | "start" | How the menu lines up with the trigger. |
sideOffset? | number | 8 | The distance in pixels from the trigger. |
alignOffset? | number | -4 | The shift in pixels along the alignment axis. |
className? | string | None | Classes merged onto the popup. Use it to set the width. |
MenubarGroup
The Base UI Menu.Group, which groups items. Put a MenubarLabel in it to name the group.
MenubarLabel
The Base UI Menu.GroupLabel, styled as a label.
| Prop | Type | Default | Description |
|---|---|---|---|
inset? | boolean | None | Indents the label to line up with items that have a check mark. |
MenubarItem
The Base UI Menu.Item. It accepts every Base UI Menu item prop and the props below.
| Prop | Type | Default | Description |
|---|---|---|---|
variant? | "default" | "destructive" | "default" | A destructive item is coloured as a warning. |
inset? | boolean | None | Indents the item to line up with items that have a check mark. |
disabled? | boolean | None | Stops the item from being chosen. |
onClick? | (event) => void | None | Called when the item is chosen. |
closeOnClick? | boolean | true | Whether choosing the item closes the menu. |
MenubarCheckboxItem
The Base UI Menu.CheckboxItem, with a check mark at the start.
| Prop | Type | Default | Description |
|---|---|---|---|
checked? | boolean | None | Whether the item is checked, when controlled. |
defaultChecked? | boolean | false | Whether the item is checked on first render, when uncontrolled. |
onCheckedChange? | (checked: boolean, eventDetails) => void | None | Called when the item is toggled. |
inset? | boolean | None | Indents the item. |
MenubarRadioGroup and MenubarRadioItem
MenubarRadioGroup is the Base UI Menu.RadioGroup, and MenubarRadioItem the Menu.RadioItem, with a check mark at the start.
| Prop | Type | Default | Description |
|---|---|---|---|
value? | string | None | The value of the selected item, when controlled. |
defaultValue? | string | None | The value of the item selected on first render, when uncontrolled. |
onValueChange? | (value: string, eventDetails) => void | None | Called when the selection changes. |
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | None | The value that the item stands for. |
disabled? | boolean | None | Stops the item from being chosen. |
MenubarSeparator
The Base UI Menu.Separator, a thin line between groups.
MenubarShortcut
A span that shows a shortcut at the end of an item. It displays the hint only: it does not register the key.
MenubarSub, MenubarSubTrigger and MenubarSubContent
MenubarSub is the Base UI Menu.SubmenuRoot. MenubarSubTrigger is the Menu.SubmenuTrigger, an item with a chevron that opens the submenu, and accepts inset. MenubarSubContent is its popup and opens to the right by default.