Skip to content

fix(virtual-core): hold scroll corrections under WebKit wheel input - #1400

Draft
piecyk wants to merge 3 commits into
TanStack:mainfrom
piecyk:fix/webkit-wheel-prepend-anchor
Draft

piecyk wants to merge 3 commits into
TanStack:mainfrom
piecyk:fix/webkit-wheel-prepend-anchor

Conversation

@piecyk

@piecyk piecyk commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

🎯 Changes

Refs #1398. An alternative to #1399, still a draft until it's tried in real Safari.

In WebKit a scrollTop write made while wheel input drives the scroll makes it discard the distance of the next wheel event. A list whose rows get measured on the way up writes on most frames, so with one wheel event per frame about a third of the distance is lost. A prepend's anchor write can also be undone whole, which is the jump from #1398. Thanks @basnijholt for finding the cadence part, my first version here only held the prepend anchor and missed it.

Now every correction is held for 150ms after a wheel event, same hold path the iOS touch gesture uses, no new option:

wheel event (WebKit)
  (re)start a 150ms timer, corrections are held while it runs
  timer ends: write the hold

element reaches the top of the list with a hold
  two frames later, if it's still there: end the wheel hold and write

The write at the top is there so a wheel that never pauses can't park the reader at scrollTop 0. It has to be exactly at the top. With a margin (tried one viewport and 1200px) WebKit applies the write about two frames after the rows moved, and they show the whole hold away for those frames. The end doesn't need it, the total size is exposed short by the hold so the element and the layout get there together.

WebKit is told from the user agent, there's no feature to test for. Chromium, Firefox, jsdom and happy-dom are left out, so nothing changes there.

Issue's repro in Playwright WebKit 26.0, 100px wheel ticks, counting how far the rows moved on screen:

cadence main this
7,000px, no prepends 60 Hz ~2,300px lost 0
7,000px, no prepends 30 Hz 0 0
30,000px, 5 prepends 60 Hz ~10,000px lost ~800
30,000px, 5 prepends 30 Hz ~300 + the jump ~650

No frame with the rows out of place in any run. What's left is the write at the top, the element sits there for two frames per prepend and that costs about two ticks.

Not measured: real Safari, trackpad momentum, the bounce at the top, a list with a scrollMargin (there the write lands while the element still moves). Playwright sends discrete wheel events.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr. (Ran virtual-core test:lib, test:types, test:eslint and the affected set from the pre-push hook, not the e2e matrix.)

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

🤖 Generated with Claude Code

…bKit wheel input

