Skip to content

Commit 2ad3866

Browse files
committed
refactor(cdk/scrolling): add lightweight token for virtual scroll viewport
Adds a lightweight token for the virtual scroll viewport so it can be injected without a hard dependency.
1 parent ee065cc commit 2ad3866

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

goldens/cdk/scrolling/index.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export type _Bottom = {
2626
bottom?: number;
2727
};
2828

29+
// @public
30+
export const CDK_VIRTUAL_SCROLL_VIEWPORT: InjectionToken<CdkVirtualScrollViewport>;
31+
2932
// @public
3033
export class CdkFixedSizeVirtualScroll implements OnChanges {
3134
get itemSize(): number;

src/cdk/scrolling/virtual-for-of.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {NumberInput, coerceNumberProperty} from '../coercion';
3636
import {Observable, Subject, of as observableOf, isObservable} from 'rxjs';
3737
import {pairwise, shareReplay, startWith, switchMap, takeUntil} from 'rxjs/operators';
3838
import {CdkVirtualScrollRepeater} from './virtual-scroll-repeater';
39-
import {CdkVirtualScrollViewport} from './virtual-scroll-viewport';
39+
import {CDK_VIRTUAL_SCROLL_VIEWPORT} from './virtual-scroll-viewport';
4040

4141
/** The context for an item rendered by `CdkVirtualForOf` */
4242
export type CdkVirtualForOfContext<T> = {
@@ -87,7 +87,7 @@ export class CdkVirtualForOf<T>
8787
private _template = inject<TemplateRef<CdkVirtualForOfContext<T>>>(TemplateRef);
8888
private _differs = inject(IterableDiffers);
8989
private _viewRepeater = new _RecycleViewRepeaterStrategy<T, T, CdkVirtualForOfContext<T>>();
90-
private _viewport = inject(CdkVirtualScrollViewport, {skipSelf: true});
90+
private _viewport = inject(CDK_VIRTUAL_SCROLL_VIEWPORT, {skipSelf: true});
9191

9292
/** Emits when the rendered view of the data changes. */
9393
readonly viewChange = new Subject<ListRange>();

src/cdk/scrolling/virtual-scroll-viewport.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
effect,
2020
ElementRef,
2121
inject,
22+
InjectionToken,
2223
Injector,
2324
Input,
2425
OnDestroy,
@@ -57,6 +58,14 @@ function rangesEqual(r1: ListRange, r2: ListRange): boolean {
5758
const SCROLL_SCHEDULER =
5859
typeof requestAnimationFrame !== 'undefined' ? animationFrameScheduler : asapScheduler;
5960

61+
/**
62+
* Lightweight token that can be used to inject the `CdkVirtualScrollViewport`
63+
* without introducing a hard dependency on it.
64+
*/
65+
export const CDK_VIRTUAL_SCROLL_VIEWPORT = new InjectionToken<CdkVirtualScrollViewport>(
66+
'CDK_VIRTUAL_SCROLL_VIEWPORT',
67+
);
68+
6069
/** A viewport that virtualizes its scrolling with the help of `CdkVirtualForOf`. */
6170
@Component({
6271
selector: 'cdk-virtual-scroll-viewport',
@@ -75,6 +84,7 @@ const SCROLL_SCHEDULER =
7584
useFactory: () =>
7685
inject(VIRTUAL_SCROLLABLE, {optional: true}) || inject(CdkVirtualScrollViewport),
7786
},
87+
{provide: CDK_VIRTUAL_SCROLL_VIEWPORT, useExisting: CdkVirtualScrollViewport},
7888
],
7989
})
8090
export class CdkVirtualScrollViewport extends CdkVirtualScrollable implements OnInit, OnDestroy {

0 commit comments

Comments
 (0)