@tanstack/virtual-core 3.19.2 → 3.19.3

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.
@@ -209,12 +209,9 @@ class Virtualizer {
209
209
  this.scrollRect = null;
210
210
  this.scrollOffset = null;
211
211
  this.scrollDirection = null;
212
- this.scrollAdjustments = 0;
213
212
  this._iosDeferredAdjustment = 0;
214
- this._iosTouching = false;
215
- this._iosJustTouchEnded = false;
216
- this._iosTouchEndTimerId = null;
217
- this._intendedScrollOffset = null;
213
+ this._iosTouch = null;
214
+ this.pendingWrite = null;
218
215
  this._clampedAdjustment = null;
219
216
  this.elementsCache = /* @__PURE__ */ new Map();
220
217
  this._resizeBatch = null;
@@ -358,8 +355,7 @@ class Virtualizer {
358
355
  this.pendingMin = 0;
359
356
  this.itemSizeCacheVersion++;
360
357
  }
361
- let anchorResolved = false;
362
- let anchorDelta = 0;
358
+ let anchorDelta = null;
363
359
  if (anchor && this.scrollOffset !== null) {
364
360
  const [anchorKey, anchorOffset] = anchor;
365
361
  const newMeasurements = this.getMeasurements();
@@ -375,18 +371,14 @@ class Virtualizer {
375
371
  if (!followOnAppend && newOffset !== this.scrollOffset) {
376
372
  anchorDelta = newOffset - this.scrollOffset;
377
373
  this.scrollOffset = newOffset;
378
- anchorResolved = true;
379
374
  }
380
375
  }
381
376
  }
382
377
  }
383
- if (anchorResolved || followOnAppend) {
384
- this.pendingScrollAnchor = [
385
- anchorResolved ? anchor[0] : null,
386
- anchorResolved ? anchor[1] : 0,
387
- followOnAppend,
388
- anchorDelta
389
- ];
378
+ if (followOnAppend) {
379
+ this.pendingScrollAnchor = { kind: "follow", behavior: followOnAppend };
380
+ } else if (anchorDelta !== null) {
381
+ this.pendingScrollAnchor = { kind: "anchor", delta: anchorDelta };
390
382
  }
391
383
  };
