apps/www/content/docs/components/tree-view.mdx
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>
To setup the tree view, you need to use the tree collection to register the tree data.
TreeView.NodeThis 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>
)
}
Use the size prop to change the size of the tree view.
Use the variant prop to change the variant of the tree view.
Use the colorPalette prop to change the color palette of the tree view.
Adding the disabled prop to a node's property will disable the node and
prevent interaction.
Use the expandedValue and onExpandedChange props to programmatically control
node expansion behavior.
Render the TreeView.BranchTrigger to manually control node expansion behavior.
<ExampleTabs name="tree-view-explicit-expand" />You might need to set
role="none"on theTreeView.BranchControlto avoid accessibility issues.
Use the nodeState.expanded prop to swap the rendered icon on the branch when
it's expanded or collapsed.
Set the css variable --tree-indentation to 0px to remove the indentation of
the tree view.
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.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.
Use the animateContent prop to animate the tree view content expand/collapse
state.
Provide controls to expand or collapse all nodes at once.
<ExampleTabs name="tree-view-expand-collapse-all" />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.
Render the tree items as links by leveraging the asChild prop on the
TreeView.Item component.
Add the selectionMode="multiple" prop to the TreeView.Root component to
enable multi-select functionality.
:::info
This mode requires a modifier key to be pressed to select multiple items.
Ctrl or ⌘ on macOS and click the items.Shift while clicking on another item.:::
<ExampleTabs name="tree-view-multi-select" />Add checkboxes to tree nodes for selection functionality.
<ExampleTabs name="tree-view-checkbox" />Here's an example of how to design add/remove nodes in the tree view.
<ExampleTabs name="tree-view-mutation" />Here's an example of how to render a custom icon for the tree view based on its data.
<ExampleTabs name="tree-view-custom-icon" />Explore the TreeView component parts interactively. Click on parts in the
sidebar to highlight them in the preview.