Tree

A hierarchical list that expands and collapses: file browsers, categories, org charts. Nodes are typed and identified by key, so the selection and the expanded nodes are plain arrays of keys that you can store, put in a URL or send to a server.

  • vig-tree
  • ng-template[vigTreeNode]
Import
import { Tree, TreeNodeTemplate } from '@vigdx/ui/tree';

Basic

Pass the top-level nodes , each a TreeNode<T> with a unique key , a label , optional children , icon , disabled and typed data . selectionMode="single" makes nodes selectable (the default none is a navigation tree, where a click expands); [(value)] holds the selected keys and [(expandedKeys)] the open ones. selectable: false keeps a node out of the selection without dimming it, e.g. a folder of pickable files. nodeActivated fires on a click or Enter, for opening a file; nodeExpanded , nodeCollapsed , nodeSelected and nodeUnselected report the rest.

src
main.ts
styles.css
package.json
README.md

Selected: src/main.ts · Last opened: none

Checkboxes

selectionMode="checkbox" draws a checkbox per node. With cascade (the default) checking a folder checks its branch and a folder shows checked, unchecked or mixed from its children. Disabled and selectable: false nodes keep their own state: checking passes through them to their descendants, and their parent ignores them. While filterQuery is set, checking reaches the nodes the filter shows only. The value lists every checked key, folders included. Turn cascade off for independent checkboxes. multiple selects without checkboxes: each click toggles a node, Shift+click selects a range.

src
app
app.ts
app.html
routes.ts
main.ts
styles.css
docs
guide.md
legacy.md
package.json
README.md

1 keys checked

Lazy loading

loadChildren(node, signal) returns a promise of the children of a node without children ; it runs on first expand, in the browser only. The node is busy meanwhile ( aria-busy ), a rejected promise shows a row that retries, and collapsing the node aborts signal , so pass it on to fetch . Mark nodes that never have children with leaf: true . Loaded children are kept per key, also across new nodes with the same keys, and dropped for keys that leave the data or when loadChildren changes; call reload(key?) when the server's data changed.

notes.txt

Filtering and custom nodes

filterQuery keeps the nodes whose label contains the text (case- and accent-insensitive, in the active locale), their ancestors, which open, and their descendants. Compose it with any search field. A vigTreeNode template replaces the icon and label of each node; bind [vigTreeNodeOf] to type let-node and its data .

src
app
app.ts2 KB
routes.ts1 KB
main.ts1 KB

Virtual scrolling

virtual renders only the rows in view, for trees of thousands of nodes. The tree then scrolls itself at --vig-tree-height (20rem by default) and labels stay on one line. Keyboard focus, aria-setsize and aria-posinset describe the whole tree, not the rendered window.

Styling hooks

Internal elements carry a stable data-part and the component's data-scope; both are public API, so removing one is a breaking change. Select a part by both, e.g. [data-scope="tree"][data-part="root"] { … }: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through aria-* and, where listed, a data-state; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other data-* attributes listed below (such as data-opening while an overlay animates) are API too. See Customizing.

Scope: [data-scope="tree"]

Tree styling hooks
Part Element data-state
root The vig-tree host. —
tree The role="tree" element (the one Tab stop is a row in it). —
item A role="treeitem" row ( aria-level , aria-expanded , aria-selected or aria-checked , aria-busy while its children load). open, closed
toggle The expand chevron of a row (pointer only). —
loading The busy ring in place of the chevron while children load. —
checkbox The tri-state box of a row ( selectionMode="checkbox" ). —
icon The node icon. —
label The node text. —
content Wrapper of a custom vigTreeNode template. —
error The row under a node whose children failed to load. —
retry The retry text of that row. —
empty The role="status" region: the message without nodes or filter matches, and the announcement of a failed load. —
viewport The scrolling viewport of a virtual tree. —

