Build faster with Premium Chakra UI Components 💎
Learn moreWe've just shipped Chakra UI v3.22! This release introduces the Tree View component, along with quality improvements to make building with Chakra even smoother.
The Tree View component is a versatile component that allows users to display hierarchical data in a tree structure.
The basic tree view provides a tree structure with nested items.
"use client"
import { TreeView, createTreeCollection } from "@chakra-ui/react"
import { LuFile, LuFolder } from "react-icons/lu"
const Demo = () => {
return (
<TreeView.Root collection={collection} maxW="sm">
<TreeView.Label>Tree</TreeView.Label>
<TreeView.Tree>
<TreeView.Node
indentGuide={<TreeView.BranchIndentGuide />}
render={({ node, nodeState }) =>
nodeState.isBranch ? (
<TreeView.BranchControl>
<LuFolder />
<TreeView.BranchText>{node.name}</TreeView.BranchText>
</TreeView.BranchControl>
) : (
<TreeView.Item>
<LuFile />
<TreeView.ItemText>{node.name}</TreeView.ItemText>
</TreeView.Item>
)
}
/>
</TreeView.Tree>
</TreeView.Root>
)
}
interface Node {
id: string
name: string
children?: Node[]
}
const collection = createTreeCollection<Node>({
nodeToValue: (node) => node.id,
nodeToString: (node) => node.name,
rootNode: {
id: "ROOT",
name: "",
children: [
{
id: "node_modules",
name: "node_modules",
children: [
{ id: "node_modules/zag-js", name: "zag-js" },
{ id: "node_modules/pandacss", name: "panda" },
{
id: "node_modules/@types",
name: "@types",
children: [
{ id: "node_modules/@types/react", name: "react" },
{ id: "node_modules/@types/react-dom", name: "react-dom" },
],
},
],
},
{
id: "src",
name: "src",
children: [
{ id: "src/app.tsx", name: "app.tsx" },
{ id: "src/index.ts", name: "index.ts" },
],
},
{ id: "panda.config", name: "panda.config.ts" },
{ id: "package.json", name: "package.json" },
{ id: "renovate.json", name: "renovate.json" },
{ id: "readme.md", name: "README.md" },
],
},
})
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.
"use client"
import { TreeView, createTreeCollection } from "@chakra-ui/react"
import { useState } from "react"
import { LuFile, LuFolder, LuLoaderCircle } from "react-icons/lu"
// mock api result
const response: Record<string, Node[]> = {
node_modules: [
{ id: "zag-js", name: "zag-js" },
{ id: "pandacss", name: "panda" },
{ id: "@types", name: "@types", childrenCount: 2 },
],
"node_modules/@types": [
{ id: "react", name: "react" },
{ id: "react-dom", name: "react-dom" },
],
src: [
{ id: "app.tsx", name: "app.tsx" },
{ id: "index.ts", name: "index.ts" },
],
}
// function to load children of a node
function loadChildren(
details: TreeView.LoadChildrenDetails<Node>,
): Promise<Node[]> {
const value = details.valuePath.join("/")
return new Promise((resolve) => {
setTimeout(() => {
resolve(response[value] ?? [])
}, 1200)
})
}
const Demo = () => {
const [collection, setCollection] = useState(initialCollection)
return (
<TreeView.Root
collection={collection}
loadChildren={loadChildren}
onLoadChildrenComplete={(e) => setCollection(e.collection)}
>
<TreeView.Label>Tree</TreeView.Label>
<TreeView.Tree>
<TreeView.Node<Node>
indentGuide={<TreeView.BranchIndentGuide />}
render={({ node, nodeState }) =>
nodeState.isBranch ? (
<TreeView.BranchControl>
{nodeState.loading ? (
<LuLoaderCircle style={{ animation: "spin 1s infinite" }} />
) : (
<LuFolder />
)}
<TreeView.BranchText>{node.name}</TreeView.BranchText>
</TreeView.BranchControl>
) : (
<TreeView.Item>
<LuFile />
<TreeView.ItemText>{node.name}</TreeView.ItemText>
</TreeView.Item>
)
}
/>
</TreeView.Tree>
</TreeView.Root>
)
}
interface Node {
id: string
name: string
children?: Node[]
childrenCount?: number
}
const initialCollection = createTreeCollection<Node>({
nodeToValue: (node) => node.id,
nodeToString: (node) => node.name,
rootNode: {
id: "ROOT",
name: "",
children: [
{ id: "node_modules", name: "node_modules", childrenCount: 3 },
{ id: "src", name: "src", childrenCount: 2 },
{ id: "panda.config", name: "panda.config.ts" },
{ id: "package.json", name: "package.json" },
{ id: "renovate.json", name: "renovate.json" },
{ id: "readme.md", name: "README.md" },
],
},
})
clipboardAnatomy
was not exported from
@chakra-ui/react/anatomy
reason
to onOpenChange
and onInputValueChange
callbacksapi.clearHighlightedValue
function to clear highlighted valuetitle
or description
could not accept React elementapi.clearHighlightedValue
function to clear highlighted valueTo upgrade to the latest version, run:
npm install @chakra-ui/react@latest