{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "sidebar-tree",
  "type": "registry:component",
  "title": "Sidebar Tree",
  "description": "A file explorer tree with animated folders, colored file type icons, a filter field and the full tree keyboard model.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "sidebars",
    "tree",
    "file explorer",
    "sidebar",
    "folders",
    "keyboard",
    "filter"
  ],
  "docs": "https://hyperblur.dev/effects/sidebar-tree",
  "files": [
    {
      "path": "registry/hyperblur/sidebar-tree.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/sidebar-tree.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface SidebarTreeNode {\n  id: string;\n  name: string;\n  children?: SidebarTreeNode[];\n}\n\nexport interface SidebarTreeProps\n  extends Omit<React.HTMLAttributes<HTMLElement>, \"onChange\" | \"onSelect\" | \"children\" | \"defaultValue\" | \"title\"> {\n  nodes?: SidebarTreeNode[];\n  selected?: string | null;\n  defaultSelected?: string | null;\n  onSelect?: (id: string, node: SidebarTreeNode) => void;\n  expanded?: string[];\n  defaultExpanded?: string[];\n  onExpandedChange?: (ids: string[]) => void;\n  filterable?: boolean;\n  placeholder?: string;\n  title?: string;\n  rowHeight?: number;\n  indent?: number;\n  color?: string;\n}\n\nconst sampleNodes: SidebarTreeNode[] = [\n  {\n    id: \"app\",\n    name: \"app\",\n    children: [\n      { id: \"app/layout.tsx\", name: \"layout.tsx\" },\n      { id: \"app/page.tsx\", name: \"page.tsx\" },\n      { id: \"app/globals.css\", name: \"globals.css\" },\n    ],\n  },\n  {\n    id: \"components\",\n    name: \"components\",\n    children: [\n      { id: \"components/button.tsx\", name: \"button.tsx\" },\n      { id: \"components/card.tsx\", name: \"card.tsx\" },\n      { id: \"components/navbar.tsx\", name: \"navbar.tsx\" },\n    ],\n  },\n  { id: \"lib\", name: \"lib\", children: [{ id: \"lib/utils.ts\", name: \"utils.ts\" }] },\n  { id: \"package.json\", name: \"package.json\" },\n  { id: \"tsconfig.json\", name: \"tsconfig.json\" },\n  { id: \"README.md\", name: \"README.md\" },\n];\n\nconst css = `\n.hb-tree {\n  position: relative;\n  display: flex;\n  flex-direction: column;\n  box-sizing: border-box;\n  width: var(--hb-tree-w, 100%);\n  max-width: 100%;\n  height: 100%;\n  min-height: 0;\n  background: #0f0f0f;\n  color: #c9c9c9;\n  font-family: var(--font-sans, ui-sans-serif, system-ui, sans-serif);\n  font-size: 13px;\n  line-height: 18px;\n  --hb-tree-accent: #2f7bff;\n}\n.hb-tree *, .hb-tree *::before, .hb-tree *::after { box-sizing: border-box; }\n.hb-tree-title {\n  flex: none;\n  padding: 10px 14px 6px;\n  color: #77777f;\n  font-size: 11px;\n  font-weight: 600;\n  letter-spacing: .08em;\n  line-height: 16px;\n  text-transform: uppercase;\n}\n.hb-tree-filter { position: relative; flex: none; margin: 4px 10px 6px; }\n.hb-tree-filter > svg { position: absolute; top: 50%; left: 9px; width: 13px; height: 13px; color: #77777f; transform: translateY(-50%); pointer-events: none; }\n.hb-tree-input {\n  display: block;\n  width: 100%;\n  height: 28px;\n  margin: 0;\n  padding: 0 28px 0 28px;\n  border: 1px solid #262626;\n  border-radius: 7px;\n  outline: 0;\n  background: #141414;\n  color: #ededed;\n  font: inherit;\n  font-size: 12.5px;\n  transition: border-color .18s ease, box-shadow .18s ease;\n}\n.hb-tree-input::placeholder { color: #6b6b73; }\n.hb-tree-input:focus { border-color: var(--hb-tree-accent); box-shadow: 0 0 0 3px rgba(47, 123, 255, .18); box-shadow: 0 0 0 3px color-mix(in srgb, var(--hb-tree-accent) 22%, transparent); }\n.hb-tree-clear {\n  position: absolute;\n  top: 50%;\n  right: 5px;\n  display: grid;\n  place-items: center;\n  width: 18px;\n  height: 18px;\n  margin: 0;\n  padding: 0;\n  border: 0;\n  border-radius: 5px;\n  background: #222222;\n  color: #a1a1a1;\n  cursor: pointer;\n  transform: translateY(-50%);\n}\n.hb-tree-clear:hover { color: #ededed; }\n.hb-tree-clear:focus-visible { outline: 2px solid var(--hb-tree-accent); outline-offset: 1px; }\n.hb-tree-clear svg { width: 9px; height: 9px; }\n.hb-tree-scroll { flex: 1; min-height: 0; padding: 2px 6px 8px; overflow: auto; scrollbar-width: thin; scrollbar-color: #2a2a2a transparent; }\n.hb-tree-list { position: relative; margin: 0; padding: 0; list-style: none; }\n.hb-tree-row {\n  position: relative;\n  display: flex;\n  align-items: center;\n  gap: 6px;\n  height: var(--hb-tree-row, 26px);\n  padding: 0 8px 0 calc(var(--hb-tree-d, 0) * var(--hb-tree-indent, 14px) + 6px);\n  border-radius: 6px;\n  color: #a8a8a8;\n  white-space: nowrap;\n  cursor: pointer;\n  outline: 0;\n  user-select: none;\n  -webkit-user-select: none;\n  transition: background-color .12s ease, color .12s ease;\n  animation: hb-tree-in .22s cubic-bezier(.2, .9, .2, 1) both;\n}\n.hb-tree-row:hover { background: #181818; color: #ededed; }\n.hb-tree-row[aria-selected=\"true\"] {\n  background: rgba(47, 123, 255, .18);\n  background: color-mix(in srgb, var(--hb-tree-accent) 20%, transparent);\n  color: #ffffff;\n}\n.hb-tree-row:focus-visible { box-shadow: inset 0 0 0 1.5px var(--hb-tree-accent); }\n.hb-tree-chev { flex: none; width: 12px; height: 12px; color: #6f6f78; transition: transform .22s cubic-bezier(.2, .9, .2, 1), color .15s ease; }\n.hb-tree-row[aria-expanded=\"true\"] > .hb-tree-chev { transform: rotate(90deg); color: #c9c9c9; }\n.hb-tree-gap { flex: none; width: 12px; height: 12px; }\n.hb-tree-ico { flex: none; width: 16px; height: 16px; }\n.hb-tree-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }\n.hb-tree-name mark { border-radius: 3px; background: rgba(245, 197, 66, .28); color: #ffe08a; }\n.hb-tree-guide { position: absolute; top: 0; bottom: 0; width: 1px; background: #232323; transition: background-color .2s ease; pointer-events: none; }\n.hb-tree-list:hover .hb-tree-guide { background: #333333; }\n.hb-tree-empty { padding: 18px 12px; color: #77777f; text-align: center; }\n@keyframes hb-tree-in {\n  from { opacity: 0; transform: translate3d(0, -4px, 0); }\n  to { opacity: 1; transform: translate3d(0, 0, 0); }\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-tree-row, .hb-tree-chev, .hb-tree-input, .hb-tree-guide { transition: none; animation: none; }\n}\n`;\n\ninterface Row {\n  node: SidebarTreeNode;\n  depth: number;\n  parent: string | null;\n  pos: number;\n  size: number;\n}\n\nfunction prune(nodes: SidebarTreeNode[], q: string): SidebarTreeNode[] {\n  return nodes.flatMap((node) => {\n    if (!node.children) return node.name.toLowerCase().includes(q) ? [node] : [];\n    const kids = prune(node.children, q);\n    if (kids.length > 0) return [{ ...node, children: kids }];\n    return node.name.toLowerCase().includes(q) ? [{ ...node, children: [] }] : [];\n  });\n}\n\nfunction flatten(nodes: SidebarTreeNode[], open: (id: string) => boolean, depth: number, parent: string | null, out: Row[]) {\n  nodes.forEach((node, i) => {\n    out.push({ node, depth, parent, pos: i + 1, size: nodes.length });\n    if (node.children && node.children.length > 0 && open(node.id)) flatten(node.children, open, depth + 1, node.id, out);\n  });\n}\n\nfunction extOf(name: string) {\n  const dot = name.lastIndexOf(\".\");\n  return dot > 0 ? name.slice(dot + 1).toLowerCase() : \"\";\n}\n\nfunction highlight(name: string, q: string): React.ReactNode {\n  if (!q) return name;\n  const at = name.toLowerCase().indexOf(q);\n  if (at < 0) return name;\n  return (\n    <>\n      {name.slice(0, at)}\n      <mark>{name.slice(at, at + q.length)}</mark>\n      {name.slice(at + q.length)}\n    </>\n  );\n}\n\nfunction FileIcon({ name }: { name: string }) {\n  const ext = extOf(name);\n  const base = { className: \"hb-tree-ico\", viewBox: \"0 0 16 16\", fill: \"none\", \"aria-hidden\": true } as const;\n  if (ext === \"ts\") {\n    return (\n      <svg {...base}>\n        <rect x=\"1.5\" y=\"1.5\" width=\"13\" height=\"13\" rx=\"3\" fill=\"#3178c6\" />\n        <text x=\"8\" y=\"11.4\" textAnchor=\"middle\" fontSize=\"7.6\" fontWeight=\"700\" fill=\"#ffffff\" fontFamily=\"ui-sans-serif, system-ui, sans-serif\">\n          TS\n        </text>\n      </svg>\n    );\n  }\n  if (ext === \"tsx\") {\n    return (\n      <svg {...base} stroke=\"#4fc3f7\" strokeWidth=\"0.95\">\n        <circle cx=\"8\" cy=\"8\" r=\"1.3\" fill=\"#4fc3f7\" stroke=\"none\" />\n        <ellipse cx=\"8\" cy=\"8\" rx=\"6.6\" ry=\"2.5\" />\n        <ellipse cx=\"8\" cy=\"8\" rx=\"6.6\" ry=\"2.5\" transform=\"rotate(60 8 8)\" />\n        <ellipse cx=\"8\" cy=\"8\" rx=\"6.6\" ry=\"2.5\" transform=\"rotate(120 8 8)\" />\n      </svg>\n    );\n  }\n  if (ext === \"css\") {\n    return (\n      <svg {...base} stroke=\"#a78bfa\" strokeWidth=\"1.3\" strokeLinecap=\"round\">\n        <path d=\"M6.2 2.8L5.2 13.2M11.2 2.8l-1 10.4M3 6.2h10.4M2.6 9.8H13\" />\n      </svg>\n    );\n  }\n  if (ext === \"json\") {\n    return (\n      <svg {...base} stroke=\"#f5c542\" strokeWidth=\"1.3\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n        <path d=\"M6 2.8C4.6 2.8 4.6 3.8 4.6 5.3S4.2 7.9 3 8c1.2.1 1.6.6 1.6 2.7s0 2.5 1.4 2.5M10 2.8c1.4 0 1.4 1 1.4 2.5s.4 2.6 1.6 2.7c-1.2.1-1.6.6-1.6 2.7s0 2.5-1.4 2.5\" />\n      </svg>\n    );\n  }\n  if (ext === \"md\") {\n    return (\n      <svg {...base} stroke=\"#9aa4b2\" strokeWidth=\"1.2\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n        <rect x=\"1.5\" y=\"3.5\" width=\"13\" height=\"9\" rx=\"2\" />\n        <path d=\"M4 10V6l1.8 2.2L7.6 6v4M11.2 6v3.6M9.8 8.4l1.4 1.5 1.4-1.5\" />\n      </svg>\n    );\n  }\n  return (\n    <svg {...base} stroke=\"#8f8f8f\" strokeWidth=\"1.2\" strokeLinejoin=\"round\">\n      <path d=\"M4 1.8h5l3.2 3.2v8.2a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2.8a1 1 0 0 1 1-1zM9 1.8V5h3.2\" />\n    </svg>\n  );\n}\n\nfunction FolderIcon({ open }: { open: boolean }) {\n  return (\n    <svg className=\"hb-tree-ico\" viewBox=\"0 0 16 16\" fill=\"none\" aria-hidden=\"true\">\n      {open ? (\n        <path d=\"M1.8 4.4a1 1 0 0 1 1-1h3l1.3 1.4h5.1a1 1 0 0 1 1 1V6H4.4a1 1 0 0 0-.95.7L1.8 11.5zM3.5 12.6l1.5-4.7a.8.8 0 0 1 .76-.55h8.4a.6.6 0 0 1 .57.8l-1.4 4.2a.9.9 0 0 1-.85.6H3.9a.6.6 0 0 1-.4-.35z\" fill=\"#7a8ba6\" />\n      ) : (\n        <path d=\"M1.8 4.4a1 1 0 0 1 1-1h3l1.3 1.4h5.1a1 1 0 0 1 1 1v6.2a1 1 0 0 1-1 1H2.8a1 1 0 0 1-1-1z\" fill=\"#7a8ba6\" />\n      )}\n    </svg>\n  );\n}\n\nexport function SidebarTree({\n  nodes,\n  selected,\n  defaultSelected = null,\n  onSelect,\n  expanded,\n  defaultExpanded,\n  onExpandedChange,\n  filterable = false,\n  placeholder = \"Filter files\",\n  title = \"Explorer\",\n  rowHeight = 26,\n  indent = 14,\n  color = \"#2f7bff\",\n  className = \"\",\n  style,\n  ...rest\n}: SidebarTreeProps) {\n  const data = nodes ?? sampleNodes;\n  const rowRefs = React.useRef<Map<string, HTMLElement>>(new Map());\n  const [innerSel, setInnerSel] = React.useState<string | null>(defaultSelected);\n  const [innerOpen, setInnerOpen] = React.useState<string[]>(defaultExpanded ?? (nodes ? [] : [\"app\", \"components\"]));\n  const [focusId, setFocusId] = React.useState(\"\");\n  const [query, setQuery] = React.useState(\"\");\n  const sel = selected !== undefined ? selected : innerSel;\n  const openIds = expanded ?? innerOpen;\n  const q = query.trim().toLowerCase();\n\n  const rows = React.useMemo(() => {\n    const out: Row[] = [];\n    if (q) flatten(prune(data, q), () => true, 0, null, out);\n    else flatten(data, (id) => openIds.includes(id), 0, null, out);\n    return out;\n  }, [data, openIds, q]);\n\n  const tabStop = rows.some((r) => r.node.id === focusId)\n    ? focusId\n    : (rows.find((r) => r.node.id === sel)?.node.id ?? rows[0]?.node.id);\n\n  function setOpenIds(next: string[]) {\n    if (expanded === undefined) setInnerOpen(next);\n    onExpandedChange?.(next);\n  }\n\n  function toggle(id: string, force?: boolean) {\n    const isOpen = openIds.includes(id);\n    const want = force ?? !isOpen;\n    if (want === isOpen) return;\n    setOpenIds(want ? [...openIds, id] : openIds.filter((x) => x !== id));\n  }\n\n  function choose(node: SidebarTreeNode) {\n    if (selected === undefined) setInnerSel(node.id);\n    onSelect?.(node.id, node);\n  }\n\n  function focusRow(id: string | undefined) {\n    if (id) rowRefs.current.get(id)?.focus();\n  }\n\n  function handleKeyDown(e: React.KeyboardEvent<HTMLUListElement>) {\n    const el = (e.target as HTMLElement).closest<HTMLElement>(\"[data-tree-id]\");\n    if (!el) return;\n    const id = el.dataset.treeId as string;\n    const index = rows.findIndex((r) => r.node.id === id);\n    if (index < 0) return;\n    const row = rows[index];\n    const isFolder = !!row.node.children;\n    const isOpen = q ? true : openIds.includes(id);\n    if (e.key === \"ArrowDown\") {\n      e.preventDefault();\n      focusRow(rows[Math.min(rows.length - 1, index + 1)]?.node.id);\n    } else if (e.key === \"ArrowUp\") {\n      e.preventDefault();\n      focusRow(rows[Math.max(0, index - 1)]?.node.id);\n    } else if (e.key === \"Home\") {\n      e.preventDefault();\n      focusRow(rows[0]?.node.id);\n    } else if (e.key === \"End\") {\n      e.preventDefault();\n      focusRow(rows[rows.length - 1]?.node.id);\n    } else if (e.key === \"ArrowRight\") {\n      e.preventDefault();\n      if (!isFolder) return;\n      if (!isOpen) toggle(id, true);\n      else if (rows[index + 1] && rows[index + 1].parent === id) focusRow(rows[index + 1].node.id);\n    } else if (e.key === \"ArrowLeft\") {\n      e.preventDefault();\n      if (isFolder && isOpen && !q) toggle(id, false);\n      else if (row.parent) focusRow(row.parent);\n    } else if (e.key === \"Enter\" || e.key === \" \") {\n      e.preventDefault();\n      choose(row.node);\n      if (isFolder && !q) toggle(id);\n    }\n  }\n\n  const vars = {\n    \"--hb-tree-accent\": color,\n    \"--hb-tree-row\": `${rowHeight}px`,\n    \"--hb-tree-indent\": `${indent}px`,\n    ...style,\n  } as React.CSSProperties;\n\n  return (\n    <div className={`hb-tree ${className}`} style={vars} {...rest}>\n      <style href=\"hb-sidebar-tree\" precedence=\"default\">{css}</style>\n      {title ? <div className=\"hb-tree-title\">{title}</div> : null}\n      {filterable ? (\n        <div className=\"hb-tree-filter\">\n          <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" aria-hidden=\"true\">\n            <circle cx=\"7\" cy=\"7\" r=\"4.5\" />\n            <path d=\"M10.5 10.5L14 14\" />\n          </svg>\n          <input\n            className=\"hb-tree-input\"\n            type=\"text\"\n            aria-label={placeholder}\n            placeholder={placeholder}\n            value={query}\n            autoComplete=\"off\"\n            spellCheck={false}\n            onChange={(e) => setQuery(e.target.value)}\n            onKeyDown={(e) => {\n              if (e.key === \"Escape\") setQuery(\"\");\n              else if (e.key === \"ArrowDown\") {\n                e.preventDefault();\n                focusRow(tabStop);\n              }\n            }}\n          />\n          {query ? (\n            <button type=\"button\" className=\"hb-tree-clear\" aria-label=\"Clear filter\" onClick={() => setQuery(\"\")}>\n              <svg viewBox=\"0 0 10 10\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.7\" strokeLinecap=\"round\" aria-hidden=\"true\">\n                <path d=\"M2 2l6 6M8 2L2 8\" />\n              </svg>\n            </button>\n          ) : null}\n        </div>\n      ) : null}\n      <div className=\"hb-tree-scroll\">\n        {rows.length === 0 ? (\n          <div className=\"hb-tree-empty\" role=\"status\">\n            No files match\n          </div>\n        ) : (\n          <ul className=\"hb-tree-list\" role=\"tree\" aria-label={title || \"Files\"} onKeyDown={handleKeyDown}>\n            {rows.map((row) => {\n              const { node } = row;\n              const isFolder = !!node.children;\n              const isOpen = isFolder && (q ? true : openIds.includes(node.id));\n              return (\n                <li\n                  key={node.id}\n                  ref={(el) => {\n                    if (el) rowRefs.current.set(node.id, el);\n                    else rowRefs.current.delete(node.id);\n                  }}\n                  role=\"treeitem\"\n                  className=\"hb-tree-row\"\n                  data-tree-id={node.id}\n                  aria-level={row.depth + 1}\n                  aria-setsize={row.size}\n                  aria-posinset={row.pos}\n                  aria-expanded={isFolder ? isOpen : undefined}\n                  aria-selected={node.id === sel}\n                  tabIndex={node.id === tabStop ? 0 : -1}\n                  style={{ \"--hb-tree-d\": row.depth } as React.CSSProperties}\n                  onFocus={() => setFocusId(node.id)}\n                  onClick={() => {\n                    setFocusId(node.id);\n                    choose(node);\n                    if (isFolder && !q) toggle(node.id);\n                  }}\n                >\n                  {Array.from({ length: row.depth }, (_, d) => (\n                    <span key={d} className=\"hb-tree-guide\" aria-hidden=\"true\" style={{ left: `calc(${d} * var(--hb-tree-indent, 14px) + 12px)` }} />\n                  ))}\n                  {isFolder ? (\n                    <svg className=\"hb-tree-chev\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                      <path d=\"M6 3.5L10.5 8 6 12.5\" />\n                    </svg>\n                  ) : (\n                    <span className=\"hb-tree-gap\" aria-hidden=\"true\" />\n                  )}\n                  {isFolder ? <FolderIcon open={isOpen} /> : <FileIcon name={node.name} />}\n                  <span className=\"hb-tree-name\">{highlight(node.name, q)}</span>\n                </li>\n              );\n            })}\n          </ul>\n        )}\n      </div>\n    </div>\n  );\n}\n"
    }
  ]
}