Tree
A nested list of items that open and close, with one selected item.
SourceStyles source@monoline/react 0.4.0Markdown for AI toolsstable
- Built on native elements
- role=tree with treeitem children and role=group for branches; aria-expanded on branches, aria-selected on every item, aria-level on each.
- Roving tabindex: one item is in the tab order; Tab enters and leaves the tree as one stop.
- ArrowUp/ArrowDown move, ArrowRight opens a branch then enters it, ArrowLeft closes it then goes to the parent, Home and End jump.
- Styles in one file:
tree.css
Import
import '@monoline/css';
import {Tree} from '@monoline/react';
Not installed yet? Pick a route.
Copies the source and its styles into your project.
npx monoline add tree
Usage
import {Tree, type TreeNode} from '@monoline/react';
const items: TreeNode[] = [
{id: 'src', label: 'src', children: [
{id: 'components', label: 'components', children: [{id: 'button', label: 'button.tsx'}, {id: 'tree', label: 'tree.tsx'}]},
{id: 'index', label: 'index.ts'},
]},
{id: 'docs', label: 'docs', children: [{id: 'readme', label: 'README.md'}]},
{id: 'pkg', label: 'package.json'},
];
<Tree aria-label="Project files" items={items} defaultExpanded={['src', 'components']} />
Examples
File tree
Controlled
Props
TreeProps
| Prop | Type | Default | Description |
|---|---|---|---|
items required | TreeNode[] | – | The top-level nodes; each node has an `id`, a `label` and optional `children`. |
value | string | – | Selected node id. Controlled with `value` + `onValueChange`, or uncontrolled with `defaultValue`. |
defaultValue | string | – | The id of the node selected at first when uncontrolled. Default none. |
onValueChange | ((id: string) => void) | – | Called with the id of the node the user selects. |
expanded | string[] | – | Open branches. Controlled with `expanded` + `onExpandedChange`, or uncontrolled with `defaultExpanded`. |
defaultExpanded | string[] | [] | Ids of the branches open at first when uncontrolled. Default none. |
onExpandedChange | ((ids: string[]) => void) | – | Called with the full list of open branch ids when the user opens or closes a branch. |
aria-label required | string | – | Required: names the tree. |
Data Attributes
State is exposed through attributes. Style it with these selectors, not with class toggles.
| Attribute | Stylesheet |
|---|---|
[aria-disabled="true"] | tree.css |
[data-selected] | tree.css |
CSS classes
| Class | Stylesheet |
|---|---|
.tree | tree.css |
.tree-group | tree.css |
.tree-item | tree.css |
.tree-row | tree.css |
.tree-twisty | tree.css |
Customization
Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.
app.css
.tree {
border-color: var(--accent);
}
.tree[aria-disabled="true"] {
background: var(--wash);
}
Accessibility
- role=tree with treeitem children and role=group for branches; aria-expanded on branches, aria-selected on every item, aria-level on each.
- Roving tabindex: one item is in the tab order; Tab enters and leaves the tree as one stop.
- ArrowUp/ArrowDown move, ArrowRight opens a branch then enters it, ArrowLeft closes it then goes to the parent, Home and End jump.
- Enter or Space selects; typing letters jumps to the next label that starts with them. The tree needs an aria-label.
Keyboard
| Keys | Action |
|---|---|
| Tab | Enters and leaves the tree as one stop |
| ArrowUp / ArrowDown | Move |
| ArrowRight | Opens a branch then enters it |
| ArrowLeft | Closes it then goes to the parent |
| Home / End | Jump |
| Enter / Space | Selects |