Containment · Material 3: Menus
Menu
Actions on an object, opened from its button. The highlight slides and stretches toward submenus; danger items tint red.
Examples and variants
Basic
Shortcut, submenu, disabled and danger items.
function MenuBasic() {
return (
<DropdownMenu>
<DropdownMenuTrigger render={<Button variant="outline" size="icon" aria-label="Project actions" />}>
<MoreHorizontalIcon />
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem>
Rename <DropdownMenuShortcut>⌘R</DropdownMenuShortcut>
</DropdownMenuItem>
<DropdownMenuItem>Duplicate</DropdownMenuItem>
<DropdownMenuSub>
<DropdownMenuSubTrigger>Share</DropdownMenuSubTrigger>
<DropdownMenuSubContent>
<DropdownMenuItem>Copy link</DropdownMenuItem>
<DropdownMenuItem>Email</DropdownMenuItem>
</DropdownMenuSubContent>
</DropdownMenuSub>
<DropdownMenuItem disabled>Archive</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem tone="danger">Delete</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)
}Spec drawing
Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.
Install
With the CLI
The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/dropdown-menu.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install @base-ui/react cn lucide-reactCopy this file to the same paths in your project. Imports use the
@/alias for your project root.components/ui/dropdown-menu.tsxdropdown-menu
components/ui/dropdown-menu.tsx"use client" import * as React from "react" import { Menu as MenuPrimitive } from "@base-ui/react/menu" import { ChevronRightIcon } from "lucide-react" import { cn } from "cn" import { layoutTop } from "@/lib/motion/layout-offset" import { usePortalMotion } from "@/lib/overlay" function DropdownMenu(props: MenuPrimitive.Root.Props) { return <MenuPrimitive.Root {...props} /> } function DropdownMenuTrigger(props: MenuPrimitive.Trigger.Props) { return <MenuPrimitive.Trigger data-slot="menu-trigger" {...props} /> } /* On phones (< 640px) a root menu presents as a bottom sheet with a scrim (component-model.md §3: one Menu, adaptive presentation). Submenus and presentation="popover" keep the anchored popover everywhere. */ function DropdownMenuContent({ className, children, side, align, sideOffset = 6, presentation = "auto", ...props }: MenuPrimitive.Popup.Props & Pick<MenuPrimitive.Positioner.Props, "side" | "align" | "sideOffset"> & { presentation?: "auto" | "popover" }) { const submenu = (props as Record<string, unknown>)["data-submenu"] !== undefined const sheet = presentation === "auto" && !submenu const ref = React.useRef<HTMLDivElement>(null) usePortalMotion(ref) return ( <MenuPrimitive.Portal> {sheet ? <MenuPrimitive.Backdrop className="menu-sheet-scrim fixed inset-0 z-dropdown" /> : null} <MenuPrimitive.Positioner className={cn("z-dropdown", sheet && "menu-sheet-positioner")} side={side} align={align} sideOffset={sideOffset} > <MenuPrimitive.Popup ref={ref} data-slot="menu-content" data-sheet={sheet || undefined} className={cn( "overlay-surface overlay-grow z-dropdown min-w-(--menu-min-width) rounded-(--menu-corner) p-(--menu-padding) text-sm text-foreground", className )} {...props} > <MenuHighlight>{children}</MenuHighlight> </MenuPrimitive.Popup> </MenuPrimitive.Positioner> </MenuPrimitive.Portal> ) } export function MenuHighlight({ children }: { children: React.ReactNode }) { const ref = React.useRef<HTMLDivElement>(null) const [box, setBox] = React.useState({ top: 0, height: 0, stretch: false, danger: false }) const [ready, setReady] = React.useState(false) React.useLayoutEffect(() => { const root = ref.current if (!root) return const read = () => { const item = root.querySelector<HTMLElement>("[data-highlighted]") if (!item) return const motion = root.closest<HTMLElement>("[data-motion]")?.dataset.motion setBox({ top: layoutTop(item, root), height: item.offsetHeight, stretch: item.hasAttribute("data-popup-open") && motion !== "calm" && motion !== "still", danger: item.dataset.tone === "danger", }) setReady(true) } read() const observer = new MutationObserver(read) observer.observe(root, { subtree: true, attributes: true, attributeFilter: ["data-highlighted", "data-popup-open", "data-motion"], }) const resize = new ResizeObserver(read) resize.observe(root) return () => { observer.disconnect() resize.disconnect() } }, []) return ( <div ref={ref} className="relative"> {ready ? ( <span aria-hidden className={cn( "menu-pill pointer-events-none absolute left-1 rounded-(--menu-item-corner) bg-(--menu-item-highlight) transition-[background-color] duration-(--duration-fast)", box.danger && "bg-(--color-danger-subtle)" )} style={{ top: box.top, height: box.height, right: box.stretch ? "calc(var(--motion-distance-lg) * -1)" : "var(--space-1)", }} /> ) : null} {children} </div> ) } function DropdownMenuItem({ className, tone = "neutral", ...props }: MenuPrimitive.Item.Props & { tone?: "neutral" | "danger" }) { return ( <MenuPrimitive.Item data-slot="menu-item" data-tone={tone} className={cn( "menu-item relative z-[1] flex cursor-default items-center gap-inline-sm rounded-(--menu-item-corner) text-sm outline-none data-disabled:text-(--menu-item-label-disabled)", tone === "danger" && "text-(--color-danger-fg)", className )} {...props} /> ) } function DropdownMenuCheckboxItem({ className, ...props }: MenuPrimitive.CheckboxItem.Props) { return ( <MenuPrimitive.CheckboxItem data-slot="menu-item" className={cn( "menu-item relative z-[1] flex cursor-default items-center rounded-(--menu-item-corner) text-sm outline-none", className )} {...props} /> ) } function DropdownMenuRadioGroup(props: MenuPrimitive.RadioGroup.Props) { return <MenuPrimitive.RadioGroup {...props} /> } function DropdownMenuRadioItem({ className, ...props }: MenuPrimitive.RadioItem.Props) { return ( <MenuPrimitive.RadioItem data-slot="menu-item" className={cn( "menu-item relative z-[1] flex cursor-default items-center rounded-(--menu-item-corner) text-sm outline-none", className )} {...props} /> ) } function DropdownMenuLabel({ className, ...props }: MenuPrimitive.GroupLabel.Props) { return ( <MenuPrimitive.GroupLabel className={cn("px-2 py-1 text-xs text-muted-foreground", className)} {...props} /> ) } function DropdownMenuSeparator({ className, ...props }: React.ComponentProps<"div">) { return <div aria-hidden className={cn("h-stack-sm", className)} {...props} /> } function DropdownMenuSub(props: MenuPrimitive.SubmenuRoot.Props) { return <MenuPrimitive.SubmenuRoot {...props} /> } function DropdownMenuSubTrigger({ className, children, ...props }: MenuPrimitive.SubmenuTrigger.Props) { return ( <MenuPrimitive.SubmenuTrigger data-slot="menu-sub-trigger" className={cn( "menu-item relative z-[1] flex w-full cursor-default items-center gap-inline-sm rounded-(--menu-item-corner) text-sm outline-none", className )} {...props} > {children} <ChevronRightIcon className="ml-auto size-icon-sm text-muted-foreground" /> </MenuPrimitive.SubmenuTrigger> ) } function DropdownMenuSubContent(props: MenuPrimitive.Popup.Props) { return <DropdownMenuContent data-submenu="" {...props} /> } function DropdownMenuShortcut({ className, ...props }: React.ComponentProps<"span">) { return <span className={cn("ml-auto text-xs text-muted-foreground", className)} {...props} /> } export { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuItem, DropdownMenuCheckboxItem, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuSub, DropdownMenuSubTrigger, DropdownMenuSubContent, DropdownMenuShortcut, }
Imports
What the examples on this page import:
import { MoreHorizontalIcon } from "lucide-react"
import { Button } from "@/components/ui/button"
import {Anatomy
- 1Container · --menu-container, --menu-corner, --menu-padding, --menu-elevation, --menu-min-width.
- 2Item · --menu-item-corner; highlight --menu-item-highlight; danger --menu-item-danger.
- 3Shortcut · Right-aligned hint.
- 4Submenu · Opens sideways from its row.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
DropdownMenuItem tone | "neutral" | "danger" | "neutral" | Danger tints text and highlight. |
DropdownMenuItem disabled | boolean | — | Skipped by the keyboard. |
Tokens
Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.
| Token | Points to | Light | Dark |
|---|---|---|---|
--menu-container | var(--surface-container-raised) | oklch(1 0 0) | oklch(0.22 0 0) |
--menu-corner | var(--shape-md) | 14px | same |
--menu-padding | var(--space-inset-overlay) | 8px | same |
--menu-elevation | var(--elevation-2) | 0 1px 1px -0.5px oklch(0.22 0.01 260 / 6%), 0 3px 3px -1.5px oklch(0.22 0.01 260 / 6%), 0 6px 6px -3px oklch(0.22 0.01 260 / 6%), 0 12px 12px -6px oklch(0.22 0.01 260 / 6%), 0 24px 24px -12px oklch(0.22 0.01 260 / 6%) | 0 4px 12px oklch(0 0 0 / 40%), 0 12px 28px oklch(0 0 0 / 35%) |
--menu-item-height | var(--size-row-sm) | 32px | same |
--menu-item-padding-x | var(--space-inset-control-x-sm) | 8px | same |
--menu-item-corner | var(--shape-sm) | 8px | same |
--menu-item-highlight | var(--color-bg-hover) | oklch(0.965 0.005 95) | oklch(0.22 0 0) |
--menu-item-label-disabled | var(--color-fg-disabled) | oklch(0.62 0.01 95) | oklch(0.53 0 0) |
--menu-item-danger | var(--color-danger-fg) | oklch(0.48 0.18 25) | oklch(0.78 0.14 22) |
--menu-min-width | var(--layout-menu-min) | 180px | same |
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| Open | Grows from the trigger; items rise in. | --duration-fast, --ease-bounce |
| Highlight | The pill slides; it stretches toward an opening submenu. | --duration-fast |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- Escape closes and focus returns to the trigger.
- The trigger is always a Button (render={<Button />}).
- Arrow keys move; Enter runs; Right opens a submenu.
Usage
Do
- Use for 3+ actions on one object.
Don’t
- Don't use to pick a value (use Select).
Code
"use client"
import * as React from "react"
import { Menu as MenuPrimitive } from "@base-ui/react/menu"
import { ChevronRightIcon } from "lucide-react"
import { cn } from "cn"
import { layoutTop } from "@/lib/motion/layout-offset"
import { usePortalMotion } from "@/lib/overlay"
function DropdownMenu(props: MenuPrimitive.Root.Props) {
return <MenuPrimitive.Root {...props} />
}
function DropdownMenuTrigger(props: MenuPrimitive.Trigger.Props) {
return <MenuPrimitive.Trigger data-slot="menu-trigger" {...props} />
}
/*
On phones (< 640px) a root menu presents as a bottom sheet with a scrim
(component-model.md §3: one Menu, adaptive presentation). Submenus and
presentation="popover" keep the anchored popover everywhere.
*/
function DropdownMenuContent({
className,
children,
side,
align,
sideOffset = 6,
presentation = "auto",
...props
}: MenuPrimitive.Popup.Props &
Pick<MenuPrimitive.Positioner.Props, "side" | "align" | "sideOffset"> & {
presentation?: "auto" | "popover"
}) {
const submenu = (props as Record<string, unknown>)["data-submenu"] !== undefined
const sheet = presentation === "auto" && !submenu
const ref = React.useRef<HTMLDivElement>(null)
usePortalMotion(ref)
return (
<MenuPrimitive.Portal>
{sheet ? <MenuPrimitive.Backdrop className="menu-sheet-scrim fixed inset-0 z-dropdown" /> : null}
<MenuPrimitive.Positioner
className={cn("z-dropdown", sheet && "menu-sheet-positioner")}
side={side}
align={align}
sideOffset={sideOffset}
>
<MenuPrimitive.Popup
ref={ref}
data-slot="menu-content"
data-sheet={sheet || undefined}
className={cn(
"overlay-surface overlay-grow z-dropdown min-w-(--menu-min-width) rounded-(--menu-corner) p-(--menu-padding) text-sm text-foreground",
className
)}
{...props}
>
<MenuHighlight>{children}</MenuHighlight>
</MenuPrimitive.Popup>
</MenuPrimitive.Positioner>
</MenuPrimitive.Portal>
)
}
export function MenuHighlight({ children }: { children: React.ReactNode }) {
const ref = React.useRef<HTMLDivElement>(null)
const [box, setBox] = React.useState({ top: 0, height: 0, stretch: false, danger: false })
const [ready, setReady] = React.useState(false)
React.useLayoutEffect(() => {
const root = ref.current
if (!root) return
const read = () => {
const item = root.querySelector<HTMLElement>("[data-highlighted]")
if (!item) return
const motion = root.closest<HTMLElement>("[data-motion]")?.dataset.motion
setBox({
top: layoutTop(item, root),
height: item.offsetHeight,
stretch: item.hasAttribute("data-popup-open") && motion !== "calm" && motion !== "still",
danger: item.dataset.tone === "danger",
})
setReady(true)
}
read()
const observer = new MutationObserver(read)
observer.observe(root, {
subtree: true,
attributes: true,
attributeFilter: ["data-highlighted", "data-popup-open", "data-motion"],
})
const resize = new ResizeObserver(read)
resize.observe(root)
return () => {
observer.disconnect()
resize.disconnect()
}
}, [])
return (
<div ref={ref} className="relative">
{ready ? (
<span
aria-hidden
className={cn(
"menu-pill pointer-events-none absolute left-1 rounded-(--menu-item-corner) bg-(--menu-item-highlight) transition-[background-color] duration-(--duration-fast)",
box.danger && "bg-(--color-danger-subtle)"
)}
style={{
top: box.top,
height: box.height,
right: box.stretch ? "calc(var(--motion-distance-lg) * -1)" : "var(--space-1)",
}}
/>
) : null}
{children}
</div>
)
}
function DropdownMenuItem({
className,
tone = "neutral",
...props
}: MenuPrimitive.Item.Props & { tone?: "neutral" | "danger" }) {
return (
<MenuPrimitive.Item
data-slot="menu-item"
data-tone={tone}
className={cn(
"menu-item relative z-[1] flex cursor-default items-center gap-inline-sm rounded-(--menu-item-corner) text-sm outline-none data-disabled:text-(--menu-item-label-disabled)",
tone === "danger" && "text-(--color-danger-fg)",
className
)}
{...props}
/>
)
}
function DropdownMenuCheckboxItem({ className, ...props }: MenuPrimitive.CheckboxItem.Props) {
return (
<MenuPrimitive.CheckboxItem
data-slot="menu-item"
className={cn(
"menu-item relative z-[1] flex cursor-default items-center rounded-(--menu-item-corner) text-sm outline-none",
className
)}
{...props}
/>
)
}
function DropdownMenuRadioGroup(props: MenuPrimitive.RadioGroup.Props) {
return <MenuPrimitive.RadioGroup {...props} />
}
function DropdownMenuRadioItem({ className, ...props }: MenuPrimitive.RadioItem.Props) {
return (
<MenuPrimitive.RadioItem
data-slot="menu-item"
className={cn(
"menu-item relative z-[1] flex cursor-default items-center rounded-(--menu-item-corner) text-sm outline-none",
className
)}
{...props}
/>
)
}
function DropdownMenuLabel({ className, ...props }: MenuPrimitive.GroupLabel.Props) {
return (
<MenuPrimitive.GroupLabel
className={cn("px-2 py-1 text-xs text-muted-foreground", className)}
{...props}
/>
)
}
function DropdownMenuSeparator({ className, ...props }: React.ComponentProps<"div">) {
return <div aria-hidden className={cn("h-stack-sm", className)} {...props} />
}
function DropdownMenuSub(props: MenuPrimitive.SubmenuRoot.Props) {
return <MenuPrimitive.SubmenuRoot {...props} />
}
function DropdownMenuSubTrigger({ className, children, ...props }: MenuPrimitive.SubmenuTrigger.Props) {
return (
<MenuPrimitive.SubmenuTrigger
data-slot="menu-sub-trigger"
className={cn(
"menu-item relative z-[1] flex w-full cursor-default items-center gap-inline-sm rounded-(--menu-item-corner) text-sm outline-none",
className
)}
{...props}
>
{children}
<ChevronRightIcon className="ml-auto size-icon-sm text-muted-foreground" />
</MenuPrimitive.SubmenuTrigger>
)
}
function DropdownMenuSubContent(props: MenuPrimitive.Popup.Props) {
return <DropdownMenuContent data-submenu="" {...props} />
}
function DropdownMenuShortcut({ className, ...props }: React.ComponentProps<"span">) {
return <span className={cn("ml-auto text-xs text-muted-foreground", className)} {...props} />
}
export {
DropdownMenu,
DropdownMenuTrigger,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuCheckboxItem,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuSub,
DropdownMenuSubTrigger,
DropdownMenuSubContent,
DropdownMenuShortcut,
}