createNested
Manage hierarchical tree structures with parent-child relationships, open/close state, and pluggable traversal strategies.
Usage
The createNested composable manages hierarchical tree structures with parent-child relationships, open/close states, and tree traversal.
Context / DI
Context Pattern
Use with Vue’s provide/inject for component trees:
import { createNestedContext } from '@vuetify/v0'
// Create a trinity
const [useTree, provideTree, defaultTree] = createNestedContext({
namespace: 'my-tree',
})
// In parent component
provideTree()
// In child components
const tree = useTree()Architecture
createNested extends createGroup with hierarchical tree management:
Options
| Option | Type | Default | Notes |
|---|---|---|---|
open | 'multiple' | 'single' | 'multiple' | 'single' keeps only one node open (accordion) |
openAll | boolean | false | Open a parent when a child registers |
reveal | boolean | false | open() also opens ancestors so the target stays visible |
selection | 'cascade' | 'independent' | 'leaf' | 'cascade' | How selection moves through the tree |
active | 'single' | 'multiple' | 'single' | How many items can be highlighted at once |
mandatory | MaybeRefOrGetter<boolean | 'force'> | false | Prevent deselecting the last selected item. 'force' also auto-selects the first non-disabled item on registration |
multiple | MaybeRefOrGetter<boolean> | true | Default differs from createSelection (false). In cascade, false replaces the current selection |
disabled | MaybeRefOrGetter<boolean> | false | All tree mutations become no-ops. A ticket can also carry disabled to skip only that node |
Selection
cascade selects every descendant when you select a parent, and ancestors resolve mixed state from their children: all selected, some selected (mixed), or none. independent selects each node alone. leaf lets only leaf nodes sit in selectedIds; selecting a parent selects its leaf descendants.
Worked examples live under Recipes.
Reactivity
createNested uses shallowReactive for tree state, making structural changes reactive while keeping traversal methods non-reactive for performance.
| Property/Method | Reactive | Notes |
|---|---|---|
children | ShallowReactive Map | |
parents | ShallowReactive Map | |
openedIds | ShallowReactive Set | |
openedItems | Computed from openedIds | |
roots | Computed, root nodes | |
leaves | Computed, leaf nodes | |
ticket.isOpen | Ref via toRef() | |
ticket.isLeaf | Ref via toRef() | |
ticket.depth | Ref via toRef() |
Ticket Properties
Each registered node receives additional properties:
const node = tree.register({ id: 'node', value: 'Node', parentId: 'root' })
// Reactive refs
node.isOpen.value // boolean - is this node open?
node.isLeaf.value // boolean - has no children?
node.depth.value // number - depth in tree (0 = root)
// Methods
node.open() // Open this node
node.close() // Close this node
node.flip() // Flip open/closed state
node.getPath() // Get path from root to this node
node.getAncestors() // Get all ancestors
node.getDescendants() // Get all descendantsExamples
Recipes
Selection Modes
Cascade Mode (Default)
Selection propagates through the hierarchy:
Selecting a parent selects all descendants:
tree.select('root')
// root, child-1, child-2, grandchild-1, etc. are all selectedSelecting a child updates ancestors to mixed state:
tree.select('child-1')
// child-1 is selected
// root shows mixed state (some children selected)Automatic state resolution:
All children selected → Parent becomes selected (not mixed)
Some children selected → Parent becomes mixed
No children selected → Parent becomes unselected (not mixed)
Independent Mode
Each node is selected independently with no cascading:
const tree = createNested({ selection: 'independent' })
tree.select('parent')
// Only 'parent' is selected, children unchangedLeaf Mode
Only leaf nodes can be selected. Selecting a parent selects all leaf descendants:
const tree = createNested({ selection: 'leaf' })
tree.select('folder')
// All files (leaves) under 'folder' are selected
// 'folder' itself is not in selectedIdsMandatory and single-select
When mandatory is true, unselect is a no-op if it would leave nothing selected:
const tree = createNested({ selection: 'cascade', mandatory: true })
tree.select('child-1')
tree.unselect('child-1') // no-op — would deselect the only selected itemunselectAll() with mandatory: true clears the selection, then selects seek('first').
When multiple is false in cascade mode, selecting a node clears previous selections first:
const tree = createNested({ selection: 'cascade', multiple: false })
tree.select('child-1')
tree.select('child-2') // child-1 is deselected firstConvenience Methods
Expand/Collapse All
// Open all non-leaf nodes
tree.expandAll()
// Close all nodes
tree.collapseAll()Data Transformation
Convert tree to flat array for serialization or API consumption:
const flat = tree.toFlat()
// Returns: [{ id, parentId, value }, ...]
// Useful for sending to APIs or AI systems
console.log(JSON.stringify(flat))Inline Children Registration
Define children directly when registering items:
tree.onboard([
{
id: 'nav',
value: 'Navigation',
children: [
{ id: 'home', value: 'Home' },
{ id: 'about', value: 'About' },
{
id: 'products',
value: 'Products',
children: [
{ id: 'widgets', value: 'Widgets' },
{ id: 'gadgets', value: 'Gadgets' },
],
},
],
},
])Cascade Unregister
Remove a node and optionally all its descendants:
// Remove node, orphan children (default)
tree.unregister('parent')
// Remove node and all descendants
tree.unregister('parent', true)
// Batch removal with cascade
tree.offboard(['node-1', 'node-2'], true)Release
Drop a node’s element without removing it. The ticket, its parent link, and its selection stay. A later register with the same id, or with the same parent and value when id is omitted, returns that ticket.
tree.release('child')
tree.register({ id: 'child', value: 'Child', parentId: 'parent' })FAQ
cascade (default) selects all descendants when you select a parent and shows ancestors as mixed; independent selects each node alone with no propagation; leaf lets only leaf nodes be selected, so selecting a parent selects its leaf descendants — the file-picker pattern.
Pass open: 'single' so only one node can be open at a time. The default open: 'multiple' lets several branches stay expanded simultaneously.
When mandatory is true, deselecting is blocked if it would leave no items selected. unselectAll() clears the selection, then selects seek('first'). 'force' also auto-selects the first non-disabled item on registration.
Pass disabled: true so open(), close(), select(), unselect(), and toggle() become no-ops. A ticket can also carry disabled to skip only that node. The option accepts MaybeRefOrGetter<boolean>:
const isLocked = shallowRef(false)
const tree = createNested({ disabled: isLocked })Use createNested when you’re building custom tree UI and only want the hierarchy logic. The Treeview component wraps it with focus management and ARIA already wired up. For flat multi-select with no hierarchy, createGroup is lighter.