@tanstack/virtual-core 3.17.4 → 3.17.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -651,7 +651,11 @@ export class Virtualizer<
651
651
  if (idx < count) {
652
652
  const anchorItem = newMeasurements[idx]
653
653
  if (anchorItem) {
654
- const newOffset = anchorItem.start + anchorOffset
654
+ // Clamp to the reachable range's lower bound — anchorOffset may
655
+ // have been derived from a transiently negative scrollOffset
656
+ // (rubber-band), and a negative tracked offset never self-heals
657
+ // when the element cannot scroll (#1229).
658
+ const newOffset = Math.max(0, anchorItem.start + anchorOffset)
655
659
  if (newOffset !== this.scrollOffset) {
656
660
  anchorDelta = newOffset - this.scrollOffset
657
661
  this.scrollOffset = newOffset
@@ -703,6 +707,13 @@ export class Virtualizer<
703
707
  // `scrollAdjustments` to keep their sum invariant.
704
708
  if (this.scrollOffset !== null) {
705
709
  this.scrollOffset += this.scrollAdjustments
710
+ // Clamp only the lower bound: a negative offset is unreachable, and
711
+ // on an unscrollable element (content fits the viewport) no scroll
712
+ // event ever fires to correct it, permanently skewing
713
+ // getDistanceFromEnd() and wedging _flushIosDeferredIfReady (#1229).
714
+ // Upper-bound overflow stays untouched — it is transiently
715
+ // legitimate mid-prepend while the consumer's sizer catches up.
716
+ if (this.scrollOffset < 0) this.scrollOffset = 0
706
717
  this.scrollAdjustments = 0
707
718
  }
708
719
  }
@@ -966,6 +977,14 @@ export class Virtualizer<
966
977
  const cur = this.getScrollOffset()
967
978
  const max = this.getMaxScrollOffset()
968
979
  if (cur < 0 || cur > max) return
980
+ // At the end clamp the browser already absorbed a shrink above the
981
+ // viewport (it clamped scrollTop onto the new bottom), so replaying our
982
+ // deferred negative delta would lift the view off the bottom — drop it.
983
+ // Positive deltas still replay: growth above doesn't clamp. (#1233)
984
+ if (this._iosDeferredAdjustment < 0 && cur >= max - 1) {
985
+ this._iosDeferredAdjustment = 0
986
+ return
987
+ }
969
988
  const delta = this._iosDeferredAdjustment
970
989
  this._iosDeferredAdjustment = 0
971
990
  // Roll the deferred delta into the running accumulator so any resize
@@ -1536,6 +1555,25 @@ export class Virtualizer<
1536
1555
  this.scrollState?.behavior !== 'smooth' &&
1537
1556
  this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold
1538
1557
  const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0
1558
+ // Default anchoring predicate (used unless the consumer supplies a
1559
+ // custom shouldAdjustScrollPositionOnItemSizeChange).
1560
+ const scrollOffsetWithAdj =
1561
+ this.getScrollOffset() + this.scrollAdjustments
1562
+ const isFirstMeasure = !this.itemSizeCache.has(key)
1563
+ const defaultShouldAdjust = isFirstMeasure
1564
+ ? // First measurement: compensate any item whose top sits above the
1565
+ // fold — the estimate→actual delta must be corrected regardless of
1566
+ // scroll direction, since the whole estimated block was above it.
1567
+ itemStart < scrollOffsetWithAdj
1568
+ : // Re-measurement: only compensate an item that is ENTIRELY above the
1569
+ // fold. An item that merely *spans* the fold (top above, bottom
1570
+ // below — e.g. a streaming chat message growing at its bottom)
1571
+ // changes size *below* the anchor point, so shifting scrollTop by the
1572
+ // delta would drag the viewport downward on every growth (#1218).
1573
+ // Also skip during backward scroll to avoid the "items jump while
1574
+ // scrolling up" cascade.
1575
+ itemStart + itemSize <= scrollOffsetWithAdj &&
1576
+ this.scrollDirection !== 'backward'
1539
1577
  const shouldAdjustScroll =
1540
1578
  this.scrollState?.behavior !== 'smooth' &&
1541
1579
  (this.shouldAdjustScrollPositionOnItemSizeChange !== undefined
@@ -1553,15 +1591,7 @@ export class Virtualizer<
1553
1591
  delta,
1554
1592
  this,
1555
1593
  )
1556
- : // Default: adjust when the resize is an above-viewport item.
1557
- // First measurement (!has(key)): always adjust — the item
1558
- // has never been sized, so the estimate→actual delta must
1559
- // be compensated regardless of scroll direction.
1560
- // Re-measurement (has(key)): skip during backward scroll
1561
- // to avoid the "items jump while scrolling up" cascade.
1562
- itemStart < this.getScrollOffset() + this.scrollAdjustments &&
1563
- (!this.itemSizeCache.has(key) ||
1564
- this.scrollDirection !== 'backward'))
1594
+ : defaultShouldAdjust)
1565
1595
 
1566
1596
  if (this.pendingMin === null || index < this.pendingMin) {
1567
1597
  this.pendingMin = index
@@ -1719,6 +1749,14 @@ export class Virtualizer<
1719
1749
  toOffset: number,
1720
1750
  { align = 'start', behavior = 'auto' }: ScrollToOffsetOptions = {},
1721
1751
  ) => {
1752
+ // An absolute scroll command derives its target from current
1753
+ // measurements, so any iOS-deferred compensation still pending is stale by
1754
+ // definition — the command already accounts for the measurements the delta
1755
+ // was compensating for. Drop it so _flushIosDeferredIfReady doesn't replay
1756
+ // it onto the just-established position (relative commands like scrollBy
1757
+ // intentionally keep the deferral, since they build on the current offset).
1758
+ this._iosDeferredAdjustment = 0
1759
+
1722
1760
  const offset = this.getOffsetForAlignment(toOffset, align)
1723
1761
 
1724
1762
  const now = this.now()
@@ -1743,6 +1781,10 @@ export class Virtualizer<
1743
1781
  behavior = 'auto',
1744
1782
  }: ScrollToIndexOptions = {},
1745
1783
  ) => {
1784
+ // See scrollToOffset: an absolute target invalidates any pending
1785
+ // iOS-deferred compensation.
1786
+ this._iosDeferredAdjustment = 0
1787
+
1746
1788
  index = Math.max(0, Math.min(index, this.options.count - 1))
1747
1789
 
1748
1790
  const offsetInfo = this.getOffsetForIndex(index, initialAlign)