Compact context menus
Five dense right-click menus for tight surfaces: a dense list, a minimal text-only list, pill highlights, a bare divided list and the narrowest panel. 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-compactDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/context-menu-compact.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"
/* Compact context menu family: 5 dense menus. 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 DENSITY: dense (tight rows), minimal
(plain, no border), pill (a pill-shaped highlight), bare (no frame or shadow, a
ruled separator), narrow (a very narrow panel + small typography). NO radix.
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[]
}
/* Yogun aile daha dar panel olcekleri kullanir. */
const menuSize: Record<StyledSize, string> = {
sm: "w-40",
md: "w-48",
lg: "w-56",
xl: "w-64",
}
const narrowSize: Record<StyledSize, string> = {
sm: "w-32",
md: "w-36",
lg: "w-40",
xl: "w-48",
}
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 text-popover-foreground outline-none"
const iconCompat =
"[&>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 rowBase =
"flex w-full cursor-default items-center gap-2 text-left outline-none transition-colors focus-visible:ring-[3px] focus-visible:ring-ring/50"
const dangerTone =
"text-danger 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-danger/40"
const shortcutText = "ml-auto font-mono text-[10px] 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: "Favorite", icon: <Star /> },
]
type CompactSkin = {
/* Panel yuzeyi. */
surface: string
/* Satir gorunumu (yogunluk + hover vurgusu). */
row: string
/* Satirlar arasi ayirici cizilsin mi. */
divided?: boolean
/* Whether icons are hidden (for the densest variants). */
hideIcons?: boolean
/* Kisayol metni gosterilsin mi. */
hideShortcuts?: boolean
sizes?: Record<StyledSize, string>
}
/* Shared shell: the target area plus a fixed AnimatePresence menu at the cursor position. skin carries the variant's density language. */
function CompactContextMenuShell({
className,
size = "md",
children,
items,
skin,
}: ContextMenuProps & { skin: CompactSkin }) {
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 list = items ?? defaultItems
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 anim = reduce
? { initial: { opacity: 1 }, animate: { opacity: 1 }, exit: { opacity: 1 } }
: {
initial: { opacity: 0, scale: 0.96 },
animate: { opacity: 1, scale: 1 },
exit: { opacity: 0, scale: 0.96 },
}
const widths = skin.sizes ?? menuSize
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, skin.surface, widths[size])}
initial={anim.initial}
animate={anim.animate}
exit={anim.exit}
transition={reduce ? { duration: 0 } : { duration: 0.12, ease: "easeOut" }}
>
<div className="flex flex-col">
{list.map((item, i) => (
<React.Fragment key={i}>
{skin.divided && i > 0 ? (
<div role="separator" className="h-px bg-border" />
) : null}
<button
type="button"
role="menuitem"
data-menu-item="true"
onClick={() => {
item.onSelect?.()
close()
}}
className={cn(
rowBase,
iconCompat,
skin.row,
item.danger && dangerTone
)}
>
{!skin.hideIcons && item.icon ? (
<span className="inline-flex shrink-0" aria-hidden>
{item.icon}
</span>
) : null}
<span className="flex-1 truncate">{item.label}</span>
{!skin.hideShortcuts && item.shortcut ? (
<kbd className={shortcutText}>{item.shortcut}</kbd>
) : null}
</button>
</React.Fragment>
))}
</div>
</motion.div>
) : null}
</AnimatePresence>
</div>
)
}
/* Dense: the standard shell with noticeably shortened rows. */
export function DenseContextMenu(props: ContextMenuProps) {
return (
<CompactContextMenuShell
{...props}
skin={{
surface: "rounded-lg border border-border bg-popover p-1 shadow-lg",
row: "rounded-sm px-2 py-1 text-xs hover:bg-accent hover:text-accent-foreground focus-visible:bg-accent focus-visible:text-accent-foreground [&_svg]:size-3.5 [&_i]:text-sm",
}}
/>
)
}
/* Minimal: ikonsuz, sade metin listesi, cok hafif yuzey. */
export function MinimalContextMenu(props: ContextMenuProps) {
return (
<CompactContextMenuShell
{...props}
skin={{
surface:
"rounded-lg border border-[color-mix(in_oklab,var(--color-border)_60%,transparent)] bg-popover p-1 shadow-md",
row: "rounded-sm px-2.5 py-1.5 text-xs text-muted-foreground hover:text-foreground focus-visible:text-foreground",
hideIcons: true,
}}
/>
)
}
/* Pill: hap seklinde tam yuvarlak vurgu, yuvarlak panel. */
export function PillContextMenu(props: ContextMenuProps) {
return (
<CompactContextMenuShell
{...props}
skin={{
surface: "rounded-2xl border border-border bg-popover p-1.5 shadow-lg",
row: "rounded-full px-3 py-1.5 text-xs hover:bg-[color-mix(in_oklab,var(--color-primary)_14%,transparent)] focus-visible:bg-[color-mix(in_oklab,var(--color-primary)_14%,transparent)] [&_svg]:size-3.5 [&_i]:text-sm",
}}
/>
)
}
/* Bare: cerceve/golge yok, satirlar ince cizgilerle ayrilir. */
export function BareContextMenu(props: ContextMenuProps) {
return (
<CompactContextMenuShell
{...props}
skin={{
surface: "rounded-md bg-popover p-0",
row: "px-3 py-1.5 text-xs hover:bg-[color-mix(in_oklab,var(--color-foreground)_6%,transparent)] focus-visible:bg-[color-mix(in_oklab,var(--color-foreground)_6%,transparent)] [&_svg]:size-3.5 [&_i]:text-sm",
divided: true,
hideShortcuts: true,
}}
/>
)
}
/* Narrow: en dar panel, kisayolsuz, kucuk tipografi. */
export function NarrowContextMenu(props: ContextMenuProps) {
return (
<CompactContextMenuShell
{...props}
skin={{
surface: "rounded-lg border border-border bg-popover p-1 shadow-lg",
row: "rounded-sm px-2 py-1 text-[11px] hover:bg-accent hover:text-accent-foreground focus-visible:bg-accent focus-visible:text-accent-foreground [&_svg]:size-3 [&_i]:text-xs",
hideShortcuts: true,
sizes: narrowSize,
}}
/>
)
}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.
Dense
The standard panel with noticeably shorter rows.
import { DenseContextMenu } from "@/components/ui/context-menu-compact"
<DenseContextMenu />Minimal
A plain text list with no icons and a faint surface.
import { MinimalContextMenu } from "@/components/ui/context-menu-compact"
<MinimalContextMenu />Pill
Fully rounded pill highlights inside a rounded panel.
import { PillContextMenu } from "@/components/ui/context-menu-compact"
<PillContextMenu />Bare
No border or shadow, with hairline dividers between rows.
import { BareContextMenu } from "@/components/ui/context-menu-compact"
<BareContextMenu />Narrow
The tightest panel, with small type and no shortcuts.
import { NarrowContextMenu } from "@/components/ui/context-menu-compact"
<NarrowContextMenu />ai2 Compact context menus: 5 styled variations on the token system
The ai2 Compact context menus are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around dense right-click menus for tight surfaces. 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 fades and scales the menu in at the cursor. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the scale is skipped and the menu appears instantly.
What is in the ai2 Compact context menus?
5 exports in one file: Dense, Minimal, Pill, Bare and Narrow. 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 fades and scales the menu in at the cursor.
- Reduced-motion aware: Under prefers-reduced-motion, the scale is skipped and the menu appears instantly.
- 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 Compact 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.