{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "dropdown-menu",
  "type": "registry:component",
  "title": "Dropdown Menu",
  "description": "A button-triggered menu driven by a data array, with icons, shortcuts, checkbox items, a side submenu and the full keyboard model.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "site",
    "dropdown",
    "menu",
    "actions",
    "submenu",
    "keyboard",
    "context"
  ],
  "docs": "https://hyperblur.dev/effects/dropdown-menu",
  "files": [
    {
      "path": "registry/hyperblur/dropdown-menu.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/dropdown-menu.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport type DropdownMenuItem =\n  | {\n      type?: \"item\";\n      id: string;\n      label: string;\n      icon?: React.ReactNode;\n      shortcut?: string;\n      disabled?: boolean;\n      destructive?: boolean;\n      onSelect?: () => void;\n    }\n  | {\n      type: \"checkbox\";\n      id: string;\n      label: string;\n      icon?: React.ReactNode;\n      shortcut?: string;\n      disabled?: boolean;\n      checked?: boolean;\n      defaultChecked?: boolean;\n      onCheckedChange?: (checked: boolean) => void;\n    }\n  | { type: \"separator\"; id?: string }\n  | { type: \"label\"; id?: string; label: string }\n  | {\n      type: \"submenu\";\n      id: string;\n      label: string;\n      icon?: React.ReactNode;\n      disabled?: boolean;\n      items: DropdownMenuItem[];\n    };\n\nexport interface DropdownMenuProps\n  extends Omit<React.HTMLAttributes<HTMLDivElement>, \"children\" | \"onSelect\"> {\n  items: DropdownMenuItem[];\n  trigger?: React.ReactNode;\n  triggerLabel?: string;\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  onSelect?: (id: string) => void;\n  align?: \"start\" | \"end\";\n  side?: \"auto\" | \"bottom\" | \"top\";\n  size?: \"sm\" | \"md\";\n  accent?: string;\n}\n\ntype FocusRequest = \"first\" | \"last\" | \"panel\" | null;\n\ninterface MenuContext {\n  checks: Record<string, boolean>;\n  toggle: (id: string, next: boolean) => void;\n  select: (id: string) => void;\n  closeAll: (refocus: boolean) => void;\n}\n\nconst css = `\n.hb-ddm {\n  --hb-ddm-accent: #2f7bff;\n  --hb-ddm-h: 32px;\n  --hb-ddm-fs: 13.5px;\n  position: relative;\n  display: inline-flex;\n  color: #ededed;\n  font-family: var(--font-sans, ui-sans-serif, system-ui, sans-serif);\n  text-align: left;\n}\n.hb-ddm[data-size=\"sm\"] { --hb-ddm-h: 24px; --hb-ddm-fs: 12.5px; }\n.hb-ddm[data-size=\"sm\"] .hb-ddm-trigger { height: 28px; padding: 0 9px; border-radius: 8px; }\n.hb-ddm *, .hb-ddm *::before, .hb-ddm *::after { box-sizing: border-box; }\n.hb-ddm-trigger {\n  display: inline-flex;\n  align-items: center;\n  gap: 8px;\n  height: 34px;\n  margin: 0;\n  padding: 0 12px;\n  border: 1px solid #262626;\n  border-radius: 10px;\n  background: #171717;\n  color: #ededed;\n  font: inherit;\n  font-size: 13px;\n  font-weight: 500;\n  line-height: 1;\n  cursor: pointer;\n  -webkit-tap-highlight-color: transparent;\n  transition: background-color 0.15s ease, border-color 0.15s ease;\n}\n.hb-ddm-trigger:hover, .hb-ddm-trigger[aria-expanded=\"true\"] { background: #1f1f1f; border-color: #333333; }\n.hb-ddm-trigger:focus-visible { outline: 2px solid var(--hb-ddm-accent); outline-offset: 2px; }\n.hb-ddm-trigger svg { width: 14px; height: 14px; flex: none; color: #8f8f8f; }\n.hb-ddm-panel {\n  position: absolute;\n  z-index: 60;\n  top: calc(100% + 6px);\n  left: 0;\n  width: max-content;\n  min-width: 100%;\n  max-width: 300px;\n  padding: 4px;\n  border: 1px solid #262626;\n  border-radius: 12px;\n  background: #111111;\n  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.55), 0 2px 8px rgba(0, 0, 0, 0.4);\n  outline: none;\n  transform-origin: 0 0;\n  animation: hb-ddm-in 0.16s cubic-bezier(0.2, 0.9, 0.2, 1) both;\n}\n.hb-ddm-panel[data-root=\"true\"] { min-width: max(100%, 200px); }\n.hb-ddm[data-size=\"sm\"] .hb-ddm-panel[data-root=\"true\"] { min-width: max(100%, 184px); }\n.hb-ddm-panel[data-align=\"end\"] { left: auto; right: 0; transform-origin: 100% 0; }\n.hb-ddm-panel[data-side=\"top\"] { top: auto; bottom: calc(100% + 6px); transform-origin: 0 100%; }\n.hb-ddm-panel[data-side=\"top\"][data-align=\"end\"] { transform-origin: 100% 100%; }\n.hb-ddm-panel[data-root=\"false\"] { top: -5px; left: calc(100% + 3px); min-width: 168px; }\n.hb-ddm-panel[data-root=\"false\"][data-flip=\"true\"] { left: auto; right: calc(100% + 3px); transform-origin: 100% 0; }\n.hb-ddm-sub { position: relative; }\n.hb-ddm-i {\n  position: relative;\n  display: flex;\n  align-items: center;\n  gap: 8px;\n  width: 100%;\n  min-height: var(--hb-ddm-h);\n  margin: 0;\n  padding: 0 8px;\n  border: 0;\n  border-radius: 8px;\n  background: transparent;\n  color: #d4d4d4;\n  font: inherit;\n  font-size: var(--hb-ddm-fs);\n  line-height: 1.2;\n  text-align: left;\n  white-space: nowrap;\n  cursor: pointer;\n  outline: none;\n  -webkit-tap-highlight-color: transparent;\n}\n.hb-ddm-i:focus, .hb-ddm-i[aria-expanded=\"true\"] { background: rgba(255, 255, 255, 0.08); color: #ffffff; }\n.hb-ddm-i[data-destructive=\"true\"] { color: #ff6369; }\n.hb-ddm-i[data-destructive=\"true\"]:focus { background: rgba(229, 72, 77, 0.15); color: #ff7b80; }\n.hb-ddm-i[aria-disabled=\"true\"] { opacity: 0.4; cursor: not-allowed; }\n.hb-ddm-i[aria-disabled=\"true\"]:focus { background: transparent; }\n.hb-ddm-ic { display: inline-flex; flex: none; align-items: center; justify-content: center; width: 16px; height: 16px; color: #8f8f8f; }\n.hb-ddm-i:focus .hb-ddm-ic { color: inherit; }\n.hb-ddm-i[data-destructive=\"true\"] .hb-ddm-ic { color: inherit; }\n.hb-ddm-ic svg { width: 15px; height: 15px; }\n.hb-ddm-tick { width: 13px; height: 13px; color: var(--hb-ddm-accent); }\n.hb-ddm-tick path { stroke-dasharray: 16; stroke-dashoffset: 16; transition: stroke-dashoffset 0.22s ease; }\n.hb-ddm-i[aria-checked=\"true\"] .hb-ddm-tick path { stroke-dashoffset: 0; }\n.hb-ddm-lb { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }\n.hb-ddm-kb {\n  flex: none;\n  margin-left: 16px;\n  color: #6f6f6f;\n  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);\n  font-size: 11px;\n  letter-spacing: 0.02em;\n}\n.hb-ddm-i:focus .hb-ddm-kb { color: #a1a1a1; }\n.hb-ddm-chev { flex: none; width: 12px; height: 12px; margin-left: 12px; color: #6f6f6f; }\n.hb-ddm-lab {\n  padding: 8px 8px 4px;\n  color: #6f6f6f;\n  font-size: 11px;\n  font-weight: 500;\n  letter-spacing: 0.05em;\n  text-transform: uppercase;\n}\n.hb-ddm-sep { height: 1px; margin: 3px -4px; background: #262626; }\n@keyframes hb-ddm-in { from { opacity: 0; transform: scale(0.95) translateY(-3px); } to { opacity: 1; transform: none; } }\n@media (prefers-reduced-motion: reduce) {\n  .hb-ddm-panel { animation: none; }\n  .hb-ddm-trigger, .hb-ddm-tick path { transition: none; }\n}\n`;\n\nconst MenuCtx = React.createContext<MenuContext | null>(null);\n\nfunction useMenu() {\n  const ctx = React.useContext(MenuCtx);\n  if (!ctx) throw new Error(\"Menu context missing\");\n  return ctx;\n}\n\nconst chevronDown = (\n  <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.75\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n    <path d=\"m4 6.25 4 4 4-4\" />\n  </svg>\n);\n\nfunction navItems(panel: HTMLElement) {\n  return Array.from(panel.querySelectorAll<HTMLElement>(\"[data-ddm-nav]\")).filter(\n    (el) => el.closest('[role=\"menu\"]') === panel && el.getAttribute(\"aria-disabled\") !== \"true\",\n  );\n}\n\ninterface PanelProps {\n  items: DropdownMenuItem[];\n  root: boolean;\n  request: FocusRequest;\n  onExit?: () => void;\n  align?: \"start\" | \"end\";\n  side?: \"auto\" | \"bottom\" | \"top\";\n  anchor?: React.RefObject<HTMLElement | null>;\n  labelledBy?: string;\n  panelId?: string;\n}\n\nfunction MenuPanel({\n  items,\n  root,\n  request,\n  onExit,\n  align = \"start\",\n  side = \"auto\",\n  anchor,\n  labelledBy,\n  panelId,\n}: PanelProps) {\n  const ctx = useMenu();\n  const panel = React.useRef<HTMLDivElement>(null);\n  const search = React.useRef({ text: \"\", timer: 0 });\n  const [active, setActive] = React.useState<string | null>(null);\n\n  React.useLayoutEffect(() => {\n    const node = panel.current;\n    if (!node) return;\n    const rect = node.getBoundingClientRect();\n    if (root) {\n      const base = anchor?.current?.getBoundingClientRect();\n      if (base && side === \"auto\") {\n        const below = window.innerHeight - base.bottom;\n        if (below < rect.height + 12 && base.top > below) node.dataset.side = \"top\";\n      }\n      if (rect.right > window.innerWidth - 8 && align === \"start\") node.dataset.align = \"end\";\n      else if (rect.left < 8 && align === \"end\") node.dataset.align = \"start\";\n    } else if (rect.right > window.innerWidth - 8) {\n      node.dataset.flip = \"true\";\n    }\n  }, [root, anchor, side, align]);\n\n  React.useEffect(() => {\n    const node = panel.current;\n    if (!request || !node) return;\n    const list = navItems(node);\n    const target = request === \"first\" ? list[0] : request === \"last\" ? list[list.length - 1] : node;\n    (target ?? node).focus({ preventScroll: true });\n  }, [request]);\n\n  React.useEffect(() => {\n    const state = search.current;\n    return () => window.clearTimeout(state.timer);\n  }, []);\n\n  function handleKeyDown(e: React.KeyboardEvent<HTMLDivElement>) {\n    const node = panel.current;\n    if (!node) return;\n    const list = navItems(node);\n    const index = list.indexOf(document.activeElement as HTMLElement);\n    const focusAt = (i: number) => list[i]?.focus({ preventScroll: true });\n    if (e.key === \"ArrowDown\") {\n      e.preventDefault();\n      focusAt(index < 0 ? 0 : (index + 1) % list.length);\n    } else if (e.key === \"ArrowUp\") {\n      e.preventDefault();\n      focusAt(index < 0 ? list.length - 1 : (index - 1 + list.length) % list.length);\n    } else if (e.key === \"Home\") {\n      e.preventDefault();\n      focusAt(0);\n    } else if (e.key === \"End\") {\n      e.preventDefault();\n      focusAt(list.length - 1);\n    } else if (e.key === \"ArrowLeft\" && !root) {\n      e.preventDefault();\n      onExit?.();\n    } else if (e.key === \"Escape\") {\n      e.preventDefault();\n      if (root) ctx.closeAll(true);\n      else onExit?.();\n    } else if (e.key === \"Tab\") {\n      e.preventDefault();\n      ctx.closeAll(true);\n    } else if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey && (e.key !== \" \" || search.current.text)) {\n      e.preventDefault();\n      const state = search.current;\n      window.clearTimeout(state.timer);\n      state.text += e.key.toLowerCase();\n      state.timer = window.setTimeout(() => {\n        state.text = \"\";\n      }, 600);\n      const ordered = [...list.slice(index + 1), ...list.slice(0, index + 1)];\n      const pool = state.text.length === 1 ? ordered : [...list.slice(Math.max(index, 0)), ...list.slice(0, Math.max(index, 0))];\n      const hit = pool.find((el) => (el.textContent ?? \"\").trim().toLowerCase().startsWith(state.text));\n      hit?.focus({ preventScroll: true });\n    } else {\n      return;\n    }\n    e.stopPropagation();\n  }\n\n  return (\n    <div\n      ref={panel}\n      id={panelId}\n      role=\"menu\"\n      aria-orientation=\"vertical\"\n      aria-labelledby={labelledBy}\n      tabIndex={-1}\n      className=\"hb-ddm-panel\"\n      data-root={root}\n      data-side={root ? (side === \"top\" ? \"top\" : \"bottom\") : undefined}\n      data-align={root ? align : undefined}\n      onKeyDown={handleKeyDown}\n    >\n      {items.map((item, i) => {\n        const key = item.id ?? `${item.type}-${i}`;\n        if (item.type === \"separator\") return <div key={key} role=\"separator\" className=\"hb-ddm-sep\" />;\n        if (item.type === \"label\") {\n          return (\n            <div key={key} role=\"presentation\" className=\"hb-ddm-lab\">\n              {item.label}\n            </div>\n          );\n        }\n        if (item.type === \"submenu\") {\n          return <SubmenuItem key={key} item={item} active={active} onActive={setActive} />;\n        }\n        if (item.type === \"checkbox\") {\n          const checked = item.checked ?? ctx.checks[item.id] ?? item.defaultChecked ?? false;\n          return (\n            <button\n              key={key}\n              type=\"button\"\n              role=\"menuitemcheckbox\"\n              aria-checked={checked}\n              aria-disabled={item.disabled || undefined}\n              data-ddm-nav\n              tabIndex={active === item.id ? 0 : -1}\n              className=\"hb-ddm-i\"\n              onFocus={() => setActive(item.id)}\n              onPointerMove={(e) => focusOnHover(e)}\n              onClick={() => {\n                if (item.disabled) return;\n                const next = !checked;\n                ctx.toggle(item.id, next);\n                item.onCheckedChange?.(next);\n              }}\n            >\n              <span className=\"hb-ddm-ic\" aria-hidden=\"true\">\n                <svg className=\"hb-ddm-tick\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n                  <path d=\"m3 8.5 3.2 3.2L13 4.8\" />\n                </svg>\n              </span>\n              <span className=\"hb-ddm-lb\">{item.label}</span>\n              {item.shortcut && <kbd className=\"hb-ddm-kb\">{item.shortcut}</kbd>}\n            </button>\n          );\n        }\n        return (\n          <button\n            key={key}\n            type=\"button\"\n            role=\"menuitem\"\n            aria-disabled={item.disabled || undefined}\n            data-ddm-nav\n            data-destructive={item.destructive || undefined}\n            tabIndex={active === item.id ? 0 : -1}\n            className=\"hb-ddm-i\"\n            onFocus={() => setActive(item.id)}\n            onPointerMove={(e) => focusOnHover(e)}\n            onClick={() => {\n              if (item.disabled) return;\n              item.onSelect?.();\n              ctx.select(item.id);\n            }}\n          >\n            {item.icon && (\n              <span className=\"hb-ddm-ic\" aria-hidden=\"true\">\n                {item.icon}\n              </span>\n            )}\n            <span className=\"hb-ddm-lb\">{item.label}</span>\n            {item.shortcut && <kbd className=\"hb-ddm-kb\">{item.shortcut}</kbd>}\n          </button>\n        );\n      })}\n    </div>\n  );\n}\n\nfunction focusOnHover(e: React.PointerEvent<HTMLElement>) {\n  const el = e.currentTarget;\n  if (el.getAttribute(\"aria-disabled\") === \"true\") return;\n  if (document.activeElement !== el) el.focus({ preventScroll: true });\n}\n\ninterface SubmenuProps {\n  item: Extract<DropdownMenuItem, { type: \"submenu\" }>;\n  active: string | null;\n  onActive: (id: string) => void;\n}\n\nfunction SubmenuItem({ item, active, onActive }: SubmenuProps) {\n  const [open, setOpen] = React.useState(false);\n  const wrap = React.useRef<HTMLDivElement>(null);\n  const button = React.useRef<HTMLButtonElement>(null);\n  const [request, setRequest] = React.useState<FocusRequest>(null);\n  const timer = React.useRef(0);\n  const panelId = React.useId();\n\n  React.useEffect(() => () => window.clearTimeout(timer.current), []);\n\n  function show(next: FocusRequest) {\n    window.clearTimeout(timer.current);\n    if (item.disabled) return;\n    setRequest(next);\n    setOpen(true);\n  }\n\n  function hide(refocus: boolean) {\n    window.clearTimeout(timer.current);\n    const inside = wrap.current?.contains(document.activeElement) && document.activeElement !== button.current;\n    setOpen(false);\n    setRequest(null);\n    if (refocus || inside) button.current?.focus({ preventScroll: true });\n  }\n\n  return (\n    <div\n      ref={wrap}\n      className=\"hb-ddm-sub\"\n      onMouseEnter={() => {\n        window.clearTimeout(timer.current);\n        timer.current = window.setTimeout(() => show(null), 120);\n      }}\n      onMouseLeave={() => {\n        window.clearTimeout(timer.current);\n        timer.current = window.setTimeout(() => hide(false), 160);\n      }}\n    >\n      <button\n        ref={button}\n        type=\"button\"\n        role=\"menuitem\"\n        aria-haspopup=\"menu\"\n        aria-expanded={open}\n        aria-controls={open ? panelId : undefined}\n        aria-disabled={item.disabled || undefined}\n        data-ddm-nav\n        tabIndex={active === item.id ? 0 : -1}\n        className=\"hb-ddm-i\"\n        onFocus={() => onActive(item.id)}\n        onPointerMove={(e) => focusOnHover(e)}\n        onClick={() => show(\"panel\")}\n        onKeyDown={(e) => {\n          if (e.key === \"ArrowRight\" || e.key === \"Enter\" || e.key === \" \") {\n            e.preventDefault();\n            e.stopPropagation();\n            show(\"first\");\n          }\n        }}\n      >\n        {item.icon && (\n          <span className=\"hb-ddm-ic\" aria-hidden=\"true\">\n            {item.icon}\n          </span>\n        )}\n        <span className=\"hb-ddm-lb\">{item.label}</span>\n        <svg className=\"hb-ddm-chev\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.75\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n          <path d=\"m6 3.5 4.5 4.5L6 12.5\" />\n        </svg>\n      </button>\n      {open && (\n        <MenuPanel\n          items={item.items}\n          root={false}\n          request={request}\n          onExit={() => hide(true)}\n          panelId={panelId}\n        />\n      )}\n    </div>\n  );\n}\n\nexport function DropdownMenu({\n  items,\n  trigger = \"Options\",\n  triggerLabel,\n  open,\n  defaultOpen = false,\n  onOpenChange,\n  onSelect,\n  align = \"start\",\n  side = \"auto\",\n  size = \"md\",\n  accent = \"#2f7bff\",\n  className = \"\",\n  style,\n  ...rest\n}: DropdownMenuProps) {\n  const [inner, setInner] = React.useState(defaultOpen);\n  const isOpen = open !== undefined ? open : inner;\n  const root = React.useRef<HTMLDivElement>(null);\n  const button = React.useRef<HTMLButtonElement>(null);\n  const [request, setRequest] = React.useState<FocusRequest>(null);\n  const [checks, setChecks] = React.useState<Record<string, boolean>>({});\n  const menuId = React.useId();\n  const triggerId = `${menuId}-trigger`;\n\n  const setOpen = React.useCallback(\n    (next: boolean) => {\n      if (open === undefined) setInner(next);\n      if (!next) setRequest(null);\n      onOpenChange?.(next);\n    },\n    [open, onOpenChange],\n  );\n\n  React.useEffect(() => {\n    if (!isOpen) return;\n    function onDown(e: PointerEvent) {\n      if (root.current && !root.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 ctx = React.useMemo<MenuContext>(\n    () => ({\n      checks,\n      toggle: (id, next) => setChecks((prev) => ({ ...prev, [id]: next })),\n      select: (id) => {\n        onSelect?.(id);\n        setOpen(false);\n        button.current?.focus({ preventScroll: true });\n      },\n      closeAll: (refocus) => {\n        setOpen(false);\n        if (refocus) button.current?.focus({ preventScroll: true });\n      },\n    }),\n    [checks, onSelect, setOpen],\n  );\n\n  function openWith(next: FocusRequest) {\n    setRequest(next);\n    setOpen(true);\n  }\n\n  const vars = { \"--hb-ddm-accent\": accent, ...style } as React.CSSProperties;\n\n  return (\n    <div ref={root} className={`hb-ddm ${className}`} style={vars} data-size={size} {...rest}>\n      <style href=\"hb-dropdown-menu\" precedence=\"default\">{css}</style>\n      <button\n        ref={button}\n        id={triggerId}\n        type=\"button\"\n        className=\"hb-ddm-trigger\"\n        aria-haspopup=\"menu\"\n        aria-expanded={isOpen}\n        aria-controls={isOpen ? menuId : undefined}\n        aria-label={triggerLabel}\n        onClick={(e) => {\n          if (isOpen) setOpen(false);\n          else openWith(e.detail === 0 ? \"first\" : \"panel\");\n        }}\n        onKeyDown={(e) => {\n          if (e.key === \"ArrowDown\") {\n            e.preventDefault();\n            openWith(\"first\");\n          } else if (e.key === \"ArrowUp\") {\n            e.preventDefault();\n            openWith(\"last\");\n          } else if (e.key === \"Escape\" && isOpen) {\n            e.preventDefault();\n            setOpen(false);\n          }\n        }}\n      >\n        {trigger}\n        {typeof trigger === \"string\" && chevronDown}\n      </button>\n      {isOpen && (\n        <MenuCtx.Provider value={ctx}>\n          <MenuPanel\n            items={items}\n            root\n            request={request}\n            align={align}\n            side={side}\n            anchor={button}\n            labelledBy={triggerId}\n            panelId={menuId}\n          />\n        </MenuCtx.Provider>\n      )}\n    </div>\n  );\n}\n"
    }
  ]
}