API REFERENCE · NAVIGATION
<dj-tree>
A hierarchical tree built from nodes.
The tree knows nothing about what it shows: a file tree, a mail folder list, and a MIME structure are all just nodes. Each node can carry an icon (a name registered with registerIcon or registerIcons from @dojo-ng/icon) and a count, shown as a trailing badge such as an unread count.
npm install @dojo-ng/tree
import "@dojo-ng/tree";
Selection and expansion
- Both are controlled.
valueis the selected node id, and the tree emitsdj-select. expandedis the array of open node ids, and the tree emitsdj-expand-change.- Clicking a row selects it; clicking the chevron expands or collapses it.
- Set
expand-on-row-clickto make a click on a parent row also expand or collapse it. Use it when some rows exist only to hold others, so a click on them does something visible.
Keyboard
The tree follows the APG tree pattern. Only one row is a tab stop: the selected row if it is visible, otherwise the first visible row. The arrow keys move focus without selecting.
- Down and Up move through the visible rows.
- Right expands a closed parent, moves into an open one, and does nothing on a leaf.
- Left collapses an open parent, or moves to the parent row.
- Home and End jump to the first and last visible row.
- Enter or Space selects the focused row.
Right-to-left
- Indentation uses
margin-inline-start, so it flips in a right-to-left page, and the chevron points in the reading direction.
Dropping onto rows
- Set
canDropto make the tree a target for native HTML5 drag and drop. It is called during a drag with a row id and the drag'sdataTransfer.types(the only part of a payload the browser shows before the drop), and returns whether that row accepts it. Unset, the tree ignores drags. - The row under the pointer that accepts the drag is marked: the
row--drop-targetpart and a built-in outline. A refused row is not marked and the cursor shows the drop is not allowed. - Resting on a collapsed parent for
spring-delayms (default 800; 0 turns it off) during a drag expands it, through the same path as a chevron click, sodj-expand-changefires. It stays open after the drop. - A drop emits
dj-dropwith{ id, dataTransfer }. The tree does not select the row; the consumer reads its payload fromdataTransferand applies the change. - Dragging has no keyboard form. A consumer that offers drop must offer another way to do the same thing (WCAG 2.5.7), such as a Move command with a picker.
Tracking navigation
Set track and the tree and @dojo-ng/history follow each other, with no route table to write.
- Each node has a path: its own
pathif it sets one, otherwisebase-pathplus the ids from the root down to the node, joined with/(each id URI-encoded). A tree withbase-path="/explore"gives the nodehr>comp>paythe path/explore/hr/comp/pay. - Selecting a row (click, Enter, or Space) emits
dj-navigatewith{ path, label, trail }alongsidedj-select.trailis the nodes from the root to this one, each as{ path, label }, and ends up in the history entry, so it survives Back, Forward, and a reload in the same tab. - When the location changes, the tree selects the node whose path matches it (an exact match, else the node with the longest path that is a prefix of the location) and expands that node's ancestors, emitting
dj-expand-changefor each. This is what makes Back reselect the right row. It does not emitdj-navigate, because that would loop. It does not emitdj-selecteither. - If the location has no
trail(a deep link, or a pasted URL), the tree adds the matched node's trail to the history entry, so adj-back-buttoninupmode can name the parent. - Ctrl-click, Cmd-click, or middle-click on a row opens its path in a new tab. Rows stay
treeitems and not links, because a focusable link inside a treeitem would break the tree's roving focus. Two nodes with the same path: the first in tree order wins. trackneeds@dojo-ng/historyinstalled. Without it nothing is tracked and nothing throws.
Not built
- Dragging tree rows, virtualization, checkboxes, and lazy loading.
Need one of these? Make a request on Discord or add an issue (work item) on Heptapod.
Properties
| Property | Attribute | Type | Default |
|---|---|---|---|
nodesThe tree data. |
nodes |
TreeNode[] |
[] |
valueThe selected node id. |
value |
string |
"" |
expandedThe expanded node ids (controlled). Toggling updates this and emits dj-expand-change. |
expanded |
string[] |
[] |
expandOnRowClickExpand or collapse a parent when its row is clicked, not only its chevron. Off by default, because a row click means "select" in a tree whose rows are all selectable. Turn it on when some rows exist only to contain others — an account above its mail folders, a directory above its files — where clicking the row would otherwise do nothing at all. Selection still happens and dj-select still fires; this only adds the toggle. Leaf rows are unaffected. |
expand-on-row-click reflected |
boolean |
false |
canDropMakes the tree a drop target. Called during a drag with a row id and the drag's dataTransfer.types; return true when that row accepts the drop. Unset: the tree ignores drags. |
Property only | (id: string, types: readonly string[]) => boolean |
None |
springDelayMilliseconds a drag must rest on a collapsed parent before it expands. 0 turns this off. |
spring-delay |
number |
800 |
trackFollow and drive the navigation history: selecting a row emits dj-navigate, and the location selects and expands the matching row. Needs @dojo-ng/history installed. |
track |
boolean |
false |
basePathWith track, the path every derived node path starts with, such as /explore. |
base-path |
string |
"" |
Events
| Event | Description |
|---|---|
dj-expand-change | detail { id, expanded, expandedIds } |
dj-drop | detail { id, dataTransfer } |
dj-select | detail { id } |
dj-navigate | detail { path, label, trail }, only with track |
Slots
| Slot | Description |
|---|---|
none | content comes from nodes |
CSS parts
Style these with dj-tree::part(name).
| Part | Description |
|---|---|
row | a node's clickable line |
row--drop-target | added to the row a drag would drop on |
chevron | |
label | |
count |
CSS custom properties
| Property | Default | Description |
|---|---|---|
--dj-tree-indent | 1.1rem | Indentation added per nesting level. |
--dj-tree-count-color | var(--dj-color-text-muted) | Color of the trailing count badge. |
Types
The types that the properties above use, as they are declared in the source.
TreeNode
export interface TreeNode {
id: string;
label: string;
children?: TreeNode[];
/** A registered icon name (see `@dojo-ng/icon`), shown before the label. */
icon?: string;
/** A trailing count badge (e.g. an unread count). */
count?: number;
/**
* The app path this node stands for when the tree has `track`. Without it, the path is derived:
* the tree's `base-path`, then the ids from the root down to this node, joined with `/`.
*/
path?: string;
}