Tree View

A component that is used to show a tree hierarchy.

Loading...

Anatomy

<TreeView.Root>
  <TreeView.Label />
  <TreeView.Tree>
    <TreeView.NodeProvider>
      <TreeView.Branch>
        <TreeView.BranchControl>
          <TreeView.BranchIndicator />
          <TreeView.BranchText />
        </TreeView.BranchControl>
        <TreeView.BranchContent>
          <TreeView.BranchIndentGuide />
          <TreeView.Item>
            <TreeView.ItemText />
          </TreeView.Item>
        </TreeView.BranchContent>
      </TreeView.Branch>
    </TreeView.NodeProvider>
  </TreeView.Tree>
</TreeView.Root>

Examples

Controlled Expanded

Pass expandedValue and onExpandedChange to TreeView.Root.

Controlled Selection

Pass selectedValue and onSelectionChange to TreeView.Root.

Root Provider

Use RootProvider with the useTreeView hook to access the state and methods from outside the component.

Lazy Loading

Load a node's children on demand to cut initial load time and memory use. Provide:

  • loadChildren: loads the children of a node.
  • onLoadChildrenComplete: called when children finish loading. Update the tree collection here.
  • childrenCount: the number of children of a branch node.

Lazy Mount

Set lazyMount on TreeView.Root to render branch content only when it's expanded. This helps when content is large.

Add unmountOnExit to unmount content when a branch collapses. It re-renders the next time the branch expands.

Filtering

Combine the collection's filter method with the useFilter hook to show only nodes that match a search query.

Render tree items as links, for example in a docs site. Store a link hierarchy in the collection and use asChild to render each item as an <a> with an href.

Virtualized

For trees with thousands of nodes, render only the visible rows with the Virtualizer.

  • Use useTreeView with TreeView.RootProvider.
  • getVisibleNodes() returns the flat list of visible nodes. Keep the virtualizer's count in sync with it.
  • Render TreeView.Tree as ListVirtualizer.Root and each TreeView.Node as ListVirtualizer.Item.
  • Pass scrollToIndexFn to enable keyboard navigation.

Checkbox Tree

Set defaultCheckedValue to enable checkbox selection. Checking a parent or child updates the related nodes.

Expand and Collapse All

Call expand() or collapse() from the tree view context to toggle all branches.

Mutation

Use the collection's remove() and replace() methods to add and remove nodes, for example in a file explorer.

Rename Node

Use canRename and onRenameComplete for inline renaming. Press F2 to rename the focused node.

Guides

Type Safety

Use the TreeView.RootComponent type to build wrappers that keep tree nodes fully typed.

import { TreeView as ArkTreeView } from '@ark-ui/react/tree-view'

const TreeView: ArkTreeView.RootComponent = (props) => {
  return <ArkTreeView.Root {...props}>{/* ... */}</ArkTreeView.Root>
}

onSelectionChange and other callbacks then infer the node type:

const App = () => {
  const collection = createTreeCollection({
    initialItems: [
      { id: '1', label: 'React', children: [] },
      { id: '2', label: 'Vue', children: [] },
    ],
  })
  return (
    <TreeView
      collection={collection}
      onSelectionChange={(e) => {
        // e.items is typed as Array<{ id: string, label: string, children: [] }>
        console.log(e.items)
      }}
    >
      {/* ... */}
    </TreeView>
  )
}

API Reference

Props

Root

Defines the <div> element.

AttributeDefaultType
collection
TreeCollection<T>

The collection of tree nodes

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
canRename
(node: T, indexPath: IndexPath) => boolean

Function to determine if a node can be renamed

checkedValue
string[]

The controlled checked node value

defaultCheckedValue
string[]

The initial checked node value when rendered. Use when you don't need to control the checked node value.

defaultExpandedValue
string[]

The initial expanded node ids when rendered. Use when you don't need to control the expanded node value.

defaultFocusedValue
string

The initial focused node value when rendered. Use when you don't need to control the focused node value.

defaultSelectedValue
string[]

The initial selected node value when rendered. Use when you don't need to control the selected node value.

expandedValue
string[]

The controlled expanded node ids

