@tanstack/virtual-core 3.19.0 → 3.19.2
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/dist/cjs/index.cjs +72 -28
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.js +72 -28
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +131 -34
package/src/index.ts
CHANGED
|
@@ -388,11 +388,19 @@ export interface VirtualizerOptions<
|
|
|
388
388
|
useCachedMeasurements?: boolean
|
|
389
389
|
}
|
|
390
390
|
|
|
391
|
+
// Where an in-flight scroll command is in its lifecycle, which decides how
|
|
392
|
+
// its scroll events and measurements are read:
|
|
393
|
+
// smooth a smooth animation is travelling to the target
|
|
394
|
+
// smoothLanded a smooth scroll is at its target, waiting to retire
|
|
395
|
+
// smoothFinishing a smooth scroll switched to instant writes for the
|
|
396
|
+
// final approach; its animation can still report positions
|
|
397
|
+
// instant instant writes
|
|
398
|
+
type ScrollPhase = 'smooth' | 'smoothLanded' | 'smoothFinishing' | 'instant'
|
|
399
|
+
|
|
391
400
|
type ScrollState = {
|
|
392
401
|
// what we want
|
|
393
402
|
index: number | null
|
|
394
403
|
align: ScrollAlignment
|
|
395
|
-
behavior: ScrollBehavior
|
|
396
404
|
// scrollToEnd: track the DOM max scroll offset instead of the item's offset
|
|
397
405
|
toEnd?: boolean
|
|
398
406
|
|
|
@@ -404,10 +412,33 @@ type ScrollState = {
|
|
|
404
412
|
|
|
405
413
|
// settling
|
|
406
414
|
stableFrames: number
|
|
407
|
-
|
|
408
|
-
|
|
415
|
+
phase: ScrollPhase
|
|
416
|
+
|
|
417
|
+
// Where our last non-smooth write for this command put the element, until
|
|
418
|
+
// its scroll event arrives. Until then `scrollOffset` still holds the old
|
|
419
|
+
// position, and the rows on screen are the ones rendered for it. A smooth
|
|
420
|
+
// write leaves it null: the element is somewhere between the two offsets.
|
|
421
|
+
// It goes with the scroll state, so a write the browser ignored (no read-back)
|
|
422
|
+
// does not outlive its command.
|
|
423
|
+
writtenOffset?: number | null
|
|
409
424
|
}
|
|
410
425
|
|
|
426
|
+
const initialScrollPhase = (
|
|
427
|
+
behavior: ScrollBehavior,
|
|
428
|
+
atTarget: boolean,
|
|
429
|
+
): ScrollPhase =>
|
|
430
|
+
behavior === 'smooth' ? (atTarget ? 'smoothLanded' : 'smooth') : 'instant'
|
|
431
|
+
|
|
432
|
+
// A smooth scroll owns the position: size compensation, the prepend anchor
|
|
433
|
+
// sync and far measurements hold off until it switches to instant writes.
|
|
434
|
+
const isSmoothScroll = (state: ScrollState | null): state is ScrollState =>
|
|
435
|
+
state?.phase === 'smooth' || state?.phase === 'smoothLanded'
|
|
436
|
+
|
|
437
|
+
// The smooth animation may still be reporting positions, so a scroll event
|
|
438
|
+
// that isn't the read-back of our own write may not be the user.
|
|
439
|
+
const isAnimating = (state: ScrollState) =>
|
|
440
|
+
state.phase === 'smooth' || state.phase === 'smoothFinishing'
|
|
441
|
+
|
|
411
442
|
type PendingScrollAnchor = [
|
|
412
443
|
key: Key | null,
|
|
413
444
|
offset: number,
|
|
@@ -481,9 +512,10 @@ export class Virtualizer<
|
|
|
481
512
|
private _iosDeferredAdjustment = 0
|
|
482
513
|
// Touch state. iOS WebKit cancels momentum when scrollTop is written, so
|
|
483
514
|
// we defer adjustments not only during `isScrolling` but also through the
|
|
484
|
-
// touchstart→touchend window (active drag) and a short tail
|
|
485
|
-
//
|
|
486
|
-
// the start of momentum, so we use a timer rather than
|
|
515
|
+
// touchstart→touchend/touchcancel window (active drag) and a short tail
|
|
516
|
+
// after the touch ends (early-momentum window — iOS only fires touch
|
|
517
|
+
// events once at the start of momentum, so we use a timer rather than
|
|
518
|
+
// another event).
|
|
487
519
|
private _iosTouching = false
|
|
488
520
|
private _iosJustTouchEnded = false
|
|
489
521
|
private _iosTouchEndTimerId: number | null = null
|
|
@@ -961,18 +993,32 @@ export class Virtualizer<
|
|
|
961
993
|
offset = this._intendedScrollOffset
|
|
962
994
|
} else if (
|
|
963
995
|
this.scrollState?.index != null &&
|
|
964
|
-
|
|
996
|
+
// A non-smooth write lands at once, so any other scroll event is
|
|
997
|
+
// the user's, even before reconcile saw the target reached (the
|
|
998
|
+
// target may keep moving, or iOS ignored the write under a
|
|
999
|
+
// finger). A write the browser clamped because the sizer hasn't
|
|
1000
|
+
// grown yet reads back at the max, which is also where the fresh
|
|
1001
|
+
// target clamps, so the check below keeps it reconciling (#1294).
|
|
1002
|
+
// Not when rows above the target shrink before the read-back and
|
|
1003
|
+
// pull the target below the new max: the read-back then looks
|
|
1004
|
+
// like the user and the scroll stops at the max. That needs the
|
|
1005
|
+
// sizer re-rendered before the scroll event, which browsers have
|
|
1006
|
+
// not been seen to do (pinned by a `.fails` test).
|
|
1007
|
+
// While a smooth scroll's animation may still report, its events
|
|
1008
|
+
// look like a user's, so it yields only once it has landed.
|
|
1009
|
+
!isAnimating(this.scrollState) &&
|
|
965
1010
|
!approxEqual(offset, this.scrollState.lastTargetOffset) &&
|
|
966
1011
|
!approxEqual(offset, this.getScrollStateTarget(this.scrollState)) &&
|
|
967
1012
|
(this._clampedAdjustment === null ||
|
|
968
1013
|
Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)
|
|
969
1014
|
) {
|
|
970
|
-
//
|
|
971
|
-
//
|
|
972
|
-
//
|
|
1015
|
+
// An external scroll moved away from the index scroll's target.
|
|
1016
|
+
// Our own measurement updates (including clamped writes) still
|
|
1017
|
+
// need to reconcile.
|
|
973
1018
|
this.scrollState = null
|
|
974
1019
|
}
|
|
975
1020
|
this._intendedScrollOffset = null
|
|
1021
|
+
if (this.scrollState) this.scrollState.writtenOffset = null
|
|
976
1022
|
|
|
977
1023
|
// A pending clamped compensation write (#1258) survives only its
|
|
978
1024
|
// own read-back, which the browser reports at the scroll max we
|
|
@@ -1053,9 +1099,17 @@ export class Virtualizer<
|
|
|
1053
1099
|
onTouchEnd,
|
|
1054
1100
|
addEventListenerOptions,
|
|
1055
1101
|
)
|
|
1102
|
+
// Same handler as touchend: iOS can end a touch with touchcancel
|
|
1103
|
+
// (system gesture, interrupted scroll) and never fire touchend.
|
|
1104
|
+
scrollEl.addEventListener(
|
|
1105
|
+
'touchcancel',
|
|
1106
|
+
onTouchEnd,
|
|
1107
|
+
addEventListenerOptions,
|
|
1108
|
+
)
|
|
1056
1109
|
this.unsubs.push(() => {
|
|
1057
1110
|
scrollEl.removeEventListener('touchstart', onTouchStart)
|
|
1058
1111
|
scrollEl.removeEventListener('touchend', onTouchEnd)
|
|
1112
|
+
scrollEl.removeEventListener('touchcancel', onTouchEnd)
|
|
1059
1113
|
if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
|
|
1060
1114
|
this.targetWindow.clearTimeout(this._iosTouchEndTimerId)
|
|
1061
1115
|
this._iosTouchEndTimerId = null
|
|
@@ -1093,7 +1147,7 @@ export class Virtualizer<
|
|
|
1093
1147
|
this._iosDeferredAdjustment += anchorDelta
|
|
1094
1148
|
}
|
|
1095
1149
|
} else if (
|
|
1096
|
-
this.scrollState
|
|
1150
|
+
isSmoothScroll(this.scrollState) &&
|
|
1097
1151
|
!approxEqual(
|
|
1098
1152
|
this.getScrollOffset() - anchorDelta,
|
|
1099
1153
|
this.scrollState.lastTargetOffset,
|
|
@@ -1243,7 +1297,13 @@ export class Virtualizer<
|
|
|
1243
1297
|
const targetChanged = targetOffset !== this.scrollState.lastTargetOffset
|
|
1244
1298
|
|
|
1245
1299
|
if (!targetChanged && approxEqual(targetOffset, this.getScrollOffset())) {
|
|
1246
|
-
|
|
1300
|
+
// At the target: a travelling smooth scroll has landed, and a
|
|
1301
|
+
// finishing one is now an instant scroll.
|
|
1302
|
+
if (this.scrollState.phase === 'smooth') {
|
|
1303
|
+
this.scrollState.phase = 'smoothLanded'
|
|
1304
|
+
} else if (this.scrollState.phase === 'smoothFinishing') {
|
|
1305
|
+
this.scrollState.phase = 'instant'
|
|
1306
|
+
}
|
|
1247
1307
|
this.scrollState.stableFrames++
|
|
1248
1308
|
if (this.scrollState.stableFrames >= STABLE_FRAMES) {
|
|
1249
1309
|
// Final-pass exact landing. The reconcile-stable check uses a 1.01px
|
|
@@ -1275,16 +1335,21 @@ export class Virtualizer<
|
|
|
1275
1335
|
const viewport = this.getSize() || 600
|
|
1276
1336
|
const distance = Math.abs(targetOffset - this.getScrollOffset())
|
|
1277
1337
|
const keepSmooth =
|
|
1278
|
-
this.scrollState
|
|
1338
|
+
isSmoothScroll(this.scrollState) && distance > viewport
|
|
1279
1339
|
|
|
1280
1340
|
this.scrollState.lastTargetOffset = targetOffset
|
|
1281
1341
|
if (keepSmooth) {
|
|
1282
1342
|
// A smooth retarget travels again, so its intermediate scroll
|
|
1283
1343
|
// events must not read as an external scroll away from the target.
|
|
1284
|
-
this.scrollState.
|
|
1285
|
-
} else {
|
|
1286
|
-
|
|
1344
|
+
this.scrollState.phase = 'smooth'
|
|
1345
|
+
} else if (this.scrollState.phase === 'smooth') {
|
|
1346
|
+
// The final approach is instant, but the animation can still report.
|
|
1347
|
+
this.scrollState.phase = 'smoothFinishing'
|
|
1348
|
+
} else if (this.scrollState.phase === 'smoothLanded') {
|
|
1349
|
+
this.scrollState.phase = 'instant'
|
|
1287
1350
|
}
|
|
1351
|
+
// 'smoothFinishing' stays as it is (its animation may still report)
|
|
1352
|
+
// until reconcile sees it at the target; 'instant' stays instant.
|
|
1288
1353
|
|
|
1289
1354
|
this._scrollToOffset(targetOffset, {
|
|
1290
1355
|
adjustments: undefined,
|
|
@@ -1686,7 +1751,7 @@ export class Virtualizer<
|
|
|
1686
1751
|
*/
|
|
1687
1752
|
private shouldMeasureDuringScroll = (index: number): boolean => {
|
|
1688
1753
|
// No scroll state or not smooth scroll - always allow measurements
|
|
1689
|
-
if (!this.scrollState
|
|
1754
|
+
if (!isSmoothScroll(this.scrollState)) {
|
|
1690
1755
|
return true
|
|
1691
1756
|
}
|
|
1692
1757
|
|
|
@@ -1788,7 +1853,7 @@ export class Virtualizer<
|
|
|
1788
1853
|
if (delta !== 0) {
|
|
1789
1854
|
const wasAtEnd =
|
|
1790
1855
|
this.options.anchorTo === 'end' &&
|
|
1791
|
-
this.scrollState
|
|
1856
|
+
!isSmoothScroll(this.scrollState) &&
|
|
1792
1857
|
this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold
|
|
1793
1858
|
const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0
|
|
1794
1859
|
// Default anchoring predicate (used unless the consumer supplies a
|
|
@@ -1811,7 +1876,7 @@ export class Virtualizer<
|
|
|
1811
1876
|
itemStart + itemSize <= scrollOffsetWithAdj &&
|
|
1812
1877
|
this.scrollDirection !== 'backward'
|
|
1813
1878
|
const shouldAdjustScroll =
|
|
1814
|
-
this.scrollState
|
|
1879
|
+
!isSmoothScroll(this.scrollState) &&
|
|
1815
1880
|
(this.shouldAdjustScrollPositionOnItemSizeChange !== undefined
|
|
1816
1881
|
? this.shouldAdjustScrollPositionOnItemSizeChange(
|
|
1817
1882
|
// The callback expects a VirtualItem; build one lazily only
|
|
@@ -1835,13 +1900,33 @@ export class Virtualizer<
|
|
|
1835
1900
|
this.itemSizeCache.set(key, size)
|
|
1836
1901
|
this.itemSizeCacheVersion++
|
|
1837
1902
|
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1903
|
+
// A scroll command (or reconcile) wrote a new position that neither a
|
|
1904
|
+
// scroll event nor an eager update has reflected in `scrollOffset` yet.
|
|
1905
|
+
// The rows being measured were rendered for the old position, so
|
|
1906
|
+
// "above the viewport" means nothing against either offset. An index
|
|
1907
|
+
// or end target is retargeted from fresh measurements by
|
|
1908
|
+
// reconcileScroll next frame; an offset target (scrollToOffset,
|
|
1909
|
+
// scrollBy) lands where it was written and the delta is not applied,
|
|
1910
|
+
// since compensating against the stale offset would overwrite the
|
|
1911
|
+
// command. Compensation writes and the prepend anchor sync update
|
|
1912
|
+
// `scrollOffset` eagerly, and the iOS flush writes exactly
|
|
1913
|
+
// `scrollOffset + scrollAdjustments`, so none of them count.
|
|
1914
|
+
const awaitingCommandReadBack =
|
|
1915
|
+
this.scrollState?.writtenOffset != null &&
|
|
1916
|
+
!approxEqual(
|
|
1917
|
+
this.scrollState.writtenOffset,
|
|
1918
|
+
this.getScrollOffset() + this.scrollAdjustments,
|
|
1842
1919
|
)
|
|
1843
|
-
|
|
1844
|
-
|
|
1920
|
+
|
|
1921
|
+
let adjustedSync = false
|
|
1922
|
+
if (!awaitingCommandReadBack) {
|
|
1923
|
+
if (wasAtEnd) {
|
|
1924
|
+
adjustedSync = this.applyScrollAdjustment(
|
|
1925
|
+
this.getTotalSize() - prevTotalSize,
|
|
1926
|
+
)
|
|
1927
|
+
} else if (shouldAdjustScroll) {
|
|
1928
|
+
adjustedSync = this.applyScrollAdjustment(delta)
|
|
1929
|
+
}
|
|
1845
1930
|
}
|
|
1846
1931
|
|
|
1847
1932
|
// When we just moved `scrollTop` to compensate for an above-viewport
|
|
@@ -2029,11 +2114,10 @@ export class Virtualizer<
|
|
|
2029
2114
|
this.scrollState = {
|
|
2030
2115
|
index: null,
|
|
2031
2116
|
align,
|
|
2032
|
-
behavior,
|
|
2033
2117
|
startedAt: now,
|
|
2034
2118
|
lastTargetOffset: offset,
|
|
2035
2119
|
stableFrames: 0,
|
|
2036
|
-
|
|
2120
|
+
phase: initialScrollPhase(behavior, false),
|
|
2037
2121
|
}
|
|
2038
2122
|
|
|
2039
2123
|
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
@@ -2064,11 +2148,13 @@ export class Virtualizer<
|
|
|
2064
2148
|
this.scrollState = {
|
|
2065
2149
|
index,
|
|
2066
2150
|
align,
|
|
2067
|
-
behavior,
|
|
2068
2151
|
startedAt: now,
|
|
2069
2152
|
lastTargetOffset: offset,
|
|
2070
2153
|
stableFrames: 0,
|
|
2071
|
-
|
|
2154
|
+
phase: initialScrollPhase(
|
|
2155
|
+
behavior,
|
|
2156
|
+
approxEqual(offset, this.getScrollOffset()),
|
|
2157
|
+
),
|
|
2072
2158
|
}
|
|
2073
2159
|
|
|
2074
2160
|
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
@@ -2080,17 +2166,22 @@ export class Virtualizer<
|
|
|
2080
2166
|
delta: number,
|
|
2081
2167
|
{ behavior = 'auto' }: ScrollToOffsetOptions = {},
|
|
2082
2168
|
) => {
|
|
2083
|
-
|
|
2169
|
+
// Build on a position we just wrote even before its scroll event arrives.
|
|
2170
|
+
// Clamp: a write past either edge that leaves the element where it was
|
|
2171
|
+
// fires no scroll event, so nothing would clear an unreachable offset.
|
|
2172
|
+
const offset = this.getOffsetForAlignment(
|
|
2173
|
+
(this.scrollState?.writtenOffset ?? this.getScrollOffset()) + delta,
|
|
2174
|
+
'start',
|
|
2175
|
+
)
|
|
2084
2176
|
const now = this.now()
|
|
2085
2177
|
|
|
2086
2178
|
this.scrollState = {
|
|
2087
2179
|
index: null,
|
|
2088
2180
|
align: 'start',
|
|
2089
|
-
behavior,
|
|
2090
2181
|
startedAt: now,
|
|
2091
2182
|
lastTargetOffset: offset,
|
|
2092
2183
|
stableFrames: 0,
|
|
2093
|
-
|
|
2184
|
+
phase: initialScrollPhase(behavior, false),
|
|
2094
2185
|
}
|
|
2095
2186
|
|
|
2096
2187
|
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
@@ -2109,12 +2200,14 @@ export class Virtualizer<
|
|
|
2109
2200
|
this.scrollState = {
|
|
2110
2201
|
index: this.options.count - 1,
|
|
2111
2202
|
align: 'end',
|
|
2112
|
-
behavior,
|
|
2113
2203
|
toEnd: true,
|
|
2114
2204
|
startedAt: this.now(),
|
|
2115
2205
|
lastTargetOffset: offset,
|
|
2116
2206
|
stableFrames: 0,
|
|
2117
|
-
|
|
2207
|
+
phase: initialScrollPhase(
|
|
2208
|
+
behavior,
|
|
2209
|
+
approxEqual(offset, this.getScrollOffset()),
|
|
2210
|
+
),
|
|
2118
2211
|
}
|
|
2119
2212
|
|
|
2120
2213
|
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
@@ -2227,6 +2320,10 @@ export class Virtualizer<
|
|
|
2227
2320
|
// Record the intended logical scroll target so the next scroll event
|
|
2228
2321
|
// can reconcile against subpixel rounding by the browser.
|
|
2229
2322
|
this._intendedScrollOffset = offset + (adjustments ?? 0)
|
|
2323
|
+
if (this.scrollState) {
|
|
2324
|
+
this.scrollState.writtenOffset =
|
|
2325
|
+
behavior === 'smooth' ? null : this._intendedScrollOffset
|
|
2326
|
+
}
|
|
2230
2327
|
this.options.scrollToFn(offset, { behavior, adjustments }, this)
|
|
2231
2328
|
}
|
|
2232
2329
|
|