Skip to content

feat(cdk/tree): add virtual scrolling support - #34175

Open
R-Omar wants to merge 1 commit into
angular:mainfrom
R-Omar:tree-virtual-scroll
Open

R-Omar wants to merge 1 commit into
angular:mainfrom
R-Omar:tree-virtual-scroll

Conversation

@R-Omar

@R-Omar R-Omar commented Nov 23, 2026

Copy link
Copy Markdown

Desciption

Updates the CDK tree to add support for virtual scrolling. This follows the same approach as the table support added in #32487, adapted for the tree's data model and keyboard navigation.

To enable virtual scrolling for the tree, you need to do two things.

  1. Add <cdk-virtual-scroll-viewport> around it:
<cdk-virtual-scroll-viewport itemSize="48">
  <cdk-tree [dataSource]="dataSource" [childrenAccessor]="childrenAccessor">
    <cdk-tree-node *cdkTreeNodeDef="let node">
      {{node.name}}
    </cdk-tree-node>
  </cdk-tree>
</cdk-virtual-scroll-viewport>
  1. Ensure that the viewport is scrollable in your CSS:
cdk-virtual-scroll-viewport {
  height: 600px;
  overflow: auto;
}

The tree detects the viewport in its host, attaches itself to it and only renders the nodes that are currently in the rendered range. No extra imports or inputs are required.

Implementation notes

  • Injects CDK_VIRTUAL_SCROLL_VIEWPORT with host: true, so only a viewport in the same host enables the feature (same a s CdkTable).
  • Forwards renderedRangeStream to viewChange and slices the flattened nodes to the rendered range before rendering.
  • Pushes the full flattened list through _dataStream so the viewport knows the total item count and can size the scrollbar correctly.
  • Implements measureRangeSize so the autosize strategy can measure rendered nodes.
  • Scrolls the viewport when keyboard navigation moves the active node outside of the rendered range, and handles arrow/home/end keys at the window edges.
  • Throws a dev-mode error when virtual scrolling is used with a nested tree, since nested nodes render their children inside the parent and there is no flat list to virtualize.
  • Adds an internal _disableVirtualScrolling opt out, mirroring CdkTable.

Testing

  • Added unit tests covering windowing, scrolling, total data length reporting and the nested-tree guard.
  • Added a cdk-tree-virtual-scroll example and a dev-app entry.

Related to #10122.

Adds support for rendering the tree inside of a `cdk-virtual-scroll-viewport`.
The tree detects the viewport in its host, attaches itself to it and only
renders the nodes that are currently in the rendered range.

Key pieces:
- Injects `CDK_VIRTUAL_SCROLL_VIEWPORT` with `host: true` so that only a
  viewport in the same host enables the feature.
- Forwards `renderedRangeStream` to `viewChange` and slices the flattened
  nodes to the rendered range before rendering.
- Pushes the full flattened list through `_dataStream` so that the viewport
  knows the total number of items and can size the scrollbar correctly.
- Implements `measureRangeSize` so that the `autosize` strategy can measure
  the rendered nodes.
- Scrolls the viewport when keyboard navigation moves the active node outside
  of the rendered range, and handles arrow/home/end keys at the window edges.
- Throws a dev-mode error when virtual scrolling is used with a nested tree,
  since nested nodes render their children inside the parent and there is no
  flat list to virtualize.
- Adds an internal `_disableVirtualScrolling` opt out, mirroring `CdkTable`.
@angular-robot angular-robot Bot added detected: feature PR contains a feature commit area: cdk/tree labels Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: cdk/tree detected: feature PR contains a feature commit

1 participant