{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"file-tree","type":"registry:ui","title":"File Tree","description":"Collapsible file tree where folders spring open, chevrons rotate, indent guides light up for the active folder and a selection pill glides between rows.","author":"GetUrUI <hello@geturui.dev>","dependencies":["motion","lucide-react"],"registryDependencies":[],"files":[{"path":"registry/geturui/ui/file-tree.tsx","type":"registry:ui","target":"components/ui/file-tree.tsx","content":"\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport {\n  Braces,\n  ChevronRight,\n  File,\n  FileArchive,\n  FileCode,\n  FileImage,\n  FileKey,\n  FileLock,\n  FileText,\n  FileType,\n  Folder,\n  FolderOpen,\n  type LucideIcon,\n} from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface TreeNode {\n  id: string;\n  name: string;\n  /** Present (even empty) for folders. */\n  children?: TreeNode[];\n}\n\nexport interface FileTreeProps {\n  data: TreeNode[];\n  selectedId?: string;\n  defaultSelectedId?: string;\n  onSelect?: (node: TreeNode) => void;\n  expandedIds?: string[];\n  defaultExpandedIds?: string[];\n  onExpandedChange?: (ids: string[]) => void;\n  /** Row height in px. */\n  rowHeight?: number;\n  /** Indent per level in px. */\n  indent?: number;\n  className?: string;\n  \"aria-label\"?: string;\n}\n\ntype Row = { node: TreeNode; depth: number; ancestors: string[]; posinset: number; setsize: number; parentId: string | null };\n\nconst icons: [RegExp, LucideIcon, string][] = [\n  [/\\.(tsx?|jsx?|mjs|cjs)$/i, FileCode, \"text-info\"],\n  [/\\.json$/i, Braces, \"text-warning\"],\n  [/\\.(mdx?|txt)$/i, FileText, \"text-ink-muted\"],\n  [/\\.(css|scss)$/i, FileType, \"text-volt-ink\"],\n  [/\\.(png|jpe?g|gif|svg|webp|avif|ico)$/i, FileImage, \"text-success\"],\n  [/\\.(zip|tgz|gz)$/i, FileArchive, \"text-ember-ink\"],\n  [/(\\.lock|lock\\.json|lock\\.yaml)$/i, FileLock, \"text-ink-subtle\"],\n  [/^\\.env/i, FileKey, \"text-warning\"],\n];\n\n/** Picks a lucide glyph and tint from the file name. */\nexport function fileIcon(name: string): { Icon: LucideIcon; tint: string } {\n  for (const [re, Icon, tint] of icons) if (re.test(name)) return { Icon, tint };\n  return { Icon: File, tint: \"text-ink-subtle\" };\n}\n\nfunction flatten(nodes: TreeNode[], expanded: Set<string>, depth = 0, ancestors: string[] = [], out: Row[] = []) {\n  nodes.forEach((node, i) => {\n    out.push({ node, depth, ancestors, posinset: i + 1, setsize: nodes.length, parentId: ancestors[ancestors.length - 1] ?? null });\n    if (node.children && expanded.has(node.id)) flatten(node.children, expanded, depth + 1, [...ancestors, node.id], out);\n  });\n  return out;\n}\n\nconst rowSpring = { type: \"spring\", stiffness: 380, damping: 34 } as const;\n\n/**\n * Collapsible file tree: folders spring open, chevrons rotate, indent guides light up for the active folder and a\n * selection pill glides between rows. Full keyboard support (arrows, Home/End, Enter, type-ahead).\n */\nexport function FileTree({\n  data,\n  selectedId,\n  defaultSelectedId,\n  onSelect,\n  expandedIds,\n  defaultExpandedIds = [],\n  onExpandedChange,\n  rowHeight = 30,\n  indent = 16,\n  className,\n  ...rest\n}: FileTreeProps) {\n  const reduce = useReducedMotion();\n  const uid = React.useId();\n  const [innerExpanded, setInnerExpanded] = React.useState<string[]>(defaultExpandedIds);\n  const expandedList = expandedIds ?? innerExpanded;\n  const expanded = React.useMemo(() => new Set(expandedList), [expandedList]);\n  const [innerSelected, setInnerSelected] = React.useState(defaultSelectedId);\n  const selected = selectedId ?? innerSelected;\n  const rows = React.useMemo(() => flatten(data, expanded), [data, expanded]);\n  const [focusedId, setFocusedId] = React.useState<string | undefined>(undefined);\n  const focused = rows.some((r) => r.node.id === focusedId) ? focusedId : (rows.find((r) => r.node.id === selected)?.node.id ?? rows[0]?.node.id);\n  const refs = React.useRef(new Map<string, HTMLDivElement>());\n  const typeahead = React.useRef({ text: \"\", t: 0 });\n\n  const selectedRow = rows.find((r) => r.node.id === selected);\n  const activeFolder = selectedRow ? (selectedRow.node.children ? selectedRow.node.id : selectedRow.parentId) : null;\n\n  function setExpanded(next: string[]) {\n    if (expandedIds === undefined) setInnerExpanded(next);\n    onExpandedChange?.(next);\n  }\n  function toggle(id: string, open?: boolean) {\n    const isOpen = expanded.has(id);\n    const want = open ?? !isOpen;\n    if (want === isOpen) return;\n    setExpanded(want ? [...expandedList, id] : expandedList.filter((x) => x !== id));\n  }\n  function select(node: TreeNode) {\n    if (selectedId === undefined) setInnerSelected(node.id);\n    onSelect?.(node);\n  }\n  function focusRow(id: string | undefined) {\n    if (!id) return;\n    setFocusedId(id);\n    refs.current.get(id)?.focus();\n  }\n\n  function onKeyDown(e: React.KeyboardEvent) {\n    const i = rows.findIndex((r) => r.node.id === focused);\n    const row = rows[i];\n    if (!row) return;\n    const isFolder = !!row.node.children;\n    switch (e.key) {\n      case \"ArrowDown\":\n        focusRow(rows[Math.min(rows.length - 1, i + 1)]?.node.id);\n        break;\n      case \"ArrowUp\":\n        focusRow(rows[Math.max(0, i - 1)]?.node.id);\n        break;\n      case \"Home\":\n        focusRow(rows[0]?.node.id);\n        break;\n      case \"End\":\n        focusRow(rows[rows.length - 1]?.node.id);\n        break;\n      case \"ArrowRight\":\n        if (isFolder && !expanded.has(row.node.id)) toggle(row.node.id, true);\n        else if (isFolder && row.node.children?.length) focusRow(row.node.children[0].id);\n        break;\n      case \"ArrowLeft\":\n        if (isFolder && expanded.has(row.node.id)) toggle(row.node.id, false);\n        else if (row.parentId) focusRow(row.parentId);\n        break;\n      case \"Enter\":\n      case \" \":\n        select(row.node);\n        if (isFolder) toggle(row.node.id);\n        break;\n      default:\n        if (e.key.length === 1 && !e.metaKey && !e.ctrlKey && !e.altKey) {\n          const now = e.timeStamp;\n          const ta = typeahead.current;\n          ta.text = now - ta.t > 600 ? e.key.toLowerCase() : ta.text + e.key.toLowerCase();\n          ta.t = now;\n          const ordered = [...rows.slice(i + 1), ...rows.slice(0, i + 1)];\n          const hit = ordered.find((r) => r.node.name.toLowerCase().startsWith(ta.text));\n          if (hit) focusRow(hit.node.id);\n          break;\n        }\n        return;\n    }\n    e.preventDefault();\n  }\n\n  return (\n    <div role=\"tree\" aria-label={rest[\"aria-label\"]} onKeyDown={onKeyDown} className={cn(\"relative select-none text-[13px]\", className)}>\n      <AnimatePresence initial={false}>\n        {rows.map((row) => {\n          const { node, depth } = row;\n          const isFolder = !!node.children;\n          const isOpen = isFolder && expanded.has(node.id);\n          const isSelected = node.id === selected;\n          const { Icon, tint } = isFolder ? { Icon: isOpen ? FolderOpen : Folder, tint: isOpen ? \"text-volt-ink\" : \"text-ink-muted\" } : fileIcon(node.name);\n          return (\n            <motion.div\n              key={node.id}\n              // Clip only while the row grows or shrinks, so the selection pill can glide across rows.\n              initial={{ height: 0, opacity: 0, overflow: \"hidden\" }}\n              animate={{ height: rowHeight, opacity: 1, transitionEnd: { overflow: \"visible\" } }}\n              exit={{ height: 0, opacity: 0, overflow: \"hidden\" }}\n              transition={reduce ? { duration: 0 } : { height: rowSpring, opacity: { duration: 0.16 } }}\n              className=\"relative\"\n            >\n              <div\n                ref={(el) => {\n                  if (el) refs.current.set(node.id, el);\n                  else refs.current.delete(node.id);\n                }}\n                role=\"treeitem\"\n                aria-level={depth + 1}\n                aria-posinset={row.posinset}\n                aria-setsize={row.setsize}\n                aria-expanded={isFolder ? isOpen : undefined}\n                aria-selected={isSelected}\n                tabIndex={node.id === focused ? 0 : -1}\n                onFocus={() => setFocusedId(node.id)}\n                onClick={() => {\n                  setFocusedId(node.id);\n                  select(node);\n                  if (isFolder) toggle(node.id);\n                }}\n                style={{ height: rowHeight, paddingLeft: 8 + depth * indent }}\n                className={cn(\n                  \"group/row relative flex cursor-pointer items-center gap-1.5 rounded-sm pr-2 outline-none\",\n                  \"focus-visible:shadow-[inset_0_0_0_2px_var(--color-volt-ink)]\",\n                  isSelected ? \"text-ink\" : \"text-ink-muted hover:text-ink\",\n                )}\n              >\n                {!isSelected && <span aria-hidden className=\"absolute inset-0 rounded-sm transition-colors group-hover/row:bg-surface-hover\" />}\n                {/* Indent guides */}\n                {row.ancestors.map((a, level) => (\n                  <span\n                    key={a}\n                    aria-hidden\n                    style={{ left: 8 + level * indent + 7 }}\n                    className={cn(\"absolute inset-y-0 w-px transition-colors duration-200\", a === activeFolder ? \"bg-border-strong\" : \"bg-border\")}\n                  />\n                ))}\n                {isSelected && (\n                  <motion.span\n                    layoutId={`${uid}-selected`}\n                    aria-hidden\n                    transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 480, damping: 36 }}\n                    className=\"absolute inset-0 rounded-sm bg-volt-soft\"\n                  />\n                )}\n                <span aria-hidden className=\"relative grid size-4 shrink-0 place-items-center\">\n                  {isFolder && (\n                    <motion.span\n                      initial={false}\n                      animate={{ rotate: isOpen ? 90 : 0 }}\n                      transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 500, damping: 30 }}\n                      className=\"grid place-items-center text-ink-subtle group-hover/row:text-ink\"\n                    >\n                      <ChevronRight className=\"size-3.5\" strokeWidth={2.25} />\n                    </motion.span>\n                  )}\n                </span>\n                <Icon aria-hidden className={cn(\"relative size-4 shrink-0\", tint)} />\n                <span className={cn(\"relative truncate\", isSelected && \"font-medium\")}>{node.name}</span>\n              </div>\n            </motion.div>\n          );\n        })}\n      </AnimatePresence>\n    </div>\n  );\n}\n"},{"path":"LICENSE-geturui.md","type":"registry:file","target":"LICENSE-geturui.md","content":"MIT License\n\nCopyright (c) 2026 geturui.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"}],"categories":["components","tree","file tree","explorer","sidebar","navigation","folders","keyboard","layoutid"],"meta":{"tier":"free","license":"MIT","docs":"https://geturui.com/docs/file-tree"}}