@tanstack/virtual-core 3.17.10 → 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/dist/cjs/index.cjs +50 -15
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +2 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +51 -16
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +102 -17
package/src/index.ts
CHANGED
|
@@ -199,7 +199,7 @@ const observeOffset = <T extends Element | Window>(
|
|
|
199
199
|
? null
|
|
200
200
|
: debounce(
|
|
201
201
|
targetWindow,
|
|
202
|
-
() => cb(
|
|
202
|
+
() => cb(readOffset(element), false),
|
|
203
203
|
instance.options.isScrollingResetDelay,
|
|
204
204
|
)
|
|
205
205
|
|
|
@@ -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
|
|
|
@@ -1045,6 +1061,21 @@ export class Virtualizer<
|
|
|
1045
1061
|
if (anchorDelta !== 0) {
|
|
1046
1062
|
this._iosDeferredAdjustment += anchorDelta
|
|
1047
1063
|
}
|
|
1064
|
+
} else if (
|
|
1065
|
+
this.scrollState?.behavior === 'smooth' &&
|
|
1066
|
+
!approxEqual(
|
|
1067
|
+
this.getScrollOffset() - anchorDelta,
|
|
1068
|
+
this.scrollState.lastTargetOffset,
|
|
1069
|
+
)
|
|
1070
|
+
) {
|
|
1071
|
+
// A smooth programmatic scroll is still travelling. Writing scrollTop
|
|
1072
|
+
// here would cancel the browser's animation, and Chromium drops a
|
|
1073
|
+
// smooth request re-issued in the frame right after that cancel, so
|
|
1074
|
+
// the journey would be stranded. The target is index-based and
|
|
1075
|
+
// recomputes against the new layout in reconcileScroll, so let the
|
|
1076
|
+
// animation run; the next scroll event re-syncs the tracked offset.
|
|
1077
|
+
// A smooth scroll that has already landed (offset at its target,
|
|
1078
|
+
// reconcile not yet retired it) still gets the anchor sync.
|
|
1048
1079
|
} else {
|
|
1049
1080
|
this._scrollToOffset(this.getScrollOffset(), {
|
|
1050
1081
|
adjustments: undefined,
|
|
@@ -1140,6 +1171,14 @@ export class Virtualizer<
|
|
|
1140
1171
|
this.reconcileScroll()
|
|
1141
1172
|
})
|
|
1142
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
|
+
}
|
|
1143
1182
|
private reconcileScroll() {
|
|
1144
1183
|
if (!this.scrollState) return
|
|
1145
1184
|
|
|
@@ -1153,22 +1192,23 @@ export class Virtualizer<
|
|
|
1153
1192
|
return
|
|
1154
1193
|
}
|
|
1155
1194
|
|
|
1156
|
-
const
|
|
1157
|
-
this.scrollState.index != null
|
|
1158
|
-
? this.getOffsetForIndex(this.scrollState.index, this.scrollState.align)
|
|
1159
|
-
: undefined
|
|
1160
|
-
const targetOffset = offsetInfo
|
|
1161
|
-
? offsetInfo[0]
|
|
1162
|
-
: this.scrollState.lastTargetOffset
|
|
1195
|
+
const targetOffset = this.getScrollStateTarget(this.scrollState)
|
|
1163
1196
|
|
|
1164
|
-
//
|
|
1165
|
-
//
|
|
1166
|
-
//
|
|
1167
|
-
|
|
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
|
|
1168
1207
|
|
|
1169
1208
|
const targetChanged = targetOffset !== this.scrollState.lastTargetOffset
|
|
1170
1209
|
|
|
1171
1210
|
if (!targetChanged && approxEqual(targetOffset, this.getScrollOffset())) {
|
|
1211
|
+
this.scrollState.hasReachedTarget = true
|
|
1172
1212
|
this.scrollState.stableFrames++
|
|
1173
1213
|
if (this.scrollState.stableFrames >= STABLE_FRAMES) {
|
|
1174
1214
|
// Final-pass exact landing. The reconcile-stable check uses a 1.01px
|
|
@@ -1203,7 +1243,11 @@ export class Virtualizer<
|
|
|
1203
1243
|
this.scrollState.behavior === 'smooth' && distance > viewport
|
|
1204
1244
|
|
|
1205
1245
|
this.scrollState.lastTargetOffset = targetOffset
|
|
1206
|
-
if (
|
|
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 {
|
|
1207
1251
|
this.scrollState.behavior = 'auto'
|
|
1208
1252
|
}
|
|
1209
1253
|
|
|
@@ -1658,7 +1702,12 @@ export class Virtualizer<
|
|
|
1658
1702
|
// Sync-measure when idle (initial render) or during programmatic scrolling
|
|
1659
1703
|
// (scrollToIndex/scrollToOffset) where reconcileScroll needs sizes in the same frame.
|
|
1660
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.
|
|
1661
1709
|
if (
|
|
1710
|
+
node.isConnected &&
|
|
1662
1711
|
(!this.isScrolling || this.scrollState) &&
|
|
1663
1712
|
this.shouldMeasureDuringScroll(index)
|
|
1664
1713
|
) {
|
|
@@ -1889,9 +1938,14 @@ export class Virtualizer<
|
|
|
1889
1938
|
}
|
|
1890
1939
|
|
|
1891
1940
|
// For the last item with 'end' alignment, use browser's actual max scroll
|
|
1892
|
-
// 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).
|
|
1893
1944
|
if (align === 'end' && index === this.options.count - 1) {
|
|
1894
|
-
|
|
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
|
|
1895
1949
|
}
|
|
1896
1950
|
|
|
1897
1951
|
const toOffset =
|
|
@@ -1927,6 +1981,7 @@ export class Virtualizer<
|
|
|
1927
1981
|
startedAt: now,
|
|
1928
1982
|
lastTargetOffset: offset,
|
|
1929
1983
|
stableFrames: 0,
|
|
1984
|
+
hasReachedTarget: false,
|
|
1930
1985
|
}
|
|
1931
1986
|
|
|
1932
1987
|
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
@@ -1961,6 +2016,7 @@ export class Virtualizer<
|
|
|
1961
2016
|
startedAt: now,
|
|
1962
2017
|
lastTargetOffset: offset,
|
|
1963
2018
|
stableFrames: 0,
|
|
2019
|
+
hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
|
|
1964
2020
|
}
|
|
1965
2021
|
|
|
1966
2022
|
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
@@ -1982,6 +2038,7 @@ export class Virtualizer<
|
|
|
1982
2038
|
startedAt: now,
|
|
1983
2039
|
lastTargetOffset: offset,
|
|
1984
2040
|
stableFrames: 0,
|
|
2041
|
+
hasReachedTarget: false,
|
|
1985
2042
|
}
|
|
1986
2043
|
|
|
1987
2044
|
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
@@ -1991,10 +2048,26 @@ export class Virtualizer<
|
|
|
1991
2048
|
|
|
1992
2049
|
scrollToEnd = ({ behavior = 'auto' }: ScrollToEndOptions = {}) => {
|
|
1993
2050
|
if (this.options.count > 0) {
|
|
1994
|
-
|
|
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,
|
|
1995
2059
|
align: 'end',
|
|
1996
2060
|
behavior,
|
|
1997
|
-
|
|
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()
|
|
1998
2071
|
return
|
|
1999
2072
|
}
|
|
2000
2073
|
|
|
@@ -2003,6 +2076,18 @@ export class Virtualizer<
|
|
|
2003
2076
|
})
|
|
2004
2077
|
}
|
|
2005
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
|
+
|
|
2006
2091
|
getTotalSize = () => {
|
|
2007
2092
|
const measurements = this.getMeasurements()
|
|
2008
2093
|
|