Dropdown Menu
The shadcn/ui Dropdown Menu with menus that grow out of their trigger, smooth item highlights and indicators that fade in — plus an animated React dropdown menu block built with Motion and Radix, with staggered items and a highlight pill that glides between them.
- Drop-in shadcn API
- Radix keyboard & typeahead
- Sub-menus
- Motion block
"use client";
import { ChevronDown, Copy, Pencil, Share2, Star, Archive, Trash2 } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import {
AnimatedDropdown,
AnimatedDropdownContent,
AnimatedDropdownItem,
AnimatedDropdownLabel,
AnimatedDropdownSeparator,
AnimatedDropdownTrigger,
} from "@/components/dusk/dropdown-menu/animated-dropdown";
export default function AnimatedDropdownDemo() {
return (
<AnimatedDropdown>
<AnimatedDropdownTrigger asChild>
<Button variant="outline" className="group">
Actions
<ChevronDown className="transition-transform duration-200 group-data-[state=open]:rotate-180" />
</Button>
</AnimatedDropdownTrigger>
<AnimatedDropdownContent align="start" className="w-56">
<AnimatedDropdownLabel>Project</AnimatedDropdownLabel>
<AnimatedDropdownItem icon={<Pencil />} shortcut="⌘E" onSelect={() => toast("Renaming…")}>
Rename
</AnimatedDropdownItem>
<AnimatedDropdownItem icon={<Copy />} shortcut="⌘D" onSelect={() => toast("Duplicated")}>
Duplicate
</AnimatedDropdownItem>
<AnimatedDropdownItem icon={<Share2 />} onSelect={() => toast("Link copied")}>
Share
</AnimatedDropdownItem>
<AnimatedDropdownItem icon={<Star />} onSelect={() => toast("Added to favorites")}>
Add to favorites
</AnimatedDropdownItem>
<AnimatedDropdownSeparator />
<AnimatedDropdownItem icon={<Archive />} disabled>
Archive
</AnimatedDropdownItem>
<AnimatedDropdownItem icon={<Trash2 />} shortcut="⌫" variant="destructive" onSelect={() => toast.error("Project deleted")}>
Delete
</AnimatedDropdownItem>
</AnimatedDropdownContent>
</AnimatedDropdown>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/dropdown-menu.json1. Install the dependencies.
npm install @radix-ui/react-dropdown-menu lucide-react2. Copy the source into
components/ui/dropdown-menu.tsx.dropdown-menu.tsx "use client" import * as React from "react" import * as DropdownMenuPrimitive from "@radix-ui/react-dropdown-menu" import { CheckIcon, ChevronRightIcon, CircleIcon } from "lucide-react" import { cn } from "@/lib/utils" /** * DuskUI Dropdown Menu — a drop-in upgrade of the shadcn/ui Dropdown Menu. * * 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. * - Highlighted items ease their background in; destructive items tint red. * - 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 focus, typeahead, arrow keys, * sub-menus and aria. */ function DropdownMenu({ ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.Root>) { return <DropdownMenuPrimitive.Root data-slot="dropdown-menu" {...props} /> } /** Portals content to `document.body`. Content already portals itself. */ function DropdownMenuPortal({ ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.Portal>) { return ( <DropdownMenuPrimitive.Portal data-slot="dropdown-menu-portal" {...props} /> ) } /** The element that opens the menu. Use `asChild` to render your own Button. */ function DropdownMenuTrigger({ ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.Trigger>) { return ( <DropdownMenuPrimitive.Trigger data-slot="dropdown-menu-trigger" {...props} /> ) } const surface = [ "z-50 min-w-[8rem] 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 next to the trigger and scrollable when long. */ function DropdownMenuContent({ className, sideOffset = 6, ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.Content>) { return ( <DropdownMenuPrimitive.Portal> <DropdownMenuPrimitive.Content data-slot="dropdown-menu-content" sideOffset={sideOffset} className={cn( surface, "max-h-(--radix-dropdown-menu-content-available-height) origin-(--radix-dropdown-menu-content-transform-origin)", className )} {...props} /> </DropdownMenuPrimitive.Portal> ) } /** Groups related items. Purely semantic (role="group"). */ function DropdownMenuGroup({ ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.Group>) { return ( <DropdownMenuPrimitive.Group data-slot="dropdown-menu-group" {...props} /> ) } 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 DropdownMenuItem({ className, inset, variant = "default", ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.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 ( <DropdownMenuPrimitive.Item data-slot="dropdown-menu-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} /> ) } /** The check / radio indicator slot. */ 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 DropdownMenuCheckboxItem({ className, children, checked, ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.CheckboxItem>) { return ( <DropdownMenuPrimitive.CheckboxItem data-slot="dropdown-menu-checkbox-item" className={cn(itemBase, "py-1.5 pr-2 pl-8", className)} checked={checked} {...props} > <span className={indicatorSlot}> <DropdownMenuPrimitive.ItemIndicator className={indicatorPop}> <CheckIcon className="size-4 !text-foreground" /> </DropdownMenuPrimitive.ItemIndicator> </span> {children} </DropdownMenuPrimitive.CheckboxItem> ) } /** Holds radio items; only one can be selected. Use `value` / `onValueChange`. */ function DropdownMenuRadioGroup({ ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.RadioGroup>) { return ( <DropdownMenuPrimitive.RadioGroup data-slot="dropdown-menu-radio-group" {...props} /> ) } /** One choice inside a DropdownMenuRadioGroup. */ function DropdownMenuRadioItem({ className, children, ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.RadioItem>) { return ( <DropdownMenuPrimitive.RadioItem data-slot="dropdown-menu-radio-item" className={cn(itemBase, "py-1.5 pr-2 pl-8", className)} {...props} > <span className={indicatorSlot}> <DropdownMenuPrimitive.ItemIndicator className={indicatorPop}> <CircleIcon className="size-2 fill-current !text-foreground" /> </DropdownMenuPrimitive.ItemIndicator> </span> {children} </DropdownMenuPrimitive.RadioItem> ) } /** A non-interactive heading for a group of items. */ function DropdownMenuLabel({ className, inset, ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.Label> & { inset?: boolean }) { return ( <DropdownMenuPrimitive.Label 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} /> ) } /** A thin divider between groups. */ function DropdownMenuSeparator({ className, ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.Separator>) { return ( <DropdownMenuPrimitive.Separator data-slot="dropdown-menu-separator" className={cn("-mx-1 my-1 h-px bg-border", className)} {...props} /> ) } /** Right-aligned keyboard hint, e.g. ⌘K. Visual only; wire the shortcut yourself. */ function DropdownMenuShortcut({ className, ...props }: React.ComponentProps<"span">) { return ( <span data-slot="dropdown-menu-shortcut" className={cn( "ml-auto font-mono text-[0.7rem] tracking-widest text-muted-foreground/80", className )} {...props} /> ) } /** Wraps a sub-menu: a SubTrigger and its SubContent. */ function DropdownMenuSub({ ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.Sub>) { return <DropdownMenuPrimitive.Sub data-slot="dropdown-menu-sub" {...props} /> } /** The item that opens a sub-menu (hover, click, or the arrow key toward it). */ function DropdownMenuSubTrigger({ className, inset, children, ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.SubTrigger> & { inset?: boolean }) { return ( <DropdownMenuPrimitive.SubTrigger data-slot="dropdown-menu-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" /> </DropdownMenuPrimitive.SubTrigger> ) } /** The nested menu surface. */ function DropdownMenuSubContent({ className, ...props }: React.ComponentProps<typeof DropdownMenuPrimitive.SubContent>) { return ( <DropdownMenuPrimitive.Portal> <DropdownMenuPrimitive.SubContent data-slot="dropdown-menu-sub-content" className={cn( surface, "origin-(--radix-dropdown-menu-content-transform-origin)", className )} {...props} /> </DropdownMenuPrimitive.Portal> ) } export { DropdownMenu, DropdownMenuPortal, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuGroup, DropdownMenuLabel, DropdownMenuItem, DropdownMenuCheckboxItem, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuShortcut, DropdownMenuSub, DropdownMenuSubTrigger, DropdownMenuSubContent, }
Replaces components/ui/dropdown-menu.tsx with the same exports and props (including inset and variant="destructive"). Animations are CSS (tw-animate-css), so the base needs no Motion.
Usage
import { Button } from "@/components/ui/button"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
export function AccountMenu() {
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline">Account</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="start">
<DropdownMenuLabel>My account</DropdownMenuLabel>
<DropdownMenuItem>Profile</DropdownMenuItem>
<DropdownMenuItem>Billing</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem variant="destructive">Log out</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)
}Examples
Default
The menu scales out of the trigger (using Radix's transform origin) and slides in from the side it opens on, then closes faster than it opened. Includes groups, shortcuts, a disabled item, a sub-menu, and a destructive item.
import { CreditCard, Keyboard, LogOut, Mail, MessageSquare, Settings, User, UserPlus } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuShortcut,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
export default function DropdownMenuBasicDemo() {
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline">Open menu</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-56" align="start">
<DropdownMenuLabel>My account</DropdownMenuLabel>
<DropdownMenuGroup>
<DropdownMenuItem>
<User />
Profile
<DropdownMenuShortcut>⇧⌘P</DropdownMenuShortcut>
</DropdownMenuItem>
<DropdownMenuItem>
<CreditCard />
Billing
<DropdownMenuShortcut>⌘B</DropdownMenuShortcut>
</DropdownMenuItem>
<DropdownMenuItem>
<Settings />
Settings
<DropdownMenuShortcut>⌘,</DropdownMenuShortcut>
</DropdownMenuItem>
<DropdownMenuItem disabled>
<Keyboard />
Keyboard shortcuts
</DropdownMenuItem>
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuSub>
<DropdownMenuSubTrigger>
<UserPlus />
Invite users
</DropdownMenuSubTrigger>
<DropdownMenuSubContent>
<DropdownMenuItem>
<Mail />
Email
</DropdownMenuItem>
<DropdownMenuItem>
<MessageSquare />
Message
</DropdownMenuItem>
</DropdownMenuSubContent>
</DropdownMenuSub>
<DropdownMenuSeparator />
<DropdownMenuItem variant="destructive">
<LogOut />
Log out
<DropdownMenuShortcut>⇧⌘Q</DropdownMenuShortcut>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
}Checkboxes & radio group
Check marks and radio dots fade in when selected. Call event.preventDefault() in onSelect to keep the menu open while toggling several options.
"use client";
import * as React from "react";
import { SlidersHorizontal } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuCheckboxItem,
DropdownMenuContent,
DropdownMenuLabel,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
export default function DropdownMenuCheckboxesDemo() {
const [columns, setColumns] = React.useState({ status: true, owner: true, updated: false });
const [sort, setSort] = React.useState("newest");
const toggle = (key: keyof typeof columns) => (checked: boolean) =>
setColumns((c) => ({ ...c, [key]: checked }));
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline">
<SlidersHorizontal />
View
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-52" align="start">
<DropdownMenuLabel>Columns</DropdownMenuLabel>
{/* preventDefault keeps the menu open so you can toggle several. */}
<DropdownMenuCheckboxItem checked={columns.status} onCheckedChange={toggle("status")} onSelect={(e) => e.preventDefault()}>
Status
</DropdownMenuCheckboxItem>
<DropdownMenuCheckboxItem checked={columns.owner} onCheckedChange={toggle("owner")} onSelect={(e) => e.preventDefault()}>
Owner
</DropdownMenuCheckboxItem>
<DropdownMenuCheckboxItem checked={columns.updated} onCheckedChange={toggle("updated")} onSelect={(e) => e.preventDefault()}>
Last updated
</DropdownMenuCheckboxItem>
<DropdownMenuSeparator />
<DropdownMenuLabel>Sort by</DropdownMenuLabel>
<DropdownMenuRadioGroup value={sort} onValueChange={setSort}>
<DropdownMenuRadioItem value="newest" onSelect={(e) => e.preventDefault()}>
Newest first
</DropdownMenuRadioItem>
<DropdownMenuRadioItem value="oldest" onSelect={(e) => e.preventDefault()}>
Oldest first
</DropdownMenuRadioItem>
<DropdownMenuRadioItem value="name" onSelect={(e) => e.preventDefault()}>
Name
</DropdownMenuRadioItem>
</DropdownMenuRadioGroup>
</DropdownMenuContent>
</DropdownMenu>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Animated Dropdown
A dropdown that opens from its trigger's side while items cascade in. A highlight pill glides between items as you hover or arrow through them (a shared Motion layoutId), the hovered item's icon brightens, and separators draw in. It's still Radix underneath: focus, typeahead, arrow keys, Escape and aria all work.
"use client";
import { ChevronDown, Copy, Pencil, Share2, Star, Archive, Trash2 } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import {
AnimatedDropdown,
AnimatedDropdownContent,
AnimatedDropdownItem,
AnimatedDropdownLabel,
AnimatedDropdownSeparator,
AnimatedDropdownTrigger,
} from "@/components/dusk/dropdown-menu/animated-dropdown";
export default function AnimatedDropdownDemo() {
return (
<AnimatedDropdown>
<AnimatedDropdownTrigger asChild>
<Button variant="outline" className="group">
Actions
<ChevronDown className="transition-transform duration-200 group-data-[state=open]:rotate-180" />
</Button>
</AnimatedDropdownTrigger>
<AnimatedDropdownContent align="start" className="w-56">
<AnimatedDropdownLabel>Project</AnimatedDropdownLabel>
<AnimatedDropdownItem icon={<Pencil />} shortcut="⌘E" onSelect={() => toast("Renaming…")}>
Rename
</AnimatedDropdownItem>
<AnimatedDropdownItem icon={<Copy />} shortcut="⌘D" onSelect={() => toast("Duplicated")}>
Duplicate
</AnimatedDropdownItem>
<AnimatedDropdownItem icon={<Share2 />} onSelect={() => toast("Link copied")}>
Share
</AnimatedDropdownItem>
<AnimatedDropdownItem icon={<Star />} onSelect={() => toast("Added to favorites")}>
Add to favorites
</AnimatedDropdownItem>
<AnimatedDropdownSeparator />
<AnimatedDropdownItem icon={<Archive />} disabled>
Archive
</AnimatedDropdownItem>
<AnimatedDropdownItem icon={<Trash2 />} shortcut="⌫" variant="destructive" onSelect={() => toast.error("Project deleted")}>
Delete
</AnimatedDropdownItem>
</AnimatedDropdownContent>
</AnimatedDropdown>
);
}$ npx shadcn@latest add https://duskui.com/r/animated-dropdown.json1. Install the dependencies.
npm install motion @radix-ui/react-dropdown-menu2. Copy the source into
components/dusk/dropdown-menu/animated-dropdown.tsx.animated-dropdown.tsx "use client"; import * as React from "react"; import * as DropdownMenuPrimitive from "@radix-ui/react-dropdown-menu"; import { AnimatePresence, motion, useReducedMotion, type Transition, type Variants } from "motion/react"; import { DropdownMenuShortcut, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"; import { cn } from "@/lib/utils"; const pillSpring: Transition = { type: "spring", stiffness: 520, damping: 38, mass: 0.6 }; type Side = "top" | "right" | "bottom" | "left"; type AnimatedDropdownContextValue = { /** Base for the shared highlight layoutId, unique per menu. */ id: string; open: boolean; /** The item under the highlight pill, by its useId. */ active: string | null; setActive: (id: string | null) => void; reduceMotion: boolean; }; const AnimatedDropdownContext = React.createContext<AnimatedDropdownContextValue | null>(null); function useAnimatedDropdown() { const context = React.useContext(AnimatedDropdownContext); if (!context) throw new Error("AnimatedDropdown parts must be used inside <AnimatedDropdown>."); return context; } export interface AnimatedDropdownProps extends Omit<React.ComponentProps<typeof DropdownMenuPrimitive.Root>, "open" | "defaultOpen" | "onOpenChange"> { open?: boolean; defaultOpen?: boolean; onOpenChange?: (open: boolean) => void; } /** * An animated dropdown menu. It opens out of its trigger, items * cascade in, and a highlight pill glides between items as you hover or arrow * through them (a shared Motion `layoutId`). Built on Radix Dropdown Menu, so * focus management, typeahead, arrow keys, Escape and aria all work. Under * reduced motion it fades and the pill jumps. */ export function AnimatedDropdown({ open: openProp, defaultOpen = false, onOpenChange, children, ...props }: AnimatedDropdownProps) { const id = React.useId(); const reduceMotion = useReducedMotion() ?? false; const [internalOpen, setInternalOpen] = React.useState(defaultOpen); const [active, setActive] = React.useState<string | null>(null); const open = openProp ?? internalOpen; const setOpen = React.useCallback( (next: boolean) => { if (openProp === undefined) setInternalOpen(next); if (!next) setActive(null); onOpenChange?.(next); }, [openProp, onOpenChange] ); const value = React.useMemo( () => ({ id, open, active, setActive, reduceMotion }), [id, open, active, reduceMotion] ); return ( <AnimatedDropdownContext.Provider value={value}> <DropdownMenuPrimitive.Root open={open} onOpenChange={setOpen} {...props}> {children} </DropdownMenuPrimitive.Root> </AnimatedDropdownContext.Provider> ); } /** The element that opens the menu. Use `asChild` to render your own Button. */ export const AnimatedDropdownTrigger = DropdownMenuTrigger; export interface AnimatedDropdownContentProps extends Omit< React.ComponentProps<typeof DropdownMenuPrimitive.Content>, "forceMount" | "asChild" | "children" > { children?: React.ReactNode; side?: Side; /** Delay between items as they cascade in, in seconds. */ stagger?: number; } const offsets: Record<Side, { x?: number; y?: number }> = { bottom: { y: -4 }, top: { y: 4 }, left: { x: 4 }, right: { x: -4 }, }; /** The menu surface. Opens from the trigger's side and staggers its items in. */ export function AnimatedDropdownContent({ className, side = "bottom", sideOffset = 6, stagger = 0.035, children, onPointerLeave, ...props }: AnimatedDropdownContentProps) { const { open, reduceMotion, setActive } = useAnimatedDropdown(); const variants: Variants = reduceMotion ? { closed: { opacity: 0, transition: { duration: 0.1 } }, open: { opacity: 1, transition: { duration: 0.15 } }, } : { closed: { opacity: 0, scale: 0.96, ...offsets[side], transition: { duration: 0.1, ease: "easeIn" } }, open: { opacity: 1, scale: 1, x: 0, y: 0, transition: { duration: 0.18, ease: "easeOut", staggerChildren: stagger, delayChildren: 0.03 }, }, }; return ( <AnimatePresence> {open && ( // forceMount hands mounting to AnimatePresence so the exit animation can play. <DropdownMenuPrimitive.Portal key="menu" forceMount> <DropdownMenuPrimitive.Content forceMount asChild side={side} sideOffset={sideOffset} onPointerLeave={(event) => { onPointerLeave?.(event); setActive(null); }} {...props} > <motion.div className={cn( "z-50 min-w-48 overflow-x-hidden overflow-y-auto rounded-xl border bg-popover p-1.5 text-popover-foreground outline-none", "max-h-(--radix-dropdown-menu-content-available-height) origin-(--radix-dropdown-menu-content-transform-origin)", "shadow-lg", className )} variants={variants} initial="closed" animate="open" exit="closed" > {children} </motion.div> </DropdownMenuPrimitive.Content> </DropdownMenuPrimitive.Portal> )} </AnimatePresence> ); } function useItemVariants(): Variants { const { reduceMotion } = useAnimatedDropdown(); return reduceMotion ? { closed: { opacity: 0, transition: { duration: 0.1 } }, open: { opacity: 1 } } : { closed: { opacity: 0, y: -4, transition: { duration: 0.1 } }, open: { opacity: 1, y: 0, transition: { duration: 0.18, ease: "easeOut" } }, }; } export interface AnimatedDropdownItemProps extends Omit<React.ComponentProps<typeof DropdownMenuPrimitive.Item>, "asChild"> { /** Leading icon. */ icon?: React.ReactNode; /** Trailing keyboard hint, e.g. "⌘K". Visual only. */ shortcut?: React.ReactNode; /** `destructive` tints the item and its highlight red. */ variant?: "default" | "destructive"; } /** A menu action. `onSelect` fires on click, Enter or Space and closes the menu. */ export function AnimatedDropdownItem({ className, children, icon, shortcut, variant = "default", onFocus, ...props }: AnimatedDropdownItemProps) { const itemId = React.useId(); const { id, active, setActive, reduceMotion } = useAnimatedDropdown(); const variants = useItemVariants(); const isActive = active === itemId; return ( <DropdownMenuPrimitive.Item asChild // Radix focuses items on hover and arrow keys, so focus drives the pill. onFocus={(event) => { onFocus?.(event); setActive(itemId); }} {...props} > <motion.div variants={variants} data-variant={variant} className={cn( "relative flex cursor-default items-center gap-2.5 rounded-lg px-2.5 py-2 text-sm outline-none select-none", "transition-colors duration-150 data-[disabled]:pointer-events-none data-[disabled]:opacity-50", variant === "destructive" ? "text-destructive" : "text-foreground/90 data-[highlighted]:text-foreground", "[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", className )} > {isActive && ( <motion.span aria-hidden layoutId={`${id}-pill`} className={cn( "absolute inset-0 rounded-lg", variant === "destructive" ? "bg-destructive/15" : "bg-accent" )} transition={reduceMotion ? { duration: 0 } : pillSpring} /> )} {icon && ( <span aria-hidden className={cn( "relative flex transition-colors duration-150", variant === "destructive" ? "text-destructive" : isActive ? "text-foreground" : "text-muted-foreground" )} > {icon} </span> )} <span className="relative flex-1 truncate">{children}</span> {shortcut && <DropdownMenuShortcut className="relative">{shortcut}</DropdownMenuShortcut>} </motion.div> </DropdownMenuPrimitive.Item> ); } /** A small heading for a group of items. Cascades in with the items. */ export function AnimatedDropdownLabel({ className, ...props }: Omit<React.ComponentProps<typeof motion.div>, "variants">) { const variants = useItemVariants(); return ( <DropdownMenuPrimitive.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} /> </DropdownMenuPrimitive.Label> ); } /** A divider between groups. Draws in from the left as the menu opens. */ export function AnimatedDropdownSeparator({ className, ...props }: Omit<React.ComponentProps<typeof motion.div>, "variants">) { const { reduceMotion } = useAnimatedDropdown(); return ( <DropdownMenuPrimitive.Separator asChild> <motion.div variants={ reduceMotion ? { closed: { opacity: 0, transition: { duration: 0.1 } }, open: { opacity: 1 } } : { closed: { scaleX: 0, opacity: 0, transition: { duration: 0.1 } }, 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} /> </DropdownMenuPrimitive.Separator> ); } /** Groups related items (role="group"). */ export const AnimatedDropdownGroup = DropdownMenuPrimitive.Group;
| Prop | Type | Default | Description |
|---|---|---|---|
| open / defaultOpen / onOpenChange | boolean / boolean / (open: boolean) => void | — | Open state (on AnimatedDropdown). |
| AnimatedDropdownTrigger | Radix Trigger props | — | Use asChild to render your own Button. |
| AnimatedDropdownContent | Radix Content props & { stagger?: number } | — | side, align, sideOffset… stagger is the delay between items in seconds (default 0.035). |
| AnimatedDropdownItem | Radix Item props & { icon?, shortcut?, variant? } | — | An action. variant="destructive" tints it red. |
| AnimatedDropdownLabel / Separator / Group | Radix props | — | Headings, dividers and groups that animate with the items. |
API Reference
Same parts as shadcn/ui: DropdownMenu, Trigger, Content, Item, CheckboxItem, RadioGroup, RadioItem, Label, Separator, Shortcut, Group, Portal, Sub, SubTrigger and SubContent (all prefixed DropdownMenu). Each forwards the Radix props.
| Prop | Type | Default | Description |
|---|---|---|---|
| open / onOpenChange | boolean / (open: boolean) => void | — | Controlled state (on DropdownMenu). |
| side / align / sideOffset | string / string / number | "bottom" / "center" / 6 | Placement (on DropdownMenuContent). |
| onSelect | (event: Event) => void | — | Fires on click, Enter or Space; preventDefault() keeps the menu open (on items). |
| variant | "default" | "destructive" | "default" | Red destructive item (on DropdownMenuItem). |
| inset | boolean | — | Indent to align with items that have an indicator (Item, Label, SubTrigger). |
| checked / onCheckedChange | boolean / (checked: boolean) => void | — | On DropdownMenuCheckboxItem. |
Accessibility
- Built on Radix Dropdown Menu:
role="menu"with menuitem, menuitemcheckbox and menuitemradio roles, roving focus, typeahead, and focus returned to the trigger on close. - Arrow keys move between items, Right/Left open and close sub-menus, Enter/Space select and Escape closes.
- In the Animated Dropdown the highlight pill follows keyboard focus as well as the pointer, so keyboard users see the same feedback.
- Under prefers-reduced-motion menus only fade, the item cascade is removed and the highlight jumps instead of gliding.