Skip to content

File Tree

A nested list of folders and files for browsing and previewing project files.

tools/forecast.yamlYAML · 8 lines
identity:
  name: forecast
parameters:
  - name: city
    type: string
    required: true
  - name: days
    type: number
hero.tsx

Usage guidelines

  • Use it to browse and preview files: Folders are disclosure buttons and files are buttons. The feature owns data loading, routing, renaming, drag and drop and per-item actions.
  • It is not an ARIA tree: FileTree renders a labelled <section> of nested lists, not role="tree". Every row is a separate tab stop, and arrow keys, Home, End and typeahead do not move between rows. Keep trees short enough to tab through, or add a search or filter next to large ones.
  • Name the tree: Give FileTree an aria-label or aria-labelledby that says what the files belong to, such as "Weather plugin files".
  • Mark the open file with selected: selected sets aria-current="true" and the highlighted row style. Keep one selected file, driven by the feature's state or route.
  • A file click previews, it does not navigate: FileTreeFile renders a <button>. Handle onClick to open the preview; do not render a link through it.
  • Use disabled for entries that cannot be opened: A disabled folder keeps its open state but cannot be toggled, and a disabled file leaves the tab order.
  • Let the context set the indent: Nesting FileTreeFolderPanel raises the level and draws one guide per level. Pass level only when rendering a row outside its folder, such as a virtualized flat list.

Anatomy

tsx
tsx

FileTreeFolder is a Base UI Collapsible root rendered as <li>; FileTreeFile wraps its button in an <li>. FileTreeIcon is decorative, so the row's name comes from FileTreeLabel and any meta or badge text. Rows render one FileTreeGuide per nesting level themselves; render it directly only in a custom row.

Examples

Rendering from data

Render folders and files recursively from the feature's data. FileTreeMeta adds a short status after the label and FileTreeBadge a compact value such as a size. Both become part of the row's accessible name.

data-driven.tsx

Controlled folders

Pass open and onOpenChange to FileTreeFolder when something outside the tree opens folders, such as an expand-all action or revealing the file that is open in the editor.

Workflow files

controlled.tsx

Disabled entries

A disabled folder keeps its current open or closed state and cannot be toggled, while a disabled file cannot be opened. Explain why with FileTreeMeta when the reason is not obvious.

disabled.tsx

Keyboard

KeyAction
TabMoves focus to the next visible folder or file. Disabled folders and files are skipped.
Shift+TabMoves focus to the previous visible folder or file.
EnterorSpaceOn a folder, expands or collapses it. On a file, activates it.

API reference

FileTree

A composable hierarchical file list built with Base UI Collapsible. Renders a <section> element.

Folders are disclosure buttons and files are preview buttons; feature code owns data loading, routing, and item actions.

Base UI FileTree docs

FileTreeBadge

FileTreeFile

Props
PropTypeDefaultDetails
levelnumberNo default
selectedbooleanfalse
Base UI FileTreeFile docs
Data attributes
AttributeDescription
data-selected

Present when the file is the selected row.

data-disabled

Present when the file is disabled.

FileTreeFolder

Groups all parts of the collapsible. Renders a <div> element.

Base UI Collapsible.Root docs

Data attributes
AttributeDescription
data-open

Present when the collapsible is open.

data-closed

Present when the collapsible is closed.

data-starting-style

Present when the collapsible begins animating in.

data-ending-style

Present when the collapsible is animating out.

FileTreeFolderPanel

A panel with the collapsible contents. Renders a <div> element.

Base UI Collapsible.Panel docs

Data attributes
AttributeDescription
data-open

Present when the collapsible panel is open.

data-closed

Present when the collapsible panel is closed.

data-starting-style

Present when the panel begins animating in.

data-ending-style

Present when the panel is animating out.

CSS variables
VariableDescription
--collapsible-panel-height

Values: number

The collapsible panel's height.

--collapsible-panel-width

Values: number

The collapsible panel's width.

FileTreeFolderTrigger

A button that opens and closes the collapsible panel. Renders a <button> element.

Base UI Collapsible.Trigger docs

Props
PropTypeDefaultDetails
levelnumberNo default
Base UI Collapsible.Trigger docs
Data attributes
AttributeDescription
data-panel-open

Present when the collapsible panel is open.

FileTreeGuide

FileTreeIcon

Props
PropTypeDefaultDetails
type'archive' | 'code' | 'database' | 'file' | 'folder' | 'image' | 'json' | 'markdown' | 'pdf' | 'table' | 'text''file'
childrenReact.ReactNodeNo default
Base UI FileTreeIcon docs

FileTreeLabel

FileTreeList

FileTreeMeta