Skip to content

TreeView

Expandable/collapsible hierarchical data display with keyboard navigation. Each node can have children, and the tree supports controlled or uncontrolled expansion state.

Import

tsx
import { TreeView } from "silvery"

Props

PropTypeDefaultDescription
dataTreeNode[]requiredHierarchical data to display
renderNode(node: TreeNode, depth: number) => ReactNoderenders node.labelCustom node renderer
expandedIdsSet<string>--Controlled: set of expanded node IDs
onToggle(nodeId: string, expanded: boolean) => void--Called when expansion state changes
defaultExpandedbooleanfalseWhether nodes start expanded
isActivebooleantrueWhether this component captures input
indentnumber2Indent per level in characters

TreeNode

ts
interface TreeNode {
  id: string // Unique identifier
  label: string // Display label
  children?: TreeNode[] // Child nodes (optional)
}

Keyboard Shortcuts

KeyAction
j / DownMove cursor down
k / UpMove cursor up
EnterToggle expand/collapse
RightExpand (if collapsed)
LeftCollapse (if expanded)

Usage

tsx
const data: TreeNode[] = [
  {
    id: "1",
    label: "Documents",
    children: [
      { id: "1.1", label: "README.md" },
      { id: "1.2", label: "notes.txt" },
    ],
  },
  { id: "2", label: "config.json" },
]

<TreeView data={data} renderNode={(node) => <Text>{node.label}</Text>} />

// Start expanded
<TreeView data={data} defaultExpanded />

// Controlled expansion
<TreeView
  data={data}
  expandedIds={expanded}
  onToggle={(id, exp) => setExpanded(prev => {
    const next = new Set(prev)
    exp ? next.add(id) : next.delete(id)
    return next
  })}
/>

See Also

Released under the MIT License.