File Tree
Folders and files a change touched, marked added, changed or removed, walked with the arrow keys.
Installation
npx shadcn@latest add "@aiellie/file-tree"shadcn needs the @aiellie registry in components.json first. npx aiellie add file-tree adds it on its first run, then installs the same way.
File Tree shows the files a change touched and where they sit, which a list of paths makes the reader work out. Files are marked added, changed or removed in the same ink as a diff, and each file wears the mark of its language.
It's a real tree for the keyboard: up and down walk the rows, right opens a folder and then steps into it, left closes it and then steps out, and Home and End reach the ends. The whole tree is one tab stop.
Getting started
Describe the files
A node with
childrenis a folder. Folders start closed unlessdefaultOpenis set.components/changed-files.tsx"use client" import * as React from "react" import { FileTree, type FileNode } from "@/components/aiellie/file-tree" const FILES: FileNode[] = [ { name: "app", defaultOpen: true, children: [{ name: "page.tsx", status: "modified" }], }, { name: "README.md", status: "added" }, ] export function ChangedFiles() { const [path, setPath] = React.useState("app/page.tsx") return <FileTree nodes={FILES} selected={path} onSelect={setPath} /> }
Anatomy
<div data-slot="file-tree" role="tree">
<button data-slot="file-tree-item" role="treeitem" data-status="added" data-selected>
{/* twisty, folder or file mark, name, status */}
</button>
</div>Rows are flat, with depth set as padding, so hover and selection span the whole width. aria-level and the set sizes give screen readers the nesting back.
Selection is by path, like app/page.tsx, since most projects have more than one page.tsx. Which folders are open the tree keeps itself, but a file chosen from outside it, like from a tab or a search, opens the folders it sits in.
Examples
Beside the code
Show the chosen file in a CodeBlock.
export const greet = (name: string) => `Hello, ${name}`API reference
FileTree
| Prop | Type | Default | Description |
|---|---|---|---|
nodes | FileNode[] | required | |
selected | string | The chosen file's path. Its folders open when it changes. | |
onSelect | (path: string) => void | Called when a file is pressed. | |
aria-label | string | "Files" |
All other div props are forwarded.
FileNode
| Field | Type | Default | Description |
|---|---|---|---|
name | string | required | |
children | FileNode[] | Makes it a folder. | |
status | "added" | "modified" | "removed" | ||
defaultOpen | boolean | Opens a folder to begin with. |