expandOnClicktrue
boolean

Whether clicking on a branch should open it or not

focusedValue
string

The value of the focused node

hideMode'display-none'
HideMode

How to hide content when mounted but not present. - `'display-none'`: HTML `hidden` attribute. Effects stay alive. - `'activity'`: React 19 `<Activity mode="hidden">`. Effects pause. Requires React 19+.

ids
ElementIds

The ids of the tree elements. Useful for composition.

lazyMountfalse
boolean

Whether to enable lazy mounting

loadChildren
(details: LoadChildrenDetails<T>) => Promise<T[]>

Function to load children for a node asynchronously. When provided, branches will wait for this promise to resolve before expanding.

onBeforeRename
(details: RenameCompleteDetails) => boolean

Called before a rename is completed. Return false to prevent the rename.

onCheckedChange
(details: CheckedChangeDetails) => void

Called when the checked value changes

onExpandedChange
(details: ExpandedChangeDetails<T>) => void

Called when the tree is opened or closed

onFocusChange
(details: FocusChangeDetails<T>) => void

Called when the focused node changes

onLoadChildrenComplete
(details: LoadChildrenCompleteDetails<T>) => void

Called when a node finishes loading children

onLoadChildrenError
(details: LoadChildrenErrorDetails<T>) => void

Called when loading children fails for one or more nodes

onRenameComplete
(details: RenameCompleteDetails) => void

Called when a node label rename is completed

onRenameStart
(details: RenameStartDetails<T>) => void

Called when a node starts being renamed

onSelectionChange
(details: SelectionChangeDetails<T>) => void

Called when the selection changes

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

scrollToIndexFn
(details: ScrollToIndexDetails<T>) => void

Function to scroll to a specific index. Useful for virtualized tree views.

selectedValue
string[]

The controlled selected node value

selectionMode'single'
'multiple' | 'single'

Whether the tree supports multiple selection - "single": only one node can be selected - "multiple": multiple nodes can be selected

translations
IntlTranslations

Specifies the localized strings that identifies the accessibility elements and their states

typeaheadtrue
boolean

Whether the tree supports typeahead search

unmountOnExitfalse
boolean

Whether to unmount on exit.

Cell

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

IndentGuide

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-tree-view-indent-guide]<uid>
[data-depth]The depth of the item

Label

Defines the <h3> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

NodeCheckbox

Defines the <span> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-tree-view-node-checkbox]<uid>
[data-state]"checked" | "unchecked" | "indeterminate"
[data-disabled]Present when disabled

NodeExpandTrigger

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-tree-view-node-expand-trigger]<uid>
[data-disabled]Present when disabled
[data-state]"open" | "closed"
[data-value]The value of the item
[data-loading]Present when loading

NodeGroupContent

Renders a <div> element.

PropDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-tree-view-node-group-content]<uid>
[data-state]"open" | "closed"
[data-depth]The depth of the item
[data-path]The path of the item
[data-value]The value of the item

NodeGroup

Renders a <div> element.

PropDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-tree-view-node-group]<uid>
[data-depth]The depth of the item
[data-value]The value of the item
[data-path]The path of the item
[data-state]"open" | "closed"
[data-disabled]Present when disabled
[data-loading]Present when loading
CSS VariableDescription
--depthThe depth value for the NodeGroup

NodeIndicator

Defines the <div> element.

AttributeDefaultType
type
NodeIndicatorType

The type of indicator - "expanded": always visible, reflects open/closed state - "selected": shown when the node is selected - "checked": shown when the node is checked - "indeterminate": shown when the node is indeterminate (partially checked)

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-tree-view-node-indicator]<uid>
[data-type]The type of the item
[data-state]"expanded" | "open" | "closed"
[data-disabled]Present when disabled
[data-selected]Present when selected
[data-focus]Present when focused
[data-loading]Present when loading

Node

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<TreeViewNodeState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-tree-view-node]<uid>
[data-path]The path of the item
[data-value]The value of the item
[data-focus]Present when focused
[data-selected]Present when selected
[data-state]"open" | "closed"
[data-disabled]Present when disabled
[data-renaming]
[data-checked]Present when checked
[data-indeterminate]
[data-depth]The depth of the item
[data-loading]Present when loading
[data-branch]
CSS VariableDescription
--depthThe depth value for the Node

