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