shadcn.io is an independent project built on shadcn/ui
On this page11 sections
File Tree shows the files an agent changed in one turn as folders that open and close, with the file the reader picked marked. Put it under a reply that touched several files, so people see where the changes landed before they open one.
It is a pattern built from Collapsible and Button, so the code is yours. For one file the agent read or wrote, a Tool line is enough. A large tree people browse with the arrow keys belongs in a Tree View.
Installation
File Tree is a pattern. The command adds the parts it's built from, Collapsible and Button, and the code to copy is the demo's.
Anatomy

Usage
import { ChevronRightIcon, FileIcon, FolderIcon } from "lucide-react";
import { useState } from "react";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import {
Collapsible, CollapsibleContent,
} from "@/components/ui/collapsible";interface FileNode {
/** A folder's files and folders. */
children?: FileNode[];
name: string;
path: string;
type: "file" | "folder";
}
const files: FileNode[] = [
{
children: [
{ name: "layout.tsx", path: "app/layout.tsx", type: "file" },
{ name: "page.tsx", path: "app/page.tsx", type: "file" },
],
name: "app",
path: "app",
type: "folder",
},
{
children: [
{
children: [
{
name: "button.tsx",
path: "components/ui/button.tsx",
type: "file",
},
],
name: "ui",
path: "components/ui",
type: "folder",
},
{
name: "login-form.tsx",
path: "components/login-form.tsx",
type: "file",
},
],
name: "components",
path: "components",
type: "folder",
},
];
function FileTreeNode(props: {
node: FileNode;
onSelect: (path: string) => void;
selected: string;
}) {
const { node, onSelect, selected } = props;
if (node.type === "folder") {
return (
<Collapsible defaultExpanded>
<Button
className="w-full justify-start font-normal aria-expanded:bg-transparent aria-expanded:hover:bg-muted dark:aria-expanded:hover:bg-muted/50"
size="sm"
slot="trigger"
variant="ghost"
>
<ChevronRightIcon
aria-hidden="true"
className="transition-transform group-aria-expanded/button:rotate-90 motion-reduce:transition-none [&:dir(rtl)]:-scale-x-100 [&:dir(rtl)]:group-aria-expanded/button:-rotate-90"
/>
<FolderIcon aria-hidden="true" />
{node.name}
</Button>
<CollapsibleContent>
<div className="ms-4 flex flex-col">
{node.children?.map((child) => (
<FileTreeNode
key={child.path}
node={child}
onSelect={onSelect}
selected={selected}
/>
))}
</div>
</CollapsibleContent>
</Collapsible>
);
}
return (
<Button
aria-current={selected === node.path ? "true" : undefined}
className={cn(
"w-full justify-start font-normal",
selected === node.path && "bg-accent text-accent-foreground"
)}
onPress={() => onSelect(node.path)}
size="sm"
variant="ghost"
>
<FileIcon aria-hidden="true" />
{node.name}
</Button>
);
}
export function FileTreeDemo() {
const [selected, setSelected] = useState("components/ui/button.tsx");
return (
<div className="flex w-full max-w-xs flex-col py-6 text-sm">
{files.map((node) => (
<FileTreeNode
key={node.path}
node={node}
onSelect={setSelected}
selected={selected}
/>
))}
</div>
);
}
Accessibility
Each folder is a <button> that reports aria-expanded, so a screen reader says whether it is open. Each file is a plain button, and the demo sets aria-current on the picked one, which screen readers announce as current. The icons carry aria-hidden, so a button is named by its text alone. There is no tree role and no arrow-key movement: Tab reaches every folder and every file in an open folder, which suits the few paths one turn touches.
- Tab
- Moves focus to the next folder or file, skipping the files inside a closed folder.
- Shift+Tab
- Moves focus back through the same stops.
- Enter
- On a folder, opens or closes it. On a file, picks it.
- Space
- On a folder, opens or closes it. On a file, picks it.
Demos
Change status
A login page's changes, with what the turn did to each file: the file node's status draws A for added, M for modified or D for deleted at the end of its row, in the success, warning and destructive colors, and a screen reader hears the word after the name. Pick it when one turn creates, edits and removes files, so people see which is which.
AI SDK
The paths come from the turn's tool calls. With a writeFile tool that takes a path, each finished call is a tool-writeFile part of the message in the output-available state, so collect those inputs, then fold the flat paths into folders.
const paths = message.parts.flatMap((part) =>
part.type === "tool-writeFile" && part.state === "output-available"
? [part.input.path]
: []
);/** "app/page.tsx" and "app/layout.tsx" become the app folder with two files. */
function toTree(paths: string[]): FileNode[] {
const root: FileNode[] = [];
for (const path of new Set(paths)) {
const names = path.split("/");
let level = root;
names.forEach((name, index) => {
const at = names.slice(0, index + 1).join("/");
if (index === names.length - 1) {
level.push({ name, path: at, type: "file" });
return;
}
let folder = level.find(
(node) => node.type === "folder" && node.path === at
);
if (!folder) {
folder = { children: [], name, path: at, type: "folder" };
level.push(folder);
}
folder.children ??= [];
level = folder.children;
});
}
return root;
}A file the agent wrote twice shows once, because toTree walks a Set of the paths. Folders and files keep the order the agent wrote them in, so sort paths first for an alphabetical tree.
In React the messages are the ones useChat returns, and their parts have this shape whatever model wrote them.
Migrating from AI SDK Elements
If you added File Tree from AI SDK Elementscomponents/ai-elements/file-tree.tsx wrapper. Paste this prompt into your coding agent to rebuild it on Collapsible and Button. Every export keeps its name and props, so the code that uses it keeps working.
Migrate components/ai-elements/file-tree.tsx from AI SDK Elements to the shadcn.io Collapsible and Button components.
Keep every export, with the same name and props, so the code that imports it keeps working. Only change how each part renders.
1. Read https://www.shadcn.io/ui/aria/file-tree.md and build File Tree the same way. If the project hasn't got Collapsible and Button, run the command in its Installation section.
2. Rebuild each part:
- FileTreeFolder renders a Collapsible. Its trigger is a Button with slot="trigger", variant="ghost", size="sm" and className="w-full justify-start", and its children go in a CollapsibleContent.
- FileTreeFile renders a Button with variant="ghost" and size="sm" that calls onSelect with its path from onPress. If a file has FileTreeActions, render them next to the Button, not inside it.
- Keep FileTree with its expanded and selection state, and keep FileTreeIcon, FileTreeName and FileTreeActions as they are.
3. Remove the imports the file no longer uses.
4. Run the type checker and fix any errors.FAQ
Should I use File Tree or Tree View to show an agent's changed files?
Use File Tree for the handful of files one turn touched. Its folders start open and every row is a plain button, so the reader sees each changed path at a glance and clicks the one they want.
Move to a Tree View when the tree is something to browse: a whole project, hundreds of rows, children loaded on demand or several files selected at once. Tree View is one Tab stop with arrow keys between its rows, which File Tree doesn't have.
How do I turn a flat list of file paths into a File Tree?
Split each path on the slash and walk its parts, adding a folder node for each part that isn't there yet and a file node for the last one. The toTree function in the AI SDK section does this, and its nodes have the children, name, path and type fields the tree renders.
A path that comes twice still makes one row, because toTree walks a Set of the paths, and the rows keep the order the paths arrive in. Sort the list first for an alphabetical tree.
How do I show which file is selected in the File Tree?
Keep the picked path in state and compare each file's path with it. The demo starts on button.tsx, gives that row the accent background and sets aria-current on it, so a screen reader hears that it is the current one and not only a shaded row.
What picking a file does next is up to you. To show the file itself, put its contents beside the conversation in a Code Block and swap them when the pick changes.
How do I make File Tree folders start closed?
Remove defaultExpanded from the folder's Collapsible, which the demo sets on every folder. Without it, a folder starts closed and shows only its name until someone opens it.
To open only the folders on the way to one file, set that prop to a condition, true where that file's path starts with the folder's path and a slash. For an agent's changes, open is the better default, since people came to see the files the turn produced.
How do I add Expand all and Collapse all buttons to a File Tree?
Hold the open folders in state, as a Set of their paths. Give each folder's Collapsible its isExpanded value from the set, and add or remove the folder's path in onExpandedChange, which receives the new state as a boolean.
Expand all then fills the set with every folder's path, which one walk over the nodes collects, and Collapse all empties it.
Can people move through a File Tree with the arrow keys?
No. Every folder and file is its own button, so Tab moves through them in order and Enter or Space opens a folder or picks a file. The files inside a closed folder are out of the tab order until it opens.
That is quick enough for the few files one turn changes. When people need to move through a large tree with the arrow keys, use a Tree View, which keeps the whole tree as one Tab stop.
Does the File Tree work in right-to-left languages?
Yes, through two classes in its code. Each folder's children are indented with ms-4, a margin on the inline start that moves to the right on a right-to-left page.
The chevron has [&:dir(rtl)]:-scale-x-100, which mirrors it so a closed folder's chevron still points at its name, and an open folder's chevron points down in both directions. Nothing in the tree sets a direction of its own, so it follows the dir of the page.
Is this the same as AI SDK Elements' File Tree?
It does the same job without a wrapper of its own. AI SDK Elements ships FileTreeFolder and FileTreeFile as components, and here a folder is a Collapsible and a file is a Button, in code you own.
The Migrating from AI SDK Elements section has a prompt that rebuilds the old file on these parts and keeps its exports, so the code that imports it doesn't change.
API Reference
File Tree has no props of its own. Each folder is a Collapsible, with defaultExpanded and its trigger and content from Collapsible. Each folder's trigger and each file is a Button, with variant and size from Button.














