@tanstack/virtual-core 3.17.11 → 3.18.0

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
@@ -383,6 +383,8 @@ type ScrollState = {
383
383
  index: number | null
384
384
  align: ScrollAlignment
385
385
  behavior: ScrollBehavior
386
+ // scrollToEnd: track the DOM max scroll offset instead of the item's offset
387
+ toEnd?: boolean
386
388
 
387
389
  // lifecycle
388
390
  startedAt: number
@@ -392,6 +394,8 @@ type ScrollState = {
392
394
 
393
395
  // settling
394
396
  stableFrames: number
397
+ // Keep arrival across measurement-driven retargets so external scrolls can cancel.
398
+ hasReachedTarget: boolean
395
399
  }
396
400
 
397
401
  type PendingScrollAnchor = [
@@ -924,6 +928,18 @@ export class Virtualizer<
924
928
  Math.abs(offset - this._intendedScrollOffset) < 1.5
925
929
  ) {
926
930
  offset = this._intendedScrollOffset
931
+ } else if (
932
+ this.scrollState?.index != null &&
933
+ this.scrollState.hasReachedTarget &&
934
+ !approxEqual(offset, this.scrollState.lastTargetOffset) &&
935
+ !approxEqual(offset, this.getScrollStateTarget(this.scrollState)) &&
936
+ (this._clampedAdjustment === null ||
937
+ Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)
938
+ ) {
939
+ // The index scroll reached its target, but an external scroll moved
940
+ // away while we waited for measurements. Our own measurement updates
941
+ // (including clamped writes) still need to reconcile.
942
+ this.scrollState = null
927
943
  }
928
944
  this._intendedScrollOffset = null
929
945
 
@@ -1155,6 +1171,14 @@ export class Virtualizer<
1155
1171
  this.reconcileScroll()
1156
1172
  })
1157
1173
  }
1174
+ private getScrollStateTarget(state: ScrollState) {
1175
+ const offsetInfo = state.toEnd
1176
+ ? ([Math.max(this.getMaxScrollOffset(), 0)] as const)
1177
+ : state.index != null
1178
+ ? this.getOffsetForIndex(state.index, state.align)
1179
+ : undefined
1180
+ return offsetInfo ? offsetInfo[0] : state.lastTargetOffset
1181
+ }
1158
1182
  private reconcileScroll() {
1159
1183
  if (!this.scrollState) return
1160
1184
 
@@ -1168,22 +1192,23 @@ export class Virtualizer<
1168
1192
  return
1169
1193
  }
1170
1194
 
1171
- const offsetInfo =
1172
- this.scrollState.index != null
1173
- ? this.getOffsetForIndex(this.scrollState.index, this.scrollState.align)
1174
- : undefined
1175
- const targetOffset = offsetInfo
1176
- ? offsetInfo[0]
1177
- : this.scrollState.lastTargetOffset
1195
+ const targetOffset = this.getScrollStateTarget(this.scrollState)
1178
1196
 
1179
- // Require one stable frame where target matches scroll offset.
1180
- // approxEqual() already tolerates minor fluctuations, so one frame is sufficient
1181
- // to confirm scroll has reached its target without premature cleanup.
1182
- const STABLE_FRAMES = 1
1197
+ // Index targets can still depend on cached sizes during the first rAF,
1198
+ // which runs before ResizeObserver delivers pending measurements. Wait
1199
+ // another frame (two when measurements are themselves deferred to rAF)
1200
+ // before retiring an index scroll. Absolute offsets don't need this wait.
1201
+ const STABLE_FRAMES =
1202
+ this.scrollState.index == null
1203
+ ? 1
1204
+ : this.options.useAnimationFrameWithResizeObserver
1205
+ ? 3
1206
+ : 2
1183
1207
 
1184
1208
  const targetChanged = targetOffset !== this.scrollState.lastTargetOffset
1185
1209
 
