Skip to content

fix(cdk/scrolling): Prevent virtual scroll 'flickering' with zoneless #31316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/cdk/scrolling/virtual-scroll-viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import {ListRange} from '../collections';
import {Platform} from '../platform';
import {
afterNextRender,
ApplicationRef,
booleanAttribute,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
DestroyRef,
effect,
ElementRef,
inject,
Inject,
Expand Down Expand Up @@ -170,8 +173,7 @@ export class CdkVirtualScrollViewport extends CdkVirtualScrollable implements On
*/
private _renderedContentOffsetNeedsRewrite = false;

/** Whether there is a pending change detection cycle. */
private _isChangeDetectionPending = false;
private _changeDetectionNeeded = signal(false);

/** A list of functions to run after the next change detection cycle. */
private _runAfterChangeDetection: Function[] = [];
Expand Down Expand Up @@ -202,6 +204,17 @@ export class CdkVirtualScrollViewport extends CdkVirtualScrollable implements On
this.elementRef.nativeElement.classList.add('cdk-virtual-scrollable');
this.scrollable = this;
}

const ref = effect(
() => {
if (!this._changeDetectionNeeded()) {
return;
}
this._doChangeDetection();
},
{injector: inject(ApplicationRef).injector},
);
inject(DestroyRef).onDestroy(() => void ref.destroy());
}

override ngOnInit() {
Expand Down Expand Up @@ -488,16 +501,7 @@ export class CdkVirtualScrollViewport extends CdkVirtualScrollable implements On
this._runAfterChangeDetection.push(runAfter);
}

// Use a Promise to batch together calls to `_doChangeDetection`. This way if we set a bunch of
// properties sequentially we only have to run `_doChangeDetection` once at the end.
if (!this._isChangeDetectionPending) {
this._isChangeDetectionPending = true;
this.ngZone.runOutsideAngular(() =>
Promise.resolve().then(() => {
this._doChangeDetection();
}),
);
}
this._changeDetectionNeeded.set(true);
}

/** Run change detection. */
Expand All @@ -520,7 +524,7 @@ export class CdkVirtualScrollViewport extends CdkVirtualScrollable implements On

afterNextRender(
() => {
this._isChangeDetectionPending = false;
this._changeDetectionNeeded.set(false);
const runAfterChangeDetection = this._runAfterChangeDetection;
this._runAfterChangeDetection = [];
for (const fn of runAfterChangeDetection) {
Expand Down
Loading