@tanstack/virtual-core 3.17.11 → 3.19.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 +98 -45
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +9 -1
- package/dist/esm/index.d.ts +9 -1
- package/dist/esm/index.js +98 -45
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +178 -56
package/src/index.ts
CHANGED
|
@@ -56,6 +56,11 @@ export interface Range {
|
|
|
56
56
|
endIndex: number
|
|
57
57
|
overscan: number
|
|
58
58
|
count: number
|
|
59
|
+
/**
|
|
60
|
+
* Lane count of the virtualizer. When greater than 1, `overscan` counts
|
|
61
|
+
* rows of `lanes` items rather than single items.
|
|
62
|
+
*/
|
|
63
|
+
lanes?: number
|
|
59
64
|
}
|
|
60
65
|
|
|
61
66
|
type Key = number | string | bigint
|
|
@@ -84,8 +89,13 @@ const getRect = (element: HTMLElement): Rect => {
|
|
|
84
89
|
export const defaultKeyExtractor = (index: number) => index
|
|
85
90
|
|
|
86
91
|
export const defaultRangeExtractor = (range: Range) => {
|
|
87
|
-
|
|
88
|
-
|
|
92
|
+
// With lanes, the visible range is already aligned to whole rows; overscan
|
|
93
|
+
// by whole rows too, so every lane gets the same number of items rendered
|
|
94
|
+
// (and measured). Overscanning single items leaves partial rows, letting one
|
|
95
|
+
// lane collect more measured sizes than the others and drift (#1036).
|
|
96
|
+
const overscan = range.overscan * Math.max(range.lanes ?? 1, 1)
|
|
97
|
+
const start = Math.max(range.startIndex - overscan, 0)
|
|
98
|
+
const end = Math.min(range.endIndex + overscan, range.count - 1)
|
|
89
99
|
const len = end - start + 1
|
|
90
100
|
|
|
91
101
|
const arr = new Array<number>(len)
|
|
@@ -383,6 +393,8 @@ type ScrollState = {
|
|
|
383
393
|
index: number | null
|
|
384
394
|
align: ScrollAlignment
|
|
385
395
|
behavior: ScrollBehavior
|
|
396
|
+
// scrollToEnd: track the DOM max scroll offset instead of the item's offset
|
|
397
|
+
toEnd?: boolean
|
|
386
398
|
|
|
387
399
|
// lifecycle
|
|
388
400
|
startedAt: number
|
|
@@ -392,6 +404,8 @@ type ScrollState = {
|
|
|
392
404
|
|
|
393
405
|
// settling
|
|
394
406
|
stableFrames: number
|
|
407
|
+
// Keep arrival across measurement-driven retargets so external scrolls can cancel.
|
|
408
|
+
hasReachedTarget: boolean
|
|
395
409
|
}
|
|
396
410
|
|
|
397
411
|
type PendingScrollAnchor = [
|
|
@@ -501,6 +515,9 @@ export class Virtualizer<
|
|
|
501
515
|
instance: Virtualizer<TScrollElement, TItemElement>,
|
|
502
516
|
) => boolean)
|
|
503
517
|
elementsCache = new Map<Key, TItemElement>()
|
|
518
|
+
// Set while a ResizeObserver callback measures its entries; `resizeItem`
|
|
519
|
+
// records the pending notify here instead of firing it per entry.
|
|
520
|
+
private _resizeBatch: { sync: boolean; notify?: boolean } | null = null
|
|
504
521
|
private now = () => this.targetWindow?.performance?.now?.() ?? Date.now()
|
|
505
522
|
private observer = (() => {
|
|
506
523
|
let _ro: ResizeObserver | null = null
|
|
@@ -515,46 +532,60 @@ export class Virtualizer<
|
|
|
515
532
|
}
|
|
516
533
|
|
|
517
534
|
return (_ro = new this.targetWindow.ResizeObserver((entries) => {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
for (const [cacheKey, cachedNode] of this.elementsCache) {
|
|
534
|
-
if (cachedNode === node) {
|
|
535
|
-
this.elementsCache.delete(cacheKey)
|
|
536
|
-
break
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
return
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
if (!this.isIndexInRange(index)) return
|
|
543
|
-
|
|
544
|
-
if (this.shouldMeasureDuringScroll(index)) {
|
|
545
|
-
this.resizeItem(
|
|
546
|
-
index,
|
|
547
|
-
this.options.measureElement(node, entry, this),
|
|
548
|
-
)
|
|
535
|
+
const run = () => {
|
|
536
|
+
// Measure every entry before notifying. A notify that adjusted
|
|
537
|
+
// scroll re-renders synchronously, and that render can unmount
|
|
538
|
+
// nodes whose entries are still queued here, leaving them
|
|
539
|
+
// unmeasured. With lanes, a half-measured row lets one lane drift
|
|
540
|
+
// away from the others (#1036).
|
|
541
|
+
const batch: { sync: boolean; notify?: boolean } = { sync: false }
|
|
542
|
+
this._resizeBatch = batch
|
|
543
|
+
try {
|
|
544
|
+
entries.forEach(measureEntry)
|
|
545
|
+
} finally {
|
|
546
|
+
this._resizeBatch = null
|
|
547
|
+
if (batch.notify) {
|
|
548
|
+
this.notify(batch.sync)
|
|
549
|
+
this._retryClampedAdjustment()
|
|
549
550
|
}
|
|
550
551
|
}
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
552
|
+
}
|
|
553
|
+
this.options.useAnimationFrameWithResizeObserver
|
|
554
|
+
? requestAnimationFrame(run)
|
|
555
|
+
: run()
|
|
555
556
|
}))
|
|
556
557
|
}
|
|
557
558
|
|
|
559
|
+
const measureEntry = (entry: ResizeObserverEntry) => {
|
|
560
|
+
const node = entry.target as TItemElement
|
|
561
|
+
const index = this.indexFromElement(node)
|
|
562
|
+
|
|
563
|
+
if (!node.isConnected) {
|
|
564
|
+
this.observer.unobserve(node)
|
|
565
|
+
// Find the cache entry pointing to this exact node and remove
|
|
566
|
+
// it. We can't call getItemKey(index) here because items may
|
|
567
|
+
// have been removed since this node was rendered — the index
|
|
568
|
+
// could be stale and out-of-bounds in the user's data array
|
|
569
|
+
// (regression test in e2e/.../stale-index.spec.ts, fix #1148).
|
|
570
|
+
// The === comparison naturally handles the React-replaced-
|
|
571
|
+
// a-node-for-the-same-key case: that entry now points to a
|
|
572
|
+
// different node, so this loop won't match.
|
|
573
|
+
for (const [cacheKey, cachedNode] of this.elementsCache) {
|
|
574
|
+
if (cachedNode === node) {
|
|
575
|
+
this.elementsCache.delete(cacheKey)
|
|
576
|
+
break
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
if (!this.isIndexInRange(index)) return
|
|
583
|
+
|
|
584
|
+
if (this.shouldMeasureDuringScroll(index)) {
|
|
585
|
+
this.resizeItem(index, this.options.measureElement(node, entry, this))
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
558
589
|
return {
|
|
559
590
|
disconnect: () => {
|
|
560
591
|
get()?.disconnect()
|
|
@@ -756,7 +787,11 @@ export class Virtualizer<
|
|
|
756
787
|
isIOSWebKit() &&
|
|
757
788
|
(this.isScrolling || this._iosTouching || this._iosJustTouchEnded)
|
|
758
789
|
) {
|
|
759
|
-
|
|
790
|
+
// An index scroll retargets from fresh measurements in reconcileScroll;
|
|
791
|
+
// replaying this delta after it lands would count it twice.
|
|
792
|
+
if (this.scrollState?.index == null) {
|
|
793
|
+
this._iosDeferredAdjustment += delta
|
|
794
|
+
}
|
|
760
795
|
return false
|
|
761
796
|
} else {
|
|
762
797
|
const target = this.getScrollOffset() + this.scrollAdjustments + delta
|
|
@@ -924,6 +959,18 @@ export class Virtualizer<
|
|
|
924
959
|
Math.abs(offset - this._intendedScrollOffset) < 1.5
|
|
925
960
|
) {
|
|
926
961
|
offset = this._intendedScrollOffset
|
|
962
|
+
} else if (
|
|
963
|
+
this.scrollState?.index != null &&
|
|
964
|
+
this.scrollState.hasReachedTarget &&
|
|
965
|
+
!approxEqual(offset, this.scrollState.lastTargetOffset) &&
|
|
966
|
+
!approxEqual(offset, this.getScrollStateTarget(this.scrollState)) &&
|
|
967
|
+
(this._clampedAdjustment === null ||
|
|
968
|
+
Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)
|
|
969
|
+
) {
|
|
970
|
+
// The index scroll reached its target, but an external scroll moved
|
|
971
|
+
// away while we waited for measurements. Our own measurement updates
|
|
972
|
+
// (including clamped writes) still need to reconcile.
|
|
973
|
+
this.scrollState = null
|
|
927
974
|
}
|
|
928
975
|
this._intendedScrollOffset = null
|
|
929
976
|
|
|
@@ -1155,6 +1202,14 @@ export class Virtualizer<
|
|
|
1155
1202
|
this.reconcileScroll()
|
|
1156
1203
|
})
|
|
1157
1204
|
}
|
|
1205
|
+
private getScrollStateTarget(state: ScrollState) {
|
|
1206
|
+
const offsetInfo = state.toEnd
|
|
1207
|
+
? ([Math.max(this.getMaxScrollOffset(), 0)] as const)
|
|
1208
|
+
: state.index != null
|
|
1209
|
+
? this.getOffsetForIndex(state.index, state.align)
|
|
1210
|
+
: undefined
|
|
1211
|
+
return offsetInfo ? offsetInfo[0] : state.lastTargetOffset
|
|
1212
|
+
}
|
|
1158
1213
|
private reconcileScroll() {
|
|
1159
1214
|
if (!this.scrollState) return
|
|
1160
1215
|
|
|
@@ -1168,22 +1223,27 @@ export class Virtualizer<
|
|
|
1168
1223
|
return
|
|
1169
1224
|
}
|
|
1170
1225
|
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
const targetOffset =
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
//
|
|
1180
|
-
//
|
|
1181
|
-
|
|
1182
|
-
|
|
1226
|
+
// A resize since the last render leaves measurementsCache stale until
|
|
1227
|
+
// something reads getMeasurements(); rebuild it (memoized) so an index
|
|
1228
|
+
// target reflects it even when no render ran in between.
|
|
1229
|
+
this.getMeasurements()
|
|
1230
|
+
const targetOffset = this.getScrollStateTarget(this.scrollState)
|
|
1231
|
+
|
|
1232
|
+
// Index targets can still depend on cached sizes during the first rAF,
|
|
1233
|
+
// which runs before ResizeObserver delivers pending measurements. Wait
|
|
1234
|
+
// another frame (two when measurements are themselves deferred to rAF)
|
|
1235
|
+
// before retiring an index scroll. Absolute offsets don't need this wait.
|
|
1236
|
+
const STABLE_FRAMES =
|
|
1237
|
+
this.scrollState.index == null
|
|
1238
|
+
? 1
|
|
1239
|
+
: this.options.useAnimationFrameWithResizeObserver
|
|
1240
|
+
? 3
|
|
1241
|
+
: 2
|
|
1183
1242
|
|
|
1184
1243
|
const targetChanged = targetOffset !== this.scrollState.lastTargetOffset
|
|
1185
1244
|
|
|
1186
1245
|
if (!targetChanged && approxEqual(targetOffset, this.getScrollOffset())) {
|
|
1246
|
+
this.scrollState.hasReachedTarget = true
|
|
1187
1247
|
this.scrollState.stableFrames++
|
|
1188
1248
|
if (this.scrollState.stableFrames >= STABLE_FRAMES) {
|
|
1189
1249
|
// Final-pass exact landing. The reconcile-stable check uses a 1.01px
|
|
@@ -1218,7 +1278,11 @@ export class Virtualizer<
|
|
|
1218
1278
|
this.scrollState.behavior === 'smooth' && distance > viewport
|
|
1219
1279
|
|
|
1220
1280
|
this.scrollState.lastTargetOffset = targetOffset
|
|
1221
|
-
if (
|
|
1281
|
+
if (keepSmooth) {
|
|
1282
|
+
// A smooth retarget travels again, so its intermediate scroll
|
|
1283
|
+
// events must not read as an external scroll away from the target.
|
|
1284
|
+
this.scrollState.hasReachedTarget = false
|
|
1285
|
+
} else {
|
|
1222
1286
|
this.scrollState.behavior = 'auto'
|
|
1223
1287
|
}
|
|
1224
1288
|
|
|
@@ -1581,9 +1645,10 @@ export class Virtualizer<
|
|
|
1581
1645
|
this.options.count,
|
|
1582
1646
|
startIndex,
|
|
1583
1647
|
endIndex,
|
|
1648
|
+
this.options.lanes,
|
|
1584
1649
|
]
|
|
1585
1650
|
},
|
|
1586
|
-
(rangeExtractor, overscan, count, startIndex, endIndex) => {
|
|
1651
|
+
(rangeExtractor, overscan, count, startIndex, endIndex, lanes) => {
|
|
1587
1652
|
return startIndex === null || endIndex === null
|
|
1588
1653
|
? []
|
|
1589
1654
|
: rangeExtractor({
|
|
@@ -1591,6 +1656,7 @@ export class Virtualizer<
|
|
|
1591
1656
|
endIndex,
|
|
1592
1657
|
overscan,
|
|
1593
1658
|
count,
|
|
1659
|
+
lanes,
|
|
1594
1660
|
})
|
|
1595
1661
|
},
|
|
1596
1662
|
{
|
|
@@ -1673,7 +1739,12 @@ export class Virtualizer<
|
|
|
1673
1739
|
// Sync-measure when idle (initial render) or during programmatic scrolling
|
|
1674
1740
|
// (scrollToIndex/scrollToOffset) where reconcileScroll needs sizes in the same frame.
|
|
1675
1741
|
// During normal user scrolling, skip sync measurement — the RO callback handles it async.
|
|
1742
|
+
// Skip nodes that aren't attached yet: they have no layout and would be
|
|
1743
|
+
// cached as 0. Vue calls function refs during patch, before the parent is
|
|
1744
|
+
// inserted. The node is observed above, so the RO reports its size once
|
|
1745
|
+
// it's connected.
|
|
1676
1746
|
if (
|
|
1747
|
+
node.isConnected &&
|
|
1677
1748
|
(!this.isScrolling || this.scrollState) &&
|
|
1678
1749
|
this.shouldMeasureDuringScroll(index)
|
|
1679
1750
|
) {
|
|
@@ -1684,6 +1755,14 @@ export class Virtualizer<
|
|
|
1684
1755
|
resizeItem = (index: number, size: number) => {
|
|
1685
1756
|
if (!this.isIndexInRange(index)) return
|
|
1686
1757
|
|
|
1758
|
+
// An earlier entry in this ResizeObserver batch moved `scrollOffset`.
|
|
1759
|
+
// Outside a batch its sync notify would have re-rendered and rebuilt the
|
|
1760
|
+
// caches read below; rebuild them here so the anchoring check compares
|
|
1761
|
+
// this item's current start, not a stale one, against the adjusted
|
|
1762
|
+
// offset (#1218). Without a scroll move, the caches are exactly as stale
|
|
1763
|
+
// as they were before batching, so skip the rebuild.
|
|
1764
|
+
if (this._resizeBatch?.sync) this.getMeasurements()
|
|
1765
|
+
|
|
1687
1766
|
// Fast field reads. For lanes===1 we read raw start/size from the flat
|
|
1688
1767
|
// typed array, avoiding a Proxy.get + VirtualItem allocation per call.
|
|
1689
1768
|
// For lanes>1 we fall back to the cached VirtualItem array.
|
|
@@ -1775,6 +1854,13 @@ export class Virtualizer<
|
|
|
1775
1854
|
// synchronous notify flushes the render in this same callback, so both
|
|
1776
1855
|
// land in one paint. When nothing moved (or the write was deferred on
|
|
1777
1856
|
// iOS), keep the cheaper async notify.
|
|
1857
|
+
if (this._resizeBatch) {
|
|
1858
|
+
// Inside a ResizeObserver callback: notify once after every entry
|
|
1859
|
+
// is measured, synchronously if any of them moved `scrollTop`.
|
|
1860
|
+
this._resizeBatch.notify = true
|
|
1861
|
+
this._resizeBatch.sync ||= adjustedSync
|
|
1862
|
+
return
|
|
1863
|
+
}
|
|
1778
1864
|
this.notify(adjustedSync)
|
|
1779
1865
|
// A consumer that grows the sizer synchronously inside `onChange`
|
|
1780
1866
|
// (direct DOM updates) may never re-render when the range is
|
|
@@ -1904,9 +1990,14 @@ export class Virtualizer<
|
|
|
1904
1990
|
}
|
|
1905
1991
|
|
|
1906
1992
|
// 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
|
|
1993
|
+
// to account for borders/padding that aren't in our measurements (#1001),
|
|
1994
|
+
// but swap paddingEnd for scrollPaddingEnd so the last item lands where
|
|
1995
|
+
// any other end-aligned item would instead of overshooting past it (#1257).
|
|
1908
1996
|
if (align === 'end' && index === this.options.count - 1) {
|
|
1909
|
-
|
|
1997
|
+
const maxOffset = this.getMaxScrollOffset()
|
|
1998
|
+
const toOffset =
|
|
1999
|
+
maxOffset - this.options.paddingEnd + this.options.scrollPaddingEnd
|
|
2000
|
+
return [Math.max(Math.min(toOffset, maxOffset), 0), align] as const
|
|
1910
2001
|
}
|
|
1911
2002
|
|
|
1912
2003
|
const toOffset =
|
|
@@ -1942,6 +2033,7 @@ export class Virtualizer<
|
|
|
1942
2033
|
startedAt: now,
|
|
1943
2034
|
lastTargetOffset: offset,
|
|
1944
2035
|
stableFrames: 0,
|
|
2036
|
+
hasReachedTarget: false,
|
|
1945
2037
|
}
|
|
1946
2038
|
|
|
1947
2039
|
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
@@ -1976,6 +2068,7 @@ export class Virtualizer<
|
|
|
1976
2068
|
startedAt: now,
|
|
1977
2069
|
lastTargetOffset: offset,
|
|
1978
2070
|
stableFrames: 0,
|
|
2071
|
+
hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
|
|
1979
2072
|
}
|
|
1980
2073
|
|
|
1981
2074
|
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
@@ -1997,6 +2090,7 @@ export class Virtualizer<
|
|
|
1997
2090
|
startedAt: now,
|
|
1998
2091
|
lastTargetOffset: offset,
|
|
1999
2092
|
stableFrames: 0,
|
|
2093
|
+
hasReachedTarget: false,
|
|
2000
2094
|
}
|
|
2001
2095
|
|
|
2002
2096
|
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
@@ -2006,10 +2100,26 @@ export class Virtualizer<
|
|
|
2006
2100
|
|
|
2007
2101
|
scrollToEnd = ({ behavior = 'auto' }: ScrollToEndOptions = {}) => {
|
|
2008
2102
|
if (this.options.count > 0) {
|
|
2009
|
-
|
|
2103
|
+
// Target the browser's max scroll rather than the last item's end-align
|
|
2104
|
+
// offset, so paddingEnd is included and isAtEnd() holds afterwards.
|
|
2105
|
+
this._iosDeferredAdjustment = 0
|
|
2106
|
+
|
|
2107
|
+
const offset = Math.max(this.getMaxScrollOffset(), 0)
|
|
2108
|
+
|
|
2109
|
+
this.scrollState = {
|
|
2110
|
+
index: this.options.count - 1,
|
|
2010
2111
|
align: 'end',
|
|
2011
2112
|
behavior,
|
|
2012
|
-
|
|
2113
|
+
toEnd: true,
|
|
2114
|
+
startedAt: this.now(),
|
|
2115
|
+
lastTargetOffset: offset,
|
|
2116
|
+
stableFrames: 0,
|
|
2117
|
+
hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
|
|
2118
|
+
}
|
|
2119
|
+
|
|
2120
|
+
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
2121
|
+
|
|
2122
|
+
this.scheduleScrollReconcile()
|
|
2013
2123
|
return
|
|
2014
2124
|
}
|
|
2015
2125
|
|
|
@@ -2018,6 +2128,18 @@ export class Virtualizer<
|
|
|
2018
2128
|
})
|
|
2019
2129
|
}
|
|
2020
2130
|
|
|
2131
|
+
// Drops the target of an in-flight scrollToIndex / scrollToOffset /
|
|
2132
|
+
// scrollBy / scrollToEnd so a user gesture can take over the viewport.
|
|
2133
|
+
// The scroll position is left untouched: writing it would stop iOS
|
|
2134
|
+
// momentum, and a real gesture already interrupts a native smooth scroll.
|
|
2135
|
+
cancelScroll = () => {
|
|
2136
|
+
if (this.rafId != null && this.targetWindow) {
|
|
2137
|
+
this.targetWindow.cancelAnimationFrame(this.rafId)
|
|
2138
|
+
this.rafId = null
|
|
2139
|
+
}
|
|
2140
|
+
this.scrollState = null
|
|
2141
|
+
}
|
|
2142
|
+
|
|
2021
2143
|
getTotalSize = () => {
|
|
2022
2144
|
const measurements = this.getMeasurements()
|
|
2023
2145
|
|