Data display
TreeView
Free15.2 KB gzippedA hierarchical list (WAI-ARIA Tree View pattern) for file explorers, org charts, and nested categories. Data-driven: pass items as a tree of TreeNodes.
Accessibility: WCAG 2.2 AA · No A or AA violations in light or dark mode · Keys: Arrow Down, Arrow Up, Arrow Right and 9 more · Full report
Import
import { TreeView } from "@vjdesigns/vjdesigns-ui";
Importing only TreeView adds about 15.2 KB of minified, gzipped JavaScript to your app (React not included). The stylesheet is shared by every component and loaded once: 75.1 KB gzipped.
Examples
File tree
Arrow keys move and expand, typing jumps to a matching item, and icon can depend on the node's state.
- src
- components
- App.tsx
- main.tsx
- package.json
- README.md
Selected: app
import { useState } from "react";
import {
FileIcon,
FolderIcon,
FolderOpenIcon,
Stack,
Text,
TreeView,
type TreeNode,
} from "@vjdesigns/vjdesigns-ui";
const files: TreeNode[] = [
{
id: "src",
label: "src",
children: [
{
id: "components",
label: "components",
children: [
{ id: "button", label: "Button.tsx" },
{ id: "card", label: "Card.tsx" },
],
},
{ id: "app", label: "App.tsx" },
{ id: "main", label: "main.tsx" },
],
},
{ id: "package", label: "package.json" },
{ id: "readme", label: "README.md" },
];
export function FileTree() {
const [selected, setSelected] = useState<string[]>(["app"]);
const withIcons = (nodes: TreeNode[]): TreeNode[] =>
nodes.map((node) => ({
...node,
icon: node.children ? (state) => (state.expanded ? <FolderOpenIcon /> : <FolderIcon />) : <FileIcon />,
children: node.children && withIcons(node.children),
}));
return (
<Stack gap={3} style={{ width: "100%", maxWidth: "20rem" }}>
<TreeView
aria-label="Project files"
items={withIcons(files)}
defaultExpanded={["src"]}
selected={selected}
onSelectedChange={setSelected}
/>
<Text size="sm" tone="muted">
Selected: <code>{selected.join(", ")}</code>
</Text>
</Stack>
);
}
Checkboxes
checkboxes with selectionMode="multiple" gives tri-state checkboxes that cascade to children.
- src
- components
- Button.tsx
- Card.tsx
- App.tsx
- main.tsx
- package.json
- README.md
import { TreeView, type TreeNode } from "@vjdesigns/vjdesigns-ui";
const files: TreeNode[] = [
{
id: "src",
label: "src",
children: [
{
id: "components",
label: "components",
children: [
{ id: "button", label: "Button.tsx" },
{ id: "card", label: "Card.tsx" },
],
},
{ id: "app", label: "App.tsx" },
{ id: "main", label: "main.tsx" },
],
},
{ id: "package", label: "package.json" },
{ id: "readme", label: "README.md" },
];
export function Checkboxes() {
return (
<div style={{ width: "100%", maxWidth: "20rem" }}>
<TreeView aria-label="Files to sync" items={files} selectionMode="multiple" checkboxes defaultExpanded={["src", "components"]} />
</div>
);
}
Accessibility
A tree is one Tab stop: arrow keys move, open and close; Home and End jump; typing jumps by name. With checkboxes, checking a folder checks everything in it, and a partly checked folder is announced as "mixed".
- src
- components
- App.tsx
- main.tsx
- package.json
- README.md
import { TreeView, type TreeNode } from "@vjdesigns/vjdesigns-ui";
const files: TreeNode[] = [
{
id: "src",
label: "src",
children: [
{
id: "components",
label: "components",
children: [
{ id: "button", label: "Button.tsx" },
{ id: "card", label: "Card.tsx" },
],
},
{ id: "app", label: "App.tsx" },
{ id: "main", label: "main.tsx" },
],
},
{ id: "package", label: "package.json" },
{ id: "readme", label: "README.md" },
];
export function Accessibility() {
return (
<div style={{ width: "100%", maxWidth: "22rem" }}>
<TreeView aria-label="Folders to sync" items={files} selectionMode="multiple" checkboxes defaultExpanded={["src"]} defaultSelected={["components"]} />
</div>
);
}
Translated
In Hebrew the tree mirrors: nesting indents from the right, and the arrow keys that open and close folders follow the reading direction. The labels are your own text.
- מסמכים
- חשבוניות
- חוזים
- תמונות
import {
TreeView,
type TreeNode,
LocaleProvider,
} from "@vjdesigns/vjdesigns-ui";
import he from "@vjdesigns/vjdesigns-ui/locales/he";
export function Translated() {
const items: TreeNode[] = [
{
id: "docs",
label: "מסמכים",
children: [
{ id: "invoices", label: "חשבוניות" },
{ id: "contracts", label: "חוזים" },
],
},
{ id: "photos", label: "תמונות" },
];
return (
<LocaleProvider locale="he" messages={he}>
<div style={{ width: "100%", maxWidth: "22rem" }}>
<TreeView aria-label="קבצים" items={items} defaultExpanded={["docs"]} />
</div>
</LocaleProvider>
);
}
Usage notes
One tab stop (roving tabindex). Arrows move, open, and close; Home and End jump; typing jumps by name; * opens all siblings; Enter activates; Space selects. Supports single, multiple, and tri-state checkbox selection, and children loaded on demand.
<TreeView aria-label="Files" items={files} onAction={(id) => open(id)} />
API reference
TreeView
| Prop | Type | Default |
|---|---|---|
itemsRequiredThe nodes to show. Each node has a unique | TreeNode[] | — |
expandedIds of the open parent nodes (controlled). | string[] | — |
defaultExpandedIds of the parent nodes open at first render (uncontrolled). | string[] | [] |
onExpandedChangeCalled with every open id whenever a node is expanded or collapsed. | (ids: string[]) => void | — |
selectionModeHow nodes can be selected. See | "none""single""multiple" | "single" |
selectedIds of the selected (or, with | string[] | — |
defaultSelectedIds selected at first render (uncontrolled). | string[] | [] |
onSelectedChangeCalled with every selected id whenever the selection changes. With | (ids: string[]) => void | — |
checkboxesWith | boolean | false |
onActionCalled when a node is activated: Enter, or a click in | (id: string, node: TreeNode) => void | — |
loadChildrenLoads the children of a node marked | (node: TreeNode) => Promise<TreeNode[]> | — |
renderLabelCustom label content. Receives the node and its state. Keep it non-interactive: the whole row is one control. | (node: TreeNode, state: TreeNodeState) => ReactNode | — |
sizeRow height, text size, and indentation. | "sm""md""lg" | "md" |
loadingLabelText of the loading row and its announcement. | string | "Loading…" |
errorLabelText of the error row when | string | "Couldn't load items. Collapse and expand to try again." |
aria-label | string | — |
aria-labelledby | string | — |
Also accepts standard HTML attributes, plus className, style and ref.
See what changed in each version in the Changelog.
Accessibility report
TreeView targets WCAG 2.2 AA. Below: a live axe-core audit of this page's examples, Lighthouse scores for the page in both modes, the accessibility behavior built into the component, and the checks only a person can do.
axe-core audit (live)
Checking the examples above…
Lighthouse audit
Light mode
Score 10035 passed · 1 failed · 29 not applicable · 10 to check by hand
Elements with visible text labels do not have matching accessible names.
Visible text labels that do not match the accessible name can result in a confusing experience for screen reader users. 5 elements.
div._preview_d5du8_33 > div._root_1737j_15 > ul._root_lahq7_13 > li._item_lahq7_117div._preview_d5du8_33 > div > ul._root_lahq7_13 > li._item_lahq7_117ul._root_lahq7_13 > li._item_lahq7_117 > ul._group_lahq7_151 > li._item_lahq7_117div._preview_d5du8_33 > div > ul._root_lahq7_13 > li._item_lahq7_117div._root_sa02l_2 > div > ul._root_lahq7_13 > li._item_lahq7_117
Dark mode
Score 10035 passed · 1 failed · 29 not applicable · 10 to check by hand
Elements with visible text labels do not have matching accessible names.
Visible text labels that do not match the accessible name can result in a confusing experience for screen reader users. 5 elements.
div._preview_d5du8_33 > div._root_1737j_15 > ul._root_lahq7_13 > li._item_lahq7_117div._preview_d5du8_33 > div > ul._root_lahq7_13 > li._item_lahq7_117ul._root_lahq7_13 > li._item_lahq7_117 > ul._group_lahq7_151 > li._item_lahq7_117div._preview_d5du8_33 > div > ul._root_lahq7_13 > li._item_lahq7_117div._root_sa02l_2 > div > ul._root_lahq7_13 > li._item_lahq7_117
Lighthouse 13.5.0 audits the whole docs page (header and sidebar included), not just the examples, in English. Last run 11/27/2026. Some failed audits carry no weight in the score, so a page can score 100 and still list one.
Built-in behavior
Keyboard
| Key | Action |
|---|---|
| Arrow DownArrow Up | Moves to the next or previous visible item. |
| Arrow Right | Opens a closed parent, or moves to its first child (mirrored right to left). |
| Arrow Left | Closes an open parent, or moves to the parent (mirrored right to left). |
| HomeEnd | Moves to the first or last visible item. |
| Enter | Activates the item (onAction), or toggles a parent. |
| Space | Selects the item (toggles it in multiple selection). |
| * | Opens every sibling parent at this level. |
| Typing | Moves to the next item that starts with the typed characters. |
| Shift+Arrow DownShift+Arrow Up | Multiple selection: extends the selection. |
| Ctrl+A | Multiple selection: selects all. |
One tab stop: Tab lands on the selected item (or the first) and leaves the tree.
Roles and states
| Element | Role | States and properties |
|---|---|---|
<ul> | tree | aria-label, aria-multiselectable |
| Item | treeitem | aria-expanded (parents), aria-selected or aria-checked, aria-level, aria-posinset, aria-setsize |
| Children | group | — |
Manual checks
Automated tools find roughly a third of accessibility issues. Test these in your app, with your content.
| Check | What to test | WCAG 2.2 |
|---|---|---|
| Keyboard | Every action works with Tab, Shift+Tab, Enter, Space and the arrow keys, and focus never gets stuck. | 2.1.1 (A), 2.1.2 (A) |
| Focus | Focus moves in a logical order, is always visible, and isn't hidden behind sticky headers. | 2.4.3 (A), 2.4.7 (AA), 2.4.11 (AA), 2.4.13 (AAA) |
| Screen readers | Names, roles and states are announced correctly, and status changes are announced without moving focus. | 1.3.1 (A), 4.1.2 (A), 4.1.3 (AA) |
| Color | Meaning never depends on color alone; your own colors meet contrast in both modes. | 1.4.1 (A), 1.4.3 (AA), 1.4.11 (AA), 1.4.6 (AAA) |
| Zoom and reflow | At 200% text size and at 320px wide (400% zoom), nothing is cut off and no sideways scrolling is needed. | 1.4.4 (AA), 1.4.10 (AA), 1.4.12 (AA) |
| Pointer targets | Click and tap targets are at least 24×24px (44×44px for AAA), or spaced so they don't overlap. | 2.5.8 (AA), 2.5.5 (AAA) |
| Motion | Animations respect the reduced-motion setting. | 2.3.3 (AAA) |