Nested items that open and close: files, categories or permissions.
$ pnpm dlx shadcn@latest add https://dashral.com/r/tree-view.jsonInstallation
$ pnpm dlx shadcn@latest add https://dashral.com/r/tree-view.jsonAdds the component, the components it builds on, its npm packages and the theme tokens it uses. Using Dashral often? Add the registry to components.json once:
{
"registries": {
"@dashral": "https://dashral.com/r/{name}.json"
}
}Then add components by name:
$ pnpm dlx shadcn@latest add @dashral/tree-viewUsage
import { TreeView } from "@/components/dashral/tree-view"
<TreeView
defaultExpanded="all"
items={[
{
id: "src",
label: "src",
children: [
{ id: "app", label: "app.tsx" },
{ id: "utils", label: "utils.ts" },
],
},
{ id: "readme", label: "README.md" },
]}
/>Examples
Checkable
checkable ticks leaves; a branch shows ticked or half-ticked from its items, and ticks them all at once.
API reference
TreeView
Also takes every prop of <ul>(opens in a new tab).
| Prop | Type | Default |
|---|---|---|
items | { id, label, textValue?, icon?, openIcon?, suffix?, children?, disabled? }[]The items; children nest. Every id is unique in the tree. Required. | — |
expanded / defaultExpanded | string[] (default also "all")The open branches, controlled or initial. | [] |
onExpandedChange | (expanded: string[]) => voidCalled when a branch opens or closes. | — |
value / defaultValue | string | nullThe selected item, controlled or initial. | null |
onValueChange | (value: string) => voidCalled with the new value. | — |
checkable | booleanA checkbox on every row instead of selection. | false |
checked / defaultChecked | string[]The ticked leaves (items without children), controlled or initial. | [] |
onCheckedChange | (checked: string[]) => voidCalled with the ticked leaves. | — |
filter | stringShows the items whose text holds it, opens the branches leading to them and marks the match. | — |
empty | ReactNodeShown when the filter matches nothing. | "No matches" |
lines | booleanGuide lines down each open branch. | true |
classNames | { item, row, icon, label, suffix, group, empty }Classes for each part. | — |
className | stringExtra classes, merged with cn() so yours win. | — |
Keyboard
| Keys | Action |
|---|---|
| ↓or↑ | Move to the next or previous row |
| → | Open a branch, or move to its first item |
| ← | Close a branch, or move to its parent |
| HomeorEnd | Move to the first or last row |
| EnterorSpace | Select the item, or tick it when checkable |
| * | Open every branch beside this one |
| A–Z | Jump to the next row starting with that letter |
Accessibility
- Follows the WAI-ARIA tree pattern: a tree of treeitems with aria-level, aria-setsize, aria-posinset and aria-expanded, and one row in the tab order.
- Each row is named by its label only, not by the items inside it. Checkable rows have aria-checked, with "mixed" for a half-ticked branch.
- Closed branches are inert, so nothing in them can be reached.
Motion
- Branches slide open and closed by their content's height and fade (--duration-standard); the chevron turns on the spring. Instant with reduced motion.