Search documentation

Search pages, components, blocks, styles and colours.

Get started
Skip to content

File Tree

PreviousNext

A compact file explorer with folder guides, file-type icons, keyboard navigation, and single or multiple selection.

Explorer
apps
web
app
layout.tsx
page.tsx
globals.css
route.ts
not-found.tsx
.env.local
next.config.mjs
package.json
tsconfig.json
README.md
.gitignore
bun.lock
package.json
README.md
Select a file to get started⌘ / Ctrl-click to select more. Shift-click for a range.

File Tree is part of the Fabricator style. It uses the same HTML implementation in Base UI, Radix and React Aria projects. Rows follow the region's density: 28px rows, 13px labels, 16px icons and 16px indentation at the default density.

Installation

bunx --bun fabricator-ui@latest add file-tree

Usage

import { FileTree, type FileTreeNode } from "@/components/ui/file-tree"
 
const nodes: FileTreeNode[] = [
  {
    id: "src",
    name: "src",
    children: [
      { id: "src/app.tsx", name: "app.tsx" },
      { id: "src/styles.css", name: "styles.css" },
    ],
  },
  { id: "package.json", name: "package.json" },
]
 
<FileTree
  aria-label="Project files"
  nodes={nodes}
  defaultExpandedIds={["src"]}
  onOpen={(node) => openFile(node.id)}
/>

Every ID must be unique across the tree. Full relative paths work well. A node with children, including an empty array, is a folder. Omit children for a file. The component preserves the order supplied by the caller.

Examples

Single selection

Single selection is the default. Clicking a file selects it; double-click or Enter calls onOpen. Clicking a folder toggles it; clicking its chevron only changes expansion.

src
app.tsx
styles.css
package.json

No file open. Double-click or press Enter to open.

Multiple selection and delete

Use selectionMode="multiple" for independent selection with Command/Ctrl-click and ranges with Shift-click. Command/Ctrl+Shift-click adds a range to the current selection. Selected children stay selected when their folder collapses.

The first demo controls both selection and expansion and includes Delete, Collapse all and Undo. onDelete receives the selected nodes, with selected descendants suppressed when their ancestor is also selected. It includes selected files inside collapsed folders and skips disabled nodes and stale IDs. Deleting a folder requests its entire subtree.

File Tree never writes to the filesystem or removes data itself. Persist the action in your app, confirm permanent deletion, or offer Undo. The installed @/lib/file-tree-model exports getFileTreeDeleteNodes(nodes, ids) for toolbar actions and removeFileTreeNodes(nodes, ids) for removing in-memory data without mutating the original tree.

Icons and muted files

Folder names and file extensions choose the default icon. Project folders have filled folder icons with a small badge. muted quiets ignored or generated items without disabling them; disabled prevents selection, opening and expansion, including for descendants.

Supply renderIcon to use your own icon pack. Return an icon element; the component handles its size and hides it from assistive technology. FileTreeIcon is also exported for falling back to the default icon:

<FileTree
  nodes={nodes}
  renderIcon={(node, expanded) =>
    node.name.endsWith(".tsx") ? (
      <ReactLogo />
    ) : (
      <FileTreeIcon node={node} expanded={expanded} />
    )
  }
/>

Import FileTreeIcon from @/components/ui/file-tree. Give the icon its own semantic colour if needed. Use emptyContent to compose an Empty state when there are no nodes.

API Reference

FileTree

Extends the props of a <div>, excluding children and onSelect.

PropTypeDefault
nodesreadonly FileTreeNode[]Required
selectionMode"single" | "multiple" | "none""single"
selectedIdsreadonly string[]Uncontrolled
defaultSelectedIdsreadonly string[][]
onSelectionChange(ids: string[]) => void-
expandedIdsreadonly string[]Uncontrolled
defaultExpandedIdsreadonly string[][]
onExpandedChange(ids: string[]) => void-
onOpen(node: FileTreeNode) => void-
onDelete(nodes: FileTreeNode[]) => void-
onNodeContextMenu(node, event) => void-
renderIcon(node, expanded) => ReactNode-
emptyContentReactNode-
disabledbooleanfalse

Set aria-label or aria-labelledby to name the tree. onNodeContextMenu selects an unselected target before calling the callback; right-clicking an existing selection preserves it. Call event.preventDefault() in your handler when providing a custom menu. Compose a Context Menu for touch users or additional actions. Renaming, moving and loading folder contents belong to the caller.

FileTreeNode

PropertyTypeMeaning
idstringUnique, stable identifier
namestringVisible name and typeahead text
childrenreadonly FileTreeNode[]Present for folders
disabledbooleanDisables this subtree
mutedbooleanQuiets this row visually

Data attributes

The parts expose data-slot="file-tree", file-tree-item, file-tree-guide, file-tree-chevron, file-tree-icon and file-tree-name. Items expose data-selected, data-disabled and data-muted. Chevrons and icons expose data-expanded; icons also expose data-kind and data-folder.

Icons

File Tree installs @fabricator-ui/icons and uses its Files pack for file-type and named-folder artwork, including expanded folders. Colours follow the theme independently of the project's icon library. Use renderIcon(node, expanded) to replace it. The automatic lookup includes all drawings and associations, about 138 KB minified and gzipped; see Files.

Accessibility

The tree uses roving focus with explicit levels, positions and sibling counts. Disabled rows are skipped. Focus and selection are distinct in multiple-selection mode, so moving the focus preserves the selection.

KeyAction
Up / DownFocus the previous / next visible enabled row
RightExpand a folder, then focus its first enabled child
LeftCollapse a folder, or focus its parent
Home / EndFocus the first / last enabled row
EnterToggle a folder or open a file
SpaceSelect; toggle in multiple-selection mode
Shift+Up / Down / Home / EndExtend a selection range
Command/Ctrl+ASelect all visible enabled nodes in multiple-selection mode
EscapeClear selection
Delete / BackspaceRequest deletion through onDelete
LettersFocus a matching visible name; repeated letters cycle matches
*Expand sibling folders

Single-selection mode selects as keyboard focus moves, unless Command/Ctrl is held. Expansion is immediate; there is no animation to wait through during repeated navigation.

Platforms

Available for all three web bases. React Native is deferred and recorded in the native parity exceptions.