Menubar
An animated React menubar built on shadcn/ui and Radix: the drop-in Menubar gives you desktop-app menus with sub-menus, checkbox and radio items and keyboard shortcuts, plus a Motion menubar block whose highlight glides between File, Edit and View while menus fade open.
- Drop-in shadcn API
- Radix Menubar
- Sub-menus
- Checkbox & radio items
- Motion block
"use client";
import * as React from "react";
import {
Copy,
FilePlus,
FolderOpen,
Link2,
Mail,
PanelLeft,
Printer,
Redo2,
Save,
Scissors,
Share2,
Trash2,
Undo2,
} from "lucide-react";
import {
AnimatedMenubar,
AnimatedMenubarCheckboxItem,
AnimatedMenubarContent,
AnimatedMenubarItem,
AnimatedMenubarLabel,
AnimatedMenubarMenu,
AnimatedMenubarSeparator,
AnimatedMenubarSub,
AnimatedMenubarSubContent,
AnimatedMenubarSubTrigger,
AnimatedMenubarTrigger,
} from "@/components/dusk/menubar/animated-menubar";
import { MenubarItem } from "@/components/ui/menubar";
export default function AnimatedMenubarDemo() {
const [sidebar, setSidebar] = React.useState(true);
const [statusBar, setStatusBar] = React.useState(true);
const [focusMode, setFocusMode] = React.useState(false);
return (
<AnimatedMenubar>
<AnimatedMenubarMenu value="file">
<AnimatedMenubarTrigger>File</AnimatedMenubarTrigger>
<AnimatedMenubarContent>
<AnimatedMenubarItem icon={<FilePlus />} shortcut="⌘N">
New file
</AnimatedMenubarItem>
<AnimatedMenubarItem icon={<FolderOpen />} shortcut="⌘O">
Open…
</AnimatedMenubarItem>
<AnimatedMenubarItem icon={<Save />} shortcut="⌘S">
Save
</AnimatedMenubarItem>
<AnimatedMenubarSeparator />
<AnimatedMenubarSub>
<AnimatedMenubarSubTrigger icon={<Share2 />}>Share</AnimatedMenubarSubTrigger>
<AnimatedMenubarSubContent>
<MenubarItem>
<Link2 /> Copy link
</MenubarItem>
<MenubarItem>
<Mail /> Email
</MenubarItem>
</AnimatedMenubarSubContent>
</AnimatedMenubarSub>
<AnimatedMenubarItem icon={<Printer />} shortcut="⌘P">
Print…
</AnimatedMenubarItem>
</AnimatedMenubarContent>
</AnimatedMenubarMenu>
<AnimatedMenubarMenu value="edit">
<AnimatedMenubarTrigger>Edit</AnimatedMenubarTrigger>
<AnimatedMenubarContent>
<AnimatedMenubarItem icon={<Undo2 />} shortcut="⌘Z">
Undo
</AnimatedMenubarItem>
<AnimatedMenubarItem icon={<Redo2 />} shortcut="⇧⌘Z">
Redo
</AnimatedMenubarItem>
<AnimatedMenubarSeparator />
<AnimatedMenubarItem icon={<Scissors />} shortcut="⌘X">
Cut
</AnimatedMenubarItem>
<AnimatedMenubarItem icon={<Copy />} shortcut="⌘C">
Copy
</AnimatedMenubarItem>
<AnimatedMenubarSeparator />
<AnimatedMenubarItem icon={<Trash2 />} variant="destructive">
Delete
</AnimatedMenubarItem>
</AnimatedMenubarContent>
</AnimatedMenubarMenu>
<AnimatedMenubarMenu value="view">
<AnimatedMenubarTrigger>View</AnimatedMenubarTrigger>
<AnimatedMenubarContent>
<AnimatedMenubarLabel>Appearance</AnimatedMenubarLabel>
<AnimatedMenubarCheckboxItem checked={sidebar} onCheckedChange={setSidebar} shortcut="⌘B">
Sidebar
</AnimatedMenubarCheckboxItem>
<AnimatedMenubarCheckboxItem checked={statusBar} onCheckedChange={setStatusBar}>
Status bar
</AnimatedMenubarCheckboxItem>
<AnimatedMenubarCheckboxItem checked={focusMode} onCheckedChange={setFocusMode} shortcut="⌘.">
Focus mode
</AnimatedMenubarCheckboxItem>
<AnimatedMenubarSeparator />
<AnimatedMenubarItem icon={<PanelLeft />} shortcut="⌃⌘F">
Enter full screen
</AnimatedMenubarItem>
</AnimatedMenubarContent>
</AnimatedMenubarMenu>
<AnimatedMenubarMenu value="window">
<AnimatedMenubarTrigger>Window</AnimatedMenubarTrigger>
<AnimatedMenubarContent>
<AnimatedMenubarItem shortcut="⌘M">Minimize</AnimatedMenubarItem>
<AnimatedMenubarItem>Zoom</AnimatedMenubarItem>
<AnimatedMenubarSeparator />
<AnimatedMenubarItem>Bring all to front</AnimatedMenubarItem>
</AnimatedMenubarContent>
</AnimatedMenubarMenu>
</AnimatedMenubar>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/menubar.json1. Install the dependencies.
npm install @radix-ui/react-menubar lucide-react2. Copy the source into
components/ui/menubar.tsx.menubar.tsx "use client" import * as React from "react" import * as MenubarPrimitive from "@radix-ui/react-menubar" import { CheckIcon, ChevronRightIcon, CircleIcon } from "lucide-react" import { cn } from "@/lib/utils" /** * DuskUI Menubar — the shadcn/ui Menubar, styled to match the DuskUI menus. * * Same parts, props and data-slot attributes as shadcn, plus: * - Menus grow out of their trigger: the scale origin follows Radix's * transform origin and they shift in slightly from the side they open on * (150ms ease-out), then close quicker than they opened. * - The open trigger keeps a neutral highlight so you can see which menu * you are in; triggers ease their background on hover. * - Check and radio indicators fade in when an item is selected. * - Under `prefers-reduced-motion` menus only fade. * CSS only (tw-animate-css). Radix handles roving focus between menus, arrow * keys, typeahead, sub-menus and aria. */ function Menubar({ className, ...props }: React.ComponentProps<typeof MenubarPrimitive.Root>) { return ( <MenubarPrimitive.Root data-slot="menubar" className={cn( "flex h-10 items-center gap-1 rounded-lg border bg-background p-1", "shadow-xs", className )} {...props} /> ) } /** One top-level menu: a MenubarTrigger and its MenubarContent. */ function MenubarMenu({ ...props }: React.ComponentProps<typeof MenubarPrimitive.Menu>) { return <MenubarPrimitive.Menu data-slot="menubar-menu" {...props} /> } /** Groups related items. Purely semantic (role="group"). */ function MenubarGroup({ ...props }: React.ComponentProps<typeof MenubarPrimitive.Group>) { return <MenubarPrimitive.Group data-slot="menubar-group" {...props} /> } /** Portals content to `document.body`. Content already portals itself. */ function MenubarPortal({ ...props }: React.ComponentProps<typeof MenubarPrimitive.Portal>) { return <MenubarPrimitive.Portal data-slot="menubar-portal" {...props} /> } /** Holds radio items; only one can be selected. Use `value` / `onValueChange`. */ function MenubarRadioGroup({ ...props }: React.ComponentProps<typeof MenubarPrimitive.RadioGroup>) { return ( <MenubarPrimitive.RadioGroup data-slot="menubar-radio-group" {...props} /> ) } /** The top-level button that opens its menu (click, Enter, Space or ArrowDown). */ function MenubarTrigger({ className, ...props }: React.ComponentProps<typeof MenubarPrimitive.Trigger>) { return ( <MenubarPrimitive.Trigger data-slot="menubar-trigger" className={cn( "flex items-center rounded-md px-2.5 py-1 text-sm font-medium outline-hidden select-none", "transition-[color,background-color,box-shadow] duration-150 ease-out motion-reduce:transition-none", "text-foreground/80 hover:bg-accent hover:text-foreground", "focus-visible:ring-[3px] focus-visible:ring-ring/50", "data-[state=open]:bg-accent data-[state=open]:text-foreground", "data-[disabled]:pointer-events-none data-[disabled]:opacity-50", className )} {...props} /> ) } const surface = [ "z-50 min-w-[12rem] overflow-x-hidden overflow-y-auto rounded-lg border bg-popover p-1 text-popover-foreground", "shadow-md ease-out", "data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95 data-[state=open]:duration-150", "data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=closed]:duration-100", "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", "motion-reduce:data-[state=open]:zoom-in-100 motion-reduce:data-[state=closed]:zoom-out-100", "motion-reduce:data-[side=bottom]:slide-in-from-top-0 motion-reduce:data-[side=left]:slide-in-from-right-0 motion-reduce:data-[side=right]:slide-in-from-left-0 motion-reduce:data-[side=top]:slide-in-from-bottom-0", ] /** The menu surface. Portalled, positioned under its trigger and scrollable when long. */ function MenubarContent({ className, align = "start", alignOffset = -4, sideOffset = 8, ...props }: React.ComponentProps<typeof MenubarPrimitive.Content>) { return ( <MenubarPortal> <MenubarPrimitive.Content data-slot="menubar-content" align={align} alignOffset={alignOffset} sideOffset={sideOffset} className={cn( surface, "max-h-(--radix-menubar-content-available-height) origin-(--radix-menubar-content-transform-origin)", className )} {...props} /> </MenubarPortal> ) } const itemBase = [ "relative flex cursor-default items-center gap-2 rounded-md px-2 py-1.5 text-sm outline-hidden select-none", "transition-[color,background-color] duration-150 ease-out motion-reduce:transition-none", "focus:bg-accent focus:text-accent-foreground", "data-[disabled]:pointer-events-none data-[disabled]:opacity-50", "[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground", ] /** A menu action. `onSelect` fires on click, Enter or Space and closes the menu. */ function MenubarItem({ className, inset, variant = "default", ...props }: React.ComponentProps<typeof MenubarPrimitive.Item> & { /** Indent to line up with items that have an indicator or icon. */ inset?: boolean /** `destructive` tints the item (and its icon) red. */ variant?: "default" | "destructive" }) { return ( <MenubarPrimitive.Item data-slot="menubar-item" data-inset={inset} data-variant={variant} className={cn( itemBase, "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-[variant=destructive]:*:[svg]:!text-destructive", className )} {...props} /> ) } const indicatorSlot = "pointer-events-none absolute left-2 flex size-3.5 items-center justify-center" /** The indicator mounts when the item becomes checked, so it fades in. */ const indicatorPop = "flex items-center justify-center animate-in fade-in-0 duration-150 ease-out motion-reduce:animate-none" /** A toggleable item with a check mark. Use `checked` / `onCheckedChange`. */ function MenubarCheckboxItem({ className, children, checked, ...props }: React.ComponentProps<typeof MenubarPrimitive.CheckboxItem>) { return ( <MenubarPrimitive.CheckboxItem data-slot="menubar-checkbox-item" className={cn(itemBase, "py-1.5 pr-2 pl-8", className)} checked={checked} {...props} > <span className={indicatorSlot}> <MenubarPrimitive.ItemIndicator className={indicatorPop}> <CheckIcon className="size-4 !text-foreground" /> </MenubarPrimitive.ItemIndicator> </span> {children} </MenubarPrimitive.CheckboxItem> ) } /** One choice inside a MenubarRadioGroup. */ function MenubarRadioItem({ className, children, ...props }: React.ComponentProps<typeof MenubarPrimitive.RadioItem>) { return ( <MenubarPrimitive.RadioItem data-slot="menubar-radio-item" className={cn(itemBase, "py-1.5 pr-2 pl-8", className)} {...props} > <span className={indicatorSlot}> <MenubarPrimitive.ItemIndicator className={indicatorPop}> <CircleIcon className="size-2 fill-current !text-foreground" /> </MenubarPrimitive.ItemIndicator> </span> {children} </MenubarPrimitive.RadioItem> ) } /** A non-interactive heading for a group of items. */ function MenubarLabel({ className, inset, ...props }: React.ComponentProps<typeof MenubarPrimitive.Label> & { inset?: boolean }) { return ( <MenubarPrimitive.Label data-slot="menubar-label" data-inset={inset} className={cn( "px-2 py-1.5 text-xs font-medium text-muted-foreground data-[inset]:pl-8", className )} {...props} /> ) } /** A thin divider between groups. */ function MenubarSeparator({ className, ...props }: React.ComponentProps<typeof MenubarPrimitive.Separator>) { return ( <MenubarPrimitive.Separator data-slot="menubar-separator" className={cn("-mx-1 my-1 h-px bg-border", className)} {...props} /> ) } /** Right-aligned keyboard hint, e.g. ⌘S. Visual only; wire the shortcut yourself. */ function MenubarShortcut({ className, ...props }: React.ComponentProps<"span">) { return ( <span data-slot="menubar-shortcut" className={cn( "ml-auto pl-4 font-mono text-[0.7rem] tracking-widest text-muted-foreground/80", className )} {...props} /> ) } /** Wraps a sub-menu: a SubTrigger and its SubContent. */ function MenubarSub({ ...props }: React.ComponentProps<typeof MenubarPrimitive.Sub>) { return <MenubarPrimitive.Sub data-slot="menubar-sub" {...props} /> } /** The item that opens a sub-menu (hover, click, or the arrow key toward it). */ function MenubarSubTrigger({ className, inset, children, ...props }: React.ComponentProps<typeof MenubarPrimitive.SubTrigger> & { inset?: boolean }) { return ( <MenubarPrimitive.SubTrigger data-slot="menubar-sub-trigger" data-inset={inset} className={cn( itemBase, "group/sub data-[inset]:pl-8 data-[state=open]:bg-accent data-[state=open]:text-accent-foreground", className )} {...props} > {children} <ChevronRightIcon className="ml-auto size-4" /> </MenubarPrimitive.SubTrigger> ) } /** The nested menu surface. */ function MenubarSubContent({ className, ...props }: React.ComponentProps<typeof MenubarPrimitive.SubContent>) { return ( <MenubarPortal> <MenubarPrimitive.SubContent data-slot="menubar-sub-content" className={cn( surface, "min-w-[8rem] origin-(--radix-menubar-content-transform-origin)", className )} {...props} /> </MenubarPortal> ) } export { Menubar, MenubarPortal, MenubarMenu, MenubarTrigger, MenubarContent, MenubarGroup, MenubarSeparator, MenubarLabel, MenubarItem, MenubarShortcut, MenubarCheckboxItem, MenubarRadioGroup, MenubarRadioItem, MenubarSub, MenubarSubTrigger, MenubarSubContent, }
Adds components/ui/menubar.tsx with the shadcn exports and props (including inset and variant="destructive" on items). Animations are CSS (tw-animate-css), so the base needs no Motion.
Usage
import {
Menubar,
MenubarContent,
MenubarItem,
MenubarMenu,
MenubarSeparator,
MenubarShortcut,
MenubarTrigger,
} from "@/components/ui/menubar"
export function AppMenubar() {
return (
<Menubar>
<MenubarMenu>
<MenubarTrigger>File</MenubarTrigger>
<MenubarContent>
<MenubarItem>
New tab <MenubarShortcut>⌘T</MenubarShortcut>
</MenubarItem>
<MenubarSeparator />
<MenubarItem>Print…</MenubarItem>
</MenubarContent>
</MenubarMenu>
</Menubar>
)
}Examples
Default
Menus with shortcuts, a disabled item, a sub-menu and a destructive action. Once a menu is open, hovering another trigger switches to it, and the left / right arrow keys move between menus.
import {
Menubar,
MenubarContent,
MenubarItem,
MenubarMenu,
MenubarSeparator,
MenubarShortcut,
MenubarSub,
MenubarSubContent,
MenubarSubTrigger,
MenubarTrigger,
} from "@/components/ui/menubar";
export default function MenubarBasicDemo() {
return (
<Menubar>
<MenubarMenu>
<MenubarTrigger>File</MenubarTrigger>
<MenubarContent>
<MenubarItem>
New tab <MenubarShortcut>⌘T</MenubarShortcut>
</MenubarItem>
<MenubarItem>
New window <MenubarShortcut>⌘N</MenubarShortcut>
</MenubarItem>
<MenubarItem disabled>New incognito window</MenubarItem>
<MenubarSeparator />
<MenubarSub>
<MenubarSubTrigger>Share</MenubarSubTrigger>
<MenubarSubContent>
<MenubarItem>Copy link</MenubarItem>
<MenubarItem>Email</MenubarItem>
<MenubarItem>Messages</MenubarItem>
</MenubarSubContent>
</MenubarSub>
<MenubarSeparator />
<MenubarItem>
Print… <MenubarShortcut>⌘P</MenubarShortcut>
</MenubarItem>
</MenubarContent>
</MenubarMenu>
<MenubarMenu>
<MenubarTrigger>Edit</MenubarTrigger>
<MenubarContent>
<MenubarItem>
Undo <MenubarShortcut>⌘Z</MenubarShortcut>
</MenubarItem>
<MenubarItem>
Redo <MenubarShortcut>⇧⌘Z</MenubarShortcut>
</MenubarItem>
<MenubarSeparator />
<MenubarItem>Cut</MenubarItem>
<MenubarItem>Copy</MenubarItem>
<MenubarItem>Paste</MenubarItem>
</MenubarContent>
</MenubarMenu>
<MenubarMenu>
<MenubarTrigger>Help</MenubarTrigger>
<MenubarContent>
<MenubarItem>Documentation</MenubarItem>
<MenubarItem>Keyboard shortcuts</MenubarItem>
<MenubarSeparator />
<MenubarItem variant="destructive">Reset all settings</MenubarItem>
</MenubarContent>
</MenubarMenu>
</Menubar>
);
}Checkbox and radio items
MenubarCheckboxItem toggles view options and MenubarRadioGroup picks one value. Indicators fade in when an item turns on.
minimap · line numbers — dusk at 100%
"use client";
import * as React from "react";
import {
Menubar,
MenubarCheckboxItem,
MenubarContent,
MenubarLabel,
MenubarMenu,
MenubarRadioGroup,
MenubarRadioItem,
MenubarSeparator,
MenubarShortcut,
MenubarTrigger,
} from "@/components/ui/menubar";
export default function MenubarCheckboxesDemo() {
const [minimap, setMinimap] = React.useState(true);
const [wordWrap, setWordWrap] = React.useState(false);
const [lineNumbers, setLineNumbers] = React.useState(true);
const [theme, setTheme] = React.useState("dusk");
const [zoom, setZoom] = React.useState("100");
return (
<div className="flex flex-col items-center gap-4">
<Menubar>
<MenubarMenu>
<MenubarTrigger>View</MenubarTrigger>
<MenubarContent>
<MenubarCheckboxItem checked={minimap} onCheckedChange={setMinimap}>
Minimap
</MenubarCheckboxItem>
<MenubarCheckboxItem checked={wordWrap} onCheckedChange={setWordWrap}>
Word wrap <MenubarShortcut>⌥Z</MenubarShortcut>
</MenubarCheckboxItem>
<MenubarCheckboxItem checked={lineNumbers} onCheckedChange={setLineNumbers}>
Line numbers
</MenubarCheckboxItem>
<MenubarSeparator />
<MenubarLabel inset>Zoom</MenubarLabel>
<MenubarRadioGroup value={zoom} onValueChange={setZoom}>
<MenubarRadioItem value="90">90%</MenubarRadioItem>
<MenubarRadioItem value="100">100%</MenubarRadioItem>
<MenubarRadioItem value="125">125%</MenubarRadioItem>
</MenubarRadioGroup>
</MenubarContent>
</MenubarMenu>
<MenubarMenu>
<MenubarTrigger>Theme</MenubarTrigger>
<MenubarContent>
<MenubarRadioGroup value={theme} onValueChange={setTheme}>
<MenubarRadioItem value="dusk">Dusk</MenubarRadioItem>
<MenubarRadioItem value="midnight">Midnight</MenubarRadioItem>
<MenubarRadioItem value="terminal">Terminal</MenubarRadioItem>
</MenubarRadioGroup>
</MenubarContent>
</MenubarMenu>
</Menubar>
<p className="text-xs text-muted-foreground" aria-live="polite">
{[minimap && "minimap", wordWrap && "wrap", lineNumbers && "line numbers"].filter(Boolean).join(" · ") ||
"no extras"}{" "}
— {theme} at {zoom}%
</p>
</div>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Animated Menubar
A desktop-app style menubar. A highlight glides between top-level triggers as you hover or arrow across them and follows the open menu as you sweep from menu to menu (a shared layoutId). Menus open from their trigger, items cascade in and a pill glides between items. Check marks scale in, and sub-menus use the DuskUI MenubarSubContent.
"use client";
import * as React from "react";
import {
Copy,
FilePlus,
FolderOpen,
Link2,
Mail,
PanelLeft,
Printer,
Redo2,
Save,
Scissors,
Share2,
Trash2,
Undo2,
} from "lucide-react";
import {
AnimatedMenubar,
AnimatedMenubarCheckboxItem,
AnimatedMenubarContent,
AnimatedMenubarItem,
AnimatedMenubarLabel,
AnimatedMenubarMenu,
AnimatedMenubarSeparator,
AnimatedMenubarSub,
AnimatedMenubarSubContent,
AnimatedMenubarSubTrigger,
AnimatedMenubarTrigger,
} from "@/components/dusk/menubar/animated-menubar";
import { MenubarItem } from "@/components/ui/menubar";
export default function AnimatedMenubarDemo() {
const [sidebar, setSidebar] = React.useState(true);
const [statusBar, setStatusBar] = React.useState(true);
const [focusMode, setFocusMode] = React.useState(false);
return (
<AnimatedMenubar>
<AnimatedMenubarMenu value="file">
<AnimatedMenubarTrigger>File</AnimatedMenubarTrigger>
<AnimatedMenubarContent>
<AnimatedMenubarItem icon={<FilePlus />} shortcut="⌘N">
New file
</AnimatedMenubarItem>
<AnimatedMenubarItem icon={<FolderOpen />} shortcut="⌘O">
Open…
</AnimatedMenubarItem>
<AnimatedMenubarItem icon={<Save />} shortcut="⌘S">
Save
</AnimatedMenubarItem>
<AnimatedMenubarSeparator />
<AnimatedMenubarSub>
<AnimatedMenubarSubTrigger icon={<Share2 />}>Share</AnimatedMenubarSubTrigger>
<AnimatedMenubarSubContent>
<MenubarItem>
<Link2 /> Copy link
</MenubarItem>
<MenubarItem>
<Mail /> Email
</MenubarItem>
</AnimatedMenubarSubContent>
</AnimatedMenubarSub>
<AnimatedMenubarItem icon={<Printer />} shortcut="⌘P">
Print…
</AnimatedMenubarItem>
</AnimatedMenubarContent>
</AnimatedMenubarMenu>
<AnimatedMenubarMenu value="edit">
<AnimatedMenubarTrigger>Edit</AnimatedMenubarTrigger>
<AnimatedMenubarContent>
<AnimatedMenubarItem icon={<Undo2 />} shortcut="⌘Z">
Undo
</AnimatedMenubarItem>
<AnimatedMenubarItem icon={<Redo2 />} shortcut="⇧⌘Z">
Redo
</AnimatedMenubarItem>
<AnimatedMenubarSeparator />
<AnimatedMenubarItem icon={<Scissors />} shortcut="⌘X">
Cut
</AnimatedMenubarItem>
<AnimatedMenubarItem icon={<Copy />} shortcut="⌘C">
Copy
</AnimatedMenubarItem>
<AnimatedMenubarSeparator />
<AnimatedMenubarItem icon={<Trash2 />} variant="destructive">
Delete
</AnimatedMenubarItem>
</AnimatedMenubarContent>
</AnimatedMenubarMenu>
<AnimatedMenubarMenu value="view">
<AnimatedMenubarTrigger>View</AnimatedMenubarTrigger>
<AnimatedMenubarContent>
<AnimatedMenubarLabel>Appearance</AnimatedMenubarLabel>
<AnimatedMenubarCheckboxItem checked={sidebar} onCheckedChange={setSidebar} shortcut="⌘B">
Sidebar
</AnimatedMenubarCheckboxItem>
<AnimatedMenubarCheckboxItem checked={statusBar} onCheckedChange={setStatusBar}>
Status bar
</AnimatedMenubarCheckboxItem>
<AnimatedMenubarCheckboxItem checked={focusMode} onCheckedChange={setFocusMode} shortcut="⌘.">
Focus mode
</AnimatedMenubarCheckboxItem>
<AnimatedMenubarSeparator />
<AnimatedMenubarItem icon={<PanelLeft />} shortcut="⌃⌘F">
Enter full screen
</AnimatedMenubarItem>
</AnimatedMenubarContent>
</AnimatedMenubarMenu>
<AnimatedMenubarMenu value="window">
<AnimatedMenubarTrigger>Window</AnimatedMenubarTrigger>
<AnimatedMenubarContent>
<AnimatedMenubarItem shortcut="⌘M">Minimize</AnimatedMenubarItem>
<AnimatedMenubarItem>Zoom</AnimatedMenubarItem>
<AnimatedMenubarSeparator />
<AnimatedMenubarItem>Bring all to front</AnimatedMenubarItem>
</AnimatedMenubarContent>
</AnimatedMenubarMenu>
</AnimatedMenubar>
);
}$ npx shadcn@latest add https://duskui.com/r/animated-menubar.json1. Install the dependencies.
npm install motion @radix-ui/react-menubar lucide-react2. Copy the source into
components/dusk/menubar/animated-menubar.tsx.animated-menubar.tsx "use client"; import * as React from "react"; import * as MenubarPrimitive from "@radix-ui/react-menubar"; import { AnimatePresence, LayoutGroup, motion, useReducedMotion, type Transition, type Variants } from "motion/react"; import { CheckIcon, ChevronRightIcon } from "lucide-react"; import { MenubarShortcut, MenubarSubContent } from "@/components/ui/menubar"; import { cn } from "@/lib/utils"; const glide: Transition = { type: "spring", stiffness: 500, damping: 38, mass: 0.7 }; type AnimatedMenubarContextValue = { /** Base for shared layoutIds, unique per menubar. */ id: string; /** Value of the open menu, or "" when none is open. */ openMenu: string; /** Trigger under the pointer or keyboard focus while no menu is open. */ hovered: string | null; setHovered: React.Dispatch<React.SetStateAction<string | null>>; reduceMotion: boolean; }; const AnimatedMenubarContext = React.createContext<AnimatedMenubarContextValue | null>(null); function useAnimatedMenubar() { const context = React.useContext(AnimatedMenubarContext); if (!context) throw new Error("AnimatedMenubar parts must be used inside <AnimatedMenubar>."); return context; } type MenuContextValue = { value: string; open: boolean; /** Item under the gliding pill, by its useId. */ activeItem: string | null; setActiveItem: (id: string | null) => void; }; const MenuContext = React.createContext<MenuContextValue | null>(null); function useMenu() { const context = React.useContext(MenuContext); if (!context) throw new Error("AnimatedMenubar menu parts must be used inside <AnimatedMenubarMenu>."); return context; } export interface AnimatedMenubarProps extends Omit<React.ComponentProps<typeof MenubarPrimitive.Root>, "value" | "defaultValue" | "onValueChange"> { /** Value of the open menu ("" when closed). */ value?: string; defaultValue?: string; onValueChange?: (value: string) => void; } /** * A desktop-app style menubar. A highlight glides between the top-level * triggers as you hover or arrow across them, and stays under the open menu * while you sweep from menu to menu. Menus open from their trigger, * items cascade in, and a pill glides between items as you hover or arrow * through them. Built on Radix Menubar for roving focus, arrow keys, * typeahead, sub-menus and aria. Under reduced motion menus fade and the * highlights jump. */ export function AnimatedMenubar({ value: valueProp, defaultValue = "", onValueChange, className, children, onPointerLeave, ...props }: AnimatedMenubarProps) { const id = React.useId(); const reduceMotion = useReducedMotion() ?? false; const [uncontrolled, setUncontrolled] = React.useState(defaultValue); const [hovered, setHovered] = React.useState<string | null>(null); const openMenu = valueProp ?? uncontrolled; const setOpenMenu = (next: string) => { if (valueProp === undefined) setUncontrolled(next); onValueChange?.(next); }; const context = React.useMemo( () => ({ id, openMenu, hovered, setHovered, reduceMotion }), [id, openMenu, hovered, reduceMotion] ); return ( <AnimatedMenubarContext.Provider value={context}> <LayoutGroup id={id}> <MenubarPrimitive.Root data-slot="menubar" value={openMenu} onValueChange={setOpenMenu} onPointerLeave={(event) => { onPointerLeave?.(event); setHovered(null); }} className={cn( "relative flex h-10 items-center gap-0.5 rounded-xl border bg-background p-1 shadow-xs", className )} {...props} > {children} </MenubarPrimitive.Root> </LayoutGroup> </AnimatedMenubarContext.Provider> ); } export interface AnimatedMenubarMenuProps extends React.ComponentProps<typeof MenubarPrimitive.Menu> { /** Identifies the menu. Generated when omitted. */ value?: string; } /** One top-level menu: an AnimatedMenubarTrigger and its AnimatedMenubarContent. */ export function AnimatedMenubarMenu({ value: valueProp, children, ...props }: AnimatedMenubarMenuProps) { const generated = React.useId(); const value = valueProp ?? generated; const { openMenu } = useAnimatedMenubar(); const open = openMenu === value; const [activeItem, setActiveItem] = React.useState<string | null>(null); const context = React.useMemo( // The pill is forgotten when the menu closes, so it never flashes on reopen. () => ({ value, open, activeItem: open ? activeItem : null, setActiveItem }), [value, open, activeItem] ); return ( <MenuContext.Provider value={context}> <MenubarPrimitive.Menu value={value} {...props}> {children} </MenubarPrimitive.Menu> </MenuContext.Provider> ); } /** The top-level button. Shows the gliding highlight while hovered, focused or open. */ export function AnimatedMenubarTrigger({ className, children, onPointerEnter, onFocus, onBlur, ...props }: Omit<React.ComponentProps<typeof MenubarPrimitive.Trigger>, "asChild">) { const { id, openMenu, hovered, setHovered, reduceMotion } = useAnimatedMenubar(); const { value, open } = useMenu(); // While a menu is open the highlight belongs to it; otherwise it follows the pointer / focus. const highlighted = openMenu ? open : hovered === value; return ( <MenubarPrimitive.Trigger data-slot="menubar-trigger" onPointerEnter={(event) => { onPointerEnter?.(event); if (event.pointerType === "mouse") setHovered(value); }} onFocus={(event) => { onFocus?.(event); setHovered(value); }} onBlur={(event) => { onBlur?.(event); setHovered((current) => (current === value ? null : current)); }} className={cn( "relative flex items-center rounded-lg px-3 py-1 text-sm font-medium outline-none select-none", "text-foreground/75 transition-colors duration-150 motion-reduce:transition-none", "hover:text-foreground data-[state=open]:text-foreground", "focus-visible:ring-[3px] focus-visible:ring-ring/50", "data-[disabled]:pointer-events-none data-[disabled]:opacity-50", className )} {...props} > {highlighted && ( <motion.span aria-hidden layoutId={`${id}-trigger`} className={cn( "absolute inset-0 rounded-lg transition-colors duration-150", open ? "bg-accent" : "bg-white/[0.06]" )} transition={reduceMotion ? { duration: 0 } : glide} /> )} <span className="relative">{children}</span> </MenubarPrimitive.Trigger> ); } export interface AnimatedMenubarContentProps extends Omit<React.ComponentProps<typeof MenubarPrimitive.Content>, "forceMount" | "asChild" | "children"> { children?: React.ReactNode; /** Delay between items as they cascade in, in seconds. */ stagger?: number; } /** The menu surface. Fades open under its trigger and cascades its items in. */ export function AnimatedMenubarContent({ className, align = "start", alignOffset = -4, sideOffset = 8, stagger = 0.025, children, onPointerLeave, ...props }: AnimatedMenubarContentProps) { const { reduceMotion } = useAnimatedMenubar(); const { open, setActiveItem } = useMenu(); const variants: Variants = reduceMotion ? { closed: { opacity: 0, transition: { duration: 0.1 } }, open: { opacity: 1, transition: { duration: 0.15 } }, } : { closed: { opacity: 0, scale: 0.96, y: -4, transition: { duration: 0.1, ease: "easeIn" } }, open: { opacity: 1, scale: 1, y: 0, transition: { duration: 0.18, ease: "easeOut", staggerChildren: stagger, delayChildren: 0.02 }, }, }; return ( <AnimatePresence> {open && ( // forceMount hands mounting to AnimatePresence so the exit animation can play. <MenubarPrimitive.Portal forceMount> <MenubarPrimitive.Content forceMount asChild align={align} alignOffset={alignOffset} sideOffset={sideOffset} onPointerLeave={(event) => { onPointerLeave?.(event); setActiveItem(null); }} {...props} > <motion.div data-slot="menubar-content" className={cn( "z-50 min-w-56 overflow-x-hidden overflow-y-auto rounded-xl border bg-popover p-1.5 text-popover-foreground outline-none", "max-h-(--radix-menubar-content-available-height) origin-(--radix-menubar-content-transform-origin)", "shadow-lg", className )} variants={variants} initial="closed" animate="open" exit="closed" > {children} </motion.div> </MenubarPrimitive.Content> </MenubarPrimitive.Portal> )} </AnimatePresence> ); } function useItemVariants(): Variants { const { reduceMotion } = useAnimatedMenubar(); return reduceMotion ? { closed: { opacity: 0, transition: { duration: 0.1 } }, open: { opacity: 1 } } : { closed: { opacity: 0, x: -4, transition: { duration: 0.08 } }, open: { opacity: 1, x: 0, transition: { duration: 0.18, ease: "easeOut" } }, }; } const itemClass = cn( "relative flex cursor-default items-center gap-2.5 rounded-lg px-2.5 py-1.5 text-sm outline-none select-none", "text-foreground/90 transition-colors duration-150 data-[highlighted]:text-foreground", "data-[disabled]:pointer-events-none data-[disabled]:opacity-45", "[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4" ); /** The gliding highlight behind the focused item. */ function ItemPill({ active, destructive }: { active: boolean; destructive?: boolean }) { const { id, reduceMotion } = useAnimatedMenubar(); const { value } = useMenu(); if (!active) return null; return ( <motion.span aria-hidden layoutId={`${id}-${value}-item`} className={cn( "absolute inset-0 rounded-lg", destructive ? "bg-destructive/15" : "bg-accent" )} transition={reduceMotion ? { duration: 0 } : glide} /> ); } /** Radix focuses items on hover and arrow keys, so focus drives the pill. */ function usePillFocus(onFocus?: React.FocusEventHandler<HTMLDivElement>) { const itemId = React.useId(); const { activeItem, setActiveItem } = useMenu(); return { active: activeItem === itemId, onFocus: (event: React.FocusEvent<HTMLDivElement>) => { onFocus?.(event); setActiveItem(itemId); }, }; } export interface AnimatedMenubarItemProps extends Omit<React.ComponentProps<typeof MenubarPrimitive.Item>, "asChild"> { /** Leading icon. */ icon?: React.ReactNode; /** Trailing keyboard hint, e.g. "⌘S". Visual only. */ shortcut?: React.ReactNode; /** `destructive` tints the item and its pill red. */ variant?: "default" | "destructive"; } /** A menu action. `onSelect` fires on click, Enter or Space and closes the menu. */ export function AnimatedMenubarItem({ className, children, icon, shortcut, variant = "default", onFocus, ...props }: AnimatedMenubarItemProps) { const variants = useItemVariants(); const pill = usePillFocus(onFocus); const destructive = variant === "destructive"; return ( <MenubarPrimitive.Item asChild onFocus={pill.onFocus} {...props}> <motion.div variants={variants} data-variant={variant} className={cn(itemClass, destructive && "text-destructive data-[highlighted]:text-destructive", className)} > <ItemPill active={pill.active} destructive={destructive} /> {icon && ( <span aria-hidden className={cn( "relative flex transition-colors duration-150", destructive ? "text-destructive" : pill.active ? "text-foreground" : "text-muted-foreground" )} > {icon} </span> )} <span className="relative flex-1 truncate">{children}</span> {shortcut && <MenubarShortcut className="relative">{shortcut}</MenubarShortcut>} </motion.div> </MenubarPrimitive.Item> ); } export interface AnimatedMenubarCheckboxItemProps extends Omit<React.ComponentProps<typeof MenubarPrimitive.CheckboxItem>, "asChild"> { shortcut?: React.ReactNode; } /** A toggleable item. The check scales in when it turns on. Use `checked` / `onCheckedChange`. */ export function AnimatedMenubarCheckboxItem({ className, children, shortcut, onFocus, ...props }: AnimatedMenubarCheckboxItemProps) { const variants = useItemVariants(); const pill = usePillFocus(onFocus); const { reduceMotion } = useAnimatedMenubar(); return ( <MenubarPrimitive.CheckboxItem asChild onFocus={pill.onFocus} {...props}> <motion.div variants={variants} className={cn(itemClass, "pl-8", className)}> <ItemPill active={pill.active} /> <span className="pointer-events-none absolute left-2.5 flex size-4 items-center justify-center"> <MenubarPrimitive.ItemIndicator asChild> <motion.span initial={reduceMotion ? { opacity: 0 } : { scale: 0.6, opacity: 0 }} animate={reduceMotion ? { opacity: 1 } : { scale: 1, opacity: 1 }} transition={reduceMotion ? { duration: 0 } : { type: "spring", duration: 0.2, bounce: 0.1 }} className="flex" > <CheckIcon className="size-4 text-primary" /> </motion.span> </MenubarPrimitive.ItemIndicator> </span> <span className="relative flex-1 truncate">{children}</span> {shortcut && <MenubarShortcut className="relative">{shortcut}</MenubarShortcut>} </motion.div> </MenubarPrimitive.CheckboxItem> ); } /** A small heading for a group of items. Cascades in with the items. */ export function AnimatedMenubarLabel({ className, ...props }: Omit<React.ComponentProps<typeof motion.div>, "variants">) { const variants = useItemVariants(); return ( <MenubarPrimitive.Label asChild> <motion.div variants={variants} className={cn("px-2.5 pt-1.5 pb-1 text-xs font-medium text-muted-foreground", className)} {...props} /> </MenubarPrimitive.Label> ); } /** A divider between groups. Draws in from the left as the menu opens. */ export function AnimatedMenubarSeparator({ className, ...props }: Omit<React.ComponentProps<typeof motion.div>, "variants">) { const { reduceMotion } = useAnimatedMenubar(); return ( <MenubarPrimitive.Separator asChild> <motion.div variants={ reduceMotion ? { closed: { opacity: 0, transition: { duration: 0.1 } }, open: { opacity: 1 } } : { closed: { scaleX: 0, opacity: 0, transition: { duration: 0.08 } }, open: { scaleX: 1, opacity: 1, transition: { duration: 0.2, ease: "easeOut" } }, } } className={cn("-mx-1.5 my-1.5 h-px origin-left bg-border", className)} {...props} /> </MenubarPrimitive.Separator> ); } /** Groups related items (role="group"). */ export const AnimatedMenubarGroup = MenubarPrimitive.Group; /** Wraps a sub-menu: an AnimatedMenubarSubTrigger and an AnimatedMenubarSubContent. */ export const AnimatedMenubarSub = MenubarPrimitive.Sub; /** The nested menu surface (the DuskUI MenubarSubContent). */ export const AnimatedMenubarSubContent = MenubarSubContent; export interface AnimatedMenubarSubTriggerProps extends Omit<React.ComponentProps<typeof MenubarPrimitive.SubTrigger>, "asChild"> { icon?: React.ReactNode; } /** The item that opens a sub-menu. Shares the gliding pill and stays lit while its sub-menu is open. */ export function AnimatedMenubarSubTrigger({ className, children, icon, onFocus, ...props }: AnimatedMenubarSubTriggerProps) { const variants = useItemVariants(); const pill = usePillFocus(onFocus); return ( <MenubarPrimitive.SubTrigger asChild onFocus={pill.onFocus} {...props}> <motion.div variants={variants} className={cn(itemClass, "group/sub data-[state=open]:bg-accent/60", className)} > <ItemPill active={pill.active} /> {icon && ( <span aria-hidden className={cn("relative flex transition-colors duration-150", pill.active ? "text-foreground" : "text-muted-foreground")} > {icon} </span> )} <span className="relative flex-1 truncate">{children}</span> <ChevronRightIcon aria-hidden className="relative size-4 text-muted-foreground transition-transform duration-200 group-data-[state=open]/sub:translate-x-0.5 motion-reduce:transition-none" /> </motion.div> </MenubarPrimitive.SubTrigger> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| value / defaultValue / onValueChange | string | "" | On AnimatedMenubar: the open menu's value, "" when closed. |
| value | string | — | On AnimatedMenubarMenu: identifies the menu. Generated when omitted. |
| stagger | number | 0.025 | On AnimatedMenubarContent: delay between items cascading in, in seconds. |
| icon | ReactNode | — | On AnimatedMenubarItem / SubTrigger: leading icon; turns mint under the pill. |
| shortcut | ReactNode | — | On items: trailing keyboard hint. Visual only. |
| variant | "default" | "destructive" | "default" | On AnimatedMenubarItem: tints the item and its pill red. |
| checked / onCheckedChange | boolean / (checked: boolean) => void | — | On AnimatedMenubarCheckboxItem. |
| Label / Separator / Group / Sub / SubTrigger / SubContent | Radix Menubar parts | — | Animated equivalents; Separator draws in from the left. |
API Reference
Same parts as shadcn/ui: Menubar, MenubarMenu, MenubarTrigger, MenubarContent, MenubarItem, MenubarCheckboxItem, MenubarRadioGroup, MenubarRadioItem, MenubarLabel, MenubarSeparator, MenubarShortcut, MenubarGroup, MenubarPortal, MenubarSub, MenubarSubTrigger and MenubarSubContent, each forwarding its Radix props.
| Prop | Type | Default | Description |
|---|---|---|---|
| value / defaultValue / onValueChange | string | — | On Menubar: the open menu (controlled or not). |
| loop | boolean | true | On Menubar: arrow keys wrap from the last trigger to the first. |
| align / alignOffset / sideOffset | "start" | "center" | "end" / number / number | "start" / -4 / 8 | On MenubarContent: placement under the trigger. |
| inset | boolean | false | On items, labels and sub-triggers: indent to line up with indicators. |
| variant | "default" | "destructive" | "default" | On MenubarItem: tints the item red. |
| checked / onCheckedChange | boolean | "indeterminate" | — | On MenubarCheckboxItem. |
| value / onValueChange | string | — | On MenubarRadioGroup: the selected radio item. |
Accessibility
- Built on Radix Menubar: role="menubar" with roving focus; left / right arrows move between menus, Enter, Space or ↓ open one, Escape closes it and returns focus to its trigger.
- Items support typeahead, Home / End and arrow keys; checkbox and radio items expose aria-checked.
- Shortcuts are visual hints only (MenubarShortcut) — wire the actual key handlers yourself.
- Under prefers-reduced-motion menus only fade; in the Motion block the gliding highlights jump instead of sliding.