File Tree
.mdCollapsible file tree where folders spring open, chevrons rotate, indent guides light up for the active folder and a selection pill glides between rows.
treefile treeexplorersidebarnavigationfolderskeyboardlayoutId
Installation
$npx shadcn@latest add @geturui/file-tree
Usage
usage.tsx
1import { FileTree, type TreeNode } from "@/components/ui/file-tree";
2
3const files: TreeNode[] = [
4 { id: "app", name: "app", children: [{ id: "app/page.tsx", name: "page.tsx" }, { id: "app/globals.css", name: "globals.css" }] },
5 { id: "components", name: "components", children: [{ id: "components/ui/button.tsx", name: "button.tsx" }] },
6 { id: "package.json", name: "package.json" },
7];
8
9export function Explorer() {
10 return (
11 <FileTree
12 aria-label="Project files"
13 data={files}
14 defaultExpandedIds={["app"]}
15 onSelect={(node) => console.log(node.id)}
16 />
17 );
18}
Props
| Prop | Type | Default | Description |
|---|---|---|---|
data* | TreeNode[] | — | Nodes as { id, name, children? }. Any node with children (even empty) is a folder. |
selectedId | string | — | Controlled selected node. |
defaultSelectedId | string | — | Selected node on mount when uncontrolled. |
onSelect | (node: TreeNode) => void | — | Called on click, Enter or Space. |
expandedIds | string[] | — | Controlled open folders. |
defaultExpandedIds | string[] | [] | Open folders on mount when uncontrolled. |
onExpandedChange | (ids: string[]) => void | — | Called when a folder opens or closes. |
rowHeight | number | 30 | Row height in px. |
indent | number | 16 | Indent per level in px. |
aria-label | string | — | Names the tree. |
className | string | — | Classes for the tree root. |
Accessibility
- role="tree" with role="treeitem" rows carrying aria-level, aria-posinset, aria-setsize, aria-selected and aria-expanded on folders.
- Roving tabindex: Up and Down move, Right opens a folder or enters it, Left closes it or goes to the parent, Home and End jump, Enter or Space selects (and toggles folders), typing jumps to a matching name.
- File-type icons are decorative; the name is always visible text. With reduced motion rows and the pill move instantly.