Motion context menus
Five right-click menus that share one panel and vary only the open motion: a pop, a plain fade, a slide, a spring and a vertical scale. Each is self-contained (no radix), sized, token-driven, and opens at the cursor on right click.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/context-menu-motionDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/context-menu-motion.tsx"use client"
import * as React from "react"
import { ClipboardPaste, Copy, Scissors, Share2, Star } from "lucide-react"
import { AnimatePresence, motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Motion context menu family: 5 opening motions. The shell is the same as
context-menu-styled - the target area opens a fixed menu at the cursor via
onContextMenu (preventDefault), and it closes on an outside pointerdown / Escape /
item selection. The only difference is the ENTER/EXIT motion: pop (an overshooting
scale), fade (opacity only), slide (sliding down from above), spring (springy),
scale (growing vertically). No transform under reduced motion, only a fade.
NO radix or portal. Color comes ONLY from tokens, via alpha color-mix. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
export type StyledContextMenuItem = {
label: React.ReactNode
icon?: React.ReactNode
onSelect?: () => void
shortcut?: React.ReactNode
group?: string
danger?: boolean
}
type ContextMenuProps = {
className?: string
size?: StyledSize
children?: React.ReactNode
items?: StyledContextMenuItem[]
}
const menuSize: Record<StyledSize, string> = {
sm: "w-48",
md: "w-56",
lg: "w-64",
xl: "w-72",
}
const targetBox =
"flex h-28 w-full select-none items-center justify-center rounded-xl border border-dashed border-border bg-[color-mix(in_oklab,var(--color-foreground)_3%,transparent)] px-6 text-center text-sm text-muted-foreground"
const menuBase =
"fixed z-50 origin-top-left overflow-hidden 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 items-center gap-2 rounded-md px-3 py-2 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 [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
const itemDanger =
"flex w-full cursor-default items-center gap-2 rounded-md px-3 py-2 text-left text-sm text-danger outline-none transition-colors hover:bg-[color-mix(in_oklab,var(--color-danger)_12%,transparent)] focus-visible:bg-[color-mix(in_oklab,var(--color-danger)_12%,transparent)] focus-visible:ring-[3px] focus-visible:ring-danger/40 [&>svg]:size-4 [&>svg]:shrink-0 [&>i]:text-base [&>i]:leading-none [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
const shortcutChip =
"ml-auto inline-flex h-5 items-center rounded border border-border bg-[color-mix(in_oklab,var(--color-foreground)_5%,transparent)] px-1.5 font-mono text-[11px] text-muted-foreground"
const defaultItems: StyledContextMenuItem[] = [
{ label: "Copy", icon: <Copy />, shortcut: "Ctrl C" },
{ label: "Cut", icon: <Scissors />, shortcut: "Ctrl X" },
{ label: "Paste", icon: <ClipboardPaste />, shortcut: "Ctrl V" },
{ label: "Share", icon: <Share2 /> },
{ label: "Add to favorites", icon: <Star /> },
]
type MenuMotion = {
initial: Record<string, number>
animate: Record<string, number>
exit: Record<string, number>
transition: Record<string, unknown>
}
function MenuItemButton({
item,
close,
}: {
item: StyledContextMenuItem
close: () => void
}) {
return (
<button
type="button"
role="menuitem"
data-menu-item="true"
onClick={() => {
item.onSelect?.()
close()
}}
className={item.danger ? itemDanger : itemBase}
>
{item.icon ? (
<span className="inline-flex shrink-0" aria-hidden>
{item.icon}
</span>
) : null}
<span className="flex-1 truncate">{item.label}</span>
{item.shortcut ? <kbd className={shortcutChip}>{item.shortcut}</kbd> : null}
</button>
)
}
/* Shared shell: the target area plus a fixed AnimatePresence menu at the cursor position. menuMotion carries the variant's enter/exit motion; it falls back to a fade under reduced motion. */
function MotionContextMenuShell({
className,
size = "md",
children,
render,
menuMotion,
}: {
className?: string
size?: StyledSize
children?: React.ReactNode
render: (close: () => void) => React.ReactNode
menuMotion: MenuMotion
}) {
const [open, setOpen] = React.useState(false)
const [coords, setCoords] = React.useState({ x: 0, y: 0 })
const reduce = useReducedMotion()
const menuRef = React.useRef<HTMLDivElement>(null)
const menuId = React.useId()
const close = React.useCallback(() => setOpen(false), [])
const onContextMenu = (e: React.MouseEvent) => {
e.preventDefault()
setCoords({ x: e.clientX, y: e.clientY })
setOpen(true)
}
React.useEffect(() => {
if (!open) return
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") {
setOpen(false)
return
}
if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return
const node = menuRef.current
if (!node) return
const rows = Array.from(
node.querySelectorAll<HTMLElement>('[data-menu-item="true"]')
)
if (rows.length === 0) return
e.preventDefault()
const current = rows.indexOf(document.activeElement as HTMLElement)
const next =
e.key === "ArrowDown"
? (current + 1) % rows.length
: current <= 0
? rows.length - 1
: current - 1
rows[next]?.focus()
}
const onPointer = (e: PointerEvent) => {
const node = menuRef.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])
const fade: MenuMotion = {
initial: { opacity: 0 },
animate: { opacity: 1 },
exit: { opacity: 0 },
transition: { duration: 0.12, ease: "easeOut" },
}
const anim = reduce ? fade : menuMotion
return (
<div data-slot="styled-context-menu" className="w-full">
<div
data-slot="styled-context-menu-target"
onContextMenu={onContextMenu}
className={cn(targetBox, className)}
>
{children ?? "Right-click here"}
</div>
<AnimatePresence>
{open ? (
<motion.div
ref={menuRef}
id={menuId}
role="menu"
data-slot="styled-context-menu-content"
style={{ left: coords.x, top: coords.y }}
className={cn(menuBase, menuSize[size])}
initial={anim.initial}
animate={anim.animate}
exit={anim.exit}
transition={anim.transition}
>
{render(close)}
</motion.div>
) : null}
</AnimatePresence>
</div>
)
}
function MotionMenu({
className,
size,
children,
items,
menuMotion,
}: ContextMenuProps & { menuMotion: MenuMotion }) {
const list = items ?? defaultItems
return (
<MotionContextMenuShell
className={className}
size={size}
menuMotion={menuMotion}
render={(close) => (
<div className="flex flex-col">
{list.map((item, i) => (
<MenuItemButton key={i} item={item} close={close} />
))}
</div>
)}
>
{children}
</MotionContextMenuShell>
)
}
/* Pop: kucukten hizli firlayan yayli scale. */
export function PopContextMenu(props: ContextMenuProps) {
return (
<MotionMenu
{...props}
menuMotion={{
initial: { opacity: 0, scale: 0.8 },
animate: { opacity: 1, scale: 1 },
exit: { opacity: 0, scale: 0.8 },
transition: { type: "spring" as const, stiffness: 520, damping: 26 },
}}
/>
)
}
/* Fade: opacity only, no transform. */
export function FadeContextMenu(props: ContextMenuProps) {
return (
<MotionMenu
{...props}
menuMotion={{
initial: { opacity: 0 },
animate: { opacity: 1 },
exit: { opacity: 0 },
transition: { duration: 0.18, ease: "easeOut" },
}}
/>
)
}
/* Slide: yukaridan asagi kayarak girer. */
export function SlideContextMenu(props: ContextMenuProps) {
return (
<MotionMenu
{...props}
menuMotion={{
initial: { opacity: 0, y: -8 },
animate: { opacity: 1, y: 0 },
exit: { opacity: 0, y: -8 },
transition: { duration: 0.16, ease: "easeOut" },
}}
/>
)
}
/* Spring: yumusak, salinimli yay ile hem scale hem kayma. */
export function SpringContextMenu(props: ContextMenuProps) {
return (
<MotionMenu
{...props}
menuMotion={{
initial: { opacity: 0, scale: 0.92, y: -6 },
animate: { opacity: 1, scale: 1, y: 0 },
exit: { opacity: 0, scale: 0.92, y: -6 },
transition: { type: "spring" as const, stiffness: 320, damping: 18 },
}}
/>
)
}
/* Scale: origin-top-left'ten dikey buyume. */
export function ScaleContextMenu(props: ContextMenuProps) {
return (
<MotionMenu
{...props}
menuMotion={{
initial: { opacity: 0, scaleY: 0.6 },
animate: { opacity: 1, scaleY: 1 },
exit: { opacity: 0, scaleY: 0.6 },
transition: { duration: 0.18, ease: "easeOut" },
}}
/>
)
}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.
Pop
A snappy spring that pops the menu up from small.
import { PopContextMenu } from "@/components/ui/context-menu-motion"
<PopContextMenu />Fade
Opacity only, with no transform at all.
import { FadeContextMenu } from "@/components/ui/context-menu-motion"
<FadeContextMenu />Slide
The menu slides down into place from above.
import { SlideContextMenu } from "@/components/ui/context-menu-motion"
<SlideContextMenu />Spring
A soft spring that combines scale and travel.
import { SpringContextMenu } from "@/components/ui/context-menu-motion"
<SpringContextMenu />Scale
A vertical grow from the top-left origin.
import { ScaleContextMenu } from "@/components/ui/context-menu-motion"
<ScaleContextMenu />ai2 Motion context menus: 5 styled variations on the token system
The ai2 Motion context menus are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around right-click menus with different open motions. 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 the enter and exit at the cursor, from a spring pop to a plain fade. 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 skipped and the menu simply fades in.
What is in the ai2 Motion context menus?
5 exports in one file: Pop, Fade, Slide, Spring and Scale. 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 the enter and exit at the cursor, from a spring pop to a plain fade.
- Reduced-motion aware: Under prefers-reduced-motion, every transform is skipped and the menu simply fades in.
- 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 context 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.