@rdlabo/ngx-cdk-scroll-strategies 22.0.1-0 → 22.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,12 +2,20 @@
2
2
 
3
3
  > Angular CDK virtual scroll with variable and dynamic item heights.
4
4
 
5
- ## Overview
6
-
7
5
  `@rdlabo/ngx-cdk-scroll-strategies` is an Angular CDK virtual scroll strategy for lists with variable item heights. It lets you supply the exact pixel size of every item instead of requiring one fixed `[itemSize]` for the whole list.
8
6
 
9
7
  Use `[itemDynamicSizes]` with known or measured item heights. Unlike the experimental `[autosize]` strategy, this library does not estimate unmeasured items from an average size. It works with `@angular/cdk/scrolling` and does not depend on Ionic.
10
8
 
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @rdlabo/ngx-cdk-scroll-strategies
13
+ ```
14
+
15
+ Then follow [Simple Usage](https://docs.rdlabo.dev/projects/ngx-cdk-scroll-strategies/docs/simple) for a complete viewport with known heights.
16
+
17
+ Every data item must have one corresponding `itemDynamicSizes` entry in the same order. Each `itemSize` must be a finite number greater than zero. If Angular updates the data and size signals in separate turns, the strategy keeps the last complete geometry until their lengths match; it never estimates unknown heights.
18
+
11
19
  ## When to use this strategy
12
20
 
13
21
  Use this library when:
@@ -19,27 +27,9 @@ Use this library when:
19
27
 
20
28
  If an item height is not known in advance, measure it and pass the result as shown in [Advanced Usage](https://docs.rdlabo.dev/projects/ngx-cdk-scroll-strategies/docs/advanced). This is not a drop-in strategy that discovers every unknown DOM height automatically.
21
29
 
22
- The basic Angular CDK variable-height virtual scroll setup is:
23
-
24
- ```html
25
- <cdk-virtual-scroll-viewport
26
- [itemDynamicSizes]="[{ itemSize: 100 } , { itemSize: 80} , { itemSize: 90 } , { itemSize: 100}]"
27
- >
28
- <div *cdkVirtualFor="let item of [100, 80, 90, 100]; trackBy: trackByFn" [style.height.px]="item">
29
- itemSize: {{ item }}
30
- </div>
31
- </cdk-virtual-scroll-viewport>
32
- ```
33
-
34
- Use the `[itemDynamicSizes]` directive instead of `[itemSize]` or `[autosize]`. Its value has the type `itemDynamicSize[]`.
35
-
36
- Every data item must have one corresponding `itemDynamicSizes` entry in the same order. Each `itemSize` must be a finite number greater than zero. If Angular updates the data and size signals in separate turns, the strategy keeps the last complete geometry until their lengths match; it never estimates unknown heights.
37
-
38
30
  This library is based largely on [Virtual scrolling of content with variable height with Angular](https://dev.to/georgii/virtual-scrolling-of-content-with-variable-height-with-angular-3a52).
39
31
 
40
- ## Features
41
-
42
- ### Choose by scrolling goal
32
+ ## Choose by scrolling goal
43
33
 
44
34
  | Goal | Guide |
45
35
  | --- | --- |
@@ -47,26 +37,6 @@ This library is based largely on [Virtual scrolling of content with variable hei
47
37
  | Measure item components | [Advanced Usage](https://docs.rdlabo.dev/projects/ngx-cdk-scroll-strategies/docs/advanced) |
48
38
  | Reverse chat-style scrolling | [Reverse Scroll](https://docs.rdlabo.dev/projects/ngx-cdk-scroll-strategies/docs/reverse) |
49
39
 
50
- ## Quick start
51
-
52
- After [Installation](#installation), bind `[itemDynamicSizes]` instead of `[itemSize]`. See [Simple Usage](https://docs.rdlabo.dev/projects/ngx-cdk-scroll-strategies/docs/simple).
53
-
54
- ## Installation
55
-
56
- ```bash
57
- npm install @rdlabo/ngx-cdk-scroll-strategies
58
- ```
59
-
60
-
61
- ## Documentation
62
-
63
- Start with [Installation](#installation), then pick a guide.
64
-
65
- - [Simple Usage](https://docs.rdlabo.dev/projects/ngx-cdk-scroll-strategies/docs/simple) — per-item heights.
66
- - [Advanced Usage](https://docs.rdlabo.dev/projects/ngx-cdk-scroll-strategies/docs/advanced) — measured item components.
67
- - [Reverse Scroll](https://docs.rdlabo.dev/projects/ngx-cdk-scroll-strategies/docs/reverse) — chat-style reverse lists.
68
- - [FAQ](https://docs.rdlabo.dev/projects/ngx-cdk-scroll-strategies/docs/faq) — why not `autosize`.
69
-
70
40
  <!-- rdlabo-docs-omit -->
71
41
  **Full documentation:** [https://docs.rdlabo.dev/projects/ngx-cdk-scroll-strategies](https://docs.rdlabo.dev/projects/ngx-cdk-scroll-strategies)
72
42
  <!-- /rdlabo-docs-omit -->
package/docs/advanced.md CHANGED
@@ -1,7 +1,94 @@
1
- Call this after [Installation](../README.md#installation).
1
+ Call this after [Installation](../README.md#installation). Reuse the [Simple Usage](./simple.md) viewport, CDK imports, `trackBy`, and size-model rules. Replace known `itemSize` values with measured heights.
2
2
 
3
- > This Angular CDK dynamic item height example measures every scroll item as a separate component.
4
- > It is difficult without basic knowledge of Angular.
3
+ > Measure each scroll item as a separate component, then write the result into the size model that drives `[itemDynamicSizes]`.
5
4
 
6
5
  - Demo: https://rdlabo-ionic-angular-library.netlify.app/main/scroll-strategies/advanced
7
- - Source: https://github.com/rdlabo-dev/ionic-angular-library/tree/v21.6.2/projects/demo/src/app/scroll-strategies/pages/scroll-advanced
6
+ - Source: https://github.com/rdlabo-dev/ionic-angular-library/tree/v22.0.0/projects/demo/src/app/scroll-strategies/pages/scroll-advanced
7
+
8
+ Keep a measurement cache keyed by `trackId`. After the item renders, read its height and update the cache. A parent `computed` maps items to `itemDynamicSize[]`, using the cache when present and a temporary estimate until the first measurement lands:
9
+
10
+ ```ts
11
+ import { afterRenderEffect, Component, computed, ElementRef, inject, Injectable, input, signal, untracked } from '@angular/core';
12
+ import { CdkVirtualForOf, CdkVirtualScrollViewport } from '@angular/cdk/scrolling';
13
+ import { CdkDynamicSizeVirtualScroll, itemDynamicSize } from '@rdlabo/ngx-cdk-scroll-strategies';
14
+
15
+ type Row = { trackId: string; body: string };
16
+ type SizeCache = { trackId: string; itemSize: number };
17
+
18
+ @Injectable()
19
+ export class RowSizeCache {
20
+ readonly entries = signal<SizeCache[]>([]);
21
+ }
22
+
23
+ @Component({
24
+ selector: 'app-measured-row',
25
+ template: `<div class="row">{{ item().body }}</div>`,
26
+ styles: `:host { display: block; } .row { white-space: pre-wrap; padding: 12px; }`,
27
+ })
28
+ export class MeasuredRow {
29
+ private readonly el = inject(ElementRef<HTMLElement>);
30
+ private readonly sizes = inject(RowSizeCache);
31
+ readonly item = input.required<Row>();
32
+
33
+ constructor() {
34
+ afterRenderEffect((onCleanup) => {
35
+ const trackId = this.item().trackId;
36
+ const element = this.el.nativeElement;
37
+ const measure = () => {
38
+ const itemSize = Math.ceil(element.getBoundingClientRect().height);
39
+ if (itemSize <= 0) return;
40
+ untracked(() => this.sizes.entries.update((cache) => {
41
+ const previous = cache.find((entry) => entry.trackId === trackId);
42
+ if (previous?.itemSize === itemSize) return cache;
43
+ return [...cache.filter((entry) => entry.trackId !== trackId), { trackId, itemSize }];
44
+ }));
45
+ };
46
+ const observer = new ResizeObserver(measure);
47
+ observer.observe(element);
48
+ measure();
49
+ onCleanup(() => observer.disconnect());
50
+ });
51
+ }
52
+ }
53
+
54
+ @Component({
55
+ selector: 'app-scroll-advanced',
56
+ providers: [RowSizeCache],
57
+ styles: `cdk-virtual-scroll-viewport { height: 320px; width: 100%; }`,
58
+ imports: [CdkVirtualScrollViewport, CdkVirtualForOf, CdkDynamicSizeVirtualScroll, MeasuredRow],
59
+ template: `
60
+ <cdk-virtual-scroll-viewport
61
+ [itemDynamicSizes]="dynamicSize()"
62
+ minBufferPx="900"
63
+ maxBufferPx="1350"
64
+ >
65
+ <app-measured-row
66
+ *cdkVirtualFor="let item of items(); trackBy: trackByFn"
67
+ [item]="item"
68
+ />
69
+ </cdk-virtual-scroll-viewport>
70
+ `,
71
+ })
72
+ export class ScrollAdvancedPage {
73
+ private readonly sizes = inject(RowSizeCache);
74
+ readonly items = signal<Row[]>(
75
+ Array.from({ length: 20 }, (_, index) => ({
76
+ trackId: String(index),
77
+ body: `Row ${index + 1}\n`.repeat(1 + (index % 4)),
78
+ })),
79
+ );
80
+ readonly dynamicSize = computed<itemDynamicSize[]>(() =>
81
+ this.items().map((item) => {
82
+ const cached = this.sizes.entries().find((entry) => entry.trackId === item.trackId)?.itemSize;
83
+ return {
84
+ trackId: item.trackId,
85
+ itemSize: cached ?? 80,
86
+ source: cached === undefined ? 'temporary' : 'cache',
87
+ };
88
+ }),
89
+ );
90
+ trackByFn = (_: number, item: Row) => item.trackId;
91
+ }
92
+ ```
93
+
94
+ Each `itemSize` must remain a finite number greater than zero. Until measurement arrives, pass a temporary positive estimate so lengths stay aligned; replace it when the cache updates. See the demo source for Infinite Scroll, refresher, and `DynamicSizeVirtualScrollService` helpers around this same measurement flow.
package/docs/reverse.md CHANGED
@@ -1,9 +1,9 @@
1
1
  Call this after [Installation](../README.md#installation).
2
2
 
3
- > This demo combines variable item heights with reverse virtual scrolling for a chat UI like WeChat.
3
+ > Reuse the data model, `dynamicSize` computed, `trackBy`, and viewport imports from [Simple Usage](./simple.md). This page adds reverse layout CSS and `[isReverse]="true"` for a chat-style list.
4
4
 
5
5
  - Demo: https://rdlabo-ionic-angular-library.netlify.app/main/scroll-strategies/reverse
6
- - Source: https://github.com/rdlabo-dev/ionic-angular-library/tree/v21.6.2/projects/demo/src/app/scroll-strategies/pages/scroll-reverse
6
+ - Source: https://github.com/rdlabo-dev/ionic-angular-library/tree/v22.0.0/projects/demo/src/app/scroll-strategies/pages/scroll-reverse
7
7
 
8
8
  If reverse scroll, add `isReverse` directive to `cdk-virtual-scroll-viewport` tag.
9
9
 
@@ -31,7 +31,7 @@ Add css to `cdk-virtual-scroll-viewport.reverse-scroll` at global css file like
31
31
  ```css
