Skip to the content

Controls and surfaces

menubar

A row of menus that stays on screen, as along the top of a desktop application: File, Edit, View.

Primitives

When to use it

A working tool (an editor, a canvas, a table) with many commands that belong in named groups. The arrow keys move between the menus and through each one. Give the bar an aria-label. For one menu behind one button use dropdown-menu; for links between the pages of a site use navigation-menu or navbar.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Primitives page.

Alert dialog

The project is kept.

Collapsible

The options are closed.

Hover card

The first published program was written by Ada Lovelace.

Menubar

Command: none yet. Ruler: hidden. Zoom: 100.

Navigation menu

Toolbar

Styles: none. Saved 0 times.

One-time code

Typed: nothing yet. Sent: nothing yet.

Password

At least twelve characters.

Words for screen readers

Add it

npx shadcn@latest add @rivier-ui/menubar

The registry is named once in components.json: see Installation.

Example

Import it from @/components/ui/menubar.

<Menubar aria-label="Document">
  <MenubarMenu>
    <MenubarTrigger>File</MenubarTrigger>
    <MenubarContent>
      <MenubarItem onSelect={create}>New <MenubarShortcut>Ctrl N</MenubarShortcut></MenubarItem>
      <MenubarSeparator />
      <MenubarItem onSelect={print}>Print</MenubarItem>
    </MenubarContent>
  </MenubarMenu>
  <MenubarMenu>
    <MenubarTrigger>View</MenubarTrigger>
    <MenubarContent>
      <MenubarCheckboxItem checked={ruler} onCheckedChange={setRuler}>Ruler</MenubarCheckboxItem>
    </MenubarContent>
  </MenubarMenu>
</Menubar>

What it needs

A client component: it uses state, effects or browser behaviour. Packages: lucide-react, radix-ui.

It imports

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/components/ui/menubar.tsx
"use client";
/**
 * @name menubar
 * @category ui
 * @description A row of menus that stays on screen, as along the top of a desktop application: File, Edit, View.
 * @when A working tool (an editor, a canvas, a table) with many commands that belong in named groups. The arrow keys move between the menus and through each one. Give the bar an `aria-label`. For one menu behind one button use dropdown-menu; for links between the pages of a site use navigation-menu or navbar.
 * @example
 * <Menubar aria-label="Document">
 *   <MenubarMenu>
 *     <MenubarTrigger>File</MenubarTrigger>
 *     <MenubarContent>
 *       <MenubarItem onSelect={create}>New <MenubarShortcut>Ctrl N</MenubarShortcut></MenubarItem>
 *       <MenubarSeparator />
 *       <MenubarItem onSelect={print}>Print</MenubarItem>
 *     </MenubarContent>
 *   </MenubarMenu>
 *   <MenubarMenu>
 *     <MenubarTrigger>View</MenubarTrigger>
 *     <MenubarContent>
 *       <MenubarCheckboxItem checked={ruler} onCheckedChange={setRuler}>Ruler</MenubarCheckboxItem>
 *     </MenubarContent>
 *   </MenubarMenu>
 * </Menubar>
 */
import { Menubar as MenubarPrimitive } from "radix-ui";
import { Check, ChevronRight, Circle } from "lucide-react";
import type { ComponentProps } from "react";
import { cn } from "@/lib/cn";

/* The same item as dropdown-menu: the highlight is the ground, the focus ring is drawn inside. */
const item =
  "relative flex min-h-9 cursor-default items-center gap-2 rounded-control px-2.5 py-1.5 text-sm select-none -outline-offset-2 data-disabled:pointer-events-none data-disabled:opacity-50 data-highlighted:bg-accent data-highlighted:text-accent-foreground pointer-coarse:min-h-10 [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4";

/*
 * The surface of dropdown-menu, without its closing animation. A menu that is still on screen while it
 * animates out sees focus arrive in the menu the arrow key has just opened, takes that for a move outside
 * the bar, and closes every menu. So a menu here closes at once.
 */
const surface =
  "z-50 max-h-(--radix-menubar-content-available-height) min-w-40 max-w-(--radix-menubar-content-available-width) origin-(--radix-menubar-content-transform-origin) overflow-x-hidden overflow-y-auto rounded-surface border bg-popover p-1 text-popover-foreground ease-out animation-duration-(--duration-base) fade-in-0 zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-[state=open]:animate-in motion-reduce:animate-none!";

/** The bar. It wraps onto a second line rather than overflow a narrow screen. */
export function Menubar({ className, ...props }: ComponentProps<typeof MenubarPrimitive.Root>) {
  return (
    <MenubarPrimitive.Root
      data-slot="menubar"
      className={cn("flex w-fit max-w-full flex-wrap items-center gap-1 rounded-surface border bg-background p-1", className)}
      {...props}
    />
  );
}

export function MenubarMenu(props: ComponentProps<typeof MenubarPrimitive.Menu>) {
  return <MenubarPrimitive.Menu {...props} />;
}

export function MenubarTrigger({ className, ...props }: ComponentProps<typeof MenubarPrimitive.Trigger>) {
  return (
    <MenubarPrimitive.Trigger
      data-slot="menubar-trigger"
      className={cn(
        "inline-flex h-control-sm cursor-default items-center rounded-control px-3 text-sm font-medium select-none transition-[background-color] duration-instant ease-out data-disabled:pointer-events-none data-disabled:opacity-50 data-highlighted:bg-accent data-[state=open]:bg-accent motion-reduce:transition-none pointer-coarse:h-control",
        className,
      )}
      {...props}
    />
  );
}

