@tanstack/virtual-core 3.17.5 → 3.17.7

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
@@ -420,7 +420,7 @@ export class Virtualizer<
420
420
  scrollRect: Rect | null = null
421
421
  scrollOffset: number | null = null
422
422
  scrollDirection: ScrollDirection | null = null
423
- private scrollAdjustments = 0
423
+ scrollAdjustments = 0
424
424
  // Sum of size-change deltas above-viewport that were skipped during
425
425
  // iOS momentum scroll (writing scrollTop mid-momentum cancels it).
426
426
  // Flushed in a single scrollTo when iOS is fully settled.
@@ -679,8 +679,15 @@ export class Virtualizer<
679
679
  this.options.onChange?.(this, sync)
680
680
  }
681
681
 
682
- private applyScrollAdjustment(delta: number, behavior?: ScrollBehavior) {
683
- if (delta === 0) return
682
+ // Returns `true` when it performed a synchronous `scrollTop` write this
683
+ // tick, `false` when the delta was zero or the write was deferred (iOS).
684
+ // `resizeItem` uses that to decide whether the follow-up `notify` must be
685
+ // synchronous so the grown transforms commit in the same paint (#1227).
686
+ private applyScrollAdjustment(
687
+ delta: number,
688
+ behavior?: ScrollBehavior,
689
+ ): boolean {
690
+ if (delta === 0) return false
684
691
 
685
692
  if (process.env.NODE_ENV !== 'production' && this.options.debug) {
686
693
  console.info('correction', delta)
@@ -691,6 +698,7 @@ export class Virtualizer<
691
698
  (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)
692
699
  ) {
693
700
  this._iosDeferredAdjustment += delta
701
+ return false
694
702
  } else {
695
703
  this._scrollToOffset(this.getScrollOffset(), {
696
704
  adjustments: (this.scrollAdjustments += delta),
@@ -716,6 +724,7 @@ export class Virtualizer<
716
724
  if (this.scrollOffset < 0) this.scrollOffset = 0
717
725
  this.scrollAdjustments = 0
718
726
  }
727
+ return true
719
728
  }
720
729
  }
721
730
 
@@ -1555,6 +1564,25 @@ export class Virtualizer<
1555
1564
  this.scrollState?.behavior !== 'smooth' &&
1556
1565
  this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold
1557
1566
  const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0
1567
+ // Default anchoring predicate (used unless the consumer supplies a
1568
+ // custom shouldAdjustScrollPositionOnItemSizeChange).
1569
+ const scrollOffsetWithAdj =
1570
+ this.getScrollOffset() + this.scrollAdjustments
1571
+ const isFirstMeasure = !this.itemSizeCache.has(key)
1572
+ const defaultShouldAdjust = isFirstMeasure
1573
+ ? // First measurement: compensate any item whose top sits above the
1574
+ // fold — the estimate→actual delta must be corrected regardless of
1575
+ // scroll direction, since the whole estimated block was above it.
1576
+ itemStart < scrollOffsetWithAdj
1577
+ : // Re-measurement: only compensate an item that is ENTIRELY above the
1578
+ // fold. An item that merely *spans* the fold (top above, bottom
1579
+ // below — e.g. a streaming chat message growing at its bottom)
1580
+ // changes size *below* the anchor point, so shifting scrollTop by the
1581
+ // delta would drag the viewport downward on every growth (#1218).
1582
+ // Also skip during backward scroll to avoid the "items jump while
1583
+ // scrolling up" cascade.
1584
+ itemStart + itemSize <= scrollOffsetWithAdj &&
1585
+ this.scrollDirection !== 'backward'
1558
1586
  const shouldAdjustScroll =
1559
1587
  this.scrollState?.behavior !== 'smooth' &&
1560
1588
  (this.shouldAdjustScrollPositionOnItemSizeChange !== undefined
@@ -1572,15 +1600,7 @@ export class Virtualizer<
1572
1600
  delta,
1573
1601
  this,
1574
1602
  )
1575
- : // Default: adjust when the resize is an above-viewport item.
1576
- // First measurement (!has(key)): always adjust — the item
1577
- // has never been sized, so the estimate→actual delta must
1578
- // be compensated regardless of scroll direction.
1579
- // Re-measurement (has(key)): skip during backward scroll
1580
- // to avoid the "items jump while scrolling up" cascade.
1581
- itemStart < this.getScrollOffset() + this.scrollAdjustments &&
1582
- (!this.itemSizeCache.has(key) ||
1583
- this.scrollDirection !== 'backward'))
1603
+ : defaultShouldAdjust)
1584
1604
 
1585
1605
  if (this.pendingMin === null || index < this.pendingMin) {
1586
1606
  this.pendingMin = index
@@ -1588,13 +1608,26 @@ export class Virtualizer<
1588
1608
  this.itemSizeCache.set(key, size)
1589
1609
  this.itemSizeCacheVersion++
1590
1610
 
1611
+ let adjustedSync = false
1591
1612
  if (wasAtEnd) {
1592
- this.applyScrollAdjustment(this.getTotalSize() - prevTotalSize)
1613
+ adjustedSync = this.applyScrollAdjustment(
1614
+ this.getTotalSize() - prevTotalSize,
1615
+ )
1593
1616
  } else if (shouldAdjustScroll) {
1594
- this.applyScrollAdjustment(delta)
1617
+ adjustedSync = this.applyScrollAdjustment(delta)
1595
1618
  }
1596
1619
 
1597
- this.notify(false)
1620
+ // When we just moved `scrollTop` to compensate for an above-viewport
1621
+ // resize, the grown item transforms must commit in the SAME frame as
1622
+ // that write. `applyScrollAdjustment` writes `scrollTop` synchronously
1623
+ // inside this ResizeObserver callback, but an async `notify` schedules
1624
+ // the transform render for a later commit — so the browser can paint
1625
+ // one frame with the new `scrollTop` and the old positions, and the
1626
+ // viewport visibly jumps by `delta` before snapping back (#1227). A
1627
+ // synchronous notify flushes the render in this same callback, so both
1628
+ // land in one paint. When nothing moved (or the write was deferred on
1629
+ // iOS), keep the cheaper async notify.
1630
+ this.notify(adjustedSync)
1598
1631
  }
1599
1632
  }
1600
1633