32
32
  cdk-virtual-scroll-viewport {
33
33
  width: 100%;
34
- height: 100%;
34
+ height: 320px;
35
35
 
36
36
  /* .reverse-scroll class is added from this directive. */
37
37
  &.reverse-scroll {
@@ -60,7 +60,7 @@ div.reverse-items {
60
60
  ```
61
61
 
62
62
  **In Reverse Scroll, CdkVirtualScrollViewport's measureScrollOffset does not work. Please use the scrollOffset of this directive.**
63
- https://github.com/rdlabo-dev/ionic-angular-library/blob/v21.6.2/projects/scroll-strategies/src/lib/dynamic-size-virtual-scroll-strategy.ts
63
+ https://github.com/rdlabo-dev/ionic-angular-library/blob/v22.0.0/projects/scroll-strategies/src/lib/dynamic-size-virtual-scroll-strategy.ts
64
64
 
65
65
  The reverse layout uses negative native `scrollTop` values. `scrollToIndex()` accepts a logical item index as usual and converts its cumulative offset to that native coordinate internally.
66
66
 
@@ -72,4 +72,4 @@ This package contains a Helper Service that simplifies development with Virtual
72
72
  import { DynamicSizeVirtualScrollService } from '@rdlabo/ngx-cdk-scroll-strategies';
73
73
  ```
74
74
 
75
- Detail is here: https://github.com/rdlabo-dev/ionic-angular-library/blob/v21.6.2/projects/scroll-strategies/src/lib/dynamic-size-virtual-scroll.service.ts
75
+ Detail is here: https://github.com/rdlabo-dev/ionic-angular-library/blob/v22.0.0/projects/scroll-strategies/src/lib/dynamic-size-virtual-scroll.service.ts
package/docs/simple.md CHANGED
@@ -1,41 +1,57 @@
1
1
  Call this after [Installation](../README.md#installation).
2
2
 
3
- > This basic Angular CDK virtual scroll example is for items whose variable heights are already known.
3
+ > For items whose variable heights are already known.
4
4
 
5
5
  - Demo: https://rdlabo-ionic-angular-library.netlify.app/main/scroll-strategies/simple
6
- - Source: https://github.com/rdlabo-dev/ionic-angular-library/tree/v21.6.2/projects/demo/src/app/scroll-strategies/pages/scroll-simple
6
+ - Source: https://github.com/rdlabo-dev/ionic-angular-library/tree/v22.0.0/projects/demo/src/app/scroll-strategies/pages/scroll-simple
7
+
8
+ Give the viewport a definite height in global CSS:
9
+
10
+ ```css
11
+ cdk-virtual-scroll-viewport {
12
+ width: 100%;
13
+ height: 320px;
14
+ }
15
+ ```
7
16
 
8
17
  ```ts
18
+ import { Component, computed, signal } from '@angular/core';
19
+ import { CdkVirtualForOf, CdkVirtualScrollViewport } from '@angular/cdk/scrolling';
9
20
  import { CdkDynamicSizeVirtualScroll, itemDynamicSize } from '@rdlabo/ngx-cdk-scroll-strategies';
10
21
 
22
+ type Item = itemDynamicSize & { trackId: number };
23
+
11
24
  @Component({
12
- ...
13
- imports: [
14
- CdkDynamicSizeVirtualScroll
15
- ],
25
+ selector: 'app-scroll-simple',
26
+ imports: [CdkVirtualScrollViewport, CdkVirtualForOf, CdkDynamicSizeVirtualScroll],
27
+ template: `
28
+ <cdk-virtual-scroll-viewport
29
+ [itemDynamicSizes]="dynamicSize()"
30
+ minBufferPx="900"
31
+ maxBufferPx="1350"
32
+ >
33
+ <div
34
+ *cdkVirtualFor="let item of items(); trackBy: trackByFn"
35
+ class="dynamic-item"
36
+ [style.height.px]="item.itemSize"
37
+ >
38
+ itemSize: {{ item.itemSize }}
39
+ </div>
40
+ </cdk-virtual-scroll-viewport>
41
+ `,
16
42
  })
17
- export class ScrollStrategiesPage implements OnInit {
18
- readonly items = signal<itemDynamicSize[]>([]);
19
- readonly dynamicSize = computed<itemDynamicSize[]>(() => {
20
- return this.items().map((item) => ({ trackId: item.trackId, itemSize: item.itemSize }));
21
- });
43
+ export class ScrollSimplePage {
44
+ readonly items = signal<Item[]>(
45
+ Array.from({ length: 20 }, (_, index) => ({
46
+ trackId: index,
47
+ itemSize: 40 + (index % 5) * 16,
48
+ })),
49
+ );
50
+ readonly dynamicSize = computed<itemDynamicSize[]>(() =>
51
+ this.items().map((item) => ({ trackId: item.trackId, itemSize: item.itemSize })),
52
+ );
53
+ trackByFn = (_: number, item: Item) => item.trackId;
22
54
  }
23
55
  ```
24
56
 
25
- ```html
26
- <cdk-virtual-scroll-viewport
27
- [itemDynamicSizes]="dynamicSize()"
28
- minBufferPx="900"
29
- maxBufferPx="1350"
30
- >
31
- <div
32
- *cdkVirtualFor="let item of items(); trackBy: trackByFn"
33
- class="dynamic-item"
34
- [style.height.px]="item.itemSize"
35
- >
36
- itemSize: {{ item.itemSize }}
37
- </div>
38
- </cdk-virtual-scroll-viewport>
39
- ```
40
-
41
- Other than this, it works the same way as `@angular/cdk/scrolling`.
57
+ Rows use different heights so scrolling changes which sizes stay in view. Other than `[itemDynamicSizes]`, it works the same way as `@angular/cdk/scrolling`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rdlabo/ngx-cdk-scroll-strategies",
3
- "version": "22.0.1-0",
3
+ "version": "22.0.1",
4
4
  "description": "Angular CDK virtual scroll strategy for variable and dynamic item heights.",
5
5
  "repository": {
6
6
  "type": "git",