Playground

Tree

A nested list of items that open and close, with one selected item.

  • 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

PropTypeDefaultDescription
items requiredTreeNode[]–The top-level nodes; each node has an `id`, a `label` and optional `children`.
valuestring–Selected node id. Controlled with `value` + `onValueChange`, or uncontrolled with `defaultValue`.
defaultValuestring–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.
expandedstring[]–Open branches. Controlled with `expanded` + `onExpandedChange`, or uncontrolled with `defaultExpanded`.
defaultExpandedstring[][]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 requiredstring–Required: names the tree.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[aria-disabled="true"]tree.css
[data-selected]tree.css

CSS classes

ClassStylesheet
.treetree.css
.tree-grouptree.css
.tree-itemtree.css
.tree-rowtree.css
.tree-twistytree.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

KeysAction
TabEnters and leaves the tree as one stop
ArrowUp / ArrowDownMove
ArrowRightOpens a branch then enters it
ArrowLeftCloses it then goes to the parent
Home / EndJump
Enter / SpaceSelects