Skip to content

Tree Plugin

The Tree plugin transforms your flat grid into a hierarchical tree view with expandable parent-child relationships. Great for file explorers, organizational charts, nested categories, or any data with a natural hierarchy.

import '@toolbox-web/grid/features/tree';

Just point the feature at the field containing child items (defaults to children) and the grid handles all the expand/collapse behavior, indentation, and icons automatically.

import { queryGrid } from '@toolbox-web/grid';
const grid = queryGrid('tbw-grid');
grid.gridConfig = {
columns: [
{ field: 'name', header: 'Name' },
{ field: 'type', header: 'Type' },
{ field: 'size', header: 'Size' }
],
features: {
tree: {
childrenField: 'children',
indentWidth: 24,
},
},
};
grid.rows = [
{
id: 1,
name: 'Documents',
type: 'folder',
children: [
{ id: 2, name: 'Work', type: 'folder', children: [
{ id: 3, name: 'Report.docx', type: 'file', size: '24 KB' }
]},
{ id: 4, name: 'Personal', type: 'folder', children: [] }
]
},
];

Toggle the controls to explore animation, indentation, and expand behavior.

Animation Expand/collapse animation
Default expanded Expand all nodes by default
Indent width (px) Indentation per nesting level
Show expand icons Show expand/collapse toggle icons

See TreeConfig for the full list of options and defaults.

The animation option controls how child nodes appear/disappear:

// Slide animation (default)
features: { tree: { animation: 'slide', ... } }
// Fade animation
features: { tree: { animation: 'fade', ... } }
// No animation
features: { tree: { animation: false, ... } }
const plugin = grid.getPluginByName('tree');
plugin.expand(key); // Expand a node by key
plugin.collapse(key); // Collapse a node by key
plugin.toggle(key); // Toggle a node's expanded state
plugin.expandAll(); // Expand all nodes
plugin.collapseAll(); // Collapse all nodes
plugin.expandToKey(key); // Expand all ancestors to reveal a node
const keys = plugin.getExpandedKeys(); // Get currently expanded node keys
const expanded = plugin.isExpanded(key); // Check if a node is expanded
const rows = plugin.getFlattenedRows(); // Get flattened tree rows with metadata
const row = plugin.getRowByKey(key); // Find a row by its key
Event Log:

Fired when a node is expanded or collapsed (via click or keyboard):

grid.on('tree-expand', ({ key, row, expanded, depth }) => {
console.log(`${expanded ? 'Expanded' : 'Collapsed'} node ${key} at depth ${depth}`);
});

See TreeExpandDetail for the full event payload type.

Fired when the overall expansion state changes (via toggle(), expandAll(), or collapseAll()):

grid.on('tree-state-change', ({ expandedKeys }) => {
console.log(`${expandedKeys.length} nodes expanded`);
});

The tree plugin supports CSS custom properties for theming. Override these on tbw-grid or a parent container:

PropertyDefaultDescription
--tbw-tree-toggle-size1.25em (~20px)Toggle icon and spacer width
--tbw-tree-indent-widthvar(--tbw-tree-toggle-size)Indentation per level (must be ≥ toggle size)
--tbw-tree-accentvar(--tbw-color-accent)Toggle icon hover color
--tbw-animation-duration200msExpand/collapse animation
--tbw-animation-easingease-outAnimation curve
tbw-grid {
/* Custom tree styling */
--tbw-tree-toggle-size: 1.5em; /* Larger toggle icons */
--tbw-tree-indent-width: 1.75em; /* Slightly wider indent */
--tbw-tree-accent: #10b981;
--tbw-animation-duration: 150ms;
}

The tree plugin uses these class names:

ClassElement
.tree-cell-wrapperCell content wrapper with indentation
.tree-expanderExpander cell container
.tree-toggleExpand/collapse icon
.tree-spacerIndent spacer element
.tbw-tree-slide-inChild row slide animation
.tbw-tree-fade-inChild row fade animation
AI assistants: For complete API documentation, implementation guides, and code examples for this library, see https://raw.githubusercontent.com/OysteinAmundsen/toolbox/main/llms-full.txt