fix(virtual-core): eagerly adjust scrollOffset on prepend to prevent jump #1176
main fromrefs/pull/1176/head·merged by tannerlinsley When items are prepended with anchorTo: 'end' and dynamic sizes, the virtualizer would compute the wrong visible range for one frame (using stale estimate-based positions) and then correct in the next frame via _willUpdate, producing a visible "jump".
Fix by eagerly adjusting scrollOffset in setOptions during the render pass so calculateRange/getVirtualItems return the correct items immediately. _willUpdate then only syncs the browser's actual scroll position to match.
🎯 Changes#
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally with
pnpm run test:pr.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Bug Fixes
- Prevented a visible one-frame jump when items are prepended/appended by eagerly reconciling scroll offset so visible items render consistently.
- Reduced deferred platform-specific adjustments by synchronizing the browser scroll position immediately when possible, improving render stability (iOS momentum handling deferred adjustments until safe).
- Tests
- Updated virtualization tests to reflect the new scroll reconciliation behavior and expected offsets.
- 1
View your CI Pipeline Execution ↗ for commit fc82a0a20bbcdb566c201cf1e48ed28419a3005c
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 2m 28s View ↗ nx run-many --target=build --exclude=examples/**✅ Succeeded 13s View ↗
☁️ Nx Cloud last updated this comment at
2026-06-01 17:08:52UTCMore templates
- @tanstack/virtual-example-angular-dynamic
- @tanstack/virtual-example-angular-fixed
- @tanstack/virtual-example-angular-infinite-scroll
- @tanstack/virtual-example-angular-padding
- @tanstack/virtual-example-angular-smooth-scroll
- @tanstack/virtual-example-angular-sticky
- @tanstack/virtual-example-angular-table
- @tanstack/virtual-example-angular-variable
- @tanstack/virtual-example-angular-window
- tanstack-lit-virtual-example-dynamic
- tanstack-lit-virtual-example-fixed
- tanstack-react-virtual-example-chat
- tanstack-react-virtual-example-dynamic
- tanstack-react-virtual-example-fixed
- tanstack-react-virtual-example-infinite-scroll
- tanstack-react-virtual-example-padding
- tanstack-react-virtual-example-pretext
- tanstack-react-virtual-example-scroll-padding
- tanstack-react-virtual-example-smooth-scroll
- tanstack-react-virtual-example-sticky
- tanstack-react-virtual-example-table
- tanstack-react-virtual-example-variable
- tanstack-react-virtual-example-window
- tanstack-vue-virtual-example-dynamic
- tanstack-vue-virtual-example-fixed
- tanstack-vue-virtual-example-infinite-scroll
- tanstack-vue-virtual-example-padding
- tanstack-vue-virtual-example-scroll-padding
- tanstack-vue-virtual-example-smooth-scroll
- tanstack-vue-virtual-example-sticky
- tanstack-vue-virtual-example-table
- tanstack-vue-virtual-example-variable
- tanstack-svelte-virtual-example-dynamic
- tanstack-svelte-virtual-example-fixed
- tanstack-svelte-virtual-example-infinite-scroll
- tanstack-svelte-virtual-example-smooth-scroll
- tanstack-svelte-virtual-example-sticky
- tanstack-svelte-virtual-example-table
@tanstack/angular-virtual
npm i https://pkg.pr.new/@tanstack/angular-virtual@1176@tanstack/lit-virtual
npm i https://pkg.pr.new/@tanstack/lit-virtual@1176@tanstack/react-virtual
npm i https://pkg.pr.new/@tanstack/react-virtual@1176@tanstack/solid-virtual
npm i https://pkg.pr.new/@tanstack/solid-virtual@1176@tanstack/svelte-virtual
npm i https://pkg.pr.new/@tanstack/svelte-virtual@1176@tanstack/virtual-core
npm i https://pkg.pr.new/@tanstack/virtual-core@1176@tanstack/vue-virtual
npm i https://pkg.pr.new/@tanstack/vue-virtual@1176commit:
8a58d32Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. Inline comments: In `@packages/virtual-core/src/index.ts`: - Around line 823-834: Tests expect numeric options.adjustments but _scrollToOffset now forwards undefined for the _willUpdate anchor sync path; update the two assertions in packages/virtual-core/tests/index.test.ts that reference anchorTo:end to expect undefined for options.adjustments instead of 100 and 70 (specifically the tests "anchorTo:end keeps visible content stable when older items are prepended" and "anchorTo:end keeps a pinned streaming message pinned as it grows"); leave the iOS deferred-flush test that asserts 50 unchanged because it exercises the iOS accumulator path, and ensure references to _scrollToOffset and scrollToFn are used when reviewing the behavior change.🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
9f21eedb-e3bd-4e6c-a809-62dc0e290649📥 Commits
Reviewing files that changed from the base of the PR and between 693d915e0670cbba5c3e42d0d2a46f085dd7d44e and f2ec547aba59f5a739a4ace912a9c6cc44ad1f9e.
📒 Files selected for processing (1)
packages/virtual-core/src/index.ts
packages/virtual-core/src/index.ts lines 823 to 834OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -809,10 +823,12 @@809-810- const item = index < count ? this.getMeasurements()[index] : undefined811- if (item) {812- const delta = item.start + offset - this.getScrollOffset()813-814- if (!approxEqual(delta, 0)) {815- this.applyScrollAdjustment(delta)816- }817- }823+ const [key, _offset, followOnAppend] = anchor824+825+ if (key !== null && !followOnAppend) {826+ // scrollOffset was eagerly adjusted in setOptions so the827+ // virtualizer already computed the correct range during render.828+ // Now sync the browser's actual scroll position to match.829+ // Skip when followOnAppend is set — scrollToEnd will handle it.830+ this._scrollToOffset(this.getScrollOffset(), {831+ adjustments: undefined,832+ behavior: undefined,833+ })818834 }⚠️ Potential issue | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash # Description: Find test assertions on scrollToFn.adjustments to verify they match new behavior # Search for test expectations on adjustments parameter rg -n -C5 'adjustments' --glob '**/virtual-core/**/*.test.ts'Repository: TanStack/virtual
Length of output: 7559
🏁 Script executed:
#!/bin/bash set -euo pipefail echo "=== src/index.ts: context around lines 780-870 ===" nl -ba packages/virtual-core/src/index.ts | sed -n '780,870p' echo "=== src/index.ts: find definition/implementation of _scrollToOffset ===" rg -n "_scrollToOffset\\b" packages/virtual-core/src/index.ts rg -n "function _scrollToOffset|_scrollToOffset\\(" packages/virtual-core/src/index.ts # Print a likely implementation block (first match after definition) # Use a range around the first occurrence beyond the call site. first=$(rg -n "_scrollToOffset\\b" packages/virtual-core/src/index.ts | head -n1 | cut -d: -f1) # But we need the definition; search for "private _scrollToOffset" if present defline=$(rg -n "private _scrollToOffset|_scrollToOffset\\s*\\(" packages/virtual-core/src/index.ts | head -n1 | cut -d: -f1 || true) echo "First _scrollToOffset match line: $first" echo "Definition candidate line: $defline" if [ -n "${defline:-}" ] && [ "$defline" -gt 0 ]; then nl -ba packages/virtual-core/src/index.ts | sed -n "$((defline-40)),$((defline+80))p" fi echo "=== tests/index.test.ts: context around adjustments expectations ===" nl -ba packages/virtual-core/tests/index.test.ts | sed -n '2230,2365p' echo "=== tests: any assertions for options.adjustments ===" rg -n "options\\.adjustments|adjustments\\)\\.toBe\\(|toBeUndefined\\(\\)|toBe\\(undefined\\)" packages/virtual-core/tests/index.test.tsRepository: TanStack/virtual
Length of output: 172
🏁 Script executed:
#!/bin/bash set -euo pipefail echo "=== src/index.ts: context around lines 780-870 (cat -n) ===" cat -n packages/virtual-core/src/index.ts | sed -n '780,870p' echo "=== src/index.ts: search for _scrollToOffset definition/implementation ===" rg -n "_scrollToOffset\\b" packages/virtual-core/src/index.ts echo "=== tests/index.test.ts: context around the anchorTo/end + adjustments assertions ===" cat -n packages/virtual-core/tests/index.test.ts | sed -n '1460,1565p' echo "----" cat -n packages/virtual-core/tests/index.test.ts | sed -n '2240,2360p' echo "=== tests: lines asserting options.adjustments ===" rg -n "options\\.adjustments\\s*\\.|options\\.adjustments\\s*\\)|expect\\(options\\.adjustments\\)|adjustments\\)\\.toBe\\(" packages/virtual-core/tests/index.test.tsRepository: TanStack/virtual
Length of output: 13847
🏁 Script executed:
#!/bin/bash set -euo pipefail echo "=== src/index.ts: _scrollToOffset implementation (around 1760-1860) ===" cat -n packages/virtual-core/src/index.ts | sed -n '1730,1870p' echo "=== src/index.ts: find elementScroll/windowScroll calls inside _scrollToOffset ===" rg -n "elementScroll\\(|windowScroll\\(|scrollEl\\.scrollTo\\(" packages/virtual-core/src/index.ts echo "=== src/index.ts: grep for 'adjustments:' patterns in _scrollToOffset call sites ===" rg -n "_scrollToOffset\\([^\\)]*\\{[^\\}]*adjustments" packages/virtual-core/src/index.ts echo "=== src/index.ts: locate definition of scrollToFn typing / call signature (scrollToFn usage) ===" rg -n "scrollToFn|scrollToFn\\(" packages/virtual-core/src/index.tsRepository: TanStack/virtual
Length of output: 5378
🏁 Script executed:
#!/bin/bash set -euo pipefail echo "=== index.ts: locate pendingScrollAnchor definition and creation ===" rg -n "pendingScrollAnchor" packages/virtual-core/src/index.ts echo "=== index.ts: print _willUpdate method around its anchor sync block ===" # Find start of _willUpdate will=$(rg -n "(_willUpdate\\b|private _willUpdate\\b)" packages/virtual-core/src/index.ts | head -n1 | cut -d: -f1) echo "First _willUpdate match line: $will" start=$((will-60)) end=$((will+220)) if [ "$start" -lt 1 ]; then start=1; fi cat -n packages/virtual-core/src/index.ts | sed -n "${start},${end}p" echo "=== index.ts: print around setOptions where scrollOffset eagerly adjusted (comment refers) ===" # Search for "eagerly adjusted" comment or setOptions scrollOffset adjust rg -n "eagerly adjusted|setOptions\\b|scrollOffset" packages/virtual-core/src/index.ts | head -n 80 # Narrow to setOptions implementation region setopt=$(rg -n "setOptions\\s*=\\s*\\(|setOptions\\(" packages/virtual-core/src/index.ts | head -n1 | cut -d: -f1 || true) echo "First setOptions match line: ${setopt:-none}" if [ -n "${setopt:-}" ]; then s=$((setopt-40)); e=$((setopt+220)) if [ "$s" -lt 1 ]; then s=1; fi cat -n packages/virtual-core/src/index.ts | sed -n "${s},${e}p" fi echo "=== tests: inspect createChatVirtualizer helper to see when anchorTo:end triggers scrollToFn ===" # Find createChatVirtualizer definition create=$(rg -n "function createChatVirtualizer|createChatVirtualizer\\s*\\(" packages/virtual-core/tests/index.test.ts | head -n1 | cut -d: -f1 || true) echo "First createChatVirtualizer match line: ${create:-none}" if [ -n "${create:-}" ]; then s=$((create-20)); e=$((create+120)) cat -n packages/virtual-core/tests/index.test.ts | sed -n "${s},${e}p" fiRepository: TanStack/virtual
Length of output: 27848
Fix anchorTo:end tests for
scrollToFnadjustments_scrollToOffsetforwards the passedadjustmentsvalue directly toscrollToFn, and the_willUpdateanchor sync path now calls_scrollToOffset(..., { adjustments: undefined, behavior: undefined })—soscrollToFnreceivesoptions.adjustments === undefined(not100/70). Update these assertions accordingly inpackages/virtual-core/tests/index.test.ts:anchorTo:end keeps visible content stable when older items are prepended(currentlyoptions.adjustments).toBe(100))anchorTo:end keeps a pinned streaming message pinned as it grows(currentlyoptions.adjustments).toBe(70)) Keep the iOS deferred-flush test that expectsoptions.adjustments).toBe(50)unchanged, since it exercises the iOS accumulator path which still passes a numeric delta.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/virtual-core/src/index.ts` around lines 823 - 834, Tests expect numeric options.adjustments but _scrollToOffset now forwards undefined for the _willUpdate anchor sync path; update the two assertions in packages/virtual-core/tests/index.test.ts that reference anchorTo:end to expect undefined for options.adjustments instead of 100 and 70 (specifically the tests "anchorTo:end keeps visible content stable when older items are prepended" and "anchorTo:end keeps a pinned streaming message pinned as it grows"); leave the iOS deferred-flush test that asserts 50 unchanged because it exercises the iOS accumulator path, and ensure references to _scrollToOffset and scrollToFn are used when reviewing the behavior change.✅ Addressed in commit 5b972c5
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/virtual-core/tests/index.test.ts (1)
2267-2280: ⚡ Quick winAssert the eager correction before
_willUpdate().This only proves the later DOM sync. The regression here was the one-frame wrong range during render, so it would be better to assert
scrollOffsetorgetVirtualItems()immediately aftersetOptions()and before_willUpdate()runs.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/virtual-core/tests/index.test.ts` around lines 2267 - 2280, The test currently only checks the post-DOM-sync scrollTo call; add an assertion that verifies the eager correction happened synchronously before the internal update by checking the virtualizer's scrollOffset or getVirtualItems() immediately after calling setMessages (and before _willUpdate() runs). Specifically, after calling setMessages([{ id: 'm--2' }, { id: 'm--1' }, ...messages]) assert virtualizer.scrollOffset (or virtualizer.getVirtualItems()) has the expected value/range to prove the one-frame correction occurred, and keep the existing scrollToFn assertions for the later DOM sync; locate this in the same test around the setMessages call and place the new assertion before any code that triggers or waits for _willUpdate().🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. Inline comments: In `@packages/virtual-core/src/index.ts`: - Around line 830-838: The direct call to _scrollToOffset bypasses the iOS deferral path and can be cancelled by a prepend-triggered anchor sync; instead, gate this DOM sync through the same iOS deferral used elsewhere or use the existing applyScrollAdjustment path. Replace the direct this._scrollToOffset(this.getScrollOffset(), ...) with the code path that honors _iosDeferredAdjustment (i.e., call applyScrollAdjustment / the helper that defers/queues adjustments when _iosDeferredAdjustment is set) so the scroll write is deferred during iOS touch/momentum; ensure you still pass the same offset from getScrollOffset() and preserve the adjustments/behavior options when delegating. --- Nitpick comments: In `@packages/virtual-core/tests/index.test.ts`: - Around line 2267-2280: The test currently only checks the post-DOM-sync scrollTo call; add an assertion that verifies the eager correction happened synchronously before the internal update by checking the virtualizer's scrollOffset or getVirtualItems() immediately after calling setMessages (and before _willUpdate() runs). Specifically, after calling setMessages([{ id: 'm--2' }, { id: 'm--1' }, ...messages]) assert virtualizer.scrollOffset (or virtualizer.getVirtualItems()) has the expected value/range to prove the one-frame correction occurred, and keep the existing scrollToFn assertions for the later DOM sync; locate this in the same test around the setMessages call and place the new assertion before any code that triggers or waits for _willUpdate().🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
7da3ba74-d701-4a3a-962b-68f55dc9942b📥 Commits
Reviewing files that changed from the base of the PR and between f2ec547aba59f5a739a4ace912a9c6cc44ad1f9e and 5b972c56499a622fbcb16a6777e3800900b3b3ea.
📒 Files selected for processing (2)
packages/virtual-core/src/index.tspackages/virtual-core/tests/index.test.ts
packages/virtual-core/src/index.ts lines 830 to 838OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -808,10 +828,11 @@808- }809-810- const item = index < count ? this.getMeasurements()[index] : undefined811- if (item) {812- const delta = item.start + offset - this.getScrollOffset()813-814- if (!approxEqual(delta, 0)) {815- this.applyScrollAdjustment(delta)816- }817- }828+ const [key, _offset, followOnAppend] = anchor829+830+ if (key !== null && !followOnAppend) {831+ // scrollOffset was eagerly adjusted in setOptions so the832+ // virtualizer already computed the correct range during render.833+ // Now sync the browser's actual scroll position to match.834+ // Skip when followOnAppend is set — scrollToEnd will handle it.835+ this._scrollToOffset(this.getScrollOffset(), {836+ adjustments: undefined,837+ behavior: undefined,838+ })⚠️ Potential issue | 🟠 Major | 🏗️ Heavy lift
Preserve the iOS deferral path for pending anchor sync.
This direct
_scrollToOffsetcall skipsapplyScrollAdjustment, so prepend-triggered anchor sync can still writescrollTopduring iOS touch/momentum and cancel the in-flight scroll. The eager logicalscrollOffsetfix is good, but the DOM sync still needs the same_iosDeferredAdjustmentgating as the other mid-scroll corrections.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/virtual-core/src/index.ts` around lines 830 - 838, The direct call to _scrollToOffset bypasses the iOS deferral path and can be cancelled by a prepend-triggered anchor sync; instead, gate this DOM sync through the same iOS deferral used elsewhere or use the existing applyScrollAdjustment path. Replace the direct this._scrollToOffset(this.getScrollOffset(), ...) with the code path that honors _iosDeferredAdjustment (i.e., call applyScrollAdjustment / the helper that defers/queues adjustments when _iosDeferredAdjustment is set) so the scroll write is deferred during iOS touch/momentum; ensure you still pass the same offset from getScrollOffset() and preserve the adjustments/behavior options when delegating.✅ Addressed in commit ee4877c
Actionable comments posted: 1
♻️ Duplicate comments (1)
packages/virtual-core/src/index.ts (1)
830-838: ⚠️ Potential issue | 🟠 Major | 🏗️ Heavy liftPreserve the iOS deferred-scroll path for this DOM sync.
This direct
_scrollToOffsetwrite bypassesapplyScrollAdjustment'sisIOSWebKit()gate. If a prepend lands during touch or momentum scrolling, this path still writesscrollTopimmediately and cancels the native scroll on iOS, even though the logicalscrollOffsetwas already fixed eagerly.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/virtual-core/src/index.ts` around lines 830 - 838, The direct call to this._scrollToOffset bypasses applyScrollAdjustment's isIOSWebKit() logic and can cancel native iOS momentum; instead of calling _scrollToOffset directly when key !== null && !followOnAppend, route the synchronization through applyScrollAdjustment (or its equivalent helper) so the iOS-specific deferred-scroll gate is respected; use this.getScrollOffset() as the target offset and pass through the adjustments/behavior params (or undefined) to applyScrollAdjustment so iOS WebKit will defer the DOM write while other platforms still sync immediately.🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. Inline comments: In `@packages/virtual-core/src/index.ts`: - Around line 604-615: The code may read stale measurement data when computing newOffset via newMeasurements[idx].start; before using newMeasurements[idx] ensure measurements are rebuilt for the current key ordering: either call your measurement invalidation/rebuild helper (e.g., this.invalidateMeasurements() or this.rebuildMeasurements()) or include the item key identity in the measurement memo so getMeasurements() returns a fresh array for the current getItemKey order; specifically, after computing idx using getItemKey and before accessing newMeasurements[idx].start, force a measurements refresh (or verify newMeasurements[idx] corresponds to getItemKey(idx) and rebuild if it does not) so this.scrollOffset is computed from up-to-date measurements. --- Duplicate comments: In `@packages/virtual-core/src/index.ts`: - Around line 830-838: The direct call to this._scrollToOffset bypasses applyScrollAdjustment's isIOSWebKit() logic and can cancel native iOS momentum; instead of calling _scrollToOffset directly when key !== null && !followOnAppend, route the synchronization through applyScrollAdjustment (or its equivalent helper) so the iOS-specific deferred-scroll gate is respected; use this.getScrollOffset() as the target offset and pass through the adjustments/behavior params (or undefined) to applyScrollAdjustment so iOS WebKit will defer the DOM write while other platforms still sync immediately.🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
fad320ff-17ef-4872-9c13-0986c5a892c7📥 Commits
Reviewing files that changed from the base of the PR and between 5b972c56499a622fbcb16a6777e3800900b3b3ea and 2b35365b4b42a1c18791d67f999138f61de0bcd9.
📒 Files selected for processing (3)
.changeset/eager-scroll-anchor.mdpackages/virtual-core/src/index.tspackages/virtual-core/tests/index.test.ts
✅ Files skipped from review due to trivial changes (1)
- .changeset/eager-scroll-anchor.md
packages/virtual-core/src/index.ts lines 618 to 630Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -595,0 +595,21 @@595+ // Eagerly adjust scrollOffset so the virtualizer computes the correct596+ // visible range during the current render pass — before _willUpdate597+ // syncs the DOM scroll position in a layout effect. Without this,598+ // the virtualizer would render the wrong items for one frame (the599+ // estimate-based positions are stale) and then correct in the next600+ // frame, producing a visible "jump" on prepend with dynamic sizes.601+ let anchorResolved = false602+ if (anchor && this.scrollOffset !== null) {603+ const [anchorKey, anchorOffset] = anchor604+ const newMeasurements = this.getMeasurements()605+ const { count, getItemKey } = this.options606+ let idx = 0607+ while (idx < count && getItemKey(idx) !== anchorKey) {608+ idx++609+ }610+ if (idx < count) {611+ const anchorItem = newMeasurements[idx]612+ if (anchorItem) {613+ const newOffset = anchorItem.start + anchorOffset614+ if (newOffset !== this.scrollOffset) {615+ this.scrollOffset = newOffset⚠️ Potential issue | 🟠 Major | ⚡ Quick win
Force a fresh measurement rebuild before reading
anchorItem.start.If
countstays the same and the caller keeps a stablegetItemKeyreference,getMeasurements()can still be serving the previous layout here.idxis found from the new key order, butnewMeasurements[idx].startcan still belong to the old item/order, so the eager correction picks the wrong offset for prepend+trim or other edge-key swaps with dynamic sizes. Invalidate measurements on edge-key changes, or include key identity in the measurement memo deps before usingnewMeasurements[idx].🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/virtual-core/src/index.ts` around lines 604 - 615, The code may read stale measurement data when computing newOffset via newMeasurements[idx].start; before using newMeasurements[idx] ensure measurements are rebuilt for the current key ordering: either call your measurement invalidation/rebuild helper (e.g., this.invalidateMeasurements() or this.rebuildMeasurements()) or include the item key identity in the measurement memo so getMeasurements() returns a fresh array for the current getItemKey order; specifically, after computing idx using getItemKey and before accessing newMeasurements[idx].start, force a measurements refresh (or verify newMeasurements[idx] corresponds to getItemKey(idx) and rebuild if it does not) so this.scrollOffset is computed from up-to-date measurements.✅ Addressed in commit ee4877c
tannerlinsley merged thisc746841
tannerlinsley closed this
Sign in to join this conversation.
Note
Reviews paused#
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
reviews.auto_review.auto_pause_after_reviewed_commitssetting.Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.Use the checkboxes below for quick actions:
📝 Walkthrough
Walkthrough#
Eagerly resolve append/prepend scroll anchors inside setOptions (precompute anchorDelta and update scrollOffset), record pendingScrollAnchor, and have _willUpdate consume the precomputed delta to sync DOM (with iOS deferral). A changeset and a test expectation were added/updated.
Changes#
Scroll Anchor Lifecycle Optimization
.changeset/eager-scroll-anchor.mdscrollOffsetadjustment fix for prepend scenarios usinganchorTo: 'end'and dynamic sizes.packages/virtual-core/src/index.tsPendingScrollAnchortuple gains ananchorDelta: numberelement so the delta is computed insetOptionsand consumed later in_willUpdate.packages/virtual-core/src/index.tssetOptionsresolves the anchored item viagetMeasurements(), updatesthis.scrollOffsettoanchorItem.start + anchorOffsetwhen resolvable, computesanchorDelta, and stores key/offset/anchorDelta intopendingScrollAnchor.packages/virtual-core/src/index.ts,packages/virtual-core/tests/index.test.ts_willUpdatenow uses the precomputedanchorDelta: for key !== null and falsyfollowOnAppend, it either defers adjustment on iOS by adding to_iosDeferredAdjustmentor immediately calls_scrollToOffset(getScrollOffset(), { adjustments: undefined, behavior: undefined }); test updated to expectoffset: 200andoptions.adjustments: undefined.Estimated code review effort#
🎯 4 (Complex) | ⏱️ ~45 minutes
Possibly related PRs#
Poem#
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
✨ Finishing Touches
🧪 Generate unit tests (beta)
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