VayuUI

Tree

A compound tree component with single-select and checkbox modes, search, keyboard navigation, cascade checking, and ARIA tree semantics.

Usage

src
components
package.json
tsconfig.json
README.md
import { Tree } from "@/components/ui/tree"
import type { TreeNode } from "@/components/ui/tree"

const data: TreeNode[] = [
  {
    id: "src",
    label: "src",
    children: [
      { id: "app.tsx", label: "app.tsx" },
      { id: "index.tsx", label: "index.tsx" },
    ],
  },
]

export function Example() {
  return (
    <Tree data={data} aria-label="Files">
      <Tree.Container>
        <Tree.Nodes nodes={data} />
      </Tree.Container>
    </Tree>
  )
}

Installation

src/components/ui/tree.tsx
// Copy the full component code

Features

  • Compound patternTree, Tree.Search, Tree.Actions, Tree.Container, Tree.Nodes, Tree.Node
  • Two modesnormal (single select) and checkbox (multi-select with cascade)
  • Four variantsdefault, filled, bordered, minimal
  • Three sizessm, md, lg
  • Search — filter tree with auto-expand
  • Expand / Collapse all — toolbar buttons
  • Custom actions — per-node action buttons via renderActions
  • Custom icons — per-node icon override
  • Keyboard navigation — Enter, Space, ArrowRight, ArrowLeft
  • ARIA tree semanticsrole="tree", role="treeitem", role="group", aria-expanded, aria-selected, aria-checked, aria-disabled
  • Cascade checking — parent auto-checks when all children are checked, indeterminate state

Sub-components

ComponentDescription
TreeRoot provider with context
Tree.SearchSearchbox for filtering
Tree.ActionsExpand All / Collapse All toolbar
Tree.ContainerBordered container with empty state
Tree.NodesRenders an array of root TreeNodes
Tree.NodeSingle node (used internally or manually)

Props

Tree

PropTypeDefaultDescription
dataTreeNode[]Tree data
modenormal | checkbox"normal"Selection mode
variantdefault | filled | bordered | minimal"default"Visual variant
sizesm | md | lg"md"Size
showLinesbooleantrueShow connecting lines
showIconsbooleantrueShow file/folder icons
defaultExpandAllbooleanfalseExpand all on mount
expandedKeys(string | number)[]Controlled expanded keys
onExpandedChange(keys: (string | number)[]) => voidExpanded change callback
selectedKeystring | number | nullControlled selected key
onSelect(key, node) => voidSelect callback
checkedKeys(string | number)[]Controlled checked keys
onCheck(keys, nodes) => voidCheck callback
checkStrictlybooleanfalseDisable cascade checking
disabledbooleanfalseDisable entire tree
renderActions(node: TreeNode) => ReactNodePer-node action renderer
aria-labelstring"Tree"Accessible label

TreeNode

PropTypeDefaultDescription
idstring | numberUnique identifier
labelstringDisplay text
iconReactNodeCustom icon
childrenTreeNode[]Child nodes
disabledbooleanfalseDisable node
selectablebooleantrueAllow selection
metadataRecord<string, unknown>Arbitrary metadata

useTree Hook

const ctx = useTree();

Access tree context from any component rendered inside <Tree>.

On this page