Examples

Account settings tree

Preview
  • Settings
  • Profile
  • Notifications
Choose a settings page.
Example.tsx
import { useState } from 'react';
import { Stack, TreeView, type TreeViewNode } from '@reactloom/ui';

const nodes: TreeViewNode[] = [
  { id: 'settings', label: 'Settings', children: [
    { id: 'profile', label: 'Profile' }, { id: 'notifications', label: 'Notifications' },
  ] },
  { id: 'billing', label: 'Billing', children: [{ id: 'invoices', label: 'Invoices' }] },
];

export default function Example() {
  const [selected, setSelected] = useState<string | null>(null);
  return <Stack gap="2">
    <TreeView aria-label="Account settings" nodes={nodes} selectedId={selected}
      defaultExpandedIds={['settings']} onActivate={(node) => setSelected(node.id)} />
    <div role="status">{selected ? `Selected ${selected}` : 'Choose a settings page.'}</div>
  </Stack>;
}

Controlled handbook outline

Preview
Example.tsx
import { useState } from 'react';
import { TreeView } from '@reactloom/ui';

export default function Example() {
  const [expanded, setExpanded] = useState<readonly string[]>([]);
  return <TreeView aria-label="Handbook outline" expandedIds={expanded} onExpandedChange={setExpanded}
    nodes={[
      { id: 'start', label: 'Getting started', children: [{ id: 'access', label: 'Account access' }] },
      { id: 'support', label: 'Support', children: [{ id: 'help', label: 'Contact help desk' }] },
    ]} />;
}

API reference

TreeView props
PropTypeDefault
nodes
IDs and labels with optional child nodes.
readonly TreeViewNode[]—
aria-label
Name the hierarchy.
string—
expandedIds · defaultExpandedIds
Controlled and initial open branches.
readonly string[]—
onExpandedChange
Receives the new expansion state.
(ids: string[]) => void—
selectedId
Externally selected leaf.
string | null—
onActivate
Runs when a leaf is activated.
(node: TreeViewNode) => void—

The root list accepts className, style, and a ref. Set --pl-tree-indent to change the indentation per level.

Keyboard interaction

Keyboard interactions
KeyAction
Up / DownMove through visible nodes
Left / RightCollapse, expand, or move to the parent or first child, mirrored in RTL
Home / EndFocus the first or last visible node
Enter / SpaceToggle a branch or activate a leaf
Type lettersFind a visible node by label

On first tab into the tree, focus starts on the selected leaf if it is visible, or on the first node.

Usage guidelines

Do

  • Keep node IDs unique across the hierarchy. Use the selected leaf to reflect the application’s current destination.

Don’t

  • Avoid using a tree for a single-level link list.