TreeView
Used to display hierarchical data structures in an expandable tree format
Tree
Usage
import { TreeView } from "@chakra-ui/react"<TreeView.Root>
<TreeView.Label />
<TreeView.Tree>
<TreeView.Branch>
<TreeView.BranchControl>
<TreeView.BranchIndicator />
<TreeView.BranchText />
</TreeView.BranchControl>
<TreeView.BranchContent>
<TreeView.BranchIndentGuide />
<TreeView.Item />
</TreeView.BranchContent>
</TreeView.Branch>
<TreeView.Item />
</TreeView.Tree>
</TreeView.Root>Shortcuts
TreeView.Node
This component is a helper to manage the recursive rendering of the branch and leaf nodes.
<TreeView.Node
showIndentGuide
render={({ node, nodeState }) =>
nodeState.isBranch ? (
<TreeView.BranchControl>
<TreeView.BranchText>{node.name}</TreeView.BranchText>
</TreeView.BranchControl>
) : (
<TreeView.Item>
<TreeView.ItemText>{node.name}</TreeView.ItemText>
</TreeView.Item>
)
}
/>is equivalent to:
const TreeNode = (props: TreeView.NodeProviderProps<Node>) => {
const { node, indexPath } = props
return (
<TreeView.NodeProvider key={node.id} node={node} indexPath={indexPath}>
{node.children ? (
<TreeView.Branch>
<TreeView.BranchControl>
<TreeView.BranchText>{node.name}</TreeView.BranchText>
</TreeView.BranchControl>
<TreeView.BranchContent>
<TreeView.BranchIndentGuide />
{node.children.map((child, index) => (
<TreeNode
key={child.id}
node={child}
indexPath={[...indexPath, index]}
/>
))}
</TreeView.BranchContent>
</TreeView.Branch>
) : (
<TreeView.Item>
<TreeView.ItemText>{node.name}</TreeView.ItemText>
</TreeView.Item>
)}
</TreeView.NodeProvider>
)
}Examples
Sizes
Use the size prop to change the size of the tree view.
Tree (size=xs)
Tree (size=sm)
Tree (size=md)
Variants
Use the variant prop to change the variant of the tree view.
Tree (variant=subtle)
Tree (variant=solid)
Colors
Use the colorPalette prop to change the color palette of the tree view.
Tree (colorPalette=gray)
Tree (colorPalette=red)
Tree (colorPalette=green)
Tree (colorPalette=blue)
Tree (colorPalette=teal)
Tree (colorPalette=pink)
Tree (colorPalette=purple)
Tree (colorPalette=cyan)
Tree (colorPalette=orange)
Tree (colorPalette=yellow)
Disabled Node
Adding the disabled prop to a node's property will disable the node and
prevent interaction.
Tree
Controlled Expansion
Use the expandedValue and onExpandedChange props to programmatically control
node expansion behavior.
Tree
Explicit Expand
Render the TreeView.BranchTrigger to manually control node expansion behavior.
You might need to set role="none" on the TreeView.BranchControl to avoid
accessibility issues.
Tree
Expand Icon
Use the nodeState.expanded prop to swap the rendered icon on the branch when
it's expanded or collapsed.
Tree
Remove Indentation
Set the css variable --tree-indentation to 0px to remove the indentation of
the tree view.
Tree
Async Loading
Lazy loading is a feature that allows the tree view to load children of a node on demand (or async). This helps to improve the initial load time and memory usage.
To use this, you need to provide the following:
loadChildren— A function that is used to load the children of a node.onLoadChildrenComplete— A callback that is called when the children of a node are loaded. Used to update the tree collection.childrenCount— A number that indicates the number of children of a branch node.
Tree
Filtering
Filtering is useful when you have a large tree and you want to filter the nodes to only show the ones that match the search query.
Here's an example that composes the filter method from the TreeCollection
and useFilter hook to filter the nodes.
Tree
Collapse Animation
Use the animateContent prop to animate the tree view content expand/collapse
state.
Tree
Expand/Collapse All
Provide controls to expand or collapse all nodes at once.
Tree
Store
Use the useTreeView hook to create the tree view store and pass it to the
TreeView.RootProvider component. This allows you to have maximum control over
the tree view programmatically.
Tree
[]
Links
Render the tree items as links by leveraging the asChild prop on the
TreeView.Item component.
Multi Select
Add the selectionMode="multiple" prop to the TreeView.Root component to
enable multi-select functionality.
This mode requires a modifier key to be pressed to select multiple items.
- Hold
Ctrlor⌘on macOS and click the items. - Click an item, then hold
Shiftwhile clicking on another item.
Tree
Checkbox Tree
Add checkboxes to tree nodes for selection functionality.
Tree
Mutation
Here's an example of how to design add/remove nodes in the tree view.
Tree
Custom Icon
Here's an example of how to render a custom icon for the tree view based on its data.
Tree
Props
Root
| Prop | Default | Type |
|---|---|---|
collection * | TreeCollection<T>The collection of tree nodes | |
expandOnClick | true | booleanWhether clicking on a branch should open it or not |
lazyMount | false | booleanWhether to enable lazy mounting |
selectionMode | 'single' | 'multiple' | 'single'Whether the tree supports multiple selection - "single": only one node can be selected - "multiple": multiple nodes can be selected |
typeahead | true | booleanWhether the tree supports typeahead search |
unmountOnExit | false | booleanWhether to unmount on exit. |
colorPalette | gray | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'The color palette of the component |
size | md | 'md' | 'sm' | 'xs'The size of the component |
variant | subtle | 'subtle' | 'solid'The variant of the component |
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
unstyled | booleanWhether to remove the component's style. | |
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 | |
focusedValue | stringThe value of the focused node | |
ids | Partial<{ root: string; tree: string; label: string; node: (value: string) => string }>The ids of the tree elements. Useful for composition. | |
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. | |
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 | |
onSelectionChange | (details: SelectionChangeDetails<T>) => voidCalled when the selection changes | |
selectedValue | string[]The controlled selected node value | |
animateContent | 'true' | 'false'The animateContent of the component |
Node
| Prop | Default | Type |
|---|---|---|
render * | (props | |
indentGuide | React.ReactElement | |
renderBranch | (props | |
branchProps | TreeViewBranchProps | |
branchContentProps | TreeViewBranchContentProps | |
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Explorer
Explore the TreeView component parts interactively. Click on parts in the
sidebar to highlight them in the preview.
Project Explorer
Component Anatomy
Hover to highlight, click to select parts
branch
branchContent
branchControl
branchIndentGuide
branchIndicator
branchText
branchTrigger
item
itemIndicator
itemText
label
nodeCheckbox
nodeRenameInput
root
tree