{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "dropdown-menu",
  "title": "Dropdown Menu",
  "description": "A button-triggered menu with icons, keyboard shortcuts, checkbox and radio items, labelled sections, nested submenus and a highlight that slides from row to row, with full keyboard support including typeahead.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/dropdown-menu/dropdown-menu.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type DropdownMenuItemKind = \"item\" | \"checkbox\" | \"radio\" | \"separator\" | \"label\" | \"submenu\";\nexport type DropdownMenuSize = \"sm\" | \"md\" | \"lg\";\n\nexport interface DropdownMenuItem {\n  id: string;\n  kind?: DropdownMenuItemKind;\n  label?: string;\n  icon?: React.ReactNode;\n  shortcut?: string;\n  /** Initial state for checkbox and radio items. */\n  checked?: boolean;\n  /** Radio items with the same group are mutually exclusive. */\n  group?: string;\n  disabled?: boolean;\n  danger?: boolean;\n  items?: DropdownMenuItem[];\n}\n\nexport interface DropdownMenuProps {\n  items: DropdownMenuItem[];\n  /** Text on the default trigger button. */\n  label?: string;\n  /** Replace the default trigger. It receives the props to spread on a button. */\n  renderTrigger?: (props: React.ButtonHTMLAttributes<HTMLButtonElement> & { \"data-dropdown-trigger\": string }, state: { open: boolean }) => React.ReactNode;\n  onSelect?: (item: DropdownMenuItem) => void;\n  onCheckedChange?: (item: DropdownMenuItem, checked: boolean) => void;\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  align?: \"start\" | \"end\";\n  side?: \"bottom\" | \"top\";\n  size?: DropdownMenuSize;\n  theme?: \"dark\" | \"light\";\n  width?: number;\n  disabled?: boolean;\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.5)\", faint: \"rgba(245,244,241,0.28)\", bg: \"rgba(255,255,255,0.03)\", panelBg: \"#0E0E0E\", border: \"rgba(255,255,255,0.12)\", borderHover: \"rgba(255,255,255,0.24)\", focus: \"rgba(245,244,241,0.85)\", hover: \"rgba(255,255,255,0.08)\", separator: \"rgba(255,255,255,0.08)\", kbdBg: \"rgba(255,255,255,0.06)\", kbdBorder: \"rgba(255,255,255,0.1)\", danger: \"#FF7A6B\", dangerHover: \"rgba(255,122,107,0.12)\", check: \"#F5F4F1\", checkFg: \"#0A0A0A\", shadow: \"0 24px 60px rgba(0,0,0,0.55)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.5)\", faint: \"rgba(10,10,10,0.28)\", bg: \"#FFFFFF\", panelBg: \"#FFFFFF\", border: \"rgba(10,10,10,0.16)\", borderHover: \"rgba(10,10,10,0.3)\", focus: \"rgba(10,10,10,0.85)\", hover: \"rgba(10,10,10,0.06)\", separator: \"rgba(10,10,10,0.08)\", kbdBg: \"rgba(10,10,10,0.04)\", kbdBorder: \"rgba(10,10,10,0.1)\", danger: \"#E5484D\", dangerHover: \"rgba(229,72,77,0.09)\", check: \"#0A0A0A\", checkFg: \"#FFFFFF\", shadow: \"0 20px 50px rgba(0,0,0,0.18)\" },\n};\n\nconst SIZES: Record<DropdownMenuSize, { font: number; rowH: number; radius: number; button: number; padX: number; icon: number }> = {\n  sm: { font: 13, rowH: 30, radius: 9, button: 36, padX: 12, icon: 15 },\n  md: { font: 14, rowH: 33, radius: 10, button: 44, padX: 14, icon: 16 },\n  lg: { font: 15, rowH: 36, radius: 11, button: 52, padX: 16, icon: 17 },\n};\n\ntype Palette = (typeof PALETTES)[\"dark\"];\ntype Size = (typeof SIZES)[\"md\"];\n\nconst kindOf = (item: DropdownMenuItem) => item.kind ?? \"item\";\nconst isFocusable = (item: DropdownMenuItem) => kindOf(item) !== \"separator\" && kindOf(item) !== \"label\" && !item.disabled;\n\nfunction collectChecks(items: DropdownMenuItem[], into: Record<string, boolean> = {}) {\n  for (const it of items) {\n    if (kindOf(it) === \"checkbox\" || kindOf(it) === \"radio\") into[it.id] = Boolean(it.checked);\n    if (it.items) collectChecks(it.items, into);\n  }\n  return into;\n}\n\nfunction Kbd({ children, p }: { children: string; p: Palette }) {\n  return (\n    <span className=\"inline-flex shrink-0 items-center justify-center whitespace-nowrap rounded-md font-medium\" style={{ height: 18, padding: \"0 5px\", background: p.kbdBg, border: `1px solid ${p.kbdBorder}`, color: p.muted, fontSize: 10.5 }}>\n      {children}\n    </span>\n  );\n}\n\ninterface MenuPanelProps {\n  items: DropdownMenuItem[];\n  checks: Record<string, boolean>;\n  s: Size;\n  p: Palette;\n  width: number;\n  level: number;\n  style: React.CSSProperties;\n  origin: string;\n  reduce: boolean;\n  autoFocus: \"first\" | \"last\" | \"none\";\n  onActivate: (item: DropdownMenuItem) => void;\n  onToggle: (item: DropdownMenuItem, siblings: DropdownMenuItem[]) => void;\n  onClose: () => void;\n  onCloseAll: () => void;\n}\n\nfunction MenuPanel({ items, checks, s, p, width, level, style, origin, reduce, autoFocus, onActivate, onToggle, onClose, onCloseAll }: MenuPanelProps) {\n  const uid = React.useId();\n  const focusable = items.filter(isFocusable);\n  const [activeId, setActiveId] = React.useState<string | null>(autoFocus === \"first\" ? (focusable[0]?.id ?? null) : autoFocus === \"last\" ? (focusable[focusable.length - 1]?.id ?? null) : null);\n  const [openSub, setOpenSub] = React.useState<string | null>(null);\n  const [subFocus, setSubFocus] = React.useState<\"first\" | \"none\">(\"none\");\n  const panelRef = React.useRef<HTMLDivElement>(null);\n  const typeahead = React.useRef({ text: \"\", at: 0 });\n\n  React.useEffect(() => {\n    panelRef.current?.focus({ preventScroll: true });\n  }, []);\n\n  const move = (dir: 1 | -1) => {\n    if (!focusable.length) return;\n    const i = activeId ? focusable.findIndex((n) => n.id === activeId) : -1;\n    setActiveId(focusable[(i + dir + focusable.length) % focusable.length].id);\n  };\n\n  const activate = (item: DropdownMenuItem, viaKeyboard: boolean) => {\n    if (item.disabled) return;\n    const kind = kindOf(item);\n    if (kind === \"submenu\") {\n      setSubFocus(viaKeyboard ? \"first\" : \"none\");\n      setOpenSub(item.id);\n    } else if (kind === \"checkbox\" || kind === \"radio\") onToggle(item, items);\n    else onActivate(item);\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent) => {\n    if (openSub) return;\n    const active = items.find((n) => n.id === activeId);\n    const key = e.key;\n    if (key === \"ArrowDown\" || key === \"ArrowUp\") {\n      e.preventDefault();\n      move(key === \"ArrowDown\" ? 1 : -1);\n    } else if (key === \"Home\" || key === \"End\") {\n      e.preventDefault();\n      const target = key === \"Home\" ? focusable[0] : focusable[focusable.length - 1];\n      if (target) setActiveId(target.id);\n    } else if (key === \"ArrowRight\" && active && kindOf(active) === \"submenu\") {\n      e.preventDefault();\n      activate(active, true);\n    } else if (key === \"ArrowLeft\" && level > 0) {\n      e.preventDefault();\n      onClose();\n    } else if (key === \"Enter\" || key === \" \") {\n      e.preventDefault();\n      if (active) activate(active, true);\n    } else if (key === \"Escape\") {\n      e.preventDefault();\n      if (level > 0) onClose();\n      else onCloseAll();\n    } else if (key === \"Tab\") {\n      onCloseAll();\n    } else if (key.length === 1 && /\\S/.test(key)) {\n      // Typeahead: jump to the next item whose label starts with what's been typed in the last 600ms.\n      const now = Date.now();\n      typeahead.current.text = now - typeahead.current.at > 600 ? key.toLowerCase() : typeahead.current.text + key.toLowerCase();\n      typeahead.current.at = now;\n      const start = activeId ? focusable.findIndex((n) => n.id === activeId) : -1;\n      const order = [...focusable.slice(start + 1), ...focusable.slice(0, start + 1)];\n      const hit = order.find((n) => (n.label ?? \"\").toLowerCase().startsWith(typeahead.current.text));\n      if (hit) setActiveId(hit.id);\n    }\n  };\n\n  return (\n    <motion.div\n      ref={panelRef}\n      role=\"menu\"\n      tabIndex={-1}\n      aria-activedescendant={activeId ? `${uid}-${activeId}` : undefined}\n      onKeyDown={onKeyDown}\n      onMouseLeave={() => {\n        if (!openSub) setActiveId(null);\n      }}\n      initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.96 }}\n      animate={{ opacity: 1, scale: 1 }}\n      exit={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.97 }}\n      transition={{ duration: 0.16, ease: [0.16, 1, 0.3, 1] }}\n      className=\"absolute z-50 outline-none\"\n      style={{ ...style, width, padding: 6, borderRadius: s.radius + 4, background: p.panelBg, border: `1px solid ${p.border}`, boxShadow: p.shadow, transformOrigin: origin }}\n    >\n      {items.map((item) => {\n        const kind = kindOf(item);\n        if (kind === \"separator\") return <div key={item.id} role=\"separator\" style={{ margin: \"5px 8px\", height: 1, background: p.separator }} />;\n        if (kind === \"label\")\n          return (\n            <div key={item.id} className=\"font-semibold uppercase\" style={{ padding: \"7px 10px 4px\", color: p.faint, fontSize: 10.5, letterSpacing: \"0.07em\" }}>\n              {item.label}\n            </div>\n          );\n        const active = item.id === activeId || item.id === openSub;\n        const checked = checks[item.id];\n        return (\n          <div key={item.id} className=\"relative\">\n            <div\n              id={`${uid}-${item.id}`}\n              role={kind === \"checkbox\" ? \"menuitemcheckbox\" : kind === \"radio\" ? \"menuitemradio\" : \"menuitem\"}\n              aria-checked={kind === \"checkbox\" || kind === \"radio\" ? Boolean(checked) : undefined}\n              aria-haspopup={kind === \"submenu\" ? \"menu\" : undefined}\n              aria-expanded={kind === \"submenu\" ? item.id === openSub : undefined}\n              aria-disabled={item.disabled || undefined}\n              onMouseEnter={() => {\n                if (item.disabled) return;\n                setActiveId(item.id);\n                setSubFocus(\"none\");\n                setOpenSub(kind === \"submenu\" ? item.id : null);\n              }}\n              onClick={() => activate(item, false)}\n              className=\"relative flex items-center select-none\"\n              style={{ gap: 9, height: s.rowH, padding: \"0 10px\", borderRadius: s.radius - 2, color: item.disabled ? p.faint : item.danger ? p.danger : p.text, fontSize: s.font - 0.5, fontWeight: 500, cursor: item.disabled ? \"default\" : \"pointer\" }}\n            >\n              {/* One highlight that slides from row to row instead of each row lighting up on its own. */}\n              {active && (\n                <motion.span\n                  layoutId={reduce ? undefined : `${uid}-highlight`}\n                  className=\"absolute inset-0\"\n                  style={{ borderRadius: s.radius - 2, background: item.danger ? p.dangerHover : p.hover }}\n                  transition={{ type: \"spring\", stiffness: 700, damping: 45 }}\n                />\n              )}\n              {kind === \"checkbox\" && (\n                <span className=\"relative flex shrink-0 items-center justify-center rounded\" style={{ width: 14, height: 14, border: `1.5px solid ${checked ? p.check : p.border}`, background: checked ? p.check : \"transparent\", transition: \"background 0.15s ease, border-color 0.15s ease\" }}>\n                  <svg width=\"9\" height=\"9\" viewBox=\"0 0 12 12\" fill=\"none\" aria-hidden=\"true\">\n                    <motion.path d=\"M2.5 6.3L4.8 8.6L9.5 3.5\" stroke={p.checkFg} strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\" initial={false} animate={{ pathLength: checked ? 1 : 0, opacity: checked ? 1 : 0 }} transition={{ duration: 0.18 }} />\n                  </svg>\n                </span>\n              )}\n              {kind === \"radio\" && (\n                <span className=\"relative flex shrink-0 items-center justify-center rounded-full\" style={{ width: 14, height: 14, border: `1.5px solid ${checked ? p.check : p.border}`, transition: \"border-color 0.15s ease\" }}>\n                  <motion.span initial={false} animate={{ scale: checked ? 1 : 0 }} transition={{ type: \"spring\", stiffness: 600, damping: 30 }} style={{ width: 6, height: 6, borderRadius: 999, background: p.check }} />\n                </span>\n              )}\n              {item.icon && (\n                <span className=\"relative flex shrink-0 items-center\" style={{ color: item.danger ? p.danger : p.muted, width: s.icon, height: s.icon }} aria-hidden=\"true\">\n                  {item.icon}\n                </span>\n              )}\n              <span className=\"relative min-w-0 flex-1 truncate\">{item.label}</span>\n              {item.shortcut && (\n                <span className=\"relative\">\n                  <Kbd p={p}>{item.shortcut}</Kbd>\n                </span>\n              )}\n              {kind === \"submenu\" && (\n                <svg className=\"relative\" width=\"13\" height=\"13\" viewBox=\"0 0 16 16\" fill=\"none\" stroke={p.muted} strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                  <path d=\"M6 3.5L10.5 8L6 12.5\" />\n                </svg>\n              )}\n            </div>\n            <AnimatePresence>\n              {kind === \"submenu\" && item.id === openSub && item.items && (\n                <MenuPanel\n                  items={item.items}\n                  checks={checks}\n                  s={s}\n                  p={p}\n                  width={width}\n                  level={level + 1}\n                  style={{ left: \"100%\", top: -7, marginLeft: 8 }}\n                  origin=\"top left\"\n                  reduce={reduce}\n                  autoFocus={subFocus}\n                  onActivate={onActivate}\n                  onToggle={onToggle}\n                  onClose={() => {\n                    setOpenSub(null);\n                    panelRef.current?.focus({ preventScroll: true });\n                  }}\n                  onCloseAll={onCloseAll}\n                />\n              )}\n            </AnimatePresence>\n          </div>\n        );\n      })}\n    </motion.div>\n  );\n}\n\nexport function DropdownMenu({\n  items,\n  label = \"Options\",\n  renderTrigger,\n  onSelect,\n  onCheckedChange,\n  open,\n  defaultOpen = false,\n  onOpenChange,\n  align = \"start\",\n  side = \"bottom\",\n  size = \"md\",\n  theme = \"dark\",\n  width = 228,\n  disabled = false,\n  className,\n}: DropdownMenuProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const uid = React.useId();\n  const reduce = Boolean(useReducedMotion());\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const [internalOpen, setInternalOpen] = React.useState(defaultOpen);\n  const isOpen = open ?? internalOpen;\n  const [focusOn, setFocusOn] = React.useState<\"first\" | \"last\" | \"none\">(\"none\");\n  const [checks, setChecks] = React.useState(() => collectChecks(items));\n  const [hoverTrigger, setHoverTrigger] = React.useState(false);\n\n  const setOpen = React.useCallback(\n    (next: boolean) => {\n      if (open === undefined) setInternalOpen(next);\n      onOpenChange?.(next);\n    },\n    [open, onOpenChange],\n  );\n\n  const closeAll = React.useCallback(() => {\n    setOpen(false);\n    rootRef.current?.querySelector<HTMLElement>(\"[data-dropdown-trigger]\")?.focus({ preventScroll: true });\n  }, [setOpen]);\n\n  React.useEffect(() => {\n    if (!isOpen) return;\n    const onDown = (e: PointerEvent) => {\n      if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(false);\n    };\n    document.addEventListener(\"pointerdown\", onDown);\n    return () => document.removeEventListener(\"pointerdown\", onDown);\n  }, [isOpen, setOpen]);\n\n  const toggle = (item: DropdownMenuItem, siblings: DropdownMenuItem[]) => {\n    const kind = kindOf(item);\n    const next = { ...checks };\n    if (kind === \"checkbox\") next[item.id] = !checks[item.id];\n    else {\n      for (const sib of siblings) if (kindOf(sib) === \"radio\" && sib.group === item.group) next[sib.id] = false;\n      next[item.id] = true;\n    }\n    setChecks(next);\n    onCheckedChange?.(item, next[item.id]);\n  };\n\n  const openWith = (focus: \"first\" | \"last\" | \"none\") => {\n    setFocusOn(focus);\n    setOpen(true);\n  };\n\n  const onTriggerClick = () => (isOpen ? setOpen(false) : openWith(\"none\"));\n  const onTriggerKeyDown = (e: React.KeyboardEvent<HTMLButtonElement>) => {\n    if (e.key === \"ArrowDown\" || e.key === \"Enter\" || e.key === \" \") {\n      e.preventDefault();\n      openWith(\"first\");\n    } else if (e.key === \"ArrowUp\") {\n      e.preventDefault();\n      openWith(\"last\");\n    }\n  };\n  const triggerProps = {\n    \"data-dropdown-trigger\": \"\",\n    type: \"button\" as const,\n    disabled,\n    \"aria-haspopup\": \"menu\" as const,\n    \"aria-expanded\": isOpen,\n    \"aria-controls\": isOpen ? `${uid}-menu` : undefined,\n    onClick: onTriggerClick,\n    onKeyDown: onTriggerKeyDown,\n  };\n\n  const vertical = side === \"bottom\" ? { top: \"100%\", marginTop: 8 } : { bottom: \"100%\", marginBottom: 8 };\n  const horizontal = align === \"start\" ? { left: 0 } : { right: 0 };\n  const origin = `${side === \"bottom\" ? \"top\" : \"bottom\"} ${align === \"start\" ? \"left\" : \"right\"}`;\n\n  return (\n    // self-start/h-fit: inside a stretching flex parent the wrapper would otherwise grow to the parent's\n    // height, and the menu (positioned from the wrapper's bottom edge) would open far below the trigger.\n    <div ref={rootRef} className={cn(\"relative inline-flex h-fit self-start\", className)} style={{ fontFamily: \"Inter, sans-serif\" }}>\n      {renderTrigger ? (\n        renderTrigger(triggerProps, { open: isOpen })\n      ) : (\n        <motion.button\n          data-dropdown-trigger=\"\"\n          type=\"button\"\n          disabled={disabled}\n          aria-haspopup=\"menu\"\n          aria-expanded={isOpen}\n          aria-controls={isOpen ? `${uid}-menu` : undefined}\n          onClick={onTriggerClick}\n          onKeyDown={onTriggerKeyDown}\n          onMouseEnter={() => setHoverTrigger(true)}\n          onMouseLeave={() => setHoverTrigger(false)}\n          whileTap={{ scale: 0.97 }}\n          className=\"flex cursor-pointer items-center font-semibold outline-none focus-visible:ring-2 disabled:cursor-default disabled:opacity-50\"\n          style={{ height: s.button, padding: `0 ${s.padX - 2}px 0 ${s.padX}px`, gap: 8, borderRadius: s.radius + 3, background: isOpen || hoverTrigger ? p.hover : p.bg, border: `1px solid ${isOpen ? p.focus : hoverTrigger ? p.borderHover : p.border}`, color: p.text, fontSize: s.font, transition: \"background 0.16s ease, border-color 0.16s ease\", [\"--tw-ring-color\" as string]: p.focus }}\n        >\n          {label}\n          <svg width=\"14\" height=\"14\" viewBox=\"0 0 16 16\" fill=\"none\" stroke={p.muted} strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\" style={{ transform: `rotate(${isOpen ? 180 : 0}deg)`, transition: \"transform 0.2s ease\" }}>\n            <path d=\"M4 6l4 4 4-4\" />\n          </svg>\n        </motion.button>\n      )}\n\n      <AnimatePresence>\n        {isOpen && (\n          <div key=\"menu\" id={`${uid}-menu`} className=\"contents\">\n            <MenuPanel\n              items={items}\n              checks={checks}\n              s={s}\n              p={p}\n              width={width}\n              level={0}\n              style={{ ...vertical, ...horizontal }}\n              origin={origin}\n              reduce={reduce}\n              autoFocus={focusOn}\n              onActivate={(item) => {\n                onSelect?.(item);\n                closeAll();\n              }}\n              onToggle={toggle}\n              onClose={closeAll}\n              onCloseAll={closeAll}\n            />\n          </div>\n        )}\n      </AnimatePresence>\n    </div>\n  );\n}\n\nexport default DropdownMenu;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}