392
384
  this.notify = (sync) => {
@@ -427,9 +419,9 @@ class Virtualizer {
427
419
  this.isScrolling = false;
428
420
  this.scrollDirection = null;
429
421
  this._iosDeferredAdjustment = 0;
430
- this._iosTouching = false;
431
- this._iosJustTouchEnded = false;
422
+ this._endIosTouch();
432
423
  this._clampedAdjustment = null;
424
+ this.pendingWrite = null;
433
425
  this.scrollElement = null;
434
426
  this.targetWindow = null;
435
427
  };
@@ -465,11 +457,12 @@ class Virtualizer {
465
457
  this.unsubs.push(
466
458
  this.options.observeElementOffset(this, (offset, isScrolling) => {
467
459
  var _a2;
468
- if (isScrolling && this._intendedScrollOffset === null && offset === this.scrollOffset) {
460
+ if (isScrolling && this.pendingWrite === null && offset === this.scrollOffset) {
469
461
  return;
470
462
  }
471
- if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
472
- offset = this._intendedScrollOffset;
463
+ const pending = this.pendingWrite;
464
+ if (pending !== null && Math.abs(offset - pending.target) < 1.5) {
465
+ offset = pending.target;
473
466
  } else if (((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null && // A non-smooth write lands at once, so any other scroll event is
474
467
  // the user's, even before reconcile saw the target reached (the
475
468
  // target may keep moving, or iOS ignored the write under a
@@ -486,12 +479,10 @@ class Virtualizer {
486
479
  !isAnimating(this.scrollState) && !utils.approxEqual(offset, this.scrollState.lastTargetOffset) && !utils.approxEqual(offset, this.getScrollStateTarget(this.scrollState)) && (this._clampedAdjustment === null || Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)) {
487
480
  this.scrollState = null;
488
481
  }
489
- this._intendedScrollOffset = null;
490
- if (this.scrollState) this.scrollState.writtenOffset = null;
482
+ this.pendingWrite = null;
491
483
  if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
492
484
  this._clampedAdjustment = null;
493
485
  }
494
- this.scrollAdjustments = 0;
495
486
  const prevOffset = this.getScrollOffset();
496
487
  this.scrollDirection = isScrolling ? prevOffset === offset ? this.scrollDirection : prevOffset < offset ? "forward" : "backward" : null;
497
488
  this.scrollOffset = offset;
@@ -506,24 +497,22 @@ class Virtualizer {
506
497
  if ("addEventListener" in this.scrollElement) {
507
498
  const scrollEl = this.scrollElement;
508
499
  const onTouchStart = () => {
509
- this._iosTouching = true;
510
- this._iosJustTouchEnded = false;
511
- if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
512
- this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
513
- this._iosTouchEndTimerId = null;
514
- }
500
+ this._endIosTouch();
501
+ this._iosTouch = { phase: "down" };
515
502
  };
516
503
  const onTouchEnd = () => {
517
- this._iosTouching = false;
504
+ this._endIosTouch();
518
505
  if (!isIOSWebKit() || this.targetWindow == null) {
519
506
  return;
520
507
  }
521
- this._iosJustTouchEnded = true;
522
- this._iosTouchEndTimerId = this.targetWindow.setTimeout(() => {
523
- this._iosJustTouchEnded = false;
524
- this._iosTouchEndTimerId = null;
525
- this._flushIosDeferredIfReady();
526
- }, 150);
508
+ const tail = {
509
+ phase: "tail",
510
+ timerId: this.targetWindow.setTimeout(() => {
511
+ if (this._iosTouch === tail) this._iosTouch = null;
512
+ this._flushIosDeferredIfReady();
513
+ }, 150)
514
+ };
515
+ this._iosTouch = tail;
527
516
  };
528
517
  scrollEl.addEventListener(
529
518
  "touchstart",
@@ -544,10 +533,7 @@ class Virtualizer {
544
533
  scrollEl.removeEventListener("touchstart", onTouchStart);
545
534
  scrollEl.removeEventListener("touchend", onTouchEnd);
546
535
  scrollEl.removeEventListener("touchcancel", onTouchEnd);
547
- if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
548
- this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
549
- this._iosTouchEndTimerId = null;
550
- }
536
+ this._endIosTouch();
551
537
  });
552
538
  }
553
539
  this._scrollToOffset(this.getScrollOffset(), {
@@ -558,14 +544,11 @@ class Virtualizer {
558
544
  const anchor = this.pendingScrollAnchor;
559
545
  this.pendingScrollAnchor = null;
560
546
  if (anchor && this.scrollElement && this.options.enabled) {
561
- const [key, _offset, followOnAppend, anchorDelta] = anchor;
562
- if (key !== null && !followOnAppend) {
563
- if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
564
- if (anchorDelta !== 0) {
565
- this._iosDeferredAdjustment += anchorDelta;
566
- }
547
+ if (anchor.kind === "anchor") {
548
+ if (isIOSWebKit() && this._iosInMotion()) {
549
+ this._iosDeferredAdjustment += anchor.delta;
567
550
  } else if (isSmoothScroll(this.scrollState) && !utils.approxEqual(
568
- this.getScrollOffset() - anchorDelta,
551
+ this.getScrollOffset() - anchor.delta,
569
552
  this.scrollState.lastTargetOffset
570
553
  )) ;
571
554
  else {
@@ -574,9 +557,8 @@ class Virtualizer {
574
557
  behavior: void 0
575
558
  });
576
559
  }
577
- }
578
- if (followOnAppend) {
579
- this.scrollToEnd({ behavior: followOnAppend });
560
+ } else {
561
+ this.scrollToEnd({ behavior: anchor.behavior });
580
562
  }
581
563
  }
582
564
  this._retryClampedAdjustment();
@@ -595,11 +577,17 @@ class Virtualizer {
595
577
  });
596
578
  }
597
579
  };
580
+ this._iosInMotion = () => this.isScrolling || this._iosTouch !== null;
581
+ this._endIosTouch = () => {
582
+ var _a;
583
+ if (((_a = this._iosTouch) == null ? void 0 : _a.phase) === "tail" && this.targetWindow != null) {
584
+ this.targetWindow.clearTimeout(this._iosTouch.timerId);
585
+ }
586
+ this._iosTouch = null;
587
+ };
598
588
  this._flushIosDeferredIfReady = () => {
599
589
  if (this._iosDeferredAdjustment === 0) return;
600
- if (this.isScrolling) return;
601
- if (this._iosTouching) return;
602
- if (this._iosJustTouchEnded) return;
590
+ if (this._iosInMotion()) return;
603
591
  const cur = this.getScrollOffset();
604
592
  const max = this.getMaxScrollOffset();
605
593
  if (cur < 0 || cur > max) return;
@@ -610,7 +598,7 @@ class Virtualizer {
610
598
  const delta = this._iosDeferredAdjustment;
611
599
  this._iosDeferredAdjustment = 0;
612
600
  this._scrollToOffset(cur, {
613
- adjustments: this.scrollAdjustments += delta,
601
+ adjustments: this.scrollAdjustments + delta,
614
602
  behavior: void 0
615
603
  });
616
604
  };
@@ -933,7 +921,7 @@ class Virtualizer {
933
921
  }
934
922
  };
935
923
  this.resizeItem = (index, size) => {
936
- var _a, _b, _c, _d;
924
+ var _a, _b, _c;
937
925
  if (!this.isIndexInRange(index)) return;
938
926
  if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
939
927
  let cachedSize;
@@ -992,8 +980,9 @@ class Virtualizer {
992
980
  }
993
981
  this.itemSizeCache.set(key, size);
994
982
  this.itemSizeCacheVersion++;
995
- const awaitingCommandReadBack = ((_c = this.scrollState) == null ? void 0 : _c.writtenOffset) != null && !utils.approxEqual(
996
- this.scrollState.writtenOffset,
983
+ const writtenOffset = this.getCommandWrittenOffset();
984
+ const awaitingCommandReadBack = writtenOffset !== null && !utils.approxEqual(
985
+ writtenOffset,
997
986
  this.getScrollOffset() + this.scrollAdjustments
998
987
  );
999
988
  let adjustedSync = false;
@@ -1008,7 +997,7 @@ class Virtualizer {
1008
997
  }
1009
998
  if (this._resizeBatch) {
1010
999
  this._resizeBatch.notify = true;
1011
- (_d = this._resizeBatch).sync || (_d.sync = adjustedSync);
1000
+ (_c = this._resizeBatch).sync || (_c.sync = adjustedSync);
1012
1001
  return;
1013
1002
  }
1014
1003
  this.notify(adjustedSync);
@@ -1110,82 +1099,46 @@ class Virtualizer {
1110
1099
  ];
1111
1100
  };
1112
1101
  this.scrollToOffset = (toOffset, { align = "start", behavior = "auto" } = {}) => {
1113
- this._iosDeferredAdjustment = 0;
1114
- const offset = this.getOffsetForAlignment(toOffset, align);
1115
- const now = this.now();
1116
- this.scrollState = {
1117
- index: null,
1118
- align,
1119
- startedAt: now,
1120
- lastTargetOffset: offset,
1121
- stableFrames: 0,
1122
- phase: initialScrollPhase(behavior, false)
1123
- };
1124
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1125
- this.scheduleScrollReconcile();
1102
+ this._startScroll(
1103
+ { index: null, align },
1104
+ this.getOffsetForAlignment(toOffset, align),
1105
+ behavior,
1106
+ "absolute"
1107
+ );
1126
1108
  };
1127
1109
  this.scrollToIndex = (index, {
1128
1110
  align: initialAlign = "auto",
1129
1111
  behavior = "auto"
1130
1112
  } = {}) => {
1131
- this._iosDeferredAdjustment = 0;
1132
1113
  index = Math.max(0, Math.min(index, this.options.count - 1));
1133
1114
  const offsetInfo = this.getOffsetForIndex(index, initialAlign);
1134
1115
  if (!offsetInfo) {
1116
+ this._iosDeferredAdjustment = 0;
1135
1117
  return;
1136
1118
  }
1137
1119
  const [offset, align] = offsetInfo;
1138
- const now = this.now();
1139
- this.scrollState = {
1140
- index,
1141
- align,
1142
- startedAt: now,
1143
- lastTargetOffset: offset,
1144
- stableFrames: 0,
1145
- phase: initialScrollPhase(
1146
- behavior,
1147
- utils.approxEqual(offset, this.getScrollOffset())
1148
- )
1149
- };
1150
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1151
- this.scheduleScrollReconcile();
1120
+ this._startScroll({ index, align }, offset, behavior, "absolute");
1152
1121
  };
1153
1122
  this.scrollBy = (delta, { behavior = "auto" } = {}) => {
1154
- var _a;
1155
1123
  const offset = this.getOffsetForAlignment(
1156
- (((_a = this.scrollState) == null ? void 0 : _a.writtenOffset) ?? this.getScrollOffset()) + delta,
1124
+ (this.getCommandWrittenOffset() ?? this.getScrollOffset()) + delta,
1157
1125
  "start"
1158
1126
  );
1159
- const now = this.now();
1160
- this.scrollState = {
1161
- index: null,
1162
- align: "start",
1163
- startedAt: now,
1164
- lastTargetOffset: offset,
1165
- stableFrames: 0,
1166
- phase: initialScrollPhase(behavior, false)
1167
- };
1168
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1169
- this.scheduleScrollReconcile();
1127
+ this._startScroll(
1128
+ { index: null, align: "start" },
1129
+ offset,
1130
+ behavior,
1131
+ "relative"
1132
+ );
1170
1133
  };
1171
1134
  this.scrollToEnd = ({ behavior = "auto" } = {}) => {
1172
1135
  if (this.options.count > 0) {
1173
- this._iosDeferredAdjustment = 0;
1174
- const offset = Math.max(this.getMaxScrollOffset(), 0);
1175
- this.scrollState = {
1176
- index: this.options.count - 1,
1177
- align: "end",
1178
- toEnd: true,
1179
- startedAt: this.now(),
1180
- lastTargetOffset: offset,
1181
- stableFrames: 0,
1182
- phase: initialScrollPhase(
1183
- behavior,
1184
- utils.approxEqual(offset, this.getScrollOffset())
1185
- )
1186
- };
1187
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1188
- this.scheduleScrollReconcile();
1136
+ this._startScroll(
1137
+ { index: this.options.count - 1, align: "end", toEnd: true },
1138
+ Math.max(this.getMaxScrollOffset(), 0),
1139
+ behavior,
1140
+ "absolute"
1141
+ );
1189
1142
  return;
1190
1143
  }
1191
1144
  this.scrollToOffset(Math.max(this.getTotalSize() - this.getSize(), 0), {
@@ -1252,12 +1205,14 @@ class Virtualizer {
1252
1205
  adjustments,
1253
1206
  behavior
1254
1207
  }) => {
1255
- this._intendedScrollOffset = offset + (adjustments ?? 0);
1256
- if (this.scrollState) {
1257
- this.scrollState.writtenOffset = behavior === "smooth" ? null : this._intendedScrollOffset;
1258
- }
1208
+ this.pendingWrite = {
1209
+ target: offset + (adjustments ?? 0),
1210
+ smooth: behavior === "smooth",
1211
+ adjustments: adjustments ?? this.scrollAdjustments
1212
+ };
1259
1213
  this.options.scrollToFn(offset, { behavior, adjustments }, this);
1260
1214
  };
1215
+ this.getCommandWrittenOffset = () => this.scrollState !== null && this.pendingWrite !== null && !this.pendingWrite.smooth ? this.pendingWrite.target : null;
1261
1216
  this.measure = () => {
1262
1217
  this.pendingMin = null;
1263
1218
  this.itemSizeCache.clear();
@@ -1267,6 +1222,13 @@ class Virtualizer {
1267
1222
  };
1268
1223
  this.setOptions(opts);
1269
1224
  }
1225
+ // Size compensation written to the element but not yet reflected in
1226
+ // `scrollOffset`: the element is expected at `scrollOffset +
1227
+ // scrollAdjustments` until the next scroll event reports it.
1228
+ get scrollAdjustments() {
1229
+ var _a;
1230
+ return ((_a = this.pendingWrite) == null ? void 0 : _a.adjustments) ?? 0;
1231
+ }
1270
1232
  // Returns `true` when it performed a synchronous `scrollTop` write this
1271
1233
  // tick, `false` when the delta was zero or the write was deferred (iOS).
1272
1234
  // `resizeItem` uses that to decide whether the follow-up `notify` must be
@@ -1277,7 +1239,7 @@ class Virtualizer {
1277
1239
  if (process.env.NODE_ENV !== "production" && this.options.debug) {
1278
1240
  console.info("correction", delta);
1279
1241
  }
1280
- if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
1242
+ if (isIOSWebKit() && this._iosInMotion()) {
1281
1243
  if (((_a = this.scrollState) == null ? void 0 : _a.index) == null) {
1282
1244
  this._iosDeferredAdjustment += delta;
1283
1245
  }
@@ -1288,13 +1250,13 @@ class Virtualizer {
1288
1250
  const maxAtWrite = el !== null && ("scrollHeight" in el || "document" in el) ? this.getMaxScrollOffset() : null;
1289
1251
  this._clampedAdjustment = maxAtWrite !== null && target > maxAtWrite + 0.5 ? { target, maxAtWrite } : null;
1290
1252
  this._scrollToOffset(this.getScrollOffset(), {
1291
- adjustments: this.scrollAdjustments += delta,
1253
+ adjustments: this.scrollAdjustments + delta,
1292
1254
  behavior
1293
1255
  });
1294
1256
  if (this.scrollOffset !== null) {
1295
1257
  this.scrollOffset += this.scrollAdjustments;
1296
1258
  if (this.scrollOffset < 0) this.scrollOffset = 0;
1297
- this.scrollAdjustments = 0;
1259
+ if (this.pendingWrite) this.pendingWrite.adjustments = 0;
1298
1260
  }
1299
1261
  return true;
1300
1262
  }
@@ -1366,6 +1328,36 @@ class Virtualizer {
1366
1328
  }
1367
1329
  this.scheduleScrollReconcile();
1368
1330
  }
1331
+ // Every scroll command starts here: it becomes the in-flight command,
1332
+ // writes its first target and leaves reconcile to retarget and retire it.
1333
+ //
1334
+ // An absolute command derives its target from current measurements, so
1335
+ // any iOS-deferred compensation still pending is stale by definition —
1336
+ // the command already accounts for the measurements the delta was
1337
+ // compensating for. Drop it so _flushIosDeferredIfReady doesn't replay it
1338
+ // onto the just-established position. A relative command (scrollBy)
1339
+ // builds on the current offset, so it keeps the deferral.
1340
+ //
1341
+ // An index or end target that is already at its offset starts a smooth
1342
+ // scroll as landed; an offset target never does, so its first scroll
1343
+ // event is read as the user's (see the scroll handler in `_willUpdate`).
1344
+ _startScroll(target, offset, behavior, kind) {
1345
+ if (kind === "absolute") {
1346
+ this._iosDeferredAdjustment = 0;
1347
+ }
1348
+ this.scrollState = {
1349
+ ...target,
1350
+ startedAt: this.now(),
1351
+ lastTargetOffset: offset,
1352
+ stableFrames: 0,
1353
+ phase: initialScrollPhase(
1354
+ behavior,
1355
+ target.index != null && utils.approxEqual(offset, this.getScrollOffset())
1356
+ )
1357
+ };
1358
+ this._scrollToOffset(offset, { adjustments: void 0, behavior });
1359
+ this.scheduleScrollReconcile();
1360
+ }
1369
1361
  }
1370
1362
  const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
1371
1363
  while (low <= high) {