# File Tree

> React file tree for editor sidebars with icons by file type, git status letters, spring folder animation, a filter that expands matches and keyboard support.

- Kind: Component (Dev tools)
- Docs: https://ui.minidev.pro/docs/file-tree
- Source (registry JSON with the full file): https://ui.minidev.pro/r/file-tree.json
- License: MIT. One file, yours to edit after install.

## When to use

Use for repos, artifacts and agent workspaces. Folders are nodes with a `children` array (even empty); files have none. Paths passed to `onSelect` are names joined by "/". Keyboard follows the WAI-ARIA tree pattern. For a generic nested list without file semantics use [`TreeView`](https://ui.minidev.pro/docs/tree-view); for a single file citation use [`FileReference`](https://ui.minidev.pro/docs/file-reference).

## Installation

### shadcn CLI (copies the source into your project)

```bash
npx shadcn@latest add @minidev/file-tree
```

Writes `components/ui/file-tree.tsx` and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/file-tree.json`.

### npm package

```bash
npm i minidev-ui-kit lucide-react motion
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

```tsx
import { FileTree } from "@/components/ui/file-tree"
```

From the npm package:

```tsx
import { FileTree } from "minidev-ui-kit/ui/file-tree"
```

## Usage

```tsx
import { FileTree } from "@/components/ui/file-tree"

export function Example() {
  return (
    <FileTree />
  )
}
```

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `data` | `FileTreeNode[]` | `SAMPLE` | no | Nested files and folders. Paths are built from names joined by "/". |
| `defaultExpanded` | `string[]` | `["app", "app/dashboard", "components"]` | no | Folder paths open on first render. |
| `selected` | `string` | - | no | Controlled selected path. |
| `defaultSelected` | `string` | `"app/dashboard/page.tsx"` | no |  |
| `activePath` | `string` | - | no | The file open in the editor. Defaults to the last selected file. |
| `onSelect` | `(path: string) => void` | - | no | Called with the path of a file or folder when it is selected. |
| `searchable` | `boolean` | `true` | no | Show the filter field above the tree. |
| `showStatus` | `boolean` | `true` | no | Show git status letters and folder change dots. |
| `sort` | `boolean` | `true` | no | Sort folders first, then names alphabetically. |
| `title` | `string` | `"lumen-web"` | no | Header title, usually the project name. Pass an empty string to hide the header. |
| `aria-label` | `string` | - | no |  |
| `className` | `string` | - | no |  |

## Examples

### Controlled selection

Hold the selected path in state and sync it with onSelect, which fires for files and folders alike.

```tsx
function ControlledDemo() {
  const [selected, setSelected] = React.useState("app/dashboard/page.tsx")
  return (
    <div className="flex w-full max-w-72 flex-col gap-2">
      <FileTree selected={selected} onSelect={setSelected} />
      <p className="truncate font-mono text-xs text-fg-muted">{selected}</p>
    </div>
  )
}
```

### Custom project

Pass nested data, the folders to open on first render and the file that starts selected; status letters come from each node.

```tsx
const docs: FileTreeNode[] = [
  {
    name: "docs",
    children: [
      { name: "getting-started.md" },
      {
        name: "components",
        children: [{ name: "button.mdx", status: "M" }, { name: "dialog.mdx" }, { name: "tabs.mdx", status: "A" }],
      },
      { name: "tokens.md" },
    ],
  },
  { name: "public", children: [{ name: "logo.svg" }, { name: "og.png" }] },
  { name: "package.json" },
  { name: "README.md" },
]

<FileTree data={docs} title="lumen-docs" defaultExpanded={["docs", "docs/components"]} defaultSelected="docs/components/button.mdx" />
```

### Plain tree

Turn off the filter field, the git status and the header for a quiet picker inside a form or a sheet.

```tsx
// docs is the tree from the "Custom project" example.
<FileTree data={docs} title="" searchable={false} showStatus={false} defaultExpanded={["docs"]} defaultSelected="docs/tokens.md" />
```

## Keyboard interactions

| Key | Action |
| --- | --- |
| `ArrowDown` or `ArrowUp` | Moves to the next or previous visible item. |
| `ArrowRight` | Opens a closed folder. On an open folder, moves to its first child. |
| `ArrowLeft` | Closes an open folder. On a file or closed folder, moves to its parent. |
| `Home` or `End` | Moves to the first or last visible item. |
| `Enter` or `Space` | Selects the item and calls `onSelect`. On a folder it also opens or closes it. |
| `*` | Opens every folder at the same level as the focused item. |
| `A-Z` | Moves to the next item whose name starts with the typed letters. |
| `ArrowDown` | In the filter field, moves focus to the first item in the tree. |
| `Escape` | In the filter field, clears the filter. |

## Accessibility

- The list is a WAI-ARIA `role="tree"` of `role="treeitem"` nodes with `aria-level`, `aria-posinset`, `aria-setsize`, `aria-expanded` on folders and `aria-selected`.
- Git status is part of each item's name, such as "page.tsx, modified". A closed folder with changes inside reads "contains changes". The open file has `aria-current="page"`.
- While filtering, a polite live region announces the number of matches.
- Under reduced motion folders open and close with no height animation.

## FAQ

### How do I mark a node as a folder?

Give it a `children` array, even an empty one: `{ name: "migrations", children: [] }`. A node without `children` is a file. Paths are names joined by "/", such as `app/dashboard/page.tsx`, and that is what `onSelect` receives.

### Can folders load their contents when opened?

No. There is no expand callback, and an empty folder shows no children. Load the whole tree first, or update `data` from `onSelect` when a folder path comes in.

### Why is my file order different from my data?

`sort` is on by default: folders first, then names A to Z. Pass `sort={false}` to keep the order of `data`.

### What does the filter field do?

It keeps names that contain the text plus their parent folders, opens those folders, and highlights the match. Folders cannot be closed while filtering. Pass `searchable={false}` to hide it.

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Dependencies

- npm: `lucide-react`, `motion`

## Related

- [Tree View](https://ui.minidev.pro/docs/tree-view/md): Nested tree view with expandable nodes, rotating chevrons, depth indentation and ARIA tree roles. Suited to file explorers and nested category lists in React.
- [File Reference](https://ui.minidev.pro/docs/file-reference/md): A compact card that cites a source file with its name, monospace path and line range. Useful for code assistants, docs and AI answers that point into a repo.
- [File List](https://ui.minidev.pro/docs/file-list/md): A stacked list of file rows showing each name and metadata such as size or date, with an optional download button per item. Good for React attachment lists.
- [Json Viewer](https://ui.minidev.pro/docs/json-viewer/md): Collapsible JSON tree for React with colored values, counts on folded nodes, copy value and copy path buttons, search highlighting and arrow key navigation.
- [Media Library](https://ui.minidev.pro/docs/media-library/md): A Tailwind media library panel with an upload action and a two column grid of assets showing file name, size and a type badge for image, video, vector or doc.
- [Api Key List](https://ui.minidev.pro/docs/api-key-list/md): React settings panel listing API keys with masked previews, created date badges and a rotate button per key, plus a header action to create a new key.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
