{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "file-tree",
  "type": "registry:ui",
  "title": "File Tree",
  "author": "Rishabh Bose <https://theb0se.vercel.app/>",
  "description": "Hierarchical file and folder explorer with animated expand/collapse transitions, instant file search filtering, and active path tracking.",
  "dependencies": [
    "framer-motion",
    "lucide-react",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "components/ui/file-tree.tsx",
      "content": "\"use client\";\n\nimport React, { useState, useMemo } from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"framer-motion\";\nimport {\n  Check,\n  ChevronRight,\n  Copy,\n  FileCode2,\n  FileJson,\n  FileText,\n  Folder,\n  FolderOpen,\n  Search,\n} from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nasync function copyToClipboard(text: string): Promise<boolean> {\n  if (typeof window === \"undefined\") return false;\n  try {\n    if (navigator.clipboard && window.isSecureContext) {\n      await navigator.clipboard.writeText(text);\n      return true;\n    }\n    const textArea = document.createElement(\"textarea\");\n    textArea.value = text;\n    textArea.style.position = \"fixed\";\n    textArea.style.left = \"-999999px\";\n    textArea.style.top = \"-999999px\";\n    document.body.appendChild(textArea);\n    textArea.focus();\n    textArea.select();\n    const succeeded = document.execCommand(\"copy\");\n    textArea.remove();\n    return succeeded;\n  } catch {\n    return false;\n  }\n}\n\nexport interface FileTreeNode {\n  id: string;\n  name: string;\n  type: \"file\" | \"folder\";\n  badge?: string;\n  children?: FileTreeNode[];\n}\n\nexport interface FileTreeProps {\n  /** Hierarchical file tree data */\n  data?: FileTreeNode[];\n  /** Default expanded folder IDs */\n  defaultExpandedIds?: string[];\n  /** Default selected file/folder ID */\n  defaultSelectedId?: string;\n  /** Callback when a node is clicked */\n  onSelect?: (node: FileTreeNode) => void;\n  /** Show top search filter input */\n  searchable?: boolean;\n  /** Additional container class names */\n  className?: string;\n}\n\nconst DEFAULT_TREE: FileTreeNode[] = [\n  {\n    id: \"src\",\n    name: \"src\",\n    type: \"folder\",\n    children: [\n      {\n        id: \"src/app\",\n        name: \"app\",\n        type: \"folder\",\n        children: [\n          { id: \"src/app/layout.tsx\", name: \"layout.tsx\", type: \"file\", badge: \"TSX\" },\n          { id: \"src/app/page.tsx\", name: \"page.tsx\", type: \"file\", badge: \"TSX\" },\n          { id: \"src/app/globals.css\", name: \"globals.css\", type: \"file\", badge: \"CSS\" },\n        ],\n      },\n      {\n        id: \"src/components\",\n        name: \"components\",\n        type: \"folder\",\n        children: [\n          {\n            id: \"src/components/ui\",\n            name: \"ui\",\n            type: \"folder\",\n            children: [\n              { id: \"src/components/ui/Autocomplete.tsx\", name: \"Autocomplete.tsx\", type: \"file\", badge: \"NEW\" },\n              { id: \"src/components/ui/FileTree.tsx\", name: \"FileTree.tsx\", type: \"file\", badge: \"NEW\" },\n              { id: \"src/components/ui/MagneticButton.tsx\", name: \"MagneticButton.tsx\", type: \"file\" },\n            ],\n          },\n        ],\n      },\n      {\n        id: \"src/lib\",\n        name: \"lib\",\n        type: \"folder\",\n        children: [\n          { id: \"src/lib/utils.ts\", name: \"utils.ts\", type: \"file\", badge: \"TS\" },\n        ],\n      },\n    ],\n  },\n  { id: \"package.json\", name: \"package.json\", type: \"file\", badge: \"JSON\" },\n  { id: \"README.md\", name: \"README.md\", type: \"file\", badge: \"MD\" },\n];\n\nfunction collectFolderIds(nodes: FileTreeNode[]): string[] {\n  const ids: string[] = [];\n  for (const node of nodes) {\n    if (node.type === \"folder\") {\n      ids.push(node.id);\n      if (node.children) {\n        ids.push(...collectFolderIds(node.children));\n      }\n    }\n  }\n  return ids;\n}\n\nfunction getFileIcon(name: string) {\n  if (name.endsWith(\".json\")) {\n    return <FileJson className=\"h-3.5 w-3.5 shrink-0 text-neutral-400\" />;\n  }\n  if (name.endsWith(\".md\") || name.endsWith(\".txt\")) {\n    return <FileText className=\"h-3.5 w-3.5 shrink-0 text-neutral-400\" />;\n  }\n  return <FileCode2 className=\"h-3.5 w-3.5 shrink-0 text-neutral-400\" />;\n}\n\nfunction filterTree(nodes: FileTreeNode[], query: string): FileTreeNode[] {\n  if (!query.trim()) return nodes;\n  const lower = query.toLowerCase();\n\n  return nodes.reduce<FileTreeNode[]>((acc, node) => {\n    const matchesSelf = node.name.toLowerCase().includes(lower);\n    if (node.type === \"folder\" && node.children) {\n      const filteredChildren = filterTree(node.children, query);\n      if (matchesSelf || filteredChildren.length > 0) {\n        acc.push({\n          ...node,\n          children: filteredChildren.length > 0 ? filteredChildren : node.children,\n        });\n      }\n    } else if (matchesSelf) {\n      acc.push(node);\n    }\n    return acc;\n  }, []);\n}\n\nexport function FileTree({\n  data = DEFAULT_TREE,\n  defaultExpandedIds = [\"src\", \"src/components\", \"src/components/ui\"],\n  defaultSelectedId = \"src/components/ui/FileTree.tsx\",\n  onSelect,\n  searchable = true,\n  className,\n}: FileTreeProps) {\n  const shouldReduceMotion = useReducedMotion();\n  const [expanded, setExpanded] = useState<Set<string>>(\n    () => new Set(defaultExpandedIds)\n  );\n  const [selectedId, setSelectedId] = useState<string>(defaultSelectedId);\n  const [query, setQuery] = useState(\"\");\n  const [copiedPath, setCopiedPath] = useState(false);\n\n  const allFolderIds = useMemo(() => collectFolderIds(data), [data]);\n  const visibleData = useMemo(() => filterTree(data, query), [data, query]);\n\n  const toggleFolder = (id: string) => {\n    setExpanded((prev) => {\n      const next = new Set(prev);\n      if (next.has(id)) next.delete(id);\n      else next.add(id);\n      return next;\n    });\n  };\n\n  const handleCopyPath = async () => {\n    const ok = await copyToClipboard(selectedId);\n    if (ok) {\n      setCopiedPath(true);\n      setTimeout(() => setCopiedPath(false), 1500);\n    }\n  };\n\n  const renderNode = (node: FileTreeNode, depth = 0) => {\n    const isFolder = node.type === \"folder\";\n    const isOpen = query.trim().length > 0 || expanded.has(node.id);\n    const isSelected = selectedId === node.id;\n\n    return (\n      <div\n        key={node.id}\n        role=\"treeitem\"\n        aria-expanded={isFolder ? isOpen : undefined}\n        aria-selected={isSelected}\n      >\n        <button\n          type=\"button\"\n          onClick={() => {\n            setSelectedId(node.id);\n            onSelect?.(node);\n            if (isFolder) toggleFolder(node.id);\n          }}\n          style={{ paddingLeft: `${depth * 14 + 10}px` }}\n          className={cn(\n            \"group flex w-full cursor-pointer items-center justify-between gap-2 rounded-lg py-1.5 pr-2.5 text-left text-xs transition-colors\",\n            isSelected\n              ? \"bg-black font-medium text-white dark:bg-white dark:text-black\"\n              : \"text-neutral-700 hover:bg-neutral-100 hover:text-black dark:text-neutral-300 dark:hover:bg-[#141414] dark:hover:text-white\"\n          )}\n        >\n          <span className=\"flex min-w-0 items-center gap-1.5\">\n            {isFolder ? (\n              <>\n                <ChevronRight\n                  className={cn(\n                    \"h-3.5 w-3.5 shrink-0 transition-transform duration-150\",\n                    isOpen && \"rotate-90\",\n                    isSelected\n                      ? \"text-white dark:text-black\"\n                      : \"text-neutral-400\"\n                  )}\n                />\n                {isOpen ? (\n                  <FolderOpen\n                    className={cn(\n                      \"h-3.5 w-3.5 shrink-0\",\n                      isSelected\n                        ? \"text-white dark:text-black\"\n                        : \"text-neutral-600 dark:text-neutral-300\"\n                    )}\n                  />\n                ) : (\n                  <Folder\n                    className={cn(\n                      \"h-3.5 w-3.5 shrink-0\",\n                      isSelected\n                        ? \"text-white dark:text-black\"\n                        : \"text-neutral-500 dark:text-neutral-400\"\n                    )}\n                  />\n                )}\n              </>\n            ) : (\n              <>\n                <span className=\"w-3.5 shrink-0\" />\n                {getFileIcon(node.name)}\n              </>\n            )}\n            <span className=\"truncate font-mono text-[11.5px]\">{node.name}</span>\n          </span>\n\n          {node.badge && (\n            <span\n              className={cn(\n                \"rounded px-1.5 py-0.5 font-mono text-[9px] uppercase tracking-wider\",\n                isSelected\n                  ? \"bg-white/20 text-white dark:bg-black/15 dark:text-black\"\n                  : \"border border-neutral-200 bg-neutral-50 text-neutral-500 dark:border-[#262626] dark:bg-[#121212] dark:text-neutral-400\"\n              )}\n            >\n              {node.badge}\n            </span>\n          )}\n        </button>\n\n        <AnimatePresence initial={false}>\n          {isFolder && isOpen && node.children && node.children.length > 0 && (\n            <motion.div\n              role=\"group\"\n              initial={shouldReduceMotion ? { opacity: 0 } : { height: 0, opacity: 0 }}\n              animate={\n                shouldReduceMotion ? { opacity: 1 } : { height: \"auto\", opacity: 1 }\n              }\n              exit={shouldReduceMotion ? { opacity: 0 } : { height: 0, opacity: 0 }}\n              transition={{ duration: shouldReduceMotion ? 0 : 0.18, ease: \"easeOut\" }}\n              className=\"overflow-hidden\"\n            >\n              {node.children.map((child) => renderNode(child, depth + 1))}\n            </motion.div>\n          )}\n        </AnimatePresence>\n      </div>\n    );\n  };\n\n  return (\n    <div\n      role=\"tree\"\n      aria-label=\"File explorer\"\n      className={cn(\n        \"w-full max-w-sm overflow-hidden rounded-2xl border border-neutral-200 bg-white dark:border-[#222222] dark:bg-[#0A0A0A]\",\n        className\n      )}\n    >\n      {searchable && (\n        <div className=\"flex items-center justify-between gap-2 border-b border-neutral-200 px-3 py-2 dark:border-[#1F1F1F]\">\n          <div className=\"flex flex-1 items-center gap-2\">\n            <Search className=\"h-3.5 w-3.5 shrink-0 text-neutral-400\" />\n            <input\n              type=\"text\"\n              value={query}\n              onChange={(e) => setQuery(e.target.value)}\n              placeholder=\"Filter files...\"\n              aria-label=\"Filter files\"\n              className=\"w-full bg-transparent font-mono text-xs text-black placeholder:text-neutral-400 focus:outline-none dark:text-white dark:placeholder:text-neutral-500\"\n            />\n          </div>\n          <button\n            type=\"button\"\n            onClick={() =>\n              setExpanded((prev) =>\n                prev.size >= allFolderIds.length\n                  ? new Set()\n                  : new Set(allFolderIds)\n              )\n            }\n            className=\"shrink-0 cursor-pointer rounded border border-neutral-200 px-1.5 py-0.5 font-mono text-[10px] text-neutral-500 transition-colors hover:border-black hover:text-black dark:border-[#262626] dark:hover:border-white dark:hover:text-white\"\n          >\n            {expanded.size >= allFolderIds.length ? \"Collapse\" : \"Expand\"}\n          </button>\n        </div>\n      )}\n\n      <div className=\"max-h-72 overflow-y-auto p-2\">\n        {visibleData.length === 0 ? (\n          <div className=\"py-6 text-center text-xs text-neutral-400\">\n            No matching files\n          </div>\n        ) : (\n          visibleData.map((node) => renderNode(node, 0))\n        )}\n      </div>\n\n      <div className=\"flex items-center justify-between gap-2 border-t border-neutral-200 bg-neutral-50/70 px-3 py-2 font-mono text-[10px] text-neutral-500 dark:border-[#1F1F1F] dark:bg-[#050505]\">\n        <span className=\"truncate\">{selectedId}</span>\n        <button\n          type=\"button\"\n          onClick={handleCopyPath}\n          aria-label=\"Copy selected file path\"\n          className=\"inline-flex shrink-0 cursor-pointer items-center gap-1 rounded px-1.5 py-0.5 text-neutral-500 transition-colors hover:text-black dark:hover:text-white\"\n        >\n          {copiedPath ? (\n            <>\n              <Check className=\"h-3 w-3\" />\n              <span>Copied</span>\n            </>\n          ) : (\n            <>\n              <Copy className=\"h-3 w-3\" />\n              <span>Copy</span>\n            </>\n          )}\n        </button>\n      </div>\n    </div>\n  );\n}\n\nexport default FileTree;\n",
      "type": "registry:ui",
      "target": "components/ui/file-tree.tsx"
    },
    {
      "path": "lib/utils.ts",
      "content": "import { type ClassValue, clsx } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport async function copyToClipboard(text: string): Promise<boolean> {\n  if (typeof window === \"undefined\") return false;\n\n  try {\n    if (navigator.clipboard && window.isSecureContext) {\n      await navigator.clipboard.writeText(text);\n      return true;\n    }\n\n    const textArea = document.createElement(\"textarea\");\n    textArea.value = text;\n    textArea.style.position = \"fixed\";\n    textArea.style.left = \"-999999px\";\n    textArea.style.top = \"-999999px\";\n    document.body.appendChild(textArea);\n    textArea.focus();\n    textArea.select();\n    const succeeded = document.execCommand(\"copy\");\n    textArea.remove();\n    return succeeded;\n  } catch {\n    return false;\n  }\n}\n",
      "type": "registry:lib",
      "target": "lib/utils.ts"
    }
  ]
}
