Skip to content
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

feat: Annotate our scroll events as passive #1684

Merged
merged 1 commit into from
Jan 24, 2025
Merged
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
14 changes: 9 additions & 5 deletions src/entrypoints/dead-clicks-autocapture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class LazyLoadedDeadClicksAutocapture implements LazyLoadedDeadClicksAutocapture
this._mutationObserver?.disconnect()
this._mutationObserver = undefined
assignableWindow.removeEventListener('click', this._onClick)
assignableWindow.removeEventListener('scroll', this._onScroll, true)
assignableWindow.removeEventListener('scroll', this._onScroll, { capture: true })
assignableWindow.removeEventListener('selectionchange', this._onSelectionChange)
}

Expand All @@ -114,11 +114,15 @@ class LazyLoadedDeadClicksAutocapture implements LazyLoadedDeadClicksAutocapture
}
}

// `capture: true` is required to get scroll events for other scrollable elements
// on the page, not just the window
// see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#usecapture
//
// `passive: true` is used to tell the browser that the scroll event handler will not call `preventDefault()`
// This allows the browser to optimize scrolling performance by not waiting for our handling of the scroll event
// see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#passive
private _startScrollObserver() {
// setting the third argument to `true` means that we will receive scroll events for other scrollable elements
// on the page, not just the window
// see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#usecapture
assignableWindow.addEventListener('scroll', this._onScroll, true)
assignableWindow.addEventListener('scroll', this._onScroll, { capture: true, passive: true })
}

private _onScroll = (): void => {
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/rageclick.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Naive rage click implementation: If mouse has not moved than RAGE_CLICK_THRESHOLD_PX
// Naive rage click implementation: If mouse has not moved further than RAGE_CLICK_THRESHOLD_PX
// over RAGE_CLICK_CLICK_COUNT clicks with max RAGE_CLICK_TIMEOUT_MS between clicks, it's
// counted as a rage click

Expand Down
16 changes: 10 additions & 6 deletions src/scroll-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ export class ScrollManager {
this.context.maxContentHeight = Math.max(contentHeight, this.context.maxContentHeight ?? 0)
}

// `capture: true` is required to get scroll events for other scrollable elements
// on the page, not just the window
// see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#usecapture
//
// `passive: true` is used to tell the browser that the scroll event handler will not call `preventDefault()`
// This allows the browser to optimize scrolling performance by not waiting for our handling of the scroll event
// see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#passive
startMeasuringScrollPosition() {
// setting the third argument to `true` means that we will receive scroll events for other scrollable elements
// on the page, not just the window
// see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#usecapture
window?.addEventListener('scroll', this._updateScrollData, true)
window?.addEventListener('scrollend', this._updateScrollData, true)
window?.addEventListener('resize', this._updateScrollData)
window?.addEventListener('scroll', this._updateScrollData, { capture: true, passive: true })
window?.addEventListener('scrollend', this._updateScrollData, { capture: true, passive: true })
window?.addEventListener('resize', this._updateScrollData, { passive: true })
}

public scrollElement(): Element | undefined {
Expand Down
Loading