aiellie

Search

Find a component, template or page

GitHub
Components

File Tree

Folders and files a change touched, marked added, changed or removed, walked with the arrow keys.

Open full screen
fig. 01 · open it full screen from the corner

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

  1. Describe the files

    A node with children is a folder. Folders start closed unless defaultOpen is 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.

lib/greet.ts
export const greet = (name: string) => `Hello, ${name}`

API reference

FileTree

PropTypeDefaultDescription
nodesFileNode[]required
selectedstringThe chosen file's path. Its folders open when it changes.
onSelect(path: string) => voidCalled when a file is pressed.
aria-labelstring"Files"

All other div props are forwarded.

FileNode

FieldTypeDefaultDescription
namestringrequired
childrenFileNode[]Makes it a folder.
status"added" | "modified" | "removed"
defaultOpenbooleanOpens a folder to begin with.