@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.
@@ -325,7 +325,7 @@ class Virtualizer {
325
325
  if (idx < count) {
326
326
  const anchorItem = newMeasurements[idx];
327
327
  if (anchorItem) {
328
- const newOffset = anchorItem.start + anchorOffset;
328
+ const newOffset = Math.max(0, anchorItem.start + anchorOffset);
329
329
  if (newOffset !== this.scrollOffset) {
330
330
  anchorDelta = newOffset - this.scrollOffset;
331
331
  this.scrollOffset = newOffset;
@@ -378,6 +378,9 @@ class Virtualizer {
378
378
  this.rafId = null;
379
379
  }
380
380
  this.scrollState = null;
381
+ this._iosDeferredAdjustment = 0;
382
+ this._iosTouching = false;
383
+ this._iosJustTouchEnded = false;
381
384
  this.scrollElement = null;
382
385
  this.targetWindow = null;
383
386
  };
@@ -506,6 +509,10 @@ class Virtualizer {
506
509
  const cur = this.getScrollOffset();
507
510
  const max = this.getMaxScrollOffset();
508
511
  if (cur < 0 || cur > max) return;
512
+ if (this._iosDeferredAdjustment < 0 && cur >= max - 1) {
513
+ this._iosDeferredAdjustment = 0;
514
+ return;
515
+ }
509
516
  const delta = this._iosDeferredAdjustment;
510
517
  this._iosDeferredAdjustment = 0;
511
518
  this._scrollToOffset(cur, {
@@ -530,33 +537,6 @@ class Virtualizer {
530
537
  this.scrollOffset = this.scrollOffset ?? (typeof this.options.initialOffset === "function" ? this.options.initialOffset() : this.options.initialOffset);
531
538
  return this.scrollOffset;
532
539
  };
533
- this.getFurthestMeasurement = (measurements, index) => {
534
- const furthestMeasurementsFound = /* @__PURE__ */ new Map();
535
- const furthestMeasurements = /* @__PURE__ */ new Map();
536
- for (let m = index - 1; m >= 0; m--) {
537
- const measurement = measurements[m];
538
- if (furthestMeasurementsFound.has(measurement.lane)) {
539
- continue;
540
- }
541
- const previousFurthestMeasurement = furthestMeasurements.get(
542
- measurement.lane
543
- );
544
- if (previousFurthestMeasurement == null || measurement.end > previousFurthestMeasurement.end) {
545
- furthestMeasurements.set(measurement.lane, measurement);
546
- } else if (measurement.end < previousFurthestMeasurement.end) {
547
- furthestMeasurementsFound.set(measurement.lane, true);
548
- }
549
- if (furthestMeasurementsFound.size === this.options.lanes) {
550
- break;
551
- }
552
- }
553
- return furthestMeasurements.size === this.options.lanes ? Array.from(furthestMeasurements.values()).sort((a, b) => {
554
- if (a.end === b.end) {
555
- return a.index - b.index;
556
- }
557
- return a.end - b.end;
558
- })[0] : void 0;
559
- };
560
540
  this.getMeasurementOptions = utils.memo(
561
541
  () => [
562
542
  this.options.count,
@@ -565,9 +545,10 @@ class Virtualizer {
565
545
  this.options.getItemKey,
566
546
  this.options.enabled,
567
547
  this.options.lanes,
568
- this.options.laneAssignmentMode
548
+ this.options.laneAssignmentMode,
549
+ this.options.gap
569
550
  ],
570
- (count, paddingStart, scrollMargin, getItemKey, enabled, lanes, laneAssignmentMode) => {
551
+ (count, paddingStart, scrollMargin, getItemKey, enabled, lanes, laneAssignmentMode, gap) => {
571
552
  const lanesChanged = this.prevLanes !== void 0 && this.prevLanes !== lanes;
572
553
  if (lanesChanged) {
573
554
  this.lanesChangedFlag = true;
@@ -581,7 +562,8 @@ class Virtualizer {
581
562
  getItemKey,
582
563
  enabled,
583
564
  lanes,
584
- laneAssignmentMode
565
+ laneAssignmentMode,
566
+ gap
585
567
  };
586
568
  },
587
569
  {
@@ -597,7 +579,8 @@ class Virtualizer {
597
579
  getItemKey,
598
580
  enabled,
599
581
  lanes,
600
- laneAssignmentMode
582
+ laneAssignmentMode,
583
+ gap
601
584
  }, _itemSizeCacheVersion) => {
602
585
  const itemSizeCache = this.itemSizeCache;
603
586
  if (!enabled) {
@@ -633,7 +616,6 @@ class Virtualizer {
633
616
  this.lanesSettling = false;
634
617
  }
635
618
  if (lanes === 1) {
636
- const gap = this.options.gap;
637
619
  const need = count * 2;
638
620
  let flat = this._flatMeasurements;
639
621
  if (!flat || flat.length < need) {
@@ -665,10 +647,14 @@ class Virtualizer {
665
647
  const laneLastIndex = new Array(lanes).fill(
666
648
  void 0
667
649
  );
650
+ const laneEnds = new Float64Array(lanes);
651
+ let filledLanes = 0;
668
652
  for (let m = 0; m < min; m++) {
669
653
  const item = measurements[m];
670
654
  if (item) {
655
+ if (laneLastIndex[item.lane] === void 0) filledLanes++;
671
656
  laneLastIndex[item.lane] = m;
657
+ laneEnds[item.lane] = item.end;
672
658
  }
673
659
  }
674
660
  for (let i = min; i < count; i++) {
@@ -681,12 +667,28 @@ class Virtualizer {
681
667
  lane = cachedLane;
682
668
  const prevIndex = laneLastIndex[lane];
683
669
  const prevInLane = prevIndex !== void 0 ? measurements[prevIndex] : void 0;
684
- start = prevInLane ? prevInLane.end + this.options.gap : paddingStart + scrollMargin;
670
+ start = prevInLane ? prevInLane.end + gap : paddingStart + scrollMargin;
671
+ } else if (filledLanes === lanes) {
672
+ let bestLane = 0;
673
+ let bestEnd = laneEnds[0];
674
+ let bestIdx = laneLastIndex[0];
675
+ for (let l = 1; l < lanes; l++) {
676
+ const e = laneEnds[l];
677
+ if (e < bestEnd || e === bestEnd && laneLastIndex[l] < bestIdx) {
678
+ bestLane = l;
679
+ bestEnd = e;
680
+ bestIdx = laneLastIndex[l];
681
+ }
682
+ }
683
+ lane = bestLane;
684
+ start = bestEnd + gap;
685
+ if (shouldCacheLane) {
686
+ this.laneAssignments.set(i, lane);
687
+ }
685
688
  } else {
686
- const furthestMeasurement = this.options.lanes === 1 ? measurements[i - 1] : this.getFurthestMeasurement(measurements, i);
687
- start = furthestMeasurement ? furthestMeasurement.end + this.options.gap : paddingStart + scrollMargin;
688
- lane = furthestMeasurement ? furthestMeasurement.lane : i % this.options.lanes;
689
- if (this.options.lanes > 1 && shouldCacheLane) {
689
+ lane = i % this.options.lanes;
690
+ start = paddingStart + scrollMargin;
691
+ if (shouldCacheLane) {
690
692
  this.laneAssignments.set(i, lane);
691
693
  }
692
694
  }
@@ -701,7 +703,9 @@ class Virtualizer {
701
703
  key,
702
704
  lane
703
705
  };
706
+ if (laneLastIndex[lane] === void 0) filledLanes++;
704
707
  laneLastIndex[lane] = i;
708
+ laneEnds[lane] = end;
705
709
  }
706
710
  this.measurementsCache = measurements;
707
711
  return measurements;
@@ -975,6 +979,7 @@ class Virtualizer {
975
979
  ];
976
980
  };
977
981
  this.scrollToOffset = (toOffset, { align = "start", behavior = "auto" } = {}) => {
982
+ this._iosDeferredAdjustment = 0;
978
983
  const offset = this.getOffsetForAlignment(toOffset, align);
979
984
  const now = this.now();
980
985
  this.scrollState = {
@@ -992,6 +997,7 @@ class Virtualizer {
992
997
  align: initialAlign = "auto",
993
998
  behavior = "auto"
994
999
  } = {}) => {
1000
+ this._iosDeferredAdjustment = 0;
995
1001
  index = Math.max(0, Math.min(index, this.options.count - 1));
996
1002
  const offsetInfo = this.getOffsetForIndex(index, initialAlign);
997
1003
  if (!offsetInfo) {
@@ -1115,6 +1121,7 @@ class Virtualizer {
1115
1121
  });
1116
1122
  if (this.scrollOffset !== null) {
1117
1123
  this.scrollOffset += this.scrollAdjustments;
1124
+ if (this.scrollOffset < 0) this.scrollOffset = 0;
1118
1125
  this.scrollAdjustments = 0;
1119
1126
  }
1120
1127
  }