Tree
Display a Tree structure
Source
LLM docs
Docs
Package
Usage
The Tree component is used to display hierarchical data. The Tree component
has minimal styling by default; you can customize styles with Styles API.
- src
- node_modules
- package.json
- tsconfig.json
Data prop
Data passed to the data prop should follow these rules:
- Data must be a stable reference (memoized)
- Data must be an array
- Each item in the array represents a node in the tree
- Each node must be an object with
valueandlabelkeys - Each node can have a
childrenkey with an array of child nodes - The
valueof each node must be unique
Valid data example:
Invalid data example:
Data type
You can import the TreeNodeData type to define the data type for your tree:
renderNode
Use the renderNode prop to customize node rendering.
The renderNode function receives an object with the following properties as a single argument:
- src
- node_modules
- package.json
- tsconfig.json
useTree hook
useTree hook can be used to control selected and expanded state of the tree.
The hook accepts an object with the following properties:
And returns an object with the following properties:
You can pass the value returned by the useTree hook to the tree prop of the Tree component
to control tree state:
- src
- node_modules
- package.json
- tsconfig.json
Checked state
Tree can be used to display checked state with checkboxes.
To implement checked state, you need to render Checkbox.Indicator in the renderNode function:
- src
- node_modules
- package.json
- tsconfig.json
To check/uncheck nodes, use checkAllNodes and uncheckAllNodes functions:
- src
- components
- Accordion.tsx
- Tree.tsx
- Button.tsx
- node_modules
- react
- index.d.ts
- package.json
- @mantine
- core
- index.d.ts
- package.json
- hooks
- index.d.ts
- package.json
- form
- index.d.ts
- package.json
- package.json
- tsconfig.json
Check strictly
By default, checking a parent node also checks all of its children (and unchecking works
the same way). Set checkStrictly: true on useTree to make each node's checked state
fully independent – checking a parent does not affect children and vice versa.
In this mode, isNodeIndeterminate always returns false.
- src
- node_modules
- package.json
- tsconfig.json
Initial expanded state
Expanded state is an object of node.value and boolean values that represent nodes expanded state.
To change initial expanded state, pass initialExpandedState to the useTree hook.
To generate expanded state from data with expanded nodes, you can use getTreeExpandedState function:
it accepts data and an array of expanded nodes values and returns expanded state object.
If '*' is passed as the second argument to getTreeExpandedState, all nodes will be expanded:
- src
- components
- Accordion.tsx
- Tree.tsx
- Button.tsx
- node_modules
- package.json
- tsconfig.json
Async loading
Tree supports lazy loading of children. Set hasChildren: true on a node without providing children –
when the node is expanded for the first time, onLoadChildren callback passed to useTree is called.
Use the mergeAsyncChildren utility to splice loaded children into your data:
The renderNode payload includes isLoading and loadError fields that you can use
to display a loading indicator or an error message. Use tree.invalidateNode(value) to clear
the cache for a node and allow re-fetching on next expand.
Search and filter
Tree does not include built-in search controls – search input and filtering logic are always external.
Use the filterTreeData utility to filter tree data based on a search query. The function
accepts tree data, a query string, and an optional custom filter function:
The default filter compares the query against node.label (when it is a string) or node.value as a fallback.
Matching nodes and their ancestors are preserved in the result. You can provide a custom TreeNodeFilter
function for more advanced matching (for example, fuzzy search with fuse.js).
Highlight matching nodes
In this example, all nodes remain visible and matching text is highlighted using the Highlight component
inside renderNode. Ancestor nodes of matching nodes are auto-expanded.
- src
- public
- package.json
- tsconfig.json
Filter non-matching nodes
In this example, non-matching branches are removed from the tree using filterTreeData.
The filtered tree is auto-expanded with getTreeExpandedState(filteredData, '*').
- src
- public
- package.json
- tsconfig.json
Fuzzy search with fuse.js
You can pass a custom filter function to filterTreeData for fuzzy matching. This example
uses fuse.js:
- src
- public
- package.json
- tsconfig.json
Drag and drop
Tree component supports drag-and-drop reordering of nodes. To enable it, provide onDragDrop callback.
The callback receives an object with draggedNode (value of the dragged node), targetNode (value of the node it was dropped on),
and position ('before', 'after', or 'inside').
Use moveTreeNode utility function to update the data based on the drag-and-drop result:
When dragging over a node, the drop position is determined by cursor position:
- Top zone – drop before the target node (shown as a line above)
- Middle zone – drop inside the target node as a child (shown as a background highlight, only for nodes with children)
- Bottom zone – drop after the target node (shown as a line below)
Nodes cannot be dropped onto their own descendants.
- Pages
- Components
- package.json
- tsconfig.json
Restricting drop targets
Use the allowDrop prop to forbid certain drops. The callback receives the same payload as
onDragDrop (draggedNode, targetNode, position) and should return false to reject the drop.
When it returns false, the drop indicator is hidden and the browser displays the
"not-allowed" cursor, so the user gets visual feedback before releasing the mouse.
- Pages
- Components (locked)
- package.json
Drag handle
By default, drag can be initiated from anywhere on a node. Set withDragHandle on Tree to
restrict drag initiation to an element that spreads dragHandleProps from the renderNode
payload. This is useful when a node contains interactive controls (inputs, buttons) that
would otherwise interfere with dragging.
- Pages
- Components
- package.json
Connecting lines
Set withLines prop to display connecting lines showing parent-child relationships.
Lines adapt to levelOffset spacing automatically.
- src
- components
- Accordion.tsx
- Tree.tsx
- Button.tsx
- node_modules
- react
- index.d.ts
- package.json
- @mantine
- core
- index.d.ts
- package.json
- hooks
- index.d.ts
- package.json
- form
- index.d.ts
- package.json
- package.json
- tsconfig.json
Virtualization
Tree does not depend on any virtualization library – you supply one yourself.
Use the flattenTreeData utility to convert hierarchical data into a flat list of
visible nodes based on the current expanded state, then render each node with
FlatTreeNode which provides Tree's styles, aria attributes, click/keyboard
handlers, and renderNode support.
FlatTreeNode accepts the same behavioral props as Tree (expandOnClick,
selectOnClick, expandOnSpace, checkOnSpace, renderNode) and a style prop
for virtualizer positioning. The container element must have data-tree-root
and role="tree" attributes for keyboard navigation to work.
Example: files tree
- src
- node_modules
- package.json
- tsconfig.json