export function MenubarContent({ className, align = "start", sideOffset = 6, alignOffset = -4, collisionPadding = 8, ...props }: ComponentProps<typeof MenubarPrimitive.Content>) {
  return (
    <MenubarPrimitive.Portal>
      <MenubarPrimitive.Content
        data-slot="menubar-content"
        align={align}
        sideOffset={sideOffset}
        alignOffset={alignOffset}
        collisionPadding={collisionPadding}
        className={cn(surface, className)}
        {...props}
      />
    </MenubarPrimitive.Portal>
  );
}

export function MenubarGroup(props: ComponentProps<typeof MenubarPrimitive.Group>) {
  return <MenubarPrimitive.Group data-slot="menubar-group" {...props} />;
}

export type MenubarItemProps = ComponentProps<typeof MenubarPrimitive.Item> & {
  /** Indent to line up with checkbox and radio items in the same menu. */
  inset?: boolean;
  /** "destructive" only for deleting. */
  variant?: "default" | "destructive";
};

export function MenubarItem({ className, inset = false, variant = "default", ...props }: MenubarItemProps) {
  return (
    <MenubarPrimitive.Item
      data-slot="menubar-item"
      data-variant={variant}
      className={cn(item, inset && "ps-8", variant === "destructive" && "text-destructive data-highlighted:text-destructive", className)}
      {...props}
    />
  );
}

export function MenubarCheckboxItem({ className, children, ...props }: ComponentProps<typeof MenubarPrimitive.CheckboxItem>) {
  return (
    <MenubarPrimitive.CheckboxItem data-slot="menubar-checkbox-item" className={cn(item, "ps-8", className)} {...props}>
      <span aria-hidden="true" className="pointer-events-none absolute start-2.5 flex size-4 items-center justify-center">
        <MenubarPrimitive.ItemIndicator>
          <Check strokeWidth={1.5} className="size-4" />
        </MenubarPrimitive.ItemIndicator>
      </span>
      {children}
    </MenubarPrimitive.CheckboxItem>
  );
}

export function MenubarRadioGroup(props: ComponentProps<typeof MenubarPrimitive.RadioGroup>) {
  return <MenubarPrimitive.RadioGroup data-slot="menubar-radio-group" {...props} />;
}

export function MenubarRadioItem({ className, children, ...props }: ComponentProps<typeof MenubarPrimitive.RadioItem>) {
  return (
    <MenubarPrimitive.RadioItem data-slot="menubar-radio-item" className={cn(item, "ps-8", className)} {...props}>
      <span aria-hidden="true" className="pointer-events-none absolute start-2.5 flex size-4 items-center justify-center">
        <MenubarPrimitive.ItemIndicator>
          <Circle strokeWidth={1.5} className="size-2 fill-current" />
        </MenubarPrimitive.ItemIndicator>
      </span>
      {children}
    </MenubarPrimitive.RadioItem>
  );
}

export function MenubarLabel({ className, inset = false, ...props }: ComponentProps<typeof MenubarPrimitive.Label> & { inset?: boolean }) {
  return (
    <MenubarPrimitive.Label data-slot="menubar-label" className={cn("px-2.5 py-1.5 text-xs font-medium text-muted-foreground", inset && "ps-8", className)} {...props} />
  );
}

export function MenubarSeparator({ className, ...props }: ComponentProps<typeof MenubarPrimitive.Separator>) {
  return <MenubarPrimitive.Separator data-slot="menubar-separator" className={cn("-mx-1 my-1 h-px bg-border", className)} {...props} />;
}

/** A keyboard hint at the end of an item. It only shows the keys: bind them yourself. */
export function MenubarShortcut({ className, ...props }: ComponentProps<"span">) {
  return <span data-slot="menubar-shortcut" className={cn("ms-auto ps-4 text-xs tracking-widest text-muted-foreground", className)} {...props} />;
}

export function MenubarSub(props: ComponentProps<typeof MenubarPrimitive.Sub>) {
  return <MenubarPrimitive.Sub {...props} />;
}

export function MenubarSubTrigger({ className, inset = false, children, ...props }: ComponentProps<typeof MenubarPrimitive.SubTrigger> & { inset?: boolean }) {
  return (
    <MenubarPrimitive.SubTrigger data-slot="menubar-sub-trigger" className={cn(item, "data-[state=open]:bg-accent", inset && "ps-8", className)} {...props}>
      {children}
      <ChevronRight aria-hidden="true" strokeWidth={1.5} className="ms-auto size-4 rtl:rotate-180" />
    </MenubarPrimitive.SubTrigger>
  );
}

export function MenubarSubContent({ className, collisionPadding = 8, ...props }: ComponentProps<typeof MenubarPrimitive.SubContent>) {
  return (
    <MenubarPrimitive.Portal>
      <MenubarPrimitive.SubContent data-slot="menubar-sub-content" collisionPadding={collisionPadding} className={cn(surface, className)} {...props} />
    </MenubarPrimitive.Portal>
  );
}