1186
1210
  if (!targetChanged && approxEqual(targetOffset, this.getScrollOffset())) {
1211
+ this.scrollState.hasReachedTarget = true
1187
1212
  this.scrollState.stableFrames++
1188
1213
  if (this.scrollState.stableFrames >= STABLE_FRAMES) {
1189
1214
  // Final-pass exact landing. The reconcile-stable check uses a 1.01px
@@ -1218,7 +1243,11 @@ export class Virtualizer<
1218
1243
  this.scrollState.behavior === 'smooth' && distance > viewport
1219
1244
 
1220
1245
  this.scrollState.lastTargetOffset = targetOffset
1221
- if (!keepSmooth) {
1246
+ if (keepSmooth) {
1247
+ // A smooth retarget travels again, so its intermediate scroll
1248
+ // events must not read as an external scroll away from the target.
1249
+ this.scrollState.hasReachedTarget = false
1250
+ } else {
1222
1251
  this.scrollState.behavior = 'auto'
1223
1252
  }
1224
1253
 
@@ -1673,7 +1702,12 @@ export class Virtualizer<
1673
1702
  // Sync-measure when idle (initial render) or during programmatic scrolling
1674
1703
  // (scrollToIndex/scrollToOffset) where reconcileScroll needs sizes in the same frame.
1675
1704
  // During normal user scrolling, skip sync measurement — the RO callback handles it async.
1705
+ // Skip nodes that aren't attached yet: they have no layout and would be
1706
+ // cached as 0. Vue calls function refs during patch, before the parent is
1707
+ // inserted. The node is observed above, so the RO reports its size once
1708
+ // it's connected.
1676
1709
  if (
1710
+ node.isConnected &&
1677
1711
  (!this.isScrolling || this.scrollState) &&
1678
1712
  this.shouldMeasureDuringScroll(index)
1679
1713
  ) {
@@ -1904,9 +1938,14 @@ export class Virtualizer<
1904
1938
  }
1905
1939
 
1906
1940
  // For the last item with 'end' alignment, use browser's actual max scroll
1907
- // to account for borders/padding that aren't in our measurements
1941
+ // to account for borders/padding that aren't in our measurements (#1001),
1942
+ // but swap paddingEnd for scrollPaddingEnd so the last item lands where
1943
+ // any other end-aligned item would instead of overshooting past it (#1257).
1908
1944
  if (align === 'end' && index === this.options.count - 1) {
1909
- return [this.getMaxScrollOffset(), align] as const
1945
+ const maxOffset = this.getMaxScrollOffset()
1946
+ const toOffset =
1947
+ maxOffset - this.options.paddingEnd + this.options.scrollPaddingEnd
1948
+ return [Math.max(Math.min(toOffset, maxOffset), 0), align] as const
1910
1949
  }
1911
1950
 
1912
1951
  const toOffset =
@@ -1942,6 +1981,7 @@ export class Virtualizer<
1942
1981
  startedAt: now,
1943
1982
  lastTargetOffset: offset,
1944
1983
  stableFrames: 0,
1984
+ hasReachedTarget: false,
1945
1985
  }
1946
1986
 
1947
1987
  this._scrollToOffset(offset, { adjustments: undefined, behavior })
@@ -1976,6 +2016,7 @@ export class Virtualizer<
1976
2016
  startedAt: now,
1977
2017
  lastTargetOffset: offset,
1978
2018
  stableFrames: 0,
2019
+ hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
1979
2020
  }
1980
2021
 
1981
2022
  this._scrollToOffset(offset, { adjustments: undefined, behavior })
@@ -1997,6 +2038,7 @@ export class Virtualizer<
1997
2038
  startedAt: now,
1998
2039
  lastTargetOffset: offset,
1999
2040
  stableFrames: 0,
2041
+ hasReachedTarget: false,
2000
2042
  }
2001
2043
 
2002
2044
  this._scrollToOffset(offset, { adjustments: undefined, behavior })
@@ -2006,10 +2048,26 @@ export class Virtualizer<
2006
2048
 
2007
2049
  scrollToEnd = ({ behavior = 'auto' }: ScrollToEndOptions = {}) => {
2008
2050
  if (this.options.count > 0) {
2009
- this.scrollToIndex(this.options.count - 1, {
2051
+ // Target the browser's max scroll rather than the last item's end-align
2052
+ // offset, so paddingEnd is included and isAtEnd() holds afterwards.
2053
+ this._iosDeferredAdjustment = 0
2054
+
2055
+ const offset = Math.max(this.getMaxScrollOffset(), 0)
2056
+
2057
+ this.scrollState = {
2058
+ index: this.options.count - 1,
2010
2059
  align: 'end',
2011
2060
  behavior,
2012
- })
2061
+ toEnd: true,
2062
+ startedAt: this.now(),
2063
+ lastTargetOffset: offset,
2064
+ stableFrames: 0,
2065
+ hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
2066
+ }
2067
+
2068
+ this._scrollToOffset(offset, { adjustments: undefined, behavior })
2069
+
2070
+ this.scheduleScrollReconcile()
2013
2071
  return
2014
2072
  }
2015
2073
 
@@ -2018,6 +2076,18 @@ export class Virtualizer<
2018
2076
  })
2019
2077
  }
2020
2078
 
2079
+ // Drops the target of an in-flight scrollToIndex / scrollToOffset /
2080
+ // scrollBy / scrollToEnd so a user gesture can take over the viewport.
2081
+ // The scroll position is left untouched: writing it would stop iOS
2082
+ // momentum, and a real gesture already interrupts a native smooth scroll.
2083
+ cancelScroll = () => {
2084
+ if (this.rafId != null && this.targetWindow) {
2085
+ this.targetWindow.cancelAnimationFrame(this.rafId)
2086
+ this.rafId = null
2087
+ }
2088
+ this.scrollState = null
2089
+ }
2090
+
2021
2091
  getTotalSize = () => {
2022
2092
  const measurements = this.getMeasurements()
2023
2093