WebKit can drop a scrollTop write made while wheel input drives the
scroll, so a prepend's anchor write lost that way throws the reader by
the whole prepend (TanStack#1398). Hold the anchor for two frames and write it
from the second. Size corrections are written as before.

Measured with the issue's repro in Playwright WebKit 26.0: no jump in
18 runs, against about 1 run in 4 before. Real Safari and trackpad
momentum are not measured.
@changeset-bot

changeset-bot Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 462bdac

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@tanstack/virtual-core Patch
@tanstack/angular-virtual Patch
@tanstack/lit-virtual Patch
@tanstack/marko-virtual Patch
@tanstack/react-virtual Patch
@tanstack/solid-virtual Patch
@tanstack/svelte-virtual Patch
@tanstack/vue-virtual Patch
@tanstack/virtual-benchmarks Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 26, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@nx-cloud

nx-cloud Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 9572c54

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 3m 5s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 14s View ↗

💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-27 05:58:19 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
More templates

@tanstack/angular-virtual

npm i https://pkg.pr.new/@tanstack/angular-virtual@1400

@tanstack/lit-virtual

npm i https://pkg.pr.new/@tanstack/lit-virtual@1400

@tanstack/marko-virtual

npm i https://pkg.pr.new/@tanstack/marko-virtual@1400

@tanstack/react-virtual

npm i https://pkg.pr.new/@tanstack/react-virtual@1400

@tanstack/solid-virtual

npm i https://pkg.pr.new/@tanstack/solid-virtual@1400

@tanstack/svelte-virtual

npm i https://pkg.pr.new/@tanstack/svelte-virtual@1400

@tanstack/virtual-core

npm i https://pkg.pr.new/@tanstack/virtual-core@1400

@tanstack/vue-virtual

npm i https://pkg.pr.new/@tanstack/vue-virtual@1400

commit: 462bdac

@basnijholt

Copy link
Copy Markdown
Contributor

Tried #1400 in MindRoom Chat on macOS Playwright WebKit against our variable-height timeline specs (same room/thread tests, comparing our wheel hold vs #1400):

The difference vs the gist repro looks like variable-height rows: as you scroll up into unmeasured rows, ResizeObserver triggers synchronous applyScrollAdjustment() scrollTop writes during the wheel stream, and because #1400 leaves size corrections unheld, WebKit eats ~1–2 wheel ticks on each write.

I agree on avoiding a public option and on not letting a never-pausing wheel park at scrollTop = 0. What if we hold both prepend anchors and resize corrections automatically on desktop WebKit (no option), and flush early (e.g. via the 2-frame hold) only if scrollTop actually gets close to 0 while _holdOffset > 0? Happy to test that in our app if you think it's worth pursuing.

@piecyk

piecyk commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for testing it that fast @basnijholt, and nice that the prepend specs pass.

Hmm, couldn't reproduce the dropped scroll though. Took your gist, removed the prepend limit and sent 300 wheel ticks (30k px) in Playwright WebKit, then counted how far the visible rows actually moved on screen. #1400 loses 0px, continuous and in bursts, also with the estimate at 40 and at 300 to make the resize corrections bigger. main loses 100-500px, that's the prepend jump.

How do your specs measure the drop? If it's wheel distance vs scrollTop, that one lies with unheld corrections. With the estimate at 40 I sent 7000px, the rows moved 7000px on screen, but scrollTop only moved 3360, because every row measured on the way up adds a correction the other way. Looks like ~50% eaten and nothing was lost. With your hold scrollTop stays untouched until the flush, so it would look fine there. Would also explain not reaching the prepend boundary, the content is just longer than the estimate said.

Also tried your idea, hold everything on WebKit and flush with the 2 frame write near the edge. No stuck state, but every edge flush costs about one wheel tick, 300-1000px over the same 30k, plus a few frames with a visible jump. So in the gist it's worse than #1400.

Could you check the drop against row positions on screen instead of scrollTop? If it's still there, a repro would help a lot, then it's something in your app the gist doesn't have (images, rows resizing after the first measure?).

@basnijholt

Copy link
Copy Markdown
Contributor

Good question — our probe already tracks on-screen getBoundingClientRect().top row travel (not scrollTop), and we double-checked it against which row actually ends up at the top of the viewport (0 px difference).

We figured out why you saw 0 px on the gist while we saw thousands of px lost: wheel cadence vs WebKit's ~1-frame scrollTo commit window.

On macOS Playwright WebKit, await page.mouse.wheel(0, -100) already takes ~1 VSync (~16.7 ms). Adding waitForTimeout(16) turns it into ~33 ms/tick (30 Hz — every 2nd frame), so the idle frame finishes committing scrollTo before the next wheel tick arrives. Dropping the extra sleep (sleep = 0, 60 Hz) reproduces the drop on the exact same unmodified gist (no prepends needed):

Gist (150 × -100 px = -15,000 px, est = 110, macOS WebKit #1400) avg ms/tick On-screen row travel Lost
for loop with no sleep (60 Hz) 16.6 ms 8,700 px -6,300 px (-42%)
~22 ms cadence (~42 Hz, matching our app loop) 23.9 ms 8,800 px -6,200 px (-41%)
waitForTimeout(16) (30 Hz) 33.3 ms 15,000 px 0 px

Same 60 Hz loop on Chromium #1400, or WebKit with uniform heights (0 scrollTo writes), or WebKit with resize adjustments held during wheel: all 0 px lost.

What's happening per frame (traced on the gist, 0 prepends):

t=505  wheel dy=-100  st=10180           (applied)
t=523  ResizeObserver -> scrollTo(10169) (d=-11, scrollOffset === scrollTop)
t=528  wheel dy=-100  st=10169           (DOM wheel fires, defaultPrevented=false, but WebKit moves 0 px)
t=539  scroll         st=10169           (next VSync commits the programmatic scroll)
t=540  wheel dy=-100  st=10069           (applied)

Every synchronous el.scrollTo() from applyScrollAdjustment opens a ~1-frame window where an incoming wheel event fires in JS but has its scroll delta discarded by WebKit.

That also explains the hold + edge-flush trade-off: at 30 Hz #1400 hides the per-write drops, whereas at 60 Hz #1400 does ~300 scrollTo writes and drops thousands of px, while holding resize + prepend writes and only flushing near scrollTop = 0 drops the write count to ~2–13 total (we saw 0 px lost over 30k px at 60 Hz on the gist when flushing at scrollTop < 1200 && _holdOffset > 0).

Standalone runnable repro against pkg.pr.new/@tanstack/virtual-core@1400 (0 prepends, compares #1400 vs wheel-hold vs uniform control in WebKit & Chromium): https://gist.github.com/basnijholt/a8d8ee4dc1623f74120700b265789b83 (npm install && npm run repro).

(Full disclosure: this investigation, repro, and reply were written by my AI agents.)

A scrollTop write made while wheel input drives the scroll makes WebKit
discard the distance of the next wheel event. A list whose rows are
measured on the way up writes on most frames, so a wheel event per frame
lost about a third of its distance; holding only a prepend's anchor did
not help there.

Hold every correction while wheel input runs and write it when the wheel
has been quiet for 150ms. A hold also has to be written when the element
reaches either edge, or wheel input that never pauses parks the reader
there: two frames later, and not before the edge, where WebKit applies
the write about two frames after the rows moved.

Measured with the issue's repro in Playwright WebKit 26.0, one wheel
event per frame: 0px lost of 7,000 without prepends (main: about 2,300),
about 1,000px of 30,000 with five prepends (main: about 10,000), no
frame with the rows out of place. Real Safari and trackpad momentum are
not measured.
@piecyk piecyk changed the title fix(virtual-core): write a prepend's anchor two frames later under WebKit wheel input fix(virtual-core): hold scroll corrections under WebKit wheel input Nov 27, 2026
@piecyk

piecyk commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

Yep, you're right @basnijholt, it was my cadence. Dropped the extra sleep and at 60 Hz main and the old version of this PR both lose about a third of the wheel distance on the gist, nice catch.

Pushed a rework, so it now does what you proposed: every correction is held on WebKit while the wheel runs, no option, and written when the wheel rests or at the edge. Same gist at 60 Hz, 0px lost without prepends and about 1000px of 30k with five prepends, that's the edge write.

One difference from your version. With a margin before the edge (tried one viewport and your 1200px) the rows show the whole hold away for about two frames at every prepend, WebKit applies the write that much later than the re-render while the element still moves. Does your probe catch that? Flushing exactly at scrollTop 0 doesn't have it, the cost is two frames parked at the top.

Could you run your specs against it again? Details and numbers are in the PR description.

Review follow-ups on the WebKit wheel hold:

- The end needs no early write: the total size is exposed short by the
  hold, so the element and the layout reach the end together.
- The write at the top ends the wheel hold early instead of passing a
  flag through the flush; the next wheel event starts it again.
- Check the top again when the two frames have passed: the reader can
  have turned back, and a write away from the top shows the rows out of
  place.
- The list's top is scrollMargin down the element; held rows were drawn
  over what is above the list.
- jsdom and happy-dom name AppleWebKit in their user agent: leave them
  out, so a consumer's test sees the same writes as before.
basnijholt added a commit to mindroom-ai/mindroom-chat that referenced this pull request Nov 27, 2026
Replace v1 #1400 2-frame prepend hold (_lastWheelAt, _anchorHoldFrame, _holdAnchorUnderWheel) in @tanstack/virtual-core@3.19.13 (cjs+esm) with piecyk's reworked #1400 implementation (commit 462bdacc1f9b68b3ee7e52ac05955a2d851f836c):

- Exclude jsdom and HappyDOM in isWebKit() while keeping _resetIOSDetectionForTests() resetting _isWebKitResult
- Track WebKit wheel activity with a 150ms _wheelTimer (gated in _canWriteScroll) and clean up _wheelTimer/_edgeFlushFrame on detach
- Flush held corrections when the wheel rests or 2 frames after reaching the top of the list via _holdAtTop()/_flushHoldAtTop() (invoked from observeElementOffset and onWheel)
- Preserve local PR 3 negative-hold clamp and heldBefore/notify(true) guards in _wheelTimer, _flushHoldAtTop, and onTailClosed, plus PR 4 anchorSkipLeading

Assisted-by: MindRoom DevAgent:google/gemini-4-neon <https://github.com/mindroom-ai/mindroom>
@basnijholt

Copy link
Copy Markdown
Contributor

Ran our full MindRoom Chat E2E suite against the reworked #1400 (462bdac, ported onto @tanstack/virtual-core@3.19.13 in our app, --workers=1 on macOS Playwright WebKit webkit-2388 / Version 27.2 & Chromium):

Spec Our local wheel-hold (main) Old #1400 (2-frame prepend) New #1400 (462bdac)
room-jump-measure Chromium ×8 8/8 (0 px) 8/8 (0 px) 8/8 (0 px)
room-jump-measure WebKit ×8 5/8 (3× single -240 px burst-0 drift) 0/8 (multiple -237..479 px drifts every run) 4/8 (same single -240 px burst-0 drift on 4/8 + one mid-run -240 px; 0 reversals, 0 lost-place)
room-continuous-wheel WebKit 6 s ×8 0/8 (-120 px single-tick drift; endST ~24.5–25.2k; 0 ms stuck) 0/8 (-7,281..8,001 px lost on 8/8; 3/8 zero prepends) 7/8 pass (0–1 px on 7/8, single -120 px on 1/8; maxStuckAtTopMs: 0–25 ms; settleST within 0–193 px of main for the same tick count; 0 reversals, 0 lost-place)
thread-timeline-jumps (all viewports) ×4 56/56 (WebKit fast-wheel 4/4) 52/56 (WebKit fast-wheel 0/4) 55/56 (WebKit fast-wheel 3/4; 1 run had a single -240 px burst-1 drift; 0 reversals, 0 lost-place)
Small specs (thread-prepend-scroll, room-load-older-keeps-reader, room-intro-start) ×1 — 15/15 15/15

This is a huge improvement — it completely eliminates the multi-thousand-px drops from the earlier #1400 revision and matches our local wheel-hold across the app specs, without needing a new option or getting stuck under continuous wheel (maxStuckAtTopMs peaked at 25 ms, ~1.5 frames, and settled scroll position stayed within ~0–2 ticks of our baseline).

On your question about the 1200 px margin vs flushing at scrollTop = 0:

  • Yes, our probe would catch a 2-frame jump in DOM coordinates — it checks every visible row's getBoundingClientRect().top on every rAF and flags lostPlaceFrames, maxReversalPx, and maxUnexplainedPx.
  • We also re-checked margin = 0 vs margin = 1200 on the infinite-prepend gist at 60 Hz (pkg.pr.new/@tanstack/virtual-core@1400) with both 60 Hz rAF getBoundingClientRect() tracing and Playwright recordVideo frame inspection on macOS Playwright WebKit webkit-2388 (Version 27.2), and saw 0 anomalous rAFs and 0 shifted/blank video frames on either. Since you hit the 2-frame lag in Playwright WebKit 26.0 while moving, flushing strictly at scrollTop = 0 is clearly safer across WebKit versions — and in our continuous-wheel app test the 2-frame top pause was only ~23–25 ms and barely noticeable.

We'd be happy to test this in real macOS Safari (trackpad momentum + top bounce) as well if that helps get #1400 out of draft.

(Full disclosure: these benchmarks, margin check, and reply were run and drafted by my AI agents.)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants