Tree View
A component that is used to show a tree hierarchy.
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.
Links
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
useTreeViewwithTreeView.RootProvider. getVisibleNodes()returns the flat list of visible nodes. Keep the virtualizer'scountin sync with it.- Render
TreeView.TreeasListVirtualizer.Rootand eachTreeView.NodeasListVirtualizer.Item. - Pass
scrollToIndexFnto 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.
| Attribute | Default | Type |
|---|---|---|
collection | TreeCollection<T>The collection of tree nodes | |
asChild | booleanUse 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) => booleanFunction 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 | stringThe 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 | |
expandOnClick | true | booleanWhether clicking on a branch should open it or not |
focusedValue | stringThe value of the focused node | |
hideMode | 'display-none' | HideModeHow 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 | ElementIdsThe ids of the tree elements. Useful for composition. | |
lazyMount | false | booleanWhether 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) => booleanCalled before a rename is completed. Return false to prevent the rename. | |
onCheckedChange | (details: CheckedChangeDetails) => voidCalled when the checked value changes | |
onExpandedChange | (details: ExpandedChangeDetails<T>) => voidCalled when the tree is opened or closed | |
onFocusChange | (details: FocusChangeDetails<T>) => voidCalled when the focused node changes | |
onLoadChildrenComplete | (details: LoadChildrenCompleteDetails<T>) => voidCalled when a node finishes loading children | |
onLoadChildrenError | (details: LoadChildrenErrorDetails<T>) => voidCalled when loading children fails for one or more nodes | |
onRenameComplete | (details: RenameCompleteDetails) => voidCalled when a node label rename is completed | |
onRenameStart | (details: RenameStartDetails<T>) => voidCalled when a node starts being renamed | |
onSelectionChange | (details: SelectionChangeDetails<T>) => voidCalled 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>) => voidFunction 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 | IntlTranslationsSpecifies the localized strings that identifies the accessibility elements and their states | |
typeahead | true | booleanWhether the tree supports typeahead search |
unmountOnExit | false | booleanWhether to unmount on exit. |
Cell
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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. |
| Attribute | Description |
|---|---|
[data-tree-view-indent-guide] | <uid> |
[data-depth] | The depth of the item |
Label
Defines the <h3> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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. |
| Attribute | Description |
|---|---|
[data-tree-view-node-checkbox] | <uid> |
[data-state] | "checked" | "unchecked" | "indeterminate" |
[data-disabled] | Present when disabled |
NodeExpandTrigger
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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. |
| Attribute | Description |
|---|---|
[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.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse 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. |
| Attribute | Description |
|---|---|
[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.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse 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. |
| Attribute | Description |
|---|---|
[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 Variable | Description |
|---|---|
--depth | The depth value for the NodeGroup |
NodeIndicator
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
type | NodeIndicatorTypeThe 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 | booleanUse 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. |
| Attribute | Description |
|---|---|
[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.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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. |
| Attribute | Description |
|---|---|
[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 Variable | Description |
|---|---|
--depth | The depth value for the Node |
NodeProvider
| Prop | Default | Type |
|---|---|---|
indexPath | number[]The index path of the tree node | |
node | NonNullable<T>The tree node |
NodeRenameInput
Defines the <input> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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. |
| Attribute | Description |
|---|---|
[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.
| Attribute | Default | Type |
|---|---|---|
value | UseTreeViewReturn<T> | |
asChild | booleanUse 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' | HideModeHow 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+. |
lazyMount | false | booleanWhether 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. | |
unmountOnExit | false | booleanWhether to unmount on exit. |
Tree
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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. |
| Attribute | Description |
|---|---|
[data-tree-view-tree] | <uid> |
[data-branch] |
Context
API
| Property | Type |
|---|---|
collection | TreeCollection<V>The tree collection data |
expandedValue | string[]The value of the expanded nodes. |
setExpandedValue | (value: string[]) => voidSets the expanded value |
selectedValue | string[]The value of the selected nodes. |
setSelectedValue | (value: string[]) => voidSets the selected value |
checkedValue | string[]The value of the checked nodes |
toggleChecked | (value: string, isBranch: boolean) => voidToggles the checked value of a node |
setChecked | (value: string[]) => voidSets the checked value of a node |
clearChecked | VoidFunctionClears the checked value of a node |
getCheckedMap | () => CheckedValueMapReturns 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[]) => voidFunction to expand nodes. If no value is provided, all nodes will be expanded |
collapse | (value?: string[]) => voidFunction to collapse nodes If no value is provided, all nodes will be collapsed |
select | (value?: string[]) => voidFunction to select nodes If no value is provided, all nodes will be selected |
deselect | (value?: string[]) => voidFunction to deselect nodes If no value is provided, all nodes will be deselected |
focus | (value: string) => voidFunction to focus a node by value |
selectParent | (value: string) => voidFunction to select the parent node of the focused node |
expandParent | (value: string) => voidFunction to expand the parent node of the focused node |
startRenaming | (value: string) => voidFunction to start renaming a node by value |
submitRenaming | (value: string, label: string) => voidFunction to submit the rename and update the node label |
cancelRenaming | () => voidFunction to cancel renaming without changes |
getNodeState | (props: NodeProps) => NodeStateReturns 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
| Key | Description |
|---|---|
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. |