Dojo NG is being built in the open, one section at a time — follow along on Heptapod.

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.

Try it in the playground

@dojo-ng/tree · v0.3.0

npm install @dojo-ng/tree

import "@dojo-ng/tree";

Selection and expansion

  • Both are controlled. value is the selected node id, and the tree emits dj-select.
  • expanded is the array of open node ids, and the tree emits dj-expand-change.
  • Clicking a row selects it; clicking the chevron expands or collapses it.
  • Set expand-on-row-click to 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 canDrop to make the tree a target for native HTML5 drag and drop. It is called during a drag with a row id and the drag's dataTransfer.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-target part 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-delay ms (default 800; 0 turns it off) during a drag expands it, through the same path as a chevron click, so dj-expand-change fires. It stays open after the drop.
  • A drop emits dj-drop with { id, dataTransfer }. The tree does not select the row; the consumer reads its payload from dataTransfer and 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 path if it sets one, otherwise base-path plus the ids from the root down to the node, joined with / (each id URI-encoded). A tree with base-path="/explore" gives the node hr > comp > pay the path /explore/hr/comp/pay.
  • Selecting a row (click, Enter, or Space) emits dj-navigate with { path, label, trail } alongside dj-select. trail is 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-change for each. This is what makes Back reselect the right row. It does not emit dj-navigate, because that would loop. It does not emit dj-select either.
  • 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 a dj-back-button in up mode 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.
  • track needs @dojo-ng/history installed. 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

PropertyAttributeTypeDefault
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

EventDescription
dj-expand-changedetail { id, expanded, expandedIds }
dj-dropdetail { id, dataTransfer }
dj-selectdetail { id }
dj-navigatedetail { path, label, trail }, only with track

Slots

SlotDescription
nonecontent comes from nodes

CSS parts

Style these with dj-tree::part(name).

PartDescription
rowa node's clickable line
row--drop-targetadded to the row a drag would drop on
chevron
label
count

CSS custom properties

PropertyDefaultDescription
--dj-tree-indent1.1remIndentation added per nesting level.
--dj-tree-count-colorvar(--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;
}