Skip to content

fix(cdk/scrolling): use capturing for scroll event #30824

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 17 additions & 1 deletion src/cdk/scrolling/scroll-dispatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import {Component, ViewChild, ElementRef} from '@angular/core';
import {CdkScrollable, ScrollDispatcher, ScrollingModule} from './public-api';
import {dispatchFakeEvent} from '../testing/private';
import {filter} from 'rxjs/operators';

describe('ScrollDispatcher', () => {
beforeEach(waitForAsync(() => {
Expand Down Expand Up @@ -106,7 +107,10 @@ describe('ScrollDispatcher', () => {
it('should not register the same scrollable twice', () => {
const scrollable = fixture.componentInstance.scrollable;
const scrollSpy = jasmine.createSpy('scroll spy');
const scrollSubscription = scroll.scrolled(0).subscribe(scrollSpy);
const scrollSubscription = scroll
.scrolled(0)
.pipe(filter(target => target === scrollable))
.subscribe(scrollSpy);

expect(scroll.scrollContainers.has(scrollable)).toBe(true);

Expand All @@ -119,6 +123,18 @@ describe('ScrollDispatcher', () => {
expect(scrollSpy).not.toHaveBeenCalled();
scrollSubscription.unsubscribe();
});

it('should register a capturing scroll event on the document', () => {
const spy = spyOn(document, 'addEventListener').and.callThrough();
const subscription = scroll.scrolled(0).subscribe();

expect(spy).toHaveBeenCalledWith(
'scroll',
jasmine.any(Function),
jasmine.objectContaining({capture: true}),
);
subscription.unsubscribe();
});
});

describe('Nested scrollables', () => {
Expand Down
30 changes: 27 additions & 3 deletions src/cdk/scrolling/scroll-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@

import {coerceElement} from '../coercion';
import {Platform} from '../platform';
import {ElementRef, Injectable, NgZone, OnDestroy, RendererFactory2, inject} from '@angular/core';
import {
DOCUMENT,
ElementRef,
Injectable,
NgZone,
OnDestroy,
RendererFactory2,
inject,
} from '@angular/core';
import {of as observableOf, Subject, Subscription, Observable, Observer} from 'rxjs';
import {auditTime, filter} from 'rxjs/operators';
import type {CdkScrollable} from './scrollable';
Expand All @@ -25,7 +33,9 @@ export class ScrollDispatcher implements OnDestroy {
private _ngZone = inject(NgZone);
private _platform = inject(Platform);
private _renderer = inject(RendererFactory2).createRenderer(null, null);
private _document = inject(DOCUMENT);
private _cleanupGlobalListener: (() => void) | undefined;
private _lastScrollFromDocument = false;

constructor(...args: unknown[]);
constructor() {}
Expand Down Expand Up @@ -87,7 +97,15 @@ export class ScrollDispatcher implements OnDestroy {
return new Observable((observer: Observer<CdkScrollable | void>) => {
if (!this._cleanupGlobalListener) {
this._cleanupGlobalListener = this._ngZone.runOutsideAngular(() =>
this._renderer.listen('document', 'scroll', () => this._scrolled.next()),
this._renderer.listen(
'document',
'scroll',
(event: Event) => {
this._lastScrollFromDocument = event.target === this._document;
this._scrolled.next();
},
{capture: true},
),
);
}

Expand All @@ -105,6 +123,7 @@ export class ScrollDispatcher implements OnDestroy {
this._scrolledCount--;

if (!this._scrolledCount) {
this._lastScrollFromDocument = false;
this._cleanupGlobalListener?.();
this._cleanupGlobalListener = undefined;
}
Expand Down Expand Up @@ -132,7 +151,12 @@ export class ScrollDispatcher implements OnDestroy {
const ancestors = this.getAncestorScrollContainers(elementOrElementRef);

return this.scrolled(auditTimeInMs).pipe(
filter(target => !target || ancestors.indexOf(target) > -1),
filter(target => {
// The document is using capturing for its `scroll` event which means that we'll usually
// get two events here. This is what we want in most cases, but for the ancestor scrolling
// we actually want to know the exact ancestor that was scrolled.
return target ? ancestors.indexOf(target) > -1 : this._lastScrollFromDocument;
}),
);
}

Expand Down
Loading