fix(shared): match add/removeEventListener options in onScroll cleanup (#2384) - #2560
fix(shared): match add/removeEventListener options in onScroll cleanup (#2384)#2560ErfanBagheri404 wants to merge 1 commit into
Conversation
|
joshuaellis
left a comment
There was a problem hiding this comment.
you've got your two other PR commits in this. I think realistically you wanted to stack these PRs but to be honest, they should be made in isolation with each PR a single commit aimed at next. Please review all your PRs and amend accordingly before they can be reviewed.
… cleanup Isolate PR pmndrs#2560 onto next — single commit.
cbdb8fb to
70c5ff5
Compare
|
Rebased and isolated all 5 PRs onto
Force-pushed. Each is now a single isolated commit against |
|
Source fix in Two blockers: Test fails here. Tests run in Vitest browser mode (real Chromium), so the mock See Formatting. |
Summary
useScroll(viaonScrollin@react-spring/shared) leaks event listeners on cleanup.addEventListenerwas called with{ passive: true }:but
removeEventListenerwas called without any options:Per the DOM spec on matching event listeners, the
captureflag must match between add and remove for the listener to be removed. While{ passive: true }happens to defaultcapturetofalse, strict environments that track the options object verbatim fail to match, leaving the listener attached —useScrollleaks a scroll + resize listener per component mount/unmount.Changes
packages/shared/src/dom-events/scroll/index.ts: extract a sharedpassiveOptionsobject used by bothaddEventListenerandremoveEventListenercalls (scroll + resize + per-containeronResizepath), so the options always match.packages/shared/src/dom-events/scroll/onScroll.test.ts: regression test assertingremoveEventListeneris invoked with options equal to those passed toaddEventListener.Test plan
npx vitest run packages/shared/src/dom-events/scroll/onScroll.test.ts— passes (mock throws on unmatched remove, none thrown)Fixes #2384