Virtual List
A long list rendered only where it is seen — rows measured as they render, in a box or the page, down, along or in lanes; a chat that opens at its end. On TanStack Virtual's engine.
Installation
It has its own entry point, @semantic-components/ui/virtual-list, built on @tanstack/virtual-core — an optional peer of ui: install it where you use the component.
import { ScVirtualList, ScVirtualListRow } from '@semantic-components/ui/virtual-list';Usage
Usage
Ada 1
Grace 2
Alan 3
Katherine 4
Margaret 5
Hedy 6
Linus 7
Barbara 8
Dennis 9
Radia 10
Rendered: 1–10 of 10000
import { Component, ViewEncapsulation } from '@angular/core';import { ScFrame } from '@semantic-components/ui';import { ScVirtualList, ScVirtualListRow,} from '@semantic-components/ui/virtual-list';
/** 10,000 rows, a few dozen rendered: those in view and five more each way, each measured as it renders; its place in the whole list is aria-posinset of aria-setsize. */@Component({ selector: 'app-virtual-list-usage-demo',Anatomy
The list on your ul (or any element), and your rows from its rows() — read your data by row.index, track by row.key.
import { ScVirtualList, ScVirtualListRow } from '@semantic-components/ui/virtual-list';<ul scVirtualList #list="scVirtualList" [count]="items.length" [estimate]="56" aria-label="Contacts" class="h-96 overflow-y-auto"> @for (row of list.rows(); track row.key) { <li [scVirtualListRow]="row">{{ items[row.index].name }}</li> }</ul>Sizes and scrolling
estimate: a row's size before it is measured — the largest likely one. Each row is measured as it renders and again when it changes (an image loading, text wrapping); a row above the view settling keeps your place.- Give it a size and an overflow (
h-96 overflow-y-auto) and it scrolls itself; without, it takes its full size and the box around it — or the page — scrolls it. key: your items' ids, when rows can move or come in above — sizes and the place you read are kept by key.anchor="end": a chat — it opens at the end, older rows come in above without moving what you read, and new ones are followed while you are at the end (follow); scrolling yourself stops a scroll on its way.- Before it can measure — on the server, at the first paint — it renders its first
initialCountrows (10): content, not an empty list.
With the infinite scroll
Long, and longer: an scInfiniteScroll around the list asks for 100 more near the end; the list renders only what is seen.
With the infinite scroll
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScFrame, ScInfiniteScroll } from '@semantic-components/ui';import { ScVirtualList, ScVirtualListRow,} from '@semantic-components/ui/virtual-list';
/** Long, and longer: the infinite scroll asks for 100 more near the end; the virtual list renders only what is seen. */@Component({ selector: 'app-infinite-virtual-list-demo',Accessibility
- Each row says where it is in the whole list:
aria-posinsetofaria-setsize. - The row with focus stays rendered as you scroll away: focus is never lost.
- Name the list; give it
tabindex="0"when its rows can't take focus, so it can be scrolled with the keys. - Rows out of view aren't in the page: the browser's find doesn't see them.
Examples
Variable sizes
Ada
the plan for the. the plan for the release is ready and the team would like your thoughts before friday when we meet to decide.
Grace
the plan for the release is ready and the team would.
Alan
the plan for the release is ready and the team would like your thoughts before friday when we.
Katherine
the plan for the release is ready.
Margaret
the plan for the release is ready and the team would like your thoughts.
Hedy
the plan for the release is ready and the team would like your thoughts before friday when we meet to decide. the plan for the release is ready and the team would like your thoughts before friday when we meet to decide.
Linus
the plan for the release is ready and the team.
Barbara
the plan for the release is ready and the team would like your thoughts before friday when.
Dennis
the plan for the release is.
Radia
the plan for the release is ready and the team would like your.
Rendered: 1–10 of 2000
import { Component, ViewEncapsulation } from '@angular/core';import { ScFrame } from '@semantic-components/ui';import { ScVirtualList, ScVirtualListRow,} from '@semantic-components/ui/virtual-list';
/** Rows of any height — messages long and short: estimated, then measured; the list and its scrollbar follow, and you keep your place as rows above settle. */@Component({ selector: 'app-variable-virtual-list-demo',The page scrolls
- Entry 1: all good
- Entry 2: all good
- Entry 3: all good
- Entry 4: all good
- Entry 5: all good
- Entry 6: all good
- Entry 7: all good
- Entry 8: all good
- Entry 9: all good
- Entry 10: all good
Rendered: 1–10 of 500
import { Component, ViewEncapsulation } from '@angular/core';import { ScFrame } from '@semantic-components/ui';import { ScVirtualList, ScVirtualListRow,} from '@semantic-components/ui/virtual-list';
/** Without a size of its own it takes its full height, and the page — or the box around it — scrolls it. */@Component({ selector: 'app-page-virtual-list-demo',Horizontal
- Day1
- Day2
- Day3
- Day4
- Day5
- Day6
- Day7
- Day8
- Day9
- Day10
Rendered: 1–10 of 365
import { Component, ViewEncapsulation } from '@angular/core';import { ScFrame } from '@semantic-components/ui';import { ScVirtualList, ScVirtualListRow,} from '@semantic-components/ui/virtual-list';
/** Along a row: the days of a year, as a strip. */@Component({ selector: 'app-horizontal-virtual-list-demo',Grid
Rendered: 1–10 of 600
import { NgOptimizedImage } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import { ScFrame } from '@semantic-components/ui';import { ScVirtualList, ScVirtualListRow,} from '@semantic-components/ui/virtual-list';
/** In lanes: photos in three columns, each placed in the lane that ends first. */@Component({Chat
- Ada: Message 1000
- Grace: Message 1001
- Alan: Message 1002
- Katherine: Message 1003
- Margaret: Message 1004
- Hedy: Message 1005
- Linus: Message 1006
- Barbara: Message 1007
- Dennis: Message 1008
- Radia: Message 1009
At the end · 50 messages
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScButton, ScFrame, ScInput } from '@semantic-components/ui';import { ScVirtualList, ScVirtualListRow,} from '@semantic-components/ui/virtual-list';
/** anchor="end", a chat: it opens at the end; older messages come in above and the one you read stays put; at the end, new ones are followed — read back, they are not. */@Component({ selector: 'app-chat-virtual-list-demo',Scroll to a row
Ada 1
Grace 2
Alan 3
Katherine 4
Margaret 5
Hedy 6
Linus 7
Barbara 8
Dennis 9
Radia 10
Rendered: 1–10 of 10000
import { Component, ViewEncapsulation } from '@angular/core';import { ScButton, ScFrame } from '@semantic-components/ui';import { ScVirtualList, ScVirtualListRow,} from '@semantic-components/ui/virtual-list';
/** scrollToIndex(): to any row — even one never rendered — at the start, the centre or the end, at once or smoothly. */@Component({ selector: 'app-scroll-to-virtual-list-demo',RTL
- يوم1
- يوم2
- يوم3
- يوم4
- يوم5
- يوم6
- يوم7
- يوم8
- يوم9
- يوم10
Rendered: 1–10 of 365
import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation } from '@angular/core';import { ScFrame } from '@semantic-components/ui';import { ScVirtualList, ScVirtualListRow,} from '@semantic-components/ui/virtual-list';
/** In a right-to-left page: a strip starts at the right and runs as the text reads. */@Component({API reference
From @semantic-components/ui/virtual-list. Each part takes class, merged over its own.
| Part | Selector | Description |
|---|---|---|
ScVirtualList | [scVirtualList] | The list: what to render. exportAs: rows(), range(), size(), scrollToIndex(), scrollToEnd(), isAtEnd(), cancelScroll(). |
ScVirtualListRow | [scVirtualListRow] | A row: placed, measured; aria-posinset / aria-setsize. |
| Input | Type | Default | Description |
|---|---|---|---|
count | number | — | How many rows. |
estimate | number | 40 | A row’s size before it is measured (px). |
key | (index) => number | string | the index | Each row’s key: your ids. |
overscan | number | 5 | Rows rendered beyond each end of the view. |
orientation | 'vertical' | 'horizontal' | 'vertical' | Down, or along (from the right, right to left). |
lanes, gap | number | 1, 0 | Rows side by side; room between them (px). |
anchor | 'start' | 'end' | 'start' | `end`: a chat. |
follow | boolean | anchor = end | At the end, follow rows added after it. |
initialCount | number | 10 | Rows rendered before it can measure (the server). |