Skip to content

File Tree

Shows a folder structure that opens and closes, with one file selected.

Installation

pnpm dlx shadcn@latest add @silkware/ai-file-tree

To copy the pattern by hand, install the components it composes:

pnpm dlx shadcn@latest add @silkware/button @silkware/collapsible

Then the package:

pnpm add lucide-react

Copy the source into components/ai-elements/file-tree.tsx:

components/ai-elements/file-tree.tsx
"use client"
import * as React from "react"import { ChevronRightIcon, FileIcon, FolderIcon } from "lucide-react"
import { Button } from "@/components/ui/button"import { Collapsible, CollapsibleContent } from "@/components/ui/collapsible"
type FileNode =  | { type: "file"; name: string; path: string }  | { type: "folder"; name: string; path: string; children: FileNode[] }
const files: FileNode[] = [  {    type: "folder",    name: "app",    path: "app",    children: [      { type: "file", name: "layout.tsx", path: "app/layout.tsx" },      { type: "file", name: "page.tsx", path: "app/page.tsx" },    ],  },  {    type: "folder",    name: "components",    path: "components",    children: [      {        type: "folder",        name: "ui",        path: "components/ui",        children: [          {            type: "file",            name: "button.tsx",            path: "components/ui/button.tsx",          },        ],      },      {        type: "file",        name: "login-form.tsx",        path: "components/login-form.tsx",      },    ],  },]
function FileTreeNode({  node,  selected,  onSelect,}: {  node: FileNode  selected: string  onSelect: (path: string) => void}) {  switch (node.type) {    case "folder":      return (        <Collapsible defaultExpanded className="group/folder">          <Button            slot="trigger"            variant="ghost"            size="sm"            className="w-full justify-start font-normal aria-expanded:bg-transparent aria-expanded:hover:bg-muted dark:aria-expanded:hover:bg-muted/50"          >            <ChevronRightIcon className="transition-transform group-data-expanded/folder:rotate-90" />            <FolderIcon />            {node.name}          </Button>          <CollapsibleContent>            <div className="ml-4 flex flex-col">              {node.children.map((child) => (                <FileTreeNode                  key={child.path}                  node={child}                  selected={selected}                  onSelect={onSelect}                />              ))}            </div>          </CollapsibleContent>        </Collapsible>      )    case "file":      return (        <Button          variant="ghost"          size="sm"          className={`w-full justify-start font-normal ${            selected === node.path ? "bg-accent text-accent-foreground" : ""          }`}          aria-current={selected === node.path ? "true" : undefined}          onPress={() => onSelect(node.path)}        >          <FileIcon />          {node.name}        </Button>      )    default: {      const unreachable: never = node      return unreachable    }  }}
export function FileTreeDemo() {  const [selected, setSelected] = React.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}          selected={selected}          onSelect={setSelected}        />      ))}    </div>  )}

Usage

A tree is a list of nodes, each a file or a folder. A folder is a Collapsible whose trigger is a ghost Button; a file is a ghost button too.

type FileNode =  | { type: "file"; name: string; path: string }  | { type: "folder"; name: string; path: string; children: FileNode[] }
<Collapsible defaultExpanded className="group/folder">  <Button slot="trigger" variant="ghost" size="sm" className="w-full justify-start font-normal">    <ChevronRightIcon className="transition-transform group-data-expanded/folder:rotate-90" />    <FolderIcon />    {node.name}  </Button>  <CollapsibleContent>    <div className="ml-4 flex flex-col">{children}</div>  </CollapsibleContent></Collapsible>

Give the folder button slot="trigger", so the collapsible knows which control opens it. Indent children with the margin on their container, not on each node. The file button sets aria-current on the selected file and calls onPress with its path.

Examples

Changed files

Add short text on the right of a file to say what the agent did to it: "Edited", "New". The word is the signal, so nothing depends on colour.

AI SDK

The file tree is not tied to the AI SDK. It renders a structure you pass in. To show the files an agent touched, return them from a tool and build the tree from the output:

import type { UIDataTypes, UIMessage } from "ai"
type ChatMessage = UIMessage<  unknown,  UIDataTypes,  {    listFiles: {      input: { directory: string }      output: { files: string[] }    }  }>
if (part.type === "tool-listFiles" && part.state === "output-available") {  return toTree(part.output.files).map((node) => (    <FileTreeNode key={node.path} node={node} selected={selected} onSelect={setSelected} />  ))}

toTree is yours: it turns a flat list of paths such as price-lists/q1-2027.csv into nested FileNode values. Selecting a file is local state. To open the file for the person, call your own viewer, or send a message. The scripted streams in @/lib/ai have nothing to do here: the demo uses a fixed tree and local state.

Accessibility

KeyAction
EnterOpens or closes the focused folder, or selects the focused file.
SpaceOpens or closes the focused folder, or selects the focused file.
TabMoves focus to the next folder or file.
ShiftTabMoves focus to the previous folder or file.

This pattern is a column of buttons, not an ARIA tree. It has no tree role and no arrow-key navigation: every folder and file is a tab stop. That is fine for a handful of nodes and tiring for a deep tree.

The composed components provide:

  • Real buttons with a visible focus ring, and aria-expanded on the folder trigger from the React Aria disclosure.
  • aria-current on the selected file, set by the pattern.

You provide:

  • The selection in words when it matters. The selected file has a tinted background and aria-current; a nearby label such as "Viewing components/ui/button.tsx" says it in text.
  • For a large tree, keyboard navigation with arrow keys. Build it with a primitive that has the tree role, or limit the tree to the files the agent touched.
  • A name for each node that makes sense alone. A file named index.tsx is ambiguous; show its path in a tooltip or in the label.

Guidelines

When to use

  • To show the files an agent read, created or changed in a project.
  • To let the person pick one file from a small structure to look at.

When not to use

  • For the app's own navigation. Use Sidebar.
  • For a long, deep tree that people move around by keyboard. Use a primitive with a tree role.
  • For a flat list of results. Use Item or a Table.

Content

  • Use the real names and paths. Do not abbreviate a folder to save space.
  • Show only the files that matter to the answer. Collapse the rest.
  • Say what happened to a file in a word next to it, and keep the word the same everywhere: "Edited", "New".
  • Open the folders on the path to the changed files, and close the others.
Do

Open the folders that hold the changed files and mark each file with a word.

Don’t

Don’t expand every folder in a large project. The files that changed disappear in the list.

Human in the loop

  • Show the files the agent changed, not only the ones it read, so the person can review the footprint of the work.
  • Mark new files and edited files differently. Deleting a file is the strongest change; show deletions and ask first, with Confirmation.
  • Selecting a file in the tree must not change it. Opening is a read; any change goes through an approval.

Composition

ComponentWhat it does here
CollapsibleOpens and closes a folder, with the trigger and panel linked.
ButtonThe folder trigger and the file row, as ghost buttons.

components/ai-elements/file-tree.tsx exports FileTreeDemo, which holds a fixed tree and the selected path in state. FileTreeNode and the FileNode type are in the same file but are not exported; copy them into your app.