fix(virtual-core): hold a scroll correction the element cannot take (… · TanStack/virtual@3c61b6e · GitHub
Skip to content

Commit 3c61b6e

Browse files
authored
fix(virtual-core): hold a scroll correction the element cannot take (#1319)
1 parent 4a1a4fd commit 3c61b6e

8 files changed

Lines changed: 1004 additions & 320 deletions

File tree

‎.changeset/quiet-rows-hold.md‎

Lines changed: 7 additions & 0 deletions

‎docs/api/virtualizer.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,3 +623,7 @@ scrollOffset: number
623623
```
624624

625625
This option represents the current scroll position along the scrolling axis. It is measured in pixels from the starting point of the scrollable area.
626+
627+
It is a position in the list's layout, and it stays consistent with `getOffsetForIndex`, the argument to `scrollToOffset`, and `initialOffset`: restoring a saved value returns to the same layout position. That is the same item only while the items and sizes before it are unchanged; to restore across changes, pair it with `takeSnapshot()` and `initialMeasurementsCache` as described above, or scroll to the item with `scrollToIndex`.
628+
629+
While a scroll correction is held, because a write to the element is impossible at that moment (a touch gesture or its momentum on iOS, or the element rubber-banding past an edge), `getVirtualItems()` and `getTotalSize()` describe what is rendered, shifted by the held amount so the rows on screen do not move, while `scrollOffset` keeps the layout position. The hold ends with a single write once the element can take it. Prefer `getVirtualItems()` positions over `item.start - scrollOffset` for where a row sits in the viewport, since that difference is off by the held amount for the length of the gesture or bounce.

‎examples/react/chat/src/main.tsx‎

Lines changed: 9 additions & 1 deletion

‎packages/virtual-core/src/index.ts‎

Lines changed: 185 additions & 102 deletions
Large diffs are not rendered by default.

‎packages/virtual-core/tests/__snapshots__/scroll-trace.test.ts.snap‎

Lines changed: 108 additions & 108 deletions
Large diffs are not rendered by default.

‎packages/virtual-core/tests/index.test.ts‎

Lines changed: 181 additions & 102 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)