After moving a document editor's Angular CDK `cdk-tree` file explorer from `treeControl` to `childrenAccessor`, folders open by toggle click but not Right Arrow and lose `aria-expanded`; why?
answer
- the tree no longer infers it
- a per-node input defaulting to false
- expand() checks it, the toggle does not
- objects as expansion keys
- expansionKey for refreshed data
basics
~20 sWithout a treeControl, a cdk-tree-node is expandable only when its isExpandable input says so, and it defaults to false. Keyboard expansion and aria-expanded honour that flag, while cdkTreeNodeToggle calls the tree directly. Bind isExpandable on every folder node.
solid answer
~40 sWith the deprecated `treeControl`, each `cdk-tree-node` worked out whether it could expand from the control. Once the tree is driven by `childrenAccessor` or `levelAccessor`, the node's own `isExpandable` input is the only source, and it defaults to `false`. A non-expandable node renders no `aria-expanded`, and its `expand()` and `collapse()` do nothing, which is what the tree's keyboard handling calls on Right and Left Arrow. `cdkTreeNodeToggle` instead calls the tree's `toggle()` with the node's data directly, so mouse clicks still work and hide the bug. The fix is `[isExpandable]="!!node.children?.length"` on the node. While migrating, also give the tree an `expansionKey` such as the file path, because expansion is otherwise keyed by object identity and resets when the data source emits new objects.
code
ts · 19 linesimport {Component} from '@angular/core';
import {CdkTreeModule} from '@angular/cdk/tree';
interface FileNode { name: string; path: string; children?: FileNode[] }
@Component({
selector: 'app-file-explorer',
imports: [CdkTreeModule],
templateUrl: './file-explorer.html',
})
export class FileExplorer {
roots: FileNode[] = [];
childrenOf = (node: FileNode) => node.children ?? [];
pathOf = (node: FileNode) => node.path;
trackByPath = (_: number, node: FileNode) => node.path;
open(node: FileNode) {
if (!node.children) { /* open the document at node.path */ }
}
}go deeper
Know the shape: cdk-tree, a node template with cdkTreeNodeDef, a toggle button, and one accessor telling the tree how nodes relate.
Explain why accessor-driven trees need isExpandable, and how childrenAccessor and levelAccessor differ in what the data source must contain.
Diagnose a mouse-only tree from aria-expanded and arrow-key behaviour, then fix identity with expansionKey and trackBy before data refreshes collapse the explorer.
Decide between the data-driven CDK tree and markup-driven Angular Aria tree for the kit, and require keyboard and screen-reader checks on every tree migration.
## The CDK tree in one paragraph The Angular CDK tree (`cdk-tree` from `@angular/cdk/tree`) renders hierarchical data from a **data source** through **node templates** marked `*cdkTreeNodeDef`. It ships no styles; it ships roles (`role="tree"` on the host, `role="treeitem"` on nodes), `aria-level`, `aria-posinset`, `aria-setsize`, `aria-expanded`, and keyboard handling for the WAI-ARIA tree pattern. Angular Material's `mat-tree` extends it. A document editor's file explorer is a natural fit: folders, documents, expand and collapse, Enter to open. ## Three ways to describe the hierarchy The tree must be told how nodes relate, and exactly one of these inputs must be set. In development builds it throws if none or more than one is present. | Input | Data source contains | Status in v22 | |---|---|---| | `treeControl` | depends on the control class | **deprecated**; the guide says trees using it do not get the full accessibility treatment | | `childrenAccessor` | root nodes only; the function returns a node's children | current | | `levelAccessor` | every renderable node, flattened, with a depth | current | ## Why the migration broke the keyboard With a `treeControl`, a node asked the control whether it was a leaf. Without one, the node relies on its own **`isExpandable`** input, whose backing value starts as `false`. The source documents this: when you are not using a tree control, `isExpandable` should be provided for correct node accessibility. Three things then follow for every folder: 1. **`aria-expanded` disappears.** The node renders `aria-expanded` only for expandable nodes, so screen readers announce folders as plain items. 2. **Right Arrow cannot open a closed folder, and Left Arrow cannot close an open one.** The tree's keyboard handling expands or collapses the active item by calling the node's `expand()` or `collapse()`, and both return early unless the node is expandable. Moving up, down and to a parent still works, which makes the bug easy to miss. 3. **Clicks still work.** `cdkTreeNodeToggle` calls the tree's `toggle()` (or `toggleDescendants()` with `cdkTreeNodeToggleRecursive`) with the node's data, skipping the node's check. Mouse testing therefore passes, and the regression ships. ## The fix Bind `isExpandable` from the data, and set the expansion and identity hooks while you are there: ```html <cdk-tree #tree [dataSource]="roots" [childrenAccessor]="childrenOf" [expansionKey]="pathOf" [trackBy]="trackByPath"> <cdk-nested-tree-node *cdkTreeNodeDef="let node" [isExpandable]="!!node.children?.length" (activation)="open(node)" (click)="open(node)"> <button cdkTreeNodeToggle [attr.aria-label]="'Toggle ' + node.name">▸</button> {{ node.name }} @if (tree.isExpanded(node)) { <div role="group"><ng-container cdkTreeNodeOutlet /></div> } </cdk-nested-tree-node> </cdk-tree> ``` - **`isExpandable`** restores `aria-expanded` and keyboard expansion. - **`expansionKey`** decides what the tree stores in its expansion set. Without it the node object itself is the key, so a refresh that builds new objects (after a rename or a sync) collapses every folder. Returning the file path keeps folders open across refreshes. - **`trackBy`** lets the tree reuse rendered nodes across data emissions instead of re-creating them. - **`(activation)`** fires when a keyboard user activates a node, carrying the node's data, so Enter can open a document just as a click does. - **`cdkTreeNodeOutlet`** marks where a nested node's children render. With `childrenAccessor`, a nested tree renders a node's children into that outlet whether or not the node is expanded, so hiding collapsed folders is your template's job; here `@if (tree.isExpanded(node))` removes the outlet while the folder is closed. ## Flat or nested? A **nested** tree (`cdk-nested-tree-node` with `cdkTreeNodeOutlet`) mirrors the hierarchy in the DOM and suits `childrenAccessor`. A **flat** tree (`cdk-tree-node` siblings, indented with `cdkTreeNodePadding` or CSS on `aria-level`) suits `levelAccessor`. The flattened shape is friendlier to windowing long lists, though how to window them is a separate subject. ## How Angular Aria's tree compares Angular Aria offers `ngTree` and `ngTreeItem`, which take the opposite approach: you write the nested markup yourself, each `ngTreeItem` names its `[parent]`, and child groups sit in `ng-template ngTreeItemGroup`. There is no data source or node template; expansion is an `expanded` model on each item. Choose the CDK tree when data-driven rendering, node templates per type (`cdkTreeNodeDefWhen`) or Material's `mat-tree` lineage matter; choose `ngTree` when hand-shaped markup and signal models fit the kit better. ## Checklist after any CDK tree migration - Remove `treeControl` entirely rather than leaving it beside an accessor. - Bind `isExpandable` on every node template that can hold children. - Give every icon-only `cdkTreeNodeToggle` button an accessible name, as the guide asks. - Bind `(activation)` wherever `(click)` performs an action, so Enter does the same thing. - Test with the keyboard and a screen reader, not only the mouse.
- When would you give the CDK tree `levelAccessor` instead of `childrenAccessor`?When your data already arrives flattened, one array of every renderable node with a depth. The data source then owns expand and collapse: it listens to each node's `expandedChange` and emits the new visible list. `childrenAccessor` suits nested data, where the tree walks children and computes levels. Never pass both; in development the tree throws an error naming the conflict.
- Why does a CDK tree file explorer also need `(activation)` when nodes already handle `(click)`?A tree node is not a native button, so pressing Enter on it fires no `click` event. Instead, a keyboard user who presses Enter on an active node triggers the node's `activation` output, which carries the node's data. Binding both runs the same open-document action for every user, which the CDK tree guide recommends for nodes with actions.
- childrenAccessor makes the CDK tree infer which nodes are expandable
- treeControl is still the recommended way to drive a CDK tree
- trackBy alone keeps CDK tree folders open when the data refreshes
- Passing both levelAccessor and childrenAccessor is a harmless safety net
- cdk-tree ships an indented, styled file-tree look out of the box