TreeView
A nested, expandable list for file trees and hierarchies, with full keyboard navigation.
Status: beta. Category: Data display.
import { TreeView } from '@glyphui/ui'
Exports: TreeView, treeViewStyles.
Notes
- Follows the WAI-ARIA tree pattern: one tab stop, arrow keys to move, Right and Left to expand and collapse, Home and End to jump, a letter to find the next match.
- Enter or Space selects. Clicking a parent also expands or collapses it.
- Give the tree an aria-label that says what it contains.
TreeView props
| Prop | Type | Default | Description |
|---|---|---|---|
aria-label (required) | string | Required: describes the tree, e.g. "Project files". | |
nodes (required) | readonly TreeNode[] | ||
defaultExpanded | readonly string[] | ||
defaultSelected | string | null | ||
expanded | readonly string[] | Ids of expanded nodes (controlled). | |
onExpandedChange | ((expanded: string[]) => void) | ||
onSelect | ((id: string, node: TreeNode) => void) | ||
selected | string | null | Id of the selected node (controlled). null for none. | |
size | "sm" | "md" |
Example: File tree
import { type TreeNode, TreeView } from '@glyphui/ui'
import { FileCode2, FileJson, FileText, Folder } from 'lucide-react'
const nodes: TreeNode[] = [
{
id: 'src',
label: 'src',
icon: <Folder />,
children: [
{
id: 'components',
label: 'components',
icon: <Folder />,
children: [
{ id: 'button', label: 'button.tsx', icon: <FileCode2 /> },
{ id: 'card', label: 'card.tsx', icon: <FileCode2 /> },
],
},
{ id: 'main', label: 'main.tsx', icon: <FileCode2 /> },
],
},
{ id: 'package', label: 'package.json', icon: <FileJson /> },
{ id: 'readme', label: 'README.md', icon: <FileText /> },
]
export default function Example() {
return (
<TreeView
aria-label="Project files"
nodes={nodes}
defaultExpanded={['src', 'components']}
defaultSelected="button"
className="w-64"
/>
)
}
Example: Controlled selection
Drive expanded and selected from your own state.
import { Button, Text, type TreeNode, TreeView } from '@glyphui/ui'
import { useState } from 'react'
const nodes: TreeNode[] = [
{
id: 'workspace',
label: 'Workspace',
children: [
{ id: 'general', label: 'General' },
{ id: 'members', label: 'Members' },
{
id: 'billing',
label: 'Billing',
children: [
{ id: 'plans', label: 'Plans' },
{ id: 'invoices', label: 'Invoices' },
],
},
],
},
{ id: 'account', label: 'Account', children: [{ id: 'profile', label: 'Profile' }] },
]
export default function Example() {
const [expanded, setExpanded] = useState<string[]>(['workspace'])
const [selected, setSelected] = useState<string | null>('general')
return (
<div className="grid w-64 gap-3">
<TreeView
aria-label="Settings"
nodes={nodes}
expanded={expanded}
onExpandedChange={setExpanded}
selected={selected}
onSelect={setSelected}
/>
<Text size="small" tone="muted">
Selected: <span className="font-mono text-foreground">{selected ?? 'none'}</span>
</Text>
<Button
variant="outline"
size="sm"
onClick={() => setExpanded(['workspace', 'billing', 'account'])}
>
Expand all
</Button>
</div>
)
}
Live page: https://glyphui.xyz/components/tree-view