The sheet unfolds from its button, rows cascade in and one highlight glides between items.
Part of Keyframery, the open-source animated components for UI SDK
The demo plays by itself. Point at it or press Tab to take over.
Installation
npx shadcn@latest add @keyframery/menuThis adds components/keyframery/menu.tsx and its primitive, components/keyframery/primitives/menu.tsx, to your project, and installs @ui-sdk/react. The motion tokens are added to your global CSS the first time you install a component.
Usage
import {
Menu,
MenuCheckboxItem,
MenuContent,
MenuContextTrigger,
MenuGroup,
MenuGroupLabel,
MenuItem,
MenuItemIcon,
MenuItemIndicator,
MenuItems,
MenuRadioGroup,
MenuRadioItem,
MenuSeparator,
MenuShortcut,
MenuSub,
MenuSubContent,
MenuSubTrigger,
MenuTrigger,
} from "@/components/keyframery/menu"The parts and props are the same as UI SDK's Menu. For every prop, see the UI SDK menu reference.
What moves
| Move | What you see |
|---|---|
| Unfold | The sheet unfolds from the button that opened it. |
| Cascade | Rows arrive one after another. |
| Glide | One highlight glides between items. |
| Draw | Checks draw themselves; submenus unfold sideways. |
Timing and springiness follow the motion style you set, so this component moves like the rest of your app.
Swap the import
Already using UI SDK's menu with your own styles? Keep your markup and change one line. The primitive adds motion and nothing else.
// Before
import { … } from "@ui-sdk/react/menu"
// After: same parts, same props, now animated
import { … } from "@/components/keyframery/primitives/menu"Parts
The finished component exports 18 parts:
Menu, MenuCheckboxItem, MenuContent, MenuContextTrigger, MenuGroup, MenuGroupLabel, MenuItem, MenuItemIcon, MenuItemIndicator, MenuItems, MenuRadioGroup, MenuRadioItem, MenuSeparator, MenuShortcut, MenuSub, MenuSubContent, MenuSubTrigger, MenuTrigger
Accessibility
Keyboard, focus and screen-reader behaviour come from UI SDK and are not changed. With reduced motion turned on, nothing in the menu moves: changes happen at once or with a short fade. More in Accessibility.
Source
The code that lands in your project. It is yours to change.
"use client"
/* Styled menu: the animated primitive plus the Keyframery look. */
import {
Menu as MenuRoot,
MenuCheckboxItem as MenuCheckboxItemPart,
MenuContent as MenuContentPart,
MenuContextTrigger as MenuContextTriggerPart,
type MenuData,
MenuGroup as MenuGroupPart,
MenuGroupLabel as MenuGroupLabelPart,
MenuItem as MenuItemPart,
MenuItemIcon as MenuItemIconPart,
MenuItemIndicator as MenuItemIndicatorPart,
MenuRadioGroup as MenuRadioGroupPart,
MenuRadioItem as MenuRadioItemPart,
MenuSeparator as MenuSeparatorPart,
MenuShortcut as MenuShortcutPart,
MenuSub as MenuSubRoot,
MenuSubContent as MenuSubContentPart,
MenuSubTrigger as MenuSubTriggerPart,
MenuTrigger as MenuTriggerPart,
} from "@/components/keyframery/primitives/menu"
import * as React from "react"
import { cn } from "@/lib/utils"
/* The sheet. Its motion (unfold, cascade, the gliding highlight) comes from the primitive;
here the highlight gets its colour and radius, and turns red under a destructive row. */
const sheet = cn(
"m-0 max-h-(--available-height) min-w-44 overflow-y-auto overscroll-contain border-0 p-1.5 outline-none",
"rounded-[14px] bg-popover text-popover-foreground",
"shadow-[0_0_0_1px_rgb(0_0_0/0.06),0_2px_6px_-2px_rgb(0_0_0/0.08),0_12px_32px_-8px_rgb(0_0_0/0.18)]",
"dark:shadow-[0_0_0_1px_rgb(255_255_255/0.08),0_12px_32px_-8px_rgb(0_0_0/0.7)]",
"before:rounded-[8px] before:bg-accent",
"has-[[data-destructive][data-highlighted]]:before:bg-destructive/10",
"has-[[data-disabled][data-highlighted]]:before:bg-accent/50",
)
/* One row for every kind of item. No background of its own: the sheet's highlight glides under it,
and that highlight is the focus indicator. */
const row = cn(
"relative flex min-h-8 cursor-default items-center gap-2 rounded-[8px] px-2 py-1.5 text-[13px] leading-4 outline-none select-none",
"data-highlighted:text-accent-foreground",
"data-disabled:text-muted-foreground/60 data-disabled:data-highlighted:text-muted-foreground/60",
/* Rows without an icon keep the icon column when another row in the same menu has one. */
"[[data-has-icons]>&:not([data-has-icon])]:ps-8 [[data-has-icons]>[role=group]>&:not([data-has-icon])]:ps-8",
)
const shortcut = "ms-auto ps-5 text-[12px] leading-4 tracking-[0.04em] text-muted-foreground"
function Chevron({ className }: { className?: string }) {
return (
<svg
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden
className={className}
>
<path d="m6 3.5 4.5 4.5L6 12.5" />
</svg>
)
}
/* The check. The primitive draws its stroke in. */
function Check() {
return (
<svg
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden
className="size-4"
>
<path d="m3.5 8.5 3 3 6-7" />
</svg>
)
}
function Menu(props: React.ComponentProps<typeof MenuRoot>) {
return <MenuRoot {...props} />
}
/* A key by default. Pass `render` to use a button of your own instead. */
function MenuTrigger({
className,
...props
}: React.ComponentProps<typeof MenuTriggerPart>) {
return (
<MenuTriggerPart
className={cn(
"outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
!props.render && [
"inline-flex h-8 shrink-0 cursor-pointer items-center justify-center gap-1.5 rounded-[9px] bg-background px-3 text-[13px] leading-none font-medium whitespace-nowrap text-foreground select-none [&_svg]:size-4 [&_svg]:shrink-0",
"shadow-[0_0_0_1px_rgb(0_0_0/0.06),0_1px_1px_rgb(0_0_0/0.05),0_3px_6px_-2px_rgb(0_0_0/0.1)]",
"dark:bg-accent dark:shadow-[0_0_0_1px_rgb(255_255_255/0.07),inset_0_1px_0_rgb(255_255_255/0.08),0_2px_6px_-1px_rgb(0_0_0/0.6)]",
"hover:bg-muted data-open:bg-muted dark:hover:bg-accent/80 dark:data-open:bg-accent/80",
"active:shadow-[0_0_0_1px_rgb(0_0_0/0.08),0_1px_1px_rgb(0_0_0/0.04)]",
"dark:active:shadow-[0_0_0_1px_rgb(255_255_255/0.07),inset_0_1px_0_rgb(255_255_255/0.05),0_1px_2px_rgb(0_0_0/0.5)]",
"data-disabled:cursor-not-allowed data-disabled:opacity-40 data-disabled:active:scale-100",
],
className,
)}
{...props}
/>
)
}
/* The area a right-click or a long press opens the menu from. It has no look of its own. */
function MenuContextTrigger({
className,
...props
}: React.ComponentProps<typeof MenuContextTriggerPart>) {
return (
<MenuContextTriggerPart
className={cn(
"outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
className,
)}
{...props}
/>
)
}
function MenuContent<Payload = unknown>({
className,
sideOffset = 6,
...props
}: React.ComponentProps<typeof MenuContentPart<Payload>>) {
return (
<MenuContentPart<Payload>
sideOffset={sideOffset}
className={cn(sheet, className)}
{...props}
/>
)
}
function MenuItem({
className,
...props
}: React.ComponentProps<typeof MenuItemPart>) {
return (
<MenuItemPart
className={cn(
row,
"*:data-[slot=menu-shortcut]:ms-auto *:data-[slot=menu-shortcut]:ps-5 *:data-[slot=menu-shortcut]:text-[12px] *:data-[slot=menu-shortcut]:tracking-[0.04em] *:data-[slot=menu-shortcut]:text-muted-foreground",
"data-destructive:text-destructive data-destructive:data-highlighted:text-destructive",
"data-pending:cursor-progress data-pending:text-muted-foreground",
"data-pending:after:ms-auto data-pending:after:size-3 data-pending:after:shrink-0 data-pending:after:animate-spin data-pending:after:rounded-full data-pending:after:border-[1.5px] data-pending:after:border-current data-pending:after:border-t-transparent motion-reduce:data-pending:after:animate-none!",
className,
)}
{...props}
/>
)
}
function MenuShortcut({
className,
...props
}: React.ComponentProps<typeof MenuShortcutPart>) {
return <MenuShortcutPart className={cn(shortcut, className)} {...props} />
}
function MenuCheckboxItem({
className,
...props
}: React.ComponentProps<typeof MenuCheckboxItemPart>) {
return <MenuCheckboxItemPart className={cn(row, className)} {...props} />
}
function MenuRadioGroup<T extends string = string>({
className,
...props
}: React.ComponentProps<typeof MenuRadioGroupPart<T>>) {
return <MenuRadioGroupPart<T> className={cn(className)} {...props} />
}
function MenuRadioItem({
className,
...props
}: React.ComponentProps<typeof MenuRadioItemPart>) {
return <MenuRadioItemPart className={cn(row, className)} {...props} />
}
/* The check of a checkbox or radio item, at the end of its row. It stays mounted so it can draw in and out. */
function MenuItemIndicator({
className,
children,
keepMounted = true,
...props
}: React.ComponentProps<typeof MenuItemIndicatorPart>) {
return (
<MenuItemIndicatorPart
keepMounted={keepMounted}
className={cn(
"order-last ms-auto flex size-4 shrink-0 items-center justify-center ps-0 [&>svg]:size-4",
className,
)}
{...props}
>
{children ?? <Check />}
</MenuItemIndicatorPart>
)
}
function MenuItemIcon({
className,
...props
}: React.ComponentProps<typeof MenuItemIconPart>) {
return (
<MenuItemIconPart
className={cn(
"flex size-4 shrink-0 items-center justify-center text-muted-foreground [&>svg]:size-4",
"in-data-destructive:text-current in-data-highlighted:text-current",
"rtl:data-mirror:-scale-x-100",
className,
)}
{...props}
/>
)
}
function MenuGroup({
className,
...props
}: React.ComponentProps<typeof MenuGroupPart>) {
return <MenuGroupPart className={cn(className)} {...props} />
}
function MenuGroupLabel({
className,
...props
}: React.ComponentProps<typeof MenuGroupLabelPart>) {
return (
<MenuGroupLabelPart
className={cn(
"px-2 pt-1.5 pb-1 text-[11px] leading-4 font-medium text-muted-foreground select-none",
/* Lines up with the item text when the rows carry icons. */
"[[data-has-icons]>&]:ps-8 [[data-has-icons]>[role=group]>&]:ps-8",
className,
)}
{...props}
/>
)
}
function MenuSeparator({
className,
...props
}: React.ComponentProps<typeof MenuSeparatorPart>) {
return (
<MenuSeparatorPart
className={cn("-mx-1.5 my-1.5 h-px bg-border", className)}
{...props}
/>
)
}
function MenuSub(props: React.ComponentProps<typeof MenuSubRoot>) {
return <MenuSubRoot {...props} />
}
/* A row that keeps its highlight while its submenu is open, with a chevron that points to it. */
function MenuSubTrigger({
className,
children,
...props
}: Omit<React.ComponentProps<typeof MenuSubTriggerPart>, "children"> & {
children?: React.ReactNode
}) {
return (
<MenuSubTriggerPart
className={cn(
row,
"data-open:text-accent-foreground",
className,
)}
{...props}
>
{children}
<Chevron className="ms-auto -me-0.5 size-4 shrink-0 text-muted-foreground rtl:-scale-x-100" />
</MenuSubTriggerPart>
)
}
function MenuSubContent<Payload = unknown>({
className,
sideOffset = 8,
...props
}: React.ComponentProps<typeof MenuSubContentPart<Payload>>) {
return (
<MenuSubContentPart<Payload>
sideOffset={sideOffset}
className={cn(sheet, className)}
{...props}
/>
)
}
/* Renders a menu written as data with the styled parts above. */
function MenuItems({ items }: { items: MenuData }) {
return items.map((entry, index) => {
const item = typeof entry === "string" ? { value: entry, label: entry } : entry
if (!("type" in item) || item.type === undefined || item.type === "item") {
return (
<MenuItem
key={item.value}
value={item.value}
disabled={item.disabled}
shortcut={item.shortcut}
closeOnSelect={item.closeOnSelect}
destructive={item.destructive}
>
{item.label}
</MenuItem>
)
}
switch (item.type) {
case "separator":
return <MenuSeparator key={`separator-${index}`} />
case "group":
return (
<MenuGroup key={`group-${index}`}>
{item.label && <MenuGroupLabel>{item.label}</MenuGroupLabel>}
<MenuItems items={item.items} />
</MenuGroup>
)
case "checkbox":
return (
<MenuCheckboxItem
key={item.value}
value={item.value}
disabled={item.disabled}
checked={item.checked}
defaultChecked={item.defaultChecked}
closeOnSelect={item.closeOnSelect}
>
{item.label}
<MenuItemIndicator />
</MenuCheckboxItem>
)
case "radio-group":
return (
<MenuRadioGroup
key={item.name}
name={item.name}
value={item.value}
defaultValue={item.defaultValue}
>
{item.label && <MenuGroupLabel>{item.label}</MenuGroupLabel>}
{item.items.map((radio) => (
<MenuRadioItem key={radio.value} value={radio.value} disabled={radio.disabled}>
{radio.label}
<MenuItemIndicator />
</MenuRadioItem>
))}
</MenuRadioGroup>
)
case "submenu":
return (
<MenuSub key={`submenu-${index}`}>
<MenuSubTrigger disabled={item.disabled}>{item.label}</MenuSubTrigger>
<MenuSubContent>
<MenuItems items={item.items} />
</MenuSubContent>
</MenuSub>
)
}
})
}
export { createMenuController, type MenuData } from "@/components/keyframery/primitives/menu"
export {
Menu,
MenuCheckboxItem,
MenuContent,
MenuContextTrigger,
MenuGroup,
MenuGroupLabel,
MenuItem,
MenuItemIcon,
MenuItemIndicator,
MenuItems,
MenuRadioGroup,
MenuRadioItem,
MenuSeparator,
MenuShortcut,
MenuSub,
MenuSubContent,
MenuSubTrigger,
MenuTrigger,
}"use client"
/* Animated drop-in for `@ui-sdk/react/menu`: same parts, same props. Swap the import and keep your code.
This file adds motion only. Colours, radii, shadows and spacing stay yours.
- Content and submenus: Unfold from the trigger (scale 0.86, 6px blur, 8px toward it; spring in, quick out).
A submenu unfolds sideways, from the row that opened it.
- Rows: Cascade in, one after another, as the sheet opens.
- Glide: one highlight springs from row to row (CSS anchor positioning), instead of each row lighting up.
Its neutral surface follows the text colour; give `before:bg-…` and `before:rounded-…` to restyle it.
- Checks: Draw themselves. Trigger: Press. */
import {
MenuCheckboxItem as MenuCheckboxItemPart,
MenuContent as MenuContentPart,
MenuItem as MenuItemPart,
MenuItemIndicator as MenuItemIndicatorPart,
MenuRadioItem as MenuRadioItemPart,
MenuSubContent as MenuSubContentPart,
MenuSubTrigger as MenuSubTriggerPart,
MenuTrigger as MenuTriggerPart,
} from "@ui-sdk/react/menu"
import * as React from "react"
import { cn } from "@/lib/utils"
export * from "@ui-sdk/react/menu"
/* Unfold + Cascade + Glide, shared by the content and its submenus. The cascade runs on the
direct children (rows, groups, separators), never on a nested submenu. */
const sheet = cn(
"group/kf-menu origin-(--transform-origin)",
"[transition:opacity_calc(380ms*var(--kf-speed,1))_var(--ease-spring),scale_calc(380ms*var(--kf-speed,1))_var(--ease-spring),translate_calc(380ms*var(--kf-speed,1))_var(--ease-spring),filter_calc(380ms*var(--kf-speed,1))_var(--ease-spring)]",
"data-[side=bottom]:[--kf-from:0_-8px] data-[side=top]:[--kf-from:0_8px] data-[side=left]:[--kf-from:8px_0] data-[side=right]:[--kf-from:-8px_0]",
"data-[side=inline-end]:[--kf-from:-8px_0] data-[side=inline-start]:[--kf-from:8px_0]",
"data-starting-style:[translate:var(--kf-from,0_0)] data-starting-style:scale-[0.86] data-starting-style:opacity-0 data-starting-style:blur-[6px]",
"data-ending-style:scale-[0.96] data-ending-style:opacity-0 data-ending-style:blur-[2px]",
"data-ending-style:[transition:opacity_calc(150ms*var(--kf-speed,1))_var(--ease-house),scale_calc(150ms*var(--kf-speed,1))_var(--ease-house),translate_calc(150ms*var(--kf-speed,1))_var(--ease-house),filter_calc(150ms*var(--kf-speed,1))_var(--ease-house)]",
/* Cascade */
"[&>*:not([data-slot=menu-content])]:transition-[opacity,translate,filter] [&>*:not([data-slot=menu-content])]:duration-[calc(420ms*var(--kf-speed,1))] [&>*:not([data-slot=menu-content])]:ease-spring",
"[&>*:not([data-slot=menu-content])]:[transition-delay:calc((min(sibling-index(),10)-1)*calc(32ms*var(--kf-speed,1)))]",
"data-starting-style:[&>*:not([data-slot=menu-content])]:translate-y-2 data-starting-style:[&>*:not([data-slot=menu-content])]:opacity-0 data-starting-style:[&>*:not([data-slot=menu-content])]:blur-[4px]",
"data-ending-style:[&>*:not([data-slot=menu-content])]:[transition-delay:0ms]",
"motion-reduce:[&>*:not([data-slot=menu-content])]:[translate:none]! motion-reduce:[&>*:not([data-slot=menu-content])]:[filter:none]! data-starting-style:[&>*:not([data-slot=menu-content])]:[transition:none]",
/* Glide */
"relative isolate [anchor-scope:--kf-hl]",
"before:pointer-events-none before:absolute before:-z-10 before:hidden before:bg-[color-mix(in_oklch,currentColor_9%,transparent)]",
"before:[position-anchor:--kf-hl] before:top-[anchor(top)] before:right-[anchor(right)] before:bottom-[anchor(bottom)] before:left-[anchor(left)]",
"before:[transition:top_calc(260ms*var(--kf-speed,1))_var(--ease-spring),right_calc(260ms*var(--kf-speed,1))_var(--ease-spring),bottom_calc(260ms*var(--kf-speed,1))_var(--ease-spring),left_calc(260ms*var(--kf-speed,1))_var(--ease-spring),opacity_calc(150ms*var(--kf-speed,1))_var(--ease-house)]",
"has-[[data-highlighted],[data-slot=menu-trigger][data-open]]:before:block starting:before:opacity-0",
"motion-reduce:[transition:none]! motion-reduce:before:[transition:none]! motion-reduce:[&>*]:[transition:none]",
)
/* A row anchors the highlight while it is highlighted. */
const row = "data-highlighted:[anchor-name:--kf-hl]"
function MenuTrigger({ className, ...props }: React.ComponentProps<typeof MenuTriggerPart>) {
return (
<MenuTriggerPart
className={cn(
"transition-[scale] duration-[calc(200ms*var(--kf-speed,1))] ease-pop active:scale-[0.96] motion-reduce:transition-none!",
className,
)}
{...props}
/>
)
}
function MenuContent<Payload = unknown>({
className,
...props
}: React.ComponentProps<typeof MenuContentPart<Payload>>) {
return <MenuContentPart<Payload> className={cn(sheet, className)} {...props} />
}
function MenuSubContent<Payload = unknown>({
className,
...props
}: React.ComponentProps<typeof MenuSubContentPart<Payload>>) {
return <MenuSubContentPart<Payload> className={cn(sheet, className)} {...props} />
}
function MenuItem({ className, ...props }: React.ComponentProps<typeof MenuItemPart>) {
return <MenuItemPart className={cn(row, className)} {...props} />
}
function MenuCheckboxItem({ className, ...props }: React.ComponentProps<typeof MenuCheckboxItemPart>) {
return <MenuCheckboxItemPart className={cn(row, className)} {...props} />
}
function MenuRadioItem({ className, ...props }: React.ComponentProps<typeof MenuRadioItemPart>) {
return <MenuRadioItemPart className={cn(row, className)} {...props} />
}
/* Keeps the highlight while its submenu is open; its chevron (the last svg) nudges toward it. */
function MenuSubTrigger({ className, ...props }: React.ComponentProps<typeof MenuSubTriggerPart>) {
return (
<MenuSubTriggerPart
className={cn(
row,
"data-open:[anchor-name:--kf-hl]",
"[&>svg:last-child]:transition-[translate] [&>svg:last-child]:duration-[calc(380ms*var(--kf-speed,1))] [&>svg:last-child]:ease-spring data-open:[&>svg:last-child]:translate-x-0.5 rtl:data-open:[&>svg:last-child]:-translate-x-0.5",
"motion-reduce:[&>svg:last-child]:transition-none",
className,
)}
{...props}
/>
)
}
/* Draw: the check's stroke draws in when its item turns on, and again as the sheet unfolds.
Works with any check path up to 24 units long (a 16 or 24 unit icon). */
function MenuItemIndicator({ className, ...props }: React.ComponentProps<typeof MenuItemIndicatorPart>) {
return (
<MenuItemIndicatorPart
className={cn(
"[&_:is(path,polyline)]:[stroke-dasharray:24] [&_:is(path,polyline)]:[stroke-dashoffset:24]",
"[&_:is(path,polyline)]:transition-[stroke-dashoffset] [&_:is(path,polyline)]:duration-[calc(260ms*var(--kf-speed,1))] [&_:is(path,polyline)]:ease-house",
"data-checked:[&_:is(path,polyline)]:[stroke-dashoffset:0] data-checked:[&_:is(path,polyline)]:delay-[calc(100ms*var(--kf-speed,1))]",
"data-checked:[&_:is(path,polyline)]:starting:[stroke-dashoffset:24]",
"group-data-starting-style/kf-menu:[&_:is(path,polyline)]:[stroke-dashoffset:24]!",
"motion-reduce:[&_:is(path,polyline)]:transition-none",
className,
)}
{...props}
/>
)
}
export {
MenuCheckboxItem,
MenuContent,
MenuItem,
MenuItemIndicator,
MenuRadioItem,
MenuSubContent,
MenuSubTrigger,
MenuTrigger,
}Questions
How does the animated Menu for UI SDK move?
Unfold: The sheet unfolds from the button that opened it. Cascade: Rows arrive one after another. Glide: One highlight glides between items. Draw: Checks draw themselves; submenus unfold sideways.
Do I need Motion or Framer Motion for the animated Menu?
No. Every move is plain CSS, so there is nothing to install besides the component and UI SDK.
Can I keep my own styling and only take the motion?
Yes. Import from @/components/keyframery/primitives/menu instead. It has the same parts and props as @ui-sdk/react/menu and adds motion only.
What happens to the Menu when someone turns on reduced motion?
Nothing moves. State changes happen at once, or with a short fade.