@tanstack/virtual-core 3.19.10 → 3.19.12

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.
@@ -158,9 +158,12 @@ const scrollWithAdjustments = (offset, {
158
158
  adjustments = 0,
159
159
  behavior
160
160
  }, instance) => {
161
- var _a, _b;
162
- (_b = (_a = instance.scrollElement) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, {
163
- [instance.options.horizontal ? "left" : "top"]: offset + adjustments,
161
+ var _a;
162
+ const { scrollElement, options } = instance;
163
+ const adjustedOffset = offset + adjustments;
164
+ const isRtlElement = options.horizontal && options.isRtl && scrollElement !== null && "ownerDocument" in scrollElement;
165
+ (_a = scrollElement == null ? void 0 : scrollElement.scrollTo) == null ? void 0 : _a.call(scrollElement, {
166
+ [options.horizontal ? "left" : "top"]: isRtlElement ? -adjustedOffset : adjustedOffset,
164
167
  behavior
165
168
  });
166
169
  };
@@ -211,6 +214,8 @@ class Virtualizer {
211
214
  this.scrollDirection = null;
212
215
  this._holdOffset = 0;
213
216
  this._reportedOverscroll = false;
217
+ this._trailing = 0;
218
+ this._drawnListEnd = Infinity;
214
219
  this._iosGesture = new iosGesture.IosGesture({
215
220
  getWindow: () => this.targetWindow,
216
221
  now: () => this.now(),
@@ -245,6 +250,7 @@ class Virtualizer {
245
250
  });
246
251
  this._lastElementOffset = null;
247
252
  this.pendingWrite = null;
253
+ this._ownWriteAt = null;
248
254
  this._clampedAdjustment = null;
249
255
  this.elementsCache = /* @__PURE__ */ new Map();
250
256
  this._resizeBatch = null;
@@ -453,7 +459,7 @@ class Virtualizer {
453
459
  this.pendingScrollAnchor = { kind: "anchor", delta: anchorDelta };
454
460
  } else {
455
461
  this._holdOffset += anchorDelta;
456
- this._followOffsetTarget(anchorDelta);
462
+ this._followAnchor(anchorDelta);
457
463
  }
458
464
  }
459
465
  };
@@ -499,8 +505,10 @@ class Virtualizer {
499
505
  this.scrollDirection = null;
500
506
  this._holdOffset = 0;
501
507
  this._reportedOverscroll = false;
508
+ this._trailing = 0;
502
509
  this._clampedAdjustment = null;
503
510
  this.pendingWrite = null;
511
+ this._ownWriteAt = null;
504
512
  this.scrollElement = null;
505
513
  this.targetWindow = null;
506
514
  };
@@ -537,6 +545,7 @@ class Virtualizer {
537
545
  this.options.observeElementOffset(this, (offset, isScrolling) => {
538
546
  var _a2, _b, _c, _d;
539
547
  const elementMax = isScrolling ? this._elementMaxScrollOffset() : null;
548
+ this._readTrailing(elementMax);
540
549
  const wasOverscrolled = this._reportedOverscroll;
541
550
  this._reportedOverscroll = offset < 0 || elementMax !== null && offset > elementMax + 1;
542
551
  const elementOffset = offset;
@@ -562,6 +571,11 @@ class Virtualizer {
562
571
  this.scrollState = null;
563
572
  }
564
573
  this.pendingWrite = null;
574
+ if (kind === "readBack" || kind === "clampedReadBack" || kind === "clampedEcho") {
575
+ this._ownWriteAt = offset;
576
+ } else if (kind !== "restEcho") {
577
+ this._ownWriteAt = null;
578
+ }
565
579
  if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
566
580
  this._clampedAdjustment = null;
567
581
  }
@@ -612,6 +626,8 @@ class Virtualizer {
612
626
  );
613
627
  }
614
628
  this._scrollToOffset(this.getScrollOffset());
629
+ this._readTrailing(this._elementMaxScrollOffset());
630
+ this.maybeNotify();
615
631
  }
616
632
  this._noteClampMax();
617
633
  const anchor = this.pendingScrollAnchor;
@@ -620,14 +636,11 @@ class Virtualizer {
620
636
  if (anchor.kind === "anchor") {
621
637
  if (!this._canWriteScroll()) {
622
638
  this._holdOffset += anchor.delta;
623
- this._followOffsetTarget(anchor.delta);
639
+ this._followAnchor(anchor.delta);
624
640
  this.notify(false);
625
- } else if (isSmoothScroll(this.scrollState) && !utils.approxEqual(
626
- this.getScrollOffset() - anchor.delta,
627
- this.scrollState.lastTargetOffset
628
- )) ;
641
+ } else if (isSmoothScroll(this.scrollState) && !this._landedBeforeAnchor(this.scrollState, anchor.delta)) ;
629
642
  else {
630
- this._followOffsetTarget(anchor.delta);
643
+ this._followAnchor(anchor.delta);
631
644
  this._scrollToOffset(this.getScrollOffset());
632
645
  }
633
646
  } else {
@@ -646,7 +659,8 @@ class Virtualizer {
646
659
  holdOffset: this._holdOffset,
647
660
  scrollOffset: this.scrollOffset,
648
661
  pendingWrite: this.pendingWrite,
649
- clampedMaxAtWrite: ((_a = this._clampedAdjustment) == null ? void 0 : _a.maxAtWrite) ?? null
662
+ clampedMaxAtWrite: ((_a = this._clampedAdjustment) == null ? void 0 : _a.maxAtWrite) ?? null,
663
+ ownWriteAt: this._ownWriteAt
650
664
  });
651
665
  };
652
666
  this._retryClampedAdjustment = () => {
@@ -912,14 +926,15 @@ class Virtualizer {
912
926
  this.options.lanes,
913
927
  // scrollOffset is measured in the scroll element and the layout total
914
928
  // from the list start, so the list ends scrollMargin further down.
915
- this._getLayoutTotalSize() + this.options.scrollMargin
929
+ this._getLayoutTotalSize() + this.options.scrollMargin,
930
+ this._trailing
916
931
  ],
917
- (measurements, outerSize, scrollOffset, lanes, listEnd) => {
932
+ (measurements, outerSize, scrollOffset, lanes, listEnd, trailing) => {
918
933
  if (measurements.length === 0 || outerSize === 0) {
919
934
  this.range = null;
920
935
  return null;
921
936
  }
922
- const maxScrollOffset = Math.max(listEnd - outerSize, 0);
937
+ const maxScrollOffset = Math.max(listEnd + trailing - outerSize, 0);
923
938
  this.range = calculateRangeImpl(
924
939
  measurements,
925
940
  outerSize,
@@ -1104,6 +1119,9 @@ class Virtualizer {
1104
1119
  this._noteClampMax();
1105
1120
  this._flushHoldIfReady(true);
1106
1121
  this._retryClampedAdjustment();
1122
+ } else if (this.options.lanes > 1 && !this.options.useCachedMeasurements && !this.laneAssignments.has(index)) {
1123
+ const item = this.getMeasurements()[index];
1124
+ if (item) this.laneAssignments.set(index, item.lane);
1107
1125
  }
1108
1126
  };
1109
1127
  this.getVirtualItems = utils.memo(
@@ -1163,6 +1181,11 @@ class Virtualizer {
1163
1181
  }
1164
1182
  return offset;
1165
1183
  };
1184
+ this._readTrailing = (elementMax) => {
1185
+ if (elementMax === null) return;
1186
+ const trailing = elementMax + this.getSize() - this._drawnListEnd;
1187
+ this._trailing = trailing > 0 ? trailing : 0;
1188
+ };
1166
1189
  this._elementMaxScrollOffset = () => {
1167
1190
  const el = this.scrollElement;
1168
1191
  if (el === null) return null;
@@ -1255,7 +1278,7 @@ class Virtualizer {
1255
1278
  index = Math.max(0, Math.min(index, this.options.count - 1));
1256
1279
  const size = this.getSize();
1257
1280
  const scrollOffset = this.getScrollOffset();
1258
- const item = this.measurementsCache[index];
1281
+ const item = this.getMeasurements()[index];
1259
1282
  if (!item) return;
1260
1283
  if (align === "auto") {
1261
1284
  if (item.end >= scrollOffset + size - this.options.scrollPaddingEnd) {
@@ -1328,13 +1351,17 @@ class Virtualizer {
1328
1351
  }
1329
1352
  this.scrollState = null;
1330
1353
  };
1331
- this.getTotalSize = () => Math.max(this._getLayoutTotalSize() - this._holdOffset, 0);
1354
+ this.getTotalSize = () => {
1355
+ const total = Math.max(this._getLayoutTotalSize() - this._holdOffset, 0);
1356
+ this._drawnListEnd = total + this.options.scrollMargin;
1357
+ return total;
1358
+ };
1332
1359
  this._getLayoutTotalSize = () => {
1333
1360
  var _a, _b;
1334
1361
  const measurements = this.getMeasurements();
1335
1362
  let end;
1336
1363
  if (measurements.length === 0) {
1337
- end = this.options.paddingStart;
1364
+ end = this.options.scrollMargin + this.options.paddingStart;
1338
1365
  } else if (this.options.lanes === 1) {
1339
1366
  const lastIdx = measurements.length - 1;
1340
1367
  const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
@@ -1404,6 +1431,14 @@ class Virtualizer {
1404
1431
  this.scrollState.lastTargetOffset += delta;
1405
1432
  }
1406
1433
  };
1434
+ this._landedBeforeAnchor = (state, delta) => state.phase !== "queued" && (state.stableFrames > 0 || utils.approxEqual(this.getScrollOffset() - delta, state.lastTargetOffset));
1435
+ this._followAnchor = (delta) => {
1436
+ this._followOffsetTarget(delta);
1437
+ const state = this.scrollState;
1438
+ if ((state == null ? void 0 : state.index) != null && !state.toEnd && this._landedBeforeAnchor(state, delta)) {
1439
+ this.scrollState = null;
1440
+ }
1441
+ };
1407
1442
  this.measure = () => {
1408
1443
  this.pendingMin = null;
1409
1444
  this.itemSizeCache.clear();