Motion dropdown menus
Five dropdown menus that keep the same opaque panel and vary only how it enters and leaves: a slide, a pop, a plain fade, a flip and a bouncy spring. Each is self-contained (no radix), sized, token-driven, and closes on outside click, Escape or item select.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/dropdown-menu-motionDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/dropdown-menu-motion.tsx"use client"
import * as React from "react"
import {
Bell,
ChevronDown,
CreditCard,
Layers,
LogOut,
Settings,
User,
} from "lucide-react"
import { AnimatePresence, motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Motion dropdown family: 5 open/close characters. The panel stays the same clear
popover surface in every variant - the whole difference is in the ENTER/EXIT
animation: slide, pop, fade, flip and spring. Each export is a complete dropdown:
a relative wrapper + a trigger + a panel absolutely positioned below the trigger.
NO radix or portal. It closes on an outside click, on Escape (focus returns to
the trigger) and on an item selection. The arrow keys move between items. No
transform under reduced motion, only a fade. Color comes ONLY from tokens, via
alpha color-mix. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
const panelWidth: Record<StyledSize, string> = {
sm: "w-48",
md: "w-56",
lg: "w-64",
xl: "w-72",
}
const itemHeight: Record<StyledSize, string> = {
sm: "min-h-8 py-1.5",
md: "min-h-9 py-2",
lg: "min-h-10 py-2.5",
xl: "min-h-11 py-3",
}
export interface StyledMenuItem {
label: React.ReactNode
icon?: React.ReactNode
description?: React.ReactNode
onSelect?: () => void
}
interface StyledMenuProps {
className?: string
size?: StyledSize
label?: React.ReactNode
items?: StyledMenuItem[]
}
const triggerBtn =
"inline-flex h-9 shrink-0 select-none items-center justify-center gap-2 whitespace-nowrap rounded-lg border border-border bg-secondary px-4 text-sm font-medium text-secondary-foreground outline-none transition-colors hover:bg-[color-mix(in_oklab,var(--color-foreground)_6%,transparent)] focus-visible:ring-[3px] focus-visible:ring-ring/50 [&>svg]:size-4 [&>svg]:shrink-0 [&>i]:text-base [&>i]:leading-none"
const panelBase =
"rounded-xl border border-border bg-popover p-1.5 text-sm text-popover-foreground shadow-lg outline-none"
const itemBase =
"flex w-full cursor-default select-none items-center gap-2.5 rounded-md px-3 text-left text-sm text-popover-foreground outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:bg-accent focus-visible:text-accent-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50 [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
const fade = { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 } }
interface MotionSpec {
initial: Record<string, number>
animate: Record<string, number>
exit: Record<string, number>
transition: Record<string, unknown>
/* The panel's transform origin, meaningful for flip and pop. */
origin?: string
}
function useMenuState() {
const [open, setOpen] = React.useState(false)
const wrapperRef = React.useRef<HTMLDivElement>(null)
const triggerRef = React.useRef<HTMLButtonElement>(null)
React.useEffect(() => {
if (!open) return
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") {
setOpen(false)
triggerRef.current?.focus()
}
}
const onPointer = (e: PointerEvent) => {
const node = wrapperRef.current
if (node && e.target instanceof Node && !node.contains(e.target)) {
setOpen(false)
}
}
window.addEventListener("keydown", onKey)
window.addEventListener("pointerdown", onPointer)
return () => {
window.removeEventListener("keydown", onKey)
window.removeEventListener("pointerdown", onPointer)
}
}, [open])
return { open, setOpen, wrapperRef, triggerRef }
}
function useArrowNav(panelRef: React.RefObject<HTMLDivElement | null>) {
return React.useCallback(
(e: React.KeyboardEvent<HTMLDivElement>) => {
if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return
const panel = panelRef.current
if (!panel) return
const nodes = Array.from(
panel.querySelectorAll<HTMLButtonElement>(
'[role="menuitem"],[role="menuitemcheckbox"]'
)
)
if (nodes.length === 0) return
e.preventDefault()
const current = nodes.indexOf(document.activeElement as HTMLButtonElement)
const next =
e.key === "ArrowDown"
? (current + 1) % nodes.length
: current <= 0
? nodes.length - 1
: current - 1
nodes[next]?.focus()
},
[panelRef]
)
}
/* Shared shell: motionSpec comes from outside, the rest is the same in every variant. */
function MenuShell({
size = "md",
label = "Options",
className,
spec,
children,
}: {
size?: StyledSize
label?: React.ReactNode
className?: string
spec: MotionSpec
children: (close: () => void) => React.ReactNode
}) {
const { open, setOpen, wrapperRef, triggerRef } = useMenuState()
const panelRef = React.useRef<HTMLDivElement>(null)
const onArrow = useArrowNav(panelRef)
const reduce = useReducedMotion()
const id = React.useId()
const panelId = `${id}-menu`
const active = reduce ? fade : spec
return (
<div
ref={wrapperRef}
data-slot="styled-dropdown-menu"
className={cn("relative inline-flex", className)}
>
<button
ref={triggerRef}
type="button"
data-slot="styled-dropdown-menu-trigger"
aria-haspopup="menu"
aria-expanded={open}
aria-controls={open ? panelId : undefined}
className={triggerBtn}
onClick={() => setOpen(!open)}
>
{label}
<ChevronDown
className={cn("transition-transform duration-200", open && "rotate-180")}
/>
</button>
<AnimatePresence>
{open ? (
<div
className={cn("absolute left-0 top-full z-50 mt-1", panelWidth[size])}
style={{ perspective: 800 }}
>
<motion.div
ref={panelRef}
id={panelId}
role="menu"
data-slot="styled-dropdown-menu-content"
className={cn(panelBase, spec.origin ?? "origin-top")}
initial={active.initial}
animate={active.animate}
exit={active.exit}
transition={reduce ? { duration: 0.16 } : spec.transition}
onKeyDown={onArrow}
>
{children(() => setOpen(false))}
</motion.div>
</div>
) : null}
</AnimatePresence>
</div>
)
}
function MenuList({
items,
size,
close,
}: {
items: StyledMenuItem[]
size: StyledSize
close: () => void
}) {
return (
<div className="flex flex-col">
{items.map((item, i) => (
<button
key={`${i}`}
type="button"
role="menuitem"
tabIndex={0}
className={cn(itemBase, itemHeight[size])}
onClick={() => {
item.onSelect?.()
close()
}}
>
{item.icon ? (
<span className="flex shrink-0 items-center text-muted-foreground">
{item.icon}
</span>
) : null}
{item.label}
</button>
))}
</div>
)
}
const accountItems: StyledMenuItem[] = [
{ label: "Profile", icon: <User /> },
{ label: "Billing", icon: <CreditCard /> },
{ label: "Settings", icon: <Settings /> },
{ label: "Sign out", icon: <LogOut /> },
]
const workspaceItems: StyledMenuItem[] = [
{ label: "Overview", icon: <Layers /> },
{ label: "Members", icon: <User /> },
{ label: "Notifications", icon: <Bell /> },
{ label: "Settings", icon: <Settings /> },
]
/* Slide: panel yukaridan asagi kayarak girer, cikista geri kayar. */
const slideSpec: MotionSpec = {
initial: { opacity: 0, y: -10 },
animate: { opacity: 1, y: 0 },
exit: { opacity: 0, y: -10 },
transition: { duration: 0.18, ease: "easeOut" },
}
export function SlideMenu({ className, size = "md", label = "Slide", items }: StyledMenuProps) {
return (
<MenuShell size={size} label={label} className={className} spec={slideSpec}>
{(close) => <MenuList items={items ?? accountItems} size={size} close={close} />}
</MenuShell>
)
}
/* Pop: bursts out of the trigger growing from small. */
const popSpec: MotionSpec = {
initial: { opacity: 0, scale: 0.82 },
animate: { opacity: 1, scale: 1 },
exit: { opacity: 0, scale: 0.82 },
transition: { type: "spring" as const, stiffness: 520, damping: 24 },
origin: "origin-top-left",
}
export function PopMenu({ className, size = "md", label = "Pop", items }: StyledMenuProps) {
return (
<MenuShell size={size} label={label} className={className} spec={popSpec}>
{(close) => <MenuList items={items ?? workspaceItems} size={size} close={close} />}
</MenuShell>
)
}
/* Fade: no transform, opacity only. The calmest variant. */
const fadeSpec: MotionSpec = {
initial: { opacity: 0 },
animate: { opacity: 1 },
exit: { opacity: 0 },
transition: { duration: 0.2, ease: "easeOut" },
}
export function FadeMenu({ className, size = "md", label = "Fade", items }: StyledMenuProps) {
return (
<MenuShell size={size} label={label} className={className} spec={fadeSpec}>
{(close) => <MenuList items={items ?? accountItems} size={size} close={close} />}
</MenuShell>
)
}
/* Flip: opens by rotating on the X axis from the top edge; the perspective comes from the wrapper. */
const flipSpec: MotionSpec = {
initial: { opacity: 0, rotateX: -70 },
animate: { opacity: 1, rotateX: 0 },
exit: { opacity: 0, rotateX: -70 },
transition: { duration: 0.24, ease: "easeOut" },
}
export function FlipMenu({ className, size = "md", label = "Flip", items }: StyledMenuProps) {
return (
<MenuShell size={size} label={label} className={className} spec={flipSpec}>
{(close) => <MenuList items={items ?? workspaceItems} size={size} close={close} />}
</MenuShell>
)
}
/* Spring: a soft bouncy spring - it falls down and settles into place. */
const springSpec: MotionSpec = {
initial: { opacity: 0, y: -18, scale: 0.94 },
animate: { opacity: 1, y: 0, scale: 1 },
exit: { opacity: 0, y: -12, scale: 0.94 },
transition: { type: "spring" as const, stiffness: 260, damping: 14 },
}
export function SpringMenu({ className, size = "md", label = "Spring", items }: StyledMenuProps) {
return (
<MenuShell size={size} label={label} className={className} spec={springSpec}>
{(close) => <MenuList items={items ?? accountItems} size={size} close={close} />}
</MenuShell>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Slide
The panel slides down from the trigger.
import { SlideMenu } from "@/components/ui/dropdown-menu-motion"
<SlideMenu />Pop
A spring scale that pops out of the trigger corner.
import { PopMenu } from "@/components/ui/dropdown-menu-motion"
<PopMenu />Fade
Opacity only, with no transform at all.
import { FadeMenu } from "@/components/ui/dropdown-menu-motion"
<FadeMenu />Flip
The panel flips open on the X axis from its top edge.
import { FlipMenu } from "@/components/ui/dropdown-menu-motion"
<FlipMenu />Spring
A bouncy spring that settles into place.
import { SpringMenu } from "@/components/ui/dropdown-menu-motion"
<SpringMenu />ai2 Motion dropdown menus: 5 styled variations on the token system
The ai2 Motion dropdown menus are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around click-triggered menus with a distinct open and close motion. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion drives each flavor: slide, spring pop, fade, 3D flip and bouncy spring. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, every transform is dropped and the panel only fades.
What is in the ai2 Motion dropdown menus?
5 exports in one file: Slide, Pop, Fade, Flip and Spring. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion drives each flavor: slide, spring pop, fade, 3D flip and bouncy spring.
- Reduced-motion aware: Under prefers-reduced-motion, every transform is dropped and the panel only fades.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Motion dropdown menus sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.