Skip to content

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/menubar

To copy the component by hand, install its dependencies:

pnpm add @base-ui/react cn lucide-react

Then copy the source into components/ui/menubar.tsx:

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.

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

KeyAction
TabMoves 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.
EnterOpens the menu of the focused trigger, or chooses the focused item.
EscCloses the open menu and returns focus to its trigger.

Base UI provides:

  • A container with the menubar role, 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, menuitemcheckbox and menuitemradio roles.
  • modal, on by default, which keeps the page behind an open menu inert, and orientation and disabled on 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

Do

Group commands by what they act on, so each menu holds several related items.

Don’t

Don’t make a menu for one command. The bar fills with triggers that could be buttons.

API reference

The Base UI Menubar container. It renders a div styled as a bar. It accepts every Base UI Menubar prop.

PropTypeDefaultDescription
loopFocus?booleantrueWraps keyboard focus from the last item to the first.
modal?booleantrueWhether the menubar blocks the page while a menu is open.
disabled?booleanfalseDisables the whole menubar.
orientation?"horizontal" | "vertical""horizontal"The direction of the bar.
className?stringNoneClasses merged onto the bar. Use it to set the width.

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.

The Base UI Menu.Trigger, styled as a bar item. It renders a button. It accepts every Base UI Menu trigger prop.

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.

PropTypeDefaultDescription
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?number8The distance in pixels from the trigger.
alignOffset?number-4The shift in pixels along the alignment axis.
className?stringNoneClasses merged onto the popup. Use it to set the width.

The Base UI Menu.Group, which groups items. Put a MenubarLabel in it to name the group.

The Base UI Menu.GroupLabel, styled as a label.

PropTypeDefaultDescription
inset?booleanNoneIndents the label to line up with items that have a check mark.

The Base UI Menu.Item. It accepts every Base UI Menu item prop and the props below.

PropTypeDefaultDescription
variant?"default" | "destructive""default"A destructive item is coloured as a warning.
inset?booleanNoneIndents the item to line up with items that have a check mark.
disabled?booleanNoneStops the item from being chosen.
onClick?(event) => voidNoneCalled when the item is chosen.
closeOnClick?booleantrueWhether choosing the item closes the menu.

The Base UI Menu.CheckboxItem, with a check mark at the start.

PropTypeDefaultDescription
checked?booleanNoneWhether the item is checked, when controlled.
defaultChecked?booleanfalseWhether the item is checked on first render, when uncontrolled.
onCheckedChange?(checked: boolean, eventDetails) => voidNoneCalled when the item is toggled.
inset?booleanNoneIndents the item.

MenubarRadioGroup is the Base UI Menu.RadioGroup, and MenubarRadioItem the Menu.RadioItem, with a check mark at the start.

PropTypeDefaultDescription
value?stringNoneThe value of the selected item, when controlled.
defaultValue?stringNoneThe value of the item selected on first render, when uncontrolled.
onValueChange?(value: string, eventDetails) => voidNoneCalled when the selection changes.
PropTypeDefaultDescription
valuestringNoneThe value that the item stands for.
disabled?booleanNoneStops the item from being chosen.

The Base UI Menu.Separator, a thin line between groups.

A span that shows a shortcut at the end of an item. It displays the hint only: it does not register the key.

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.