Examples
Expandable hierarchy
Arrow keys move through visible nodes; Right and Left expand or return to a parent.
Company teams tree
import { Tree } from "@phuctech/ui/components/tree";
const nodes = [
{ id: "engineering", label: "Engineering", children: [{ id: "frontend", label: "Frontend" }, { id: "platform", label: "Platform" }] },
{ id: "design", label: "Design", children: [{ id: "product-design", label: "Product design" }] },
];
export function Example() {
return <Tree label="Company teams" nodes={nodes} defaultExpandedKeys={["engineering"]} />;
}
Multiple selection
Select several nodes while keeping the expansion state separate.
Multiple-selection tree
import { Tree } from "@phuctech/ui/components/tree";
const nodes = [{ id: "engineering", label: "Engineering", children: [{ id: "frontend", label: "Frontend" }, { id: "platform", label: "Platform" }] }, { id: "design", label: "Design" }];
export function Example() {
return <Tree label="Teams to notify" nodes={nodes} selectionMode="multiple" defaultExpandedKeys={["engineering"]} />;
}
Unavailable node
A disabled node remains readable but cannot be selected.
Tree with a disabled project
import { Tree } from "@phuctech/ui/components/tree";
const nodes = [{ id: "active", label: "Active project" }, { id: "archived", label: "Archived project", disabled: true }];
export function Example() {
return <Tree label="Projects" nodes={nodes} />;
}
Large visible tree
Trees with more than 200 visible items use a bounded virtual window while preserving active-descendant navigation.
Virtualized tree with 250 departments
import { Tree } from "@phuctech/ui/components/tree";
const departments = Array.from({ length: 250 }, (_, index) => ({
id: `department-${index + 1}`,
label: `Department ${index + 1}`,
}));
export function Example() {
return <Tree label="Large department list" nodes={departments} className="max-h-96" />;
}
Try the tree
Inspect an expandable navigation hierarchy.
Tree property playground
import { Tree } from "@phuctech/ui/components/tree";
const nodes = [{ id: "workspace", label: "Workspace", children: [{ id: "projects", label: "Projects" }, { id: "settings", label: "Settings" }] }];
export function Example() {
return <Tree label="Workspace navigation" nodes={nodes} defaultExpandedKeys={["workspace"]} />;
}
Usage
import { Tree } from "@phuctech/ui/components/tree";
<Tree label="Company teams" nodes={teamTree} defaultExpandedKeys={["engineering"]} />Behavior
- The tree container uses one tab stop and keeps the active item in
aria-activedescendant. - Up/Down move through visible nodes; Home/End move to the first/last visible node.
- Right expands a closed branch or enters its first child. Left collapses a branch or moves to its parent.
- Enter or Space selects the active item. A disabled node is readable and expandable but cannot be selected.
- Expansion and selection each support controlled and uncontrolled state. When more than 200 items are visible, Tree renders a fixed 36px-row virtual window inside a bounded 24rem scroll region; keyboard navigation scrolls the active item into view.
API
Accessibility
Small trees use nested treeitem and group roles; virtualized trees expose flat treeitem rows with aria-level, aria-posinset, and aria-setsize. Both modes keep the active descendant mounted and expose a node-specific disclosure name. Expansion remains separate from selection.
Related
- TreeSelect places the same hierarchy inside a searchable selection popup.
- Cascader selects one path when only one level should be visible at a time.