NodeProvider

PropDefaultType
indexPath
number[]

The index path of the tree node

node
NonNullable<T>

The tree node

NodeRenameInput

Defines the <input> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

NodeText

Defines the <span> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-tree-view-node-text]<uid>
[data-disabled]Present when disabled
[data-selected]Present when selected
[data-focus]Present when focused
[data-state]"open" | "closed"
[data-loading]Present when loading

RootProvider

Defines the <div> element.

AttributeDefaultType
value
UseTreeViewReturn<T>

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
hideMode'display-none'
HideMode

How to hide content when mounted but not present. - `'display-none'`: HTML `hidden` attribute. Effects stay alive. - `'activity'`: React 19 `<Activity mode="hidden">`. Effects pause. Requires React 19+.

lazyMountfalse
boolean

Whether to enable lazy mounting

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

unmountOnExitfalse
boolean

Whether to unmount on exit.

Tree

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-tree-view-tree]<uid>
[data-branch]

Context

API

PropertyType
collection
TreeCollection<V>

The tree collection data

expandedValue
string[]

The value of the expanded nodes.

setExpandedValue
(value: string[]) => void

Sets the expanded value

selectedValue
string[]

The value of the selected nodes.

setSelectedValue
(value: string[]) => void

Sets the selected value

checkedValue
string[]

The value of the checked nodes

toggleChecked
(value: string, isBranch: boolean) => void

Toggles the checked value of a node

setChecked
(value: string[]) => void

Sets the checked value of a node

clearChecked
VoidFunction

Clears the checked value of a node

getCheckedMap
() => CheckedValueMap

Returns the checked details of branch and leaf nodes

getVisibleNodes
() => VisibleNode<V>[]

Returns the visible nodes as a flat array of nodes and their index path. Useful for rendering virtualized tree views.

expand
(value?: string[]) => void

Function to expand nodes. If no value is provided, all nodes will be expanded

collapse
(value?: string[]) => void

Function to collapse nodes If no value is provided, all nodes will be collapsed

select
(value?: string[]) => void

Function to select nodes If no value is provided, all nodes will be selected

deselect
(value?: string[]) => void

Function to deselect nodes If no value is provided, all nodes will be deselected

focus
(value: string) => void

Function to focus a node by value

selectParent
(value: string) => void

Function to select the parent node of the focused node

expandParent
(value: string) => void

Function to expand the parent node of the focused node

startRenaming
(value: string) => void

Function to start renaming a node by value

submitRenaming
(value: string, label: string) => void

Function to submit the rename and update the node label

cancelRenaming
() => void

Function to cancel renaming without changes

getNodeState
(props: NodeProps) => NodeState

Returns the state of a node including whether it's selected, expanded, focused, disabled, checked, loading, renaming, and its depth/path info.

Accessibility

Complies with the Tree View WAI-ARIA design pattern.

Keyboard Support

KeyDescription
Tab
Moves focus to the tree view, placing the first tree view item in focus.
EnterSpace
Selects the item or branch node
ArrowDown
Moves focus to the next node
ArrowUp
Moves focus to the previous node
ArrowRight
When focus is on a closed branch node, opens the branch.
When focus is on an open branch node, moves focus to the first item node.
ArrowLeft
When focus is on an open branch node, closes the node.
When focus is on an item or branch node, moves focus to its parent branch node.
Home
Moves focus to first node without opening or closing a node.
End
Moves focus to the last node that can be focused without expanding any nodes that are closed.
a-zA-Z
Focus moves to the next node with a name that starts with the typed character. The search logic ignores nodes that are descendants of closed branch.
*
Expands all sibling nodes that are at the same depth as the focused node.
Shift + ArrowDown
Moves focus to and toggles the selection state of the next node.
Shift + ArrowUp
Moves focus to and toggles the selection state of the previous node.
Ctrl + A
Selects all nodes in the tree. If all nodes are selected, unselects all nodes.