{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "menu-branched",
  "type": "registry:component",
  "title": "Branched Menu",
  "description": "A tree menu whose submenus branch out with connector lines that draw in one after another. Arrow keys, Enter and Escape work at every level.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "micro",
    "menu",
    "tree",
    "submenu",
    "nested",
    "keyboard",
    "connector"
  ],
  "docs": "https://hyperblur.dev/effects/menu-branched",
  "files": [
    {
      "path": "registry/hyperblur/menu-branched.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/menu-branched.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BranchedMenuItem {\n  id: string;\n  label: string;\n  hint?: string;\n  icon?: React.ReactNode;\n  disabled?: boolean;\n  children?: BranchedMenuItem[];\n}\n\nexport interface BranchedMenuProps\n  extends Omit<React.HTMLAttributes<HTMLUListElement>, \"onChange\" | \"defaultValue\"> {\n  items?: BranchedMenuItem[];\n  value?: string | null;\n  defaultValue?: string | null;\n  onChange?: (id: string, item: BranchedMenuItem, path: string[]) => void;\n  defaultExpanded?: string[];\n  accent?: string;\n  label?: string;\n}\n\nconst defaultItems: BranchedMenuItem[] = [\n  {\n    id: \"new\",\n    label: \"New\",\n    children: [\n      { id: \"new-file\", label: \"Blank file\", hint: \"N\" },\n      { id: \"new-folder\", label: \"Folder\", hint: \"F\" },\n      {\n        id: \"new-template\",\n        label: \"From template\",\n        children: [\n          { id: \"tpl-landing\", label: \"Landing page\" },\n          { id: \"tpl-docs\", label: \"Documentation\" },\n          { id: \"tpl-blog\", label: \"Blog post\" },\n        ],\n      },\n    ],\n  },\n  {\n    id: \"share\",\n    label: \"Share\",\n    children: [\n      { id: \"share-link\", label: \"Copy link\", hint: \"L\" },\n      { id: \"share-invite\", label: \"Invite people\" },\n    ],\n  },\n  { id: \"export\", label: \"Export\", hint: \"E\" },\n  { id: \"archive\", label: \"Archive\", disabled: true },\n];\n\nconst css = `\n.hb-qs1 {\n  --hb-qs1-c: #2f7bff;\n  position: relative;\n  box-sizing: border-box;\n  width: 100%;\n  margin: 0;\n  padding: 6px;\n  list-style: none;\n  font: inherit;\n  font-size: 13px;\n  line-height: 1;\n  color: #ededed;\n  background: #111111;\n  border: 1px solid #262626;\n  border-radius: 14px;\n}\n.hb-qs1 *, .hb-qs1 *::before, .hb-qs1 *::after { box-sizing: border-box; font-family: inherit; }\n.hb-qs1-it { position: relative; margin: 0; padding: 0; outline: none; }\n.hb-qs1-row {\n  position: relative;\n  display: flex;\n  align-items: center;\n  gap: 8px;\n  width: 100%;\n  height: 30px;\n  padding: 0 8px;\n  border-radius: 8px;\n  color: #c9c9c9;\n  cursor: pointer;\n  user-select: none;\n  -webkit-user-select: none;\n  -webkit-tap-highlight-color: transparent;\n  transition: background-color 0.18s ease, color 0.18s ease;\n}\n.hb-qs1-row:hover { background: rgba(255, 255, 255, 0.05); color: #ededed; }\n.hb-qs1-it:focus-visible > .hb-qs1-row { outline: 2px solid var(--hb-qs1-c); outline-offset: -2px; color: #fff; }\n.hb-qs1-it[aria-selected=\"true\"] > .hb-qs1-row {\n  color: #fff;\n  background: color-mix(in srgb, var(--hb-qs1-c) 18%, transparent);\n}\n.hb-qs1-it[aria-disabled=\"true\"] > .hb-qs1-row { opacity: 0.4; cursor: not-allowed; }\n.hb-qs1-it[aria-disabled=\"true\"] > .hb-qs1-row:hover { background: transparent; color: #c9c9c9; }\n.hb-qs1-ic { display: grid; place-items: center; width: 16px; height: 16px; flex: none; color: #8f8f8f; }\n.hb-qs1-ic svg { width: 100%; height: 100%; }\n.hb-qs1-lb { min-width: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }\n.hb-qs1-hn {\n  flex: none;\n  padding: 3px 5px;\n  font-size: 10px;\n  font-weight: 500;\n  letter-spacing: 0.04em;\n  color: #8f8f8f;\n  border: 1px solid #262626;\n  border-radius: 5px;\n}\n.hb-qs1-ch {\n  flex: none;\n  width: 14px;\n  height: 14px;\n  color: #8f8f8f;\n  transition: transform 0.28s cubic-bezier(0.34, 1.56, 0.64, 1), color 0.2s ease;\n}\n.hb-qs1-it[aria-expanded=\"true\"] > .hb-qs1-row > .hb-qs1-ch { transform: rotate(90deg); color: var(--hb-qs1-c); }\n.hb-qs1-dot {\n  flex: none;\n  width: 6px;\n  height: 6px;\n  margin-right: 5px;\n  border-radius: 999px;\n  background: var(--hb-qs1-c);\n  opacity: 0;\n  transform: scale(0.4);\n  transition: opacity 0.2s ease, transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);\n}\n.hb-qs1-it[aria-selected=\"true\"] > .hb-qs1-row .hb-qs1-dot { opacity: 1; transform: scale(1); }\n.hb-qs1-sub {\n  position: relative;\n  margin: 0 0 0 15px;\n  padding: 0 0 0 17px;\n  list-style: none;\n  animation: hb-qs1-open 0.32s cubic-bezier(0.2, 0.8, 0.2, 1) both;\n}\n.hb-qs1-sub > .hb-qs1-it::before {\n  content: \"\";\n  position: absolute;\n  left: -17px;\n  top: 0;\n  bottom: 0;\n  width: 1px;\n  background: color-mix(in srgb, var(--hb-qs1-c) 45%, #333333);\n  transform-origin: top center;\n  animation: hb-qs1-down 0.34s cubic-bezier(0.3, 0.7, 0.2, 1) both;\n  animation-delay: calc(var(--hb-qs1-i, 0) * 70ms);\n}\n.hb-qs1-sub > .hb-qs1-it:last-child::before { bottom: auto; height: 15px; }\n.hb-qs1-sub > .hb-qs1-it::after {\n  content: \"\";\n  position: absolute;\n  left: -17px;\n  top: 15px;\n  width: 13px;\n  height: 1px;\n  background: color-mix(in srgb, var(--hb-qs1-c) 45%, #333333);\n  transform-origin: left center;\n  animation: hb-qs1-across 0.26s cubic-bezier(0.3, 0.7, 0.2, 1) both;\n  animation-delay: calc(var(--hb-qs1-i, 0) * 70ms + 130ms);\n}\n.hb-qs1-sub > .hb-qs1-it > .hb-qs1-row { animation: hb-qs1-pop 0.3s ease both; animation-delay: calc(var(--hb-qs1-i, 0) * 70ms + 200ms); }\n@keyframes hb-qs1-open {\n  from { opacity: 0; transform: translateY(-4px); }\n  to { opacity: 1; transform: translateY(0); }\n}\n@keyframes hb-qs1-down {\n  from { transform: scaleY(0); }\n  to { transform: scaleY(1); }\n}\n@keyframes hb-qs1-across {\n  from { transform: scaleX(0); }\n  to { transform: scaleX(1); }\n}\n@keyframes hb-qs1-pop {\n  from { opacity: 0; transform: translateX(-6px); }\n  to { opacity: 1; transform: translateX(0); }\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-qs1-sub, .hb-qs1-sub > .hb-qs1-it::before, .hb-qs1-sub > .hb-qs1-it::after, .hb-qs1-sub > .hb-qs1-it > .hb-qs1-row { animation: none; }\n  .hb-qs1-row, .hb-qs1-ch, .hb-qs1-dot { transition: none; }\n}\n`;\n\nfunction visibleIds(nodes: BranchedMenuItem[], open: Set<string>, out: string[] = []) {\n  for (const node of nodes) {\n    if (node.disabled) continue;\n    out.push(node.id);\n    if (node.children?.length && open.has(node.id)) visibleIds(node.children, open, out);\n  }\n  return out;\n}\n\nfunction findPath(nodes: BranchedMenuItem[], id: string, trail: BranchedMenuItem[] = []): BranchedMenuItem[] | null {\n  for (const node of nodes) {\n    const next = [...trail, node];\n    if (node.id === id) return next;\n    if (node.children) {\n      const hit = findPath(node.children, id, next);\n      if (hit) return hit;\n    }\n  }\n  return null;\n}\n\nconst chevron = (\n  <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n    <path d=\"M6 3.5 10.5 8 6 12.5\" />\n  </svg>\n);\n\nexport function BranchedMenu({\n  items = defaultItems,\n  value,\n  defaultValue = null,\n  onChange,\n  defaultExpanded = [],\n  accent = \"#2f7bff\",\n  label = \"Menu\",\n  className = \"\",\n  style,\n  onKeyDown,\n  onFocus,\n  ...rest\n}: BranchedMenuProps) {\n  const rootRef = React.useRef<HTMLUListElement>(null);\n  const [inner, setInner] = React.useState<string | null>(defaultValue);\n  const [expanded, setExpanded] = React.useState<string[]>(defaultExpanded);\n  const [focusId, setFocusId] = React.useState<string | null>(null);\n  const selected = value !== undefined ? value : inner;\n  const open = React.useMemo(() => new Set(expanded), [expanded]);\n  const order = visibleIds(items, open);\n  const tabId = focusId && order.includes(focusId) ? focusId : (order[0] ?? null);\n\n  function focusItem(id: string) {\n    setFocusId(id);\n    const el = rootRef.current?.querySelector<HTMLElement>(`[data-hb-id=\"${CSS.escape(id)}\"]`);\n    el?.focus();\n  }\n\n  function toggle(id: string, force?: boolean) {\n    setExpanded((cur) => {\n      const has = cur.includes(id);\n      const next = force === undefined ? !has : force;\n      if (next === has) return cur;\n      return next ? [...cur, id] : cur.filter((x) => x !== id);\n    });\n  }\n\n  function choose(node: BranchedMenuItem) {\n    if (node.disabled) return;\n    if (node.children?.length) {\n      toggle(node.id);\n      return;\n    }\n    if (value === undefined) setInner(node.id);\n    const path = findPath(items, node.id)?.map((n) => n.id) ?? [node.id];\n    onChange?.(node.id, node, path);\n  }\n\n  function handleKey(e: React.KeyboardEvent<HTMLUListElement>) {\n    onKeyDown?.(e);\n    if (e.defaultPrevented) return;\n    const el = (e.target as HTMLElement).closest<HTMLElement>(\"[data-hb-id]\");\n    const id = el?.dataset.hbId;\n    if (!id) return;\n    const trail = findPath(items, id);\n    const node = trail?.[trail.length - 1];\n    if (!trail || !node) return;\n    const index = order.indexOf(id);\n    const hasKids = Boolean(node.children?.length);\n    const isOpen = open.has(id);\n    if (e.key === \"ArrowDown\") {\n      e.preventDefault();\n      if (index < order.length - 1) focusItem(order[index + 1]);\n    } else if (e.key === \"ArrowUp\") {\n      e.preventDefault();\n      if (index > 0) focusItem(order[index - 1]);\n    } else if (e.key === \"Home\") {\n      e.preventDefault();\n      focusItem(order[0]);\n    } else if (e.key === \"End\") {\n      e.preventDefault();\n      focusItem(order[order.length - 1]);\n    } else if (e.key === \"ArrowRight\") {\n      e.preventDefault();\n      if (hasKids && !isOpen) toggle(id, true);\n      else if (hasKids && isOpen) {\n        const first = node.children?.find((c) => !c.disabled);\n        if (first) focusItem(first.id);\n      }\n    } else if (e.key === \"ArrowLeft\") {\n      e.preventDefault();\n      if (hasKids && isOpen) toggle(id, false);\n      else if (trail.length > 1) focusItem(trail[trail.length - 2].id);\n    } else if (e.key === \"Enter\" || e.key === \" \") {\n      e.preventDefault();\n      choose(node);\n    } else if (e.key === \"Escape\") {\n      e.preventDefault();\n      if (hasKids && isOpen) toggle(id, false);\n      else if (trail.length > 1) {\n        const parent = trail[trail.length - 2];\n        toggle(parent.id, false);\n        focusItem(parent.id);\n      }\n    }\n  }\n\n  function renderNodes(nodes: BranchedMenuItem[], level: number): React.ReactNode {\n    return nodes.map((node, i) => {\n      const hasKids = Boolean(node.children?.length);\n      const isOpen = hasKids && open.has(node.id);\n      return (\n        <li\n          key={node.id}\n          role=\"treeitem\"\n          data-hb-id={node.id}\n          aria-level={level}\n          aria-expanded={hasKids ? isOpen : undefined}\n          aria-selected={!hasKids && selected === node.id}\n          aria-disabled={node.disabled || undefined}\n          tabIndex={node.id === tabId ? 0 : -1}\n          className=\"hb-qs1-it\"\n          style={{ \"--hb-qs1-i\": i } as React.CSSProperties}\n        >\n          <div\n            className=\"hb-qs1-row\"\n            onClick={() => {\n              setFocusId(node.id);\n              choose(node);\n            }}\n          >\n            {node.icon ? <span className=\"hb-qs1-ic\">{node.icon}</span> : null}\n            <span className=\"hb-qs1-lb\">{node.label}</span>\n            {node.hint && !hasKids ? <span className=\"hb-qs1-hn\">{node.hint}</span> : null}\n            {hasKids ? <span className=\"hb-qs1-ch\">{chevron}</span> : <span className=\"hb-qs1-dot\" aria-hidden=\"true\" />}\n          </div>\n          {isOpen ? (\n            <ul role=\"group\" className=\"hb-qs1-sub\">\n              {renderNodes(node.children ?? [], level + 1)}\n            </ul>\n          ) : null}\n        </li>\n      );\n    });\n  }\n\n  return (\n    <>\n      <style href=\"hb-menu-branched\" precedence=\"default\">{css}</style>\n      <ul\n        ref={rootRef}\n        role=\"tree\"\n        aria-label={label}\n        className={`hb-qs1 ${className}`}\n        style={{ \"--hb-qs1-c\": accent, ...style } as React.CSSProperties}\n        onKeyDown={handleKey}\n        onFocus={(e) => {\n          onFocus?.(e);\n          const id = (e.target as HTMLElement).closest<HTMLElement>(\"[data-hb-id]\")?.dataset.hbId;\n          if (id) setFocusId(id);\n        }}\n        {...rest}\n      >\n        {renderNodes(items, 1)}\n      </ul>\n    </>\n  );\n}\n"
    }
  ]
}