@tanstack/virtual-core 3.17.3 → 3.17.5
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 +45 -38
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +0 -1
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/index.js +45 -38
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +89 -59
package/src/index.ts
CHANGED
|
@@ -651,7 +651,11 @@ export class Virtualizer<
|
|
|
651
651
|
if (idx < count) {
|
|
652
652
|
const anchorItem = newMeasurements[idx]
|
|
653
653
|
if (anchorItem) {
|
|
654
|
-
|
|
654
|
+
// Clamp to the reachable range's lower bound — anchorOffset may
|
|
655
|
+
// have been derived from a transiently negative scrollOffset
|
|
656
|
+
// (rubber-band), and a negative tracked offset never self-heals
|
|
657
|
+
// when the element cannot scroll (#1229).
|
|
658
|
+
const newOffset = Math.max(0, anchorItem.start + anchorOffset)
|
|
655
659
|
if (newOffset !== this.scrollOffset) {
|
|
656
660
|
anchorDelta = newOffset - this.scrollOffset
|
|
657
661
|
this.scrollOffset = newOffset
|
|
@@ -703,6 +707,13 @@ export class Virtualizer<
|
|
|
703
707
|
// `scrollAdjustments` to keep their sum invariant.
|
|
704
708
|
if (this.scrollOffset !== null) {
|
|
705
709
|
this.scrollOffset += this.scrollAdjustments
|
|
710
|
+
// Clamp only the lower bound: a negative offset is unreachable, and
|
|
711
|
+
// on an unscrollable element (content fits the viewport) no scroll
|
|
712
|
+
// event ever fires to correct it, permanently skewing
|
|
713
|
+
// getDistanceFromEnd() and wedging _flushIosDeferredIfReady (#1229).
|
|
714
|
+
// Upper-bound overflow stays untouched — it is transiently
|
|
715
|
+
// legitimate mid-prepend while the consumer's sizer catches up.
|
|
716
|
+
if (this.scrollOffset < 0) this.scrollOffset = 0
|
|
706
717
|
this.scrollAdjustments = 0
|
|
707
718
|
}
|
|
708
719
|
}
|
|
@@ -741,6 +752,19 @@ export class Virtualizer<
|
|
|
741
752
|
this.rafId = null
|
|
742
753
|
}
|
|
743
754
|
this.scrollState = null
|
|
755
|
+
// The iOS gesture/deferral state is scoped to the current scroll
|
|
756
|
+
// element: the touch listeners that maintain it were just removed, and
|
|
757
|
+
// an in-flight touch keeps targeting the old element (implicit touch
|
|
758
|
+
// capture), so the new element never reports it. Carrying the state
|
|
759
|
+
// over would replay a stale deferred delta on the new element's first
|
|
760
|
+
// flush, and a cleanup that lands mid-touch or inside the post-touchend
|
|
761
|
+
// grace window would strand _iosTouching / _iosJustTouchEnded as true
|
|
762
|
+
// (the listener unsub clears the grace timer, and with it the only
|
|
763
|
+
// pending reset of the flag), deferring every adjustment on the new
|
|
764
|
+
// element until its next touch cycle.
|
|
765
|
+
this._iosDeferredAdjustment = 0
|
|
766
|
+
this._iosTouching = false
|
|
767
|
+
this._iosJustTouchEnded = false
|
|
744
768
|
this.scrollElement = null
|
|
745
769
|
this.targetWindow = null
|
|
746
770
|
}
|
|
@@ -953,6 +977,14 @@ export class Virtualizer<
|
|
|
953
977
|
const cur = this.getScrollOffset()
|
|
954
978
|
const max = this.getMaxScrollOffset()
|
|
955
979
|
if (cur < 0 || cur > max) return
|
|
980
|
+
// At the end clamp the browser already absorbed a shrink above the
|
|
981
|
+
// viewport (it clamped scrollTop onto the new bottom), so replaying our
|
|
982
|
+
// deferred negative delta would lift the view off the bottom — drop it.
|
|
983
|
+
// Positive deltas still replay: growth above doesn't clamp. (#1233)
|
|
984
|
+
if (this._iosDeferredAdjustment < 0 && cur >= max - 1) {
|
|
985
|
+
this._iosDeferredAdjustment = 0
|
|
986
|
+
return
|
|
987
|
+
}
|
|
956
988
|
const delta = this._iosDeferredAdjustment
|
|
957
989
|
this._iosDeferredAdjustment = 0
|
|
958
990
|
// Roll the deferred delta into the running accumulator so any resize
|
|
@@ -1082,47 +1114,6 @@ export class Virtualizer<
|
|
|
1082
1114
|
return this.scrollOffset
|
|
1083
1115
|
}
|
|
1084
1116
|
|
|
1085
|
-
private getFurthestMeasurement = (
|
|
1086
|
-
measurements: Array<VirtualItem>,
|
|
1087
|
-
index: number,
|
|
1088
|
-
) => {
|
|
1089
|
-
const furthestMeasurementsFound = new Map<number, true>()
|
|
1090
|
-
const furthestMeasurements = new Map<number, VirtualItem>()
|
|
1091
|
-
for (let m = index - 1; m >= 0; m--) {
|
|
1092
|
-
const measurement = measurements[m]!
|
|
1093
|
-
|
|
1094
|
-
if (furthestMeasurementsFound.has(measurement.lane)) {
|
|
1095
|
-
continue
|
|
1096
|
-
}
|
|
1097
|
-
|
|
1098
|
-
const previousFurthestMeasurement = furthestMeasurements.get(
|
|
1099
|
-
measurement.lane,
|
|
1100
|
-
)
|
|
1101
|
-
if (
|
|
1102
|
-
previousFurthestMeasurement == null ||
|
|
1103
|
-
measurement.end > previousFurthestMeasurement.end
|
|
1104
|
-
) {
|
|
1105
|
-
furthestMeasurements.set(measurement.lane, measurement)
|
|
1106
|
-
} else if (measurement.end < previousFurthestMeasurement.end) {
|
|
1107
|
-
furthestMeasurementsFound.set(measurement.lane, true)
|
|
1108
|
-
}
|
|
1109
|
-
|
|
1110
|
-
if (furthestMeasurementsFound.size === this.options.lanes) {
|
|
1111
|
-
break
|
|
1112
|
-
}
|
|
1113
|
-
}
|
|
1114
|
-
|
|
1115
|
-
return furthestMeasurements.size === this.options.lanes
|
|
1116
|
-
? Array.from(furthestMeasurements.values()).sort((a, b) => {
|
|
1117
|
-
if (a.end === b.end) {
|
|
1118
|
-
return a.index - b.index
|
|
1119
|
-
}
|
|
1120
|
-
|
|
1121
|
-
return a.end - b.end
|
|
1122
|
-
})[0]
|
|
1123
|
-
: undefined
|
|
1124
|
-
}
|
|
1125
|
-
|
|
1126
1117
|
private getMeasurementOptions = memo(
|
|
1127
1118
|
() => [
|
|
1128
1119
|
this.options.count,
|
|
@@ -1132,6 +1123,7 @@ export class Virtualizer<
|
|
|
1132
1123
|
this.options.enabled,
|
|
1133
1124
|
this.options.lanes,
|
|
1134
1125
|
this.options.laneAssignmentMode,
|
|
1126
|
+
this.options.gap,
|
|
1135
1127
|
],
|
|
1136
1128
|
(
|
|
1137
1129
|
count,
|
|
@@ -1141,6 +1133,7 @@ export class Virtualizer<
|
|
|
1141
1133
|
enabled,
|
|
1142
1134
|
lanes,
|
|
1143
1135
|
laneAssignmentMode,
|
|
1136
|
+
gap,
|
|
1144
1137
|
) => {
|
|
1145
1138
|
const lanesChanged =
|
|
1146
1139
|
this.prevLanes !== undefined && this.prevLanes !== lanes
|
|
@@ -1161,6 +1154,7 @@ export class Virtualizer<
|
|
|
1161
1154
|
enabled,
|
|
1162
1155
|
lanes,
|
|
1163
1156
|
laneAssignmentMode,
|
|
1157
|
+
gap,
|
|
1164
1158
|
}
|
|
1165
1159
|
},
|
|
1166
1160
|
{
|
|
@@ -1179,6 +1173,7 @@ export class Virtualizer<
|
|
|
1179
1173
|
enabled,
|
|
1180
1174
|
lanes,
|
|
1181
1175
|
laneAssignmentMode,
|
|
1176
|
+
gap,
|
|
1182
1177
|
},
|
|
1183
1178
|
_itemSizeCacheVersion,
|
|
1184
1179
|
) => {
|
|
@@ -1237,7 +1232,6 @@ export class Virtualizer<
|
|
|
1237
1232
|
// At n=100k this drops cold-mount cost from ~2.5ms (eager object
|
|
1238
1233
|
// allocation) to roughly the cost of a single typed-array fill.
|
|
1239
1234
|
if (lanes === 1) {
|
|
1240
|
-
const gap = this.options.gap
|
|
1241
1235
|
// Reuse flat backing if large enough; else grow (preserving data
|
|
1242
1236
|
// before `min` to mirror the slice-and-rebuild contract).
|
|
1243
1237
|
const need = count * 2
|
|
@@ -1281,12 +1275,21 @@ export class Virtualizer<
|
|
|
1281
1275
|
const laneLastIndex: Array<number | undefined> = new Array(lanes).fill(
|
|
1282
1276
|
undefined,
|
|
1283
1277
|
)
|
|
1278
|
+
// Running end position of each lane's last item, so the shortest lane
|
|
1279
|
+
// can be found with an O(lanes) argmin instead of the old backward walk
|
|
1280
|
+
// through `measurements` (getFurthestMeasurement). `filledLanes` tracks
|
|
1281
|
+
// how many lanes have at least one item, mirroring the previous
|
|
1282
|
+
// "all lanes seen → shortest lane, else i % lanes" branch.
|
|
1283
|
+
const laneEnds = new Float64Array(lanes)
|
|
1284
|
+
let filledLanes = 0
|
|
1284
1285
|
|
|
1285
1286
|
// Initialize from existing measurements (before min)
|
|
1286
1287
|
for (let m = 0; m < min; m++) {
|
|
1287
1288
|
const item = measurements[m]
|
|
1288
1289
|
if (item) {
|
|
1290
|
+
if (laneLastIndex[item.lane] === undefined) filledLanes++
|
|
1289
1291
|
laneLastIndex[item.lane] = m
|
|
1292
|
+
laneEnds[item.lane] = item.end
|
|
1290
1293
|
}
|
|
1291
1294
|
}
|
|
1292
1295
|
|
|
@@ -1308,24 +1311,37 @@ export class Virtualizer<
|
|
|
1308
1311
|
const prevInLane =
|
|
1309
1312
|
prevIndex !== undefined ? measurements[prevIndex] : undefined
|
|
1310
1313
|
start = prevInLane
|
|
1311
|
-
? prevInLane.end +
|
|
1312
|
-
: paddingStart + scrollMargin
|
|
1313
|
-
} else {
|
|
1314
|
-
// No cache - use original logic (find shortest lane)
|
|
1315
|
-
const furthestMeasurement =
|
|
1316
|
-
this.options.lanes === 1
|
|
1317
|
-
? measurements[i - 1]
|
|
1318
|
-
: this.getFurthestMeasurement(measurements, i)
|
|
1319
|
-
|
|
1320
|
-
start = furthestMeasurement
|
|
1321
|
-
? furthestMeasurement.end + this.options.gap
|
|
1314
|
+
? prevInLane.end + gap
|
|
1322
1315
|
: paddingStart + scrollMargin
|
|
1316
|
+
} else if (filledLanes === lanes) {
|
|
1317
|
+
// No cache, every lane seeded: place in the shortest lane.
|
|
1318
|
+
// Read the running per-lane ends (O(lanes) argmin) instead of the
|
|
1319
|
+
// old backward scan. Tie-break on the lane's last-item index to
|
|
1320
|
+
// preserve the previous sort-by-(end, index) placement exactly.
|
|
1321
|
+
let bestLane = 0
|
|
1322
|
+
let bestEnd = laneEnds[0]!
|
|
1323
|
+
let bestIdx = laneLastIndex[0]!
|
|
1324
|
+
for (let l = 1; l < lanes; l++) {
|
|
1325
|
+
const e = laneEnds[l]!
|
|
1326
|
+
if (e < bestEnd || (e === bestEnd && laneLastIndex[l]! < bestIdx)) {
|
|
1327
|
+
bestLane = l
|
|
1328
|
+
bestEnd = e
|
|
1329
|
+
bestIdx = laneLastIndex[l]!
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
lane = bestLane
|
|
1333
|
+
start = bestEnd + gap
|
|
1323
1334
|
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1335
|
+
if (shouldCacheLane) {
|
|
1336
|
+
this.laneAssignments.set(i, lane)
|
|
1337
|
+
}
|
|
1338
|
+
} else {
|
|
1339
|
+
// No cache and not every lane seeded yet — seed lanes in order,
|
|
1340
|
+
// matching the previous `i % lanes` fallback for the first row.
|
|
1341
|
+
lane = i % this.options.lanes
|
|
1342
|
+
start = paddingStart + scrollMargin
|
|
1327
1343
|
|
|
1328
|
-
if (
|
|
1344
|
+
if (shouldCacheLane) {
|
|
1329
1345
|
this.laneAssignments.set(i, lane)
|
|
1330
1346
|
}
|
|
1331
1347
|
}
|
|
@@ -1347,8 +1363,10 @@ export class Virtualizer<
|
|
|
1347
1363
|
lane,
|
|
1348
1364
|
}
|
|
1349
1365
|
|
|
1350
|
-
// ✅ Performance: Update lane's last item index
|
|
1366
|
+
// ✅ Performance: Update lane's last item index + running end
|
|
1367
|
+
if (laneLastIndex[lane] === undefined) filledLanes++
|
|
1351
1368
|
laneLastIndex[lane] = i
|
|
1369
|
+
laneEnds[lane] = end
|
|
1352
1370
|
}
|
|
1353
1371
|
|
|
1354
1372
|
this.measurementsCache = measurements
|
|
@@ -1720,6 +1738,14 @@ export class Virtualizer<
|
|
|
1720
1738
|
toOffset: number,
|
|
1721
1739
|
{ align = 'start', behavior = 'auto' }: ScrollToOffsetOptions = {},
|
|
1722
1740
|
) => {
|
|
1741
|
+
// An absolute scroll command derives its target from current
|
|
1742
|
+
// measurements, so any iOS-deferred compensation still pending is stale by
|
|
1743
|
+
// definition — the command already accounts for the measurements the delta
|
|
1744
|
+
// was compensating for. Drop it so _flushIosDeferredIfReady doesn't replay
|
|
1745
|
+
// it onto the just-established position (relative commands like scrollBy
|
|
1746
|
+
// intentionally keep the deferral, since they build on the current offset).
|
|
1747
|
+
this._iosDeferredAdjustment = 0
|
|
1748
|
+
|
|
1723
1749
|
const offset = this.getOffsetForAlignment(toOffset, align)
|
|
1724
1750
|
|
|
1725
1751
|
const now = this.now()
|
|
@@ -1744,6 +1770,10 @@ export class Virtualizer<
|
|
|
1744
1770
|
behavior = 'auto',
|
|
1745
1771
|
}: ScrollToIndexOptions = {},
|
|
1746
1772
|
) => {
|
|
1773
|
+
// See scrollToOffset: an absolute target invalidates any pending
|
|
1774
|
+
// iOS-deferred compensation.
|
|
1775
|
+
this._iosDeferredAdjustment = 0
|
|
1776
|
+
|
|
1747
1777
|
index = Math.max(0, Math.min(index, this.options.count - 1))
|
|
1748
1778
|
|
|
1749
1779
|
const offsetInfo = this.getOffsetForIndex(index, initialAlign)
|