Accessibility

  • Follows the WAI-ARIA APG treeview pattern: a role="tree" named by labels.ariaLabel (name every tree) of role="treeitem" rows with aria-level, aria-setsize, aria-posinset and aria-expanded. Selected rows carry aria-selected, checkbox rows aria-checked (mixed for a partly checked folder), and the tree is aria-multiselectable in multiple and checkbox modes.
  • The tree is one Tab stop (a roving tab index). ArrowDown and ArrowUp move, ArrowRight opens a node or enters it, ArrowLeft closes it or goes to its parent (mirrored in right-to-left pages), Home and End jump, * opens every sibling, and typing moves to the next node starting with the text. Enter and Space select (toggle in multiple, check in checkbox); in multiple, Shift+Arrow extends the selection and Ctrl+A selects every visible node.
  • A node whose children load is aria-busy and described as loading; a failed load is announced through the tree's status region and adds a row that retries with Enter, Space or a click.
  • The selected row has a bar at its inline start besides its fill, and the chevron's direction shows expansion, so neither state rests on color alone; in forced-colors mode the bar takes the system Highlight color.
  • With virtual, when the focused row scrolls out of the DOM focus stays in the tree, and the arrow keys bring the row back; once the row renders again it takes focus back. Arrow, Home and End keys held with Alt, Ctrl or Meta are left to the browser.

API

Tree

vig-tree component

A hierarchical list (APG treeview pattern) that expands and collapses, with optional selection: single, multiple, or tri-state checkboxes that check whole branches. Nodes are typed (TreeNode<T>) and identified by key, so value and expandedKeys are plain key arrays. Children can load on first expand (loadChildren, cancelled when the node collapses), filterQuery narrows the tree to matches and their ancestors, and virtual renders only the rows in view, for trees of thousands of nodes.

Example :
<vig-tree [nodes]="files" selectionMode="single" [(value)]="picked"
  [labels]="{ ariaLabel: 'Files' }" />

One Tab stop (a roving tab index): ArrowUp/ArrowDown move, ArrowRight expands or enters a node, ArrowLeft collapses or goes to the parent (mirrored in RTL), Home/End jump, * expands the siblings, Enter and Space select, and typing jumps to the next node starting with the text.

Tree props
Name Type Default Description
cascadebooleantrue

checkbox mode: checking a node checks its selectable descendants, and a parent shows checked, unchecked or mixed from them. Disabled and selectable: false nodes keep their own state. Off, every checkbox stands alone. While filterQuery is set, checking reaches the nodes the filter shows only.

expandedKeys model readonly string[][]

Keys of the expanded nodes.

filterQuerystring''

Narrows the tree to the nodes whose label contains the text (case- and accent-insensitive, in the active locale), their ancestors, which expand, and their descendants. Only loaded nodes are searched.

labelsPartial<TreeLabels>—

Texts (i18n); locale defaults. Set ariaLabel to name the tree.

loadChildrenTreeLoader<T> | undefinedundefined

Fetches the children of nodes that have none of their own; see TreeLoader. Another loader starts afresh: what the previous one loaded is dropped, and its pending answers are ignored.

nodesreadonly TreeNode<T>[][]

The top-level nodes.

selectionModeTreeSelectionMode'none'

How nodes are selected; none (navigation only) by default.

sizeFieldSize'md'
—
value model readonly string[][]

Keys of the selected (or, in checkbox mode, checked) nodes, in every mode an array: single uses its first key only. With cascade, a checked branch lists its parent and every loaded selectable descendant.

virtualbooleanfalse

Renders only the rows in view (CDK virtual scrolling), for large trees. The tree then scrolls itself at --vig-tree-height (20rem by default), and labels stay on one line. Rows don't size the tree, so give it a definite width inside shrink-to-fit parents (inline-block, w-fit).

TreeNodeTemplate

ng-template[vigTreeNode] directive

Custom content of each node of vig-tree (and vig-tree-select), replacing the default icon and label; the expand toggle and the checkbox stay. Bind the nodes to vigTreeNodeOf to type let-node:

Example :
<vig-tree [nodes]="files">
  <ng-template vigTreeNode [vigTreeNodeOf]="files" let-node>
    {{ node.label }} <small>{{ node.data?.size }}</small>
  </ng-template>
</vig-tree>
TreeNodeTemplate props
Name Type Default Description
vigTreeNodeOfreadonly TreeNode<T>[] | null | undefinedundefined

Type-only: the nodes the template renders, so let-node is typed. Not read at runtime.

Are you sure?