Tree view
A hierarchy that opens and closes: files, categories, a team's structure. It is one tab stop with the arrow keys inside, selects one row, several, or ticks them with tri-state boxes, and loads children when a branch first opens. For one level of links use a sidebar; for raw data use the JSON viewer.
Install
npx shadcn@latest add @thrum-pro/tree-viewThis is a Pro component. The command works once your project has a licence key: set up Pro or see pricing.
Automatically installs icons check, chevron-right, file, folder, folder-open and minus from the icon set.
Data shape
type TreeNode = {
id: string;
label: ReactNode;
textValue?: string;
children?: readonly TreeNode[];
hasChildren?: boolean;
disabled?: boolean;
icon?: ReactNode;
};const files: TreeNode[] = [
{ id: "src", label: "src", children: [{ id: "src/main.tsx", label: "main.tsx" }] },
{ id: "assets", label: "assets", hasChildren: true },
{ id: "readme.md", label: "readme.md" },
];Props
| Prop | Type | Default | Description |
|---|---|---|---|
items | readonly TreeNode[] | Required | The nodes at the top of the tree. A node with children, or with hasChildren, is a branch. |
aria-label | string | – | The tree's name. Or point aria-labelledby at a visible heading. |
expanded | readonly string[] | – | The ids of the open branches, when the parent holds them. |
defaultExpanded | readonly string[] | Default [] | The branches open at first, when the tree holds them itself. |
onExpandedChange | (ids: string[]) => void | – | Called with every open id when a branch opens or closes. |
onExpand | (id: string) => void | – | Called with a branch's id when it opens. Load the children of a hasChildren branch here; a Loading row shows until they arrive. |
selectionMode | "none" | "single" | "multiple" | Default "single" | single selects one row, multiple toggles rows with Space or Ctrl-click, none makes the tree a list of places that reports onAction. |
selected | readonly string[] | – | The selected or ticked ids, when the parent holds them. |
defaultSelected | readonly string[] | Default [] | The ids selected at first, when the tree holds them itself. |
onSelectedChange | (ids: string[]) => void | – | Called with every selected id. With checkboxes these are the ticked leaves. |
checkboxes | boolean | Default false | A tick box on every row. A branch is ticked when all its leaves are and shows a dash when some are. Ticking a branch ticks every leaf under it. |
onAction | (id: string) => void | – | Called on Enter or Space when selectionMode is none, and on a double click on a leaf. |
icons | boolean | Default true | Draw a folder or a file at the start of each row that has no icon of its own. |
guides | boolean | Default true | Draw a line along the children of each open branch. |
sound | boolean | Default true | Play a soft sound when a branch opens or closes and a click when a row is selected, if the visitor has sound on. |
framed | boolean | Default false | Wrap the tree in a rounded bordered card panel that adapts to frosted glass. |
className | string | – | Extra classes for the tree, merged last. |
visibleNodes(), checkState(), toggleChecked(), typeahead() | functions | – | More exports: the rows in view, a node's tick, the ticks after a change, and the typeahead match the tree uses. |
Motion and sound
Motion
- A branch the reader opens fades in and slides down 4 pixels over 150 ms, and its chevron turns a quarter over 150 ms. Branches open when the page loads do not move.
- Closing is immediate. Reduced motion: opening is immediate too.
Sound
- A soft rising tone when a branch opens and a falling one when it closes.
- A click when a row is selected or ticked.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- It follows the WAI-ARIA tree view pattern: role tree with nested groups of treeitems, each with its level, its place among its siblings, and aria-expanded on branches.
- The tree needs a name: an aria-label as in the example, or aria-labelledby.
- One row is in the tab order; the arrow keys move the focus. Selection is aria-selected, and the tree is aria-multiselectable in multiple mode.
- With checkboxes each row carries aria-checked, and mixed for a branch with some leaves ticked. The tick box is drawn, not a separate control, so the row stays one stop.
- A selected row is shaded, set in medium weight and marked by a bar at its start, so it does not rest on colour.
- A branch that is loading holds a status row that says Loading, and its group is aria-busy. Long names are cut with an ellipsis; the row stays one line at 160 pixels.
| Keys | Action |
|---|---|
| Tab | Move into the tree, onto the row last focused or the first selected row, and out again. |
| ↓ / ↑ | Move to the next or previous row in view. |
| → | Open a closed branch; on an open branch, move to its first child. |
| ← | Close an open branch; elsewhere, move to the parent. |
| Home / End | Move to the first or last row in view. |
| A to Z | Move to the next row that starts with the typed letters. |
| * | Open every branch beside the focused row. |
| Enter / Space | Select the row; in multiple mode or with checkboxes, toggle it; with no selection, run onAction. |
Source
The source comes with Pro
TypeScript source · Copy-paste ready · MIT-compatible for client projects · Includes all dependencies
Files and dependencies
- Adds
components/ui/tree-view.tsx- Packages
- None
- Brings along
- Sound engine
- Icons
checkchevron-rightfilefolderfolder-openminus
Changelog
- 1.0.0 · 2026-12-03First release.