Skip to content
Source
Sign inSign up
TanStack/virtual

fix(react-virtual): grow size container before scroll sync on end-anchored prepend #1237

Mergedpiecykmerged 2 commits intomain fromrefs/pull/1237/headmerged by piecyk

Showing 2 changed files with 34 additions and 5 deletions.

x
0 of 2 viewed, 2 left
5 additions and 0 deletions.changeset/fix-direct-dom-prepend-scroll-clamp.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/react-virtual': patch
+---
+​
+Fix a gap at the top of the list after an end-anchored prepend in `directDomUpdates` mode. The prepend grows the total size and bumps `scrollOffset` to the new bottom in the same pass, but the size container's height was written _after_ `_willUpdate` synced the scroll position — so the browser clamped the `scrollTop` write to the stale (shorter) `scrollHeight`, leaving whitespace at the top until the next scroll. The container is now grown before the scroll sync. Only affected `directDomUpdates` mode (React-rendered sizers receive their height during render).
29 additions and 5 deletionspackages/react-virtual/src/index.tsxModified
View file
@@ -103,11 +103,15 @@
directRef.current.enabled = directDomUpdates
directRef.current.mode = directDomUpdatesMode
​
- // Writes container size + item positions to the DOM. Idempotent — guarded
- // by lastSize / lastPositions. Called from onChange (covers scroll-driven
- // updates) and from a layout effect (covers post-render commits when refs
- // have just registered new items in elementsCache).
- const applyDirectStyles = (
+ // Writes the size container's total extent to the DOM. Idempotent — guarded
+ // by lastSize. Split out from applyDirectStyles so it can run *before* the
+ // scroll-position sync in the _willUpdate effect: an end-anchored prepend
+ // grows the total and bumps scrollOffset in the same pass, and if scrollTop
+ // is written before the container has grown the browser clamps it to the
+ // stale (shorter) scrollHeight, leaving a gap at the top until the next
+ // scroll (visible only in directDomUpdates mode — React-rendered sizers get
+ // their height during render).
+ const applyContainerSize = (
instance: Virtualizer<TScrollElement, TItemElement>,
) => {
const state = directRef.current