Tree

A hierarchy of items that open and close, such as files and folders.

  • src
    • components
      • button.tsx
      • card.tsx
      • dialog.tsx
    • app.tsx
    • main.tsx
  • package.json
  • README.md

Installation

  1. Add the registryOnce per project
  2. Add tree

Usage

Give Tree an aria-label, and each TreeItem a value that's unique within the tree. An item with TreeItems inside it is a folder that opens and closes, and children={[]} makes an empty folder. Clicking an item selects it, and clicking a folder also opens or closes it. Clicking only the caret opens or closes it without selecting it.

The tree is a single stop in the tab order, which lands on the selected item, or on the folder that holds it while that folder is closed. Inside it, the up and down arrows move between items, right opens a folder or moves into it, and left closes it or moves to its parent. Home and End jump to the first and last items, typing jumps to the next item whose label starts with what's typed, and * opens every folder beside the current one. Enter or Space selects an item, and Enter also opens or closes a folder.

Composition

Examples

From data

Render a nested array recursively, using each item's path as its value. Items can come from your own components, as long as each renders a TreeItem. A closed folder keeps its items in the page, hidden, so the tree always knows where the selected item is.

  • docs
    • getting-started.md
  • LICENSE

Controlled

Pass expanded and onExpandedChange to control which folders are open, and value and onValueChange to control which item is selected.

  • Design
    • Icons

Selected: design/components/buttons

Without guides

Lines run down each open folder's items, and the line beside the selected item darkens. Set guides={false} to leave just the indentation.

  • Workspace
    • General
    • Members
    • Billing
  • Integrations

With counts

end places text at the end of an item, such as a count. Keep it to text and badges, since the row is a single control and nothing inside it can take focus.

  • Inbox12
    • Receipts3
    • Travel
  • Starred
  • Drafts1
  • Sent
  • Archive

Long labels

A text label that doesn't fit ends in an ellipsis, and hovering it shows the whole label in a tooltip. For file names, pass a Truncate with position="middle" as the label instead, so they're cut in the middle and their extensions stay in sight.

  • Exports from the October board meeting
    • q3-board-update-final-v4-with-appendix.pdf
    • customer-interview-transcripts-batch-2.docx
    • homepage-hero-illustration-dark@2x.png
    • README.md

Disabled

A disabled item can still be reached with the arrow keys, but it can't be selected, and a disabled folder can't be opened.

  • Reports
    • Q1 summary
    • Q2 summary

Props

The tables leave out className, style and render. Parts built on Base UI take them as its styling and composition guides describe.

Tree

TreeItem