@tanstack/virtual-core 3.13.23 → 3.15.0

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.
@@ -1,6 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const lazyMeasurements = require("./lazy-measurements.cjs");
3
4
  const utils = require("./utils.cjs");
5
+ let _isIOSResult;
6
+ const isIOSWebKit = () => {
7
+ if (_isIOSResult !== void 0) return _isIOSResult;
8
+ if (typeof navigator === "undefined") return _isIOSResult = false;
9
+ if (/iP(hone|od|ad)/.test(navigator.userAgent)) return _isIOSResult = true;
10
+ const mtp = navigator.maxTouchPoints;
11
+ return _isIOSResult = navigator.platform === "MacIntel" && mtp !== void 0 && mtp > 0;
12
+ };
13
+ const _resetIOSDetectionForTests = () => {
14
+ _isIOSResult = void 0;
15
+ };
4
16
  const getRect = (element) => {
5
17
  const { offsetWidth, offsetHeight } = element;
6
18
  return { width: offsetWidth, height: offsetHeight };
@@ -9,9 +21,10 @@ const defaultKeyExtractor = (index) => index;
9
21
  const defaultRangeExtractor = (range) => {
10
22
  const start = Math.max(range.startIndex - range.overscan, 0);
11
23
  const end = Math.min(range.endIndex + range.overscan, range.count - 1);
12
- const arr = [];
13
- for (let i = start; i <= end; i++) {
14
- arr.push(i);
24
+ const len = end - start + 1;
25
+ const arr = new Array(len);
26
+ for (let i = 0; i < len; i++) {
27
+ arr[i] = start + i;
15
28
  }
16
29
  return arr;
17
30
  };
@@ -70,7 +83,7 @@ const observeWindowRect = (instance, cb) => {
70
83
  };
71
84
  };
72
85
  const supportsScrollend = typeof window == "undefined" ? true : "onscrollend" in window;
73
- const observeElementOffset = (instance, cb) => {
86
+ const observeOffset = (instance, cb, readOffset) => {
74
87
  const element = instance.scrollElement;
75
88
  if (!element) {
76
89
  return;
@@ -79,60 +92,21 @@ const observeElementOffset = (instance, cb) => {
79
92
  if (!targetWindow) {
80
93
  return;
81
94
  }
82
- let offset = 0;
83
- const fallback = instance.options.useScrollendEvent && supportsScrollend ? () => void 0 : utils.debounce(
84
- targetWindow,
85
- () => {
86
- cb(offset, false);
87
- },
88
- instance.options.isScrollingResetDelay
89
- );
90
- const createHandler = (isScrolling) => () => {
91
- const { horizontal, isRtl } = instance.options;
92
- offset = horizontal ? element["scrollLeft"] * (isRtl && -1 || 1) : element["scrollTop"];
93
- fallback();
94
- cb(offset, isScrolling);
95
- };
96
- const handler = createHandler(true);
97
- const endHandler = createHandler(false);
98
- element.addEventListener("scroll", handler, addEventListenerOptions);
99
95
  const registerScrollendEvent = instance.options.useScrollendEvent && supportsScrollend;
100
- if (registerScrollendEvent) {
101
- element.addEventListener("scrollend", endHandler, addEventListenerOptions);
102
- }
103
- return () => {
104
- element.removeEventListener("scroll", handler);
105
- if (registerScrollendEvent) {
106
- element.removeEventListener("scrollend", endHandler);
107
- }
108
- };
109
- };
110
- const observeWindowOffset = (instance, cb) => {
111
- const element = instance.scrollElement;
112
- if (!element) {
113
- return;
114
- }
115
- const targetWindow = instance.targetWindow;
116
- if (!targetWindow) {
117
- return;
118
- }
119
96
  let offset = 0;
120
- const fallback = instance.options.useScrollendEvent && supportsScrollend ? () => void 0 : utils.debounce(
97
+ const fallback = registerScrollendEvent ? null : utils.debounce(
121
98
  targetWindow,
122
- () => {
123
- cb(offset, false);
124
- },
99
+ () => cb(offset, false),
125
100
  instance.options.isScrollingResetDelay
126
101
  );
127
102
  const createHandler = (isScrolling) => () => {
128
- offset = element[instance.options.horizontal ? "scrollX" : "scrollY"];
129
- fallback();
103
+ offset = readOffset(element);
104
+ fallback == null ? void 0 : fallback();
130
105
  cb(offset, isScrolling);
131
106
  };
132
107
  const handler = createHandler(true);
133
108
  const endHandler = createHandler(false);
134
109
  element.addEventListener("scroll", handler, addEventListenerOptions);
135
- const registerScrollendEvent = instance.options.useScrollendEvent && supportsScrollend;
136
110
  if (registerScrollendEvent) {
137
111
  element.addEventListener("scrollend", endHandler, addEventListenerOptions);
138
112
  }
@@ -143,6 +117,15 @@ const observeWindowOffset = (instance, cb) => {
143
117
  }
144
118
  };
145
119
  };
120
+ const observeElementOffset = (instance, cb) => observeOffset(instance, cb, (el) => {
121
+ const { horizontal, isRtl } = instance.options;
122
+ return horizontal ? el.scrollLeft * (isRtl && -1 || 1) : el.scrollTop;
123
+ });
124
+ const observeWindowOffset = (instance, cb) => observeOffset(
125
+ instance,
126
+ cb,
127
+ (win) => instance.options.horizontal ? win.scrollX : win.scrollY
128
+ );
146
129
  const measureElement = (element, entry, instance) => {
147
130
  if (entry == null ? void 0 : entry.borderBoxSize) {
148
131
  const box = entry.borderBoxSize[0];
@@ -155,28 +138,18 @@ const measureElement = (element, entry, instance) => {
155
138
  }
156
139
  return element[instance.options.horizontal ? "offsetWidth" : "offsetHeight"];
157
140
  };
158
- const windowScroll = (offset, {
159
- adjustments = 0,
160
- behavior
161
- }, instance) => {
162
- var _a, _b;
163
- const toOffset = offset + adjustments;
164
- (_b = (_a = instance.scrollElement) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, {
165
- [instance.options.horizontal ? "left" : "top"]: toOffset,
166
- behavior
167
- });
168
- };
169
- const elementScroll = (offset, {
141
+ const scrollWithAdjustments = (offset, {
170
142
  adjustments = 0,
171
143
  behavior
172
144
  }, instance) => {
173
145
  var _a, _b;
174
- const toOffset = offset + adjustments;
175
146
  (_b = (_a = instance.scrollElement) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, {
176
- [instance.options.horizontal ? "left" : "top"]: toOffset,
147
+ [instance.options.horizontal ? "left" : "top"]: offset + adjustments,
177
148
  behavior
178
149
  });
179
150
  };
151
+ const windowScroll = scrollWithAdjustments;
152
+ const elementScroll = scrollWithAdjustments;
180
153
  class Virtualizer {
181
154
  constructor(opts) {
182
155
  this.unsubs = [];
@@ -185,9 +158,11 @@ class Virtualizer {
185
158
  this.isScrolling = false;
186
159
  this.scrollState = null;
187
160
  this.measurementsCache = [];
161
+ this._flatMeasurements = null;
188
162
  this.itemSizeCache = /* @__PURE__ */ new Map();
163
+ this.itemSizeCacheVersion = 0;
189
164
  this.laneAssignments = /* @__PURE__ */ new Map();
190
- this.pendingMeasuredCacheIndexes = [];
165
+ this.pendingMin = null;
191
166
  this.prevLanes = void 0;
192
167
  this.lanesChangedFlag = false;
193
168
  this.lanesSettling = false;
@@ -195,6 +170,11 @@ class Virtualizer {
195
170
  this.scrollOffset = null;
196
171
  this.scrollDirection = null;
197
172
  this.scrollAdjustments = 0;
173
+ this._iosDeferredAdjustment = 0;
174
+ this._iosTouching = false;
175
+ this._iosJustTouchEnded = false;
176
+ this._iosTouchEndTimerId = null;
177
+ this._intendedScrollOffset = null;
198
178
  this.elementsCache = /* @__PURE__ */ new Map();
199
179
  this.now = () => {
200
180
  var _a, _b, _c;
@@ -216,6 +196,12 @@ class Virtualizer {
216
196
  const index = this.indexFromElement(node);
217
197
  if (!node.isConnected) {
218
198
  this.observer.unobserve(node);
199
+ for (const [cacheKey, cachedNode] of this.elementsCache) {
200
+ if (cachedNode === node) {
201
+ this.elementsCache.delete(cacheKey);
202
+ break;
203
+ }
204
+ }
219
205
  return;
220
206
  }
221
207
  if (this.shouldMeasureDuringScroll(index)) {
@@ -247,10 +233,7 @@ class Virtualizer {
247
233
  })();
248
234
  this.range = null;
249
235
  this.setOptions = (opts2) => {
250
- Object.entries(opts2).forEach(([key, value]) => {
251
- if (typeof value === "undefined") delete opts2[key];
252
- });
253
- this.options = {
236
+ const merged = {
254
237
  debug: false,
255
238
  initialOffset: 0,
256
239
  overscan: 1,
@@ -275,8 +258,13 @@ class Virtualizer {
275
258
  isRtl: false,
276
259
  useScrollendEvent: false,
277
260
  useAnimationFrameWithResizeObserver: false,
278
- ...opts2
261
+ laneAssignmentMode: "estimate"
279
262
  };
263
+ for (const key in opts2) {
264
+ const v = opts2[key];
265
+ if (v !== void 0) merged[key] = v;
266
+ }
267
+ this.options = merged;
280
268
  };
281
269
  this.notify = (sync) => {
282
270
  var _a, _b;
@@ -347,22 +335,83 @@ class Virtualizer {
347
335
  );
348
336
  this.unsubs.push(
349
337
  this.options.observeElementOffset(this, (offset, isScrolling) => {
338
+ if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
339
+ offset = this._intendedScrollOffset;
340
+ }
341
+ this._intendedScrollOffset = null;
350
342
  this.scrollAdjustments = 0;
351
343
  this.scrollDirection = isScrolling ? this.getScrollOffset() < offset ? "forward" : "backward" : null;
352
344
  this.scrollOffset = offset;
353
345
  this.isScrolling = isScrolling;
346
+ this._flushIosDeferredIfReady();
354
347
  if (this.scrollState) {
355
348
  this.scheduleScrollReconcile();
356
349
  }
357
350
  this.maybeNotify();
358
351
  })
359
352
  );
353
+ if ("addEventListener" in this.scrollElement) {
354
+ const scrollEl = this.scrollElement;
355
+ const onTouchStart = () => {
356
+ this._iosTouching = true;
357
+ this._iosJustTouchEnded = false;
358
+ if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
359
+ this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
360
+ this._iosTouchEndTimerId = null;
361
+ }
362
+ };
363
+ const onTouchEnd = () => {
364
+ this._iosTouching = false;
365
+ if (!isIOSWebKit() || this.targetWindow == null) {
366
+ return;
367
+ }
368
+ this._iosJustTouchEnded = true;
369
+ this._iosTouchEndTimerId = this.targetWindow.setTimeout(() => {
370
+ this._iosJustTouchEnded = false;
371
+ this._iosTouchEndTimerId = null;
372
+ this._flushIosDeferredIfReady();
373
+ }, 150);
374
+ };
375
+ scrollEl.addEventListener(
376
+ "touchstart",
377
+ onTouchStart,
378
+ addEventListenerOptions
379
+ );
380
+ scrollEl.addEventListener(
381
+ "touchend",
382
+ onTouchEnd,
383
+ addEventListenerOptions
384
+ );
385
+ this.unsubs.push(() => {
386
+ scrollEl.removeEventListener("touchstart", onTouchStart);
387
+ scrollEl.removeEventListener("touchend", onTouchEnd);
388
+ if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
389
+ this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
390
+ this._iosTouchEndTimerId = null;
391
+ }
392
+ });
393
+ }
360
394
  this._scrollToOffset(this.getScrollOffset(), {
361
395
  adjustments: void 0,
362
396
  behavior: void 0
363
397
  });
364
398
  }
365
399
  };
400
+ this._flushIosDeferredIfReady = () => {
401
+ if (this._iosDeferredAdjustment === 0) return;
402
+ if (this.isScrolling) return;
403
+ if (this._iosTouching) return;
404
+ if (this._iosJustTouchEnded) return;
405
+ const cur = this.getScrollOffset();
406
+ const max = this.getMaxScrollOffset();
407
+ if (cur < 0 || cur > max) return;
408
+ const delta = this._iosDeferredAdjustment;
409
+ this._iosDeferredAdjustment = 0;
410
+ this._scrollToOffset(cur, {
411
+ adjustments: this.scrollAdjustments += delta,
412
+ behavior: void 0
413
+ });
414
+ };
366
415
  this.rafId = null;
367
416
  this.getSize = () => {
368
417
  if (!this.options.enabled) {
@@ -414,22 +463,24 @@ class Virtualizer {
414
463
  this.options.scrollMargin,
415
464
  this.options.getItemKey,
416
465
  this.options.enabled,
417
- this.options.lanes
466
+ this.options.lanes,
467
+ this.options.laneAssignmentMode
418
468
  ],
419
- (count, paddingStart, scrollMargin, getItemKey, enabled, lanes) => {
469
+ (count, paddingStart, scrollMargin, getItemKey, enabled, lanes, laneAssignmentMode) => {
420
470
  const lanesChanged = this.prevLanes !== void 0 && this.prevLanes !== lanes;
421
471
  if (lanesChanged) {
422
472
  this.lanesChangedFlag = true;
423
473
  }
424
474
  this.prevLanes = lanes;
425
- this.pendingMeasuredCacheIndexes = [];
475
+ this.pendingMin = null;
426
476
  return {
427
477
  count,
428
478
  paddingStart,
429
479
  scrollMargin,
430
480
  getItemKey,
431
481
  enabled,
432
- lanes
482
+ lanes,
483
+ laneAssignmentMode
433
484
  };
434
485
  },
435
486
  {
@@ -437,8 +488,17 @@ class Virtualizer {
437
488
  }
438
489
  );
439
490
  this.getMeasurements = utils.memo(
440
- () => [this.getMeasurementOptions(), this.itemSizeCache],
441
- ({ count, paddingStart, scrollMargin, getItemKey, enabled, lanes }, itemSizeCache) => {
491
+ () => [this.getMeasurementOptions(), this.itemSizeCacheVersion],
492
+ ({
493
+ count,
494
+ paddingStart,
495
+ scrollMargin,
496
+ getItemKey,
497
+ enabled,
498
+ lanes,
499
+ laneAssignmentMode
500
+ }, _itemSizeCacheVersion) => {
501
+ const itemSizeCache = this.itemSizeCache;
442
502
  if (!enabled) {
443
503
  this.measurementsCache = [];
444
504
  this.itemSizeCache.clear();
@@ -458,7 +518,7 @@ class Virtualizer {
458
518
  this.measurementsCache = [];
459
519
  this.itemSizeCache.clear();
460
520
  this.laneAssignments.clear();
461
- this.pendingMeasuredCacheIndexes = [];
521
+ this.pendingMin = null;
462
522
  }
463
523
  if (this.measurementsCache.length === 0 && !this.lanesSettling) {
464
524
  this.measurementsCache = this.options.initialMeasurementsCache;
@@ -466,11 +526,40 @@ class Virtualizer {
466
526
  this.itemSizeCache.set(item.key, item.size);
467
527
  });
468
528
  }
469
- const min = this.lanesSettling ? 0 : this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
470
- this.pendingMeasuredCacheIndexes = [];
529
+ const min = this.lanesSettling ? 0 : this.pendingMin ?? 0;
530
+ this.pendingMin = null;
471
531
  if (this.lanesSettling && this.measurementsCache.length === count) {
472
532
  this.lanesSettling = false;
473
533
  }
534
+ if (lanes === 1) {
535
+ const gap = this.options.gap;
536
+ const need = count * 2;
537
+ let flat = this._flatMeasurements;
538
+ if (!flat || flat.length < need) {
539
+ const next = new Float64Array(need);
540
+ if (flat && min > 0) next.set(flat.subarray(0, min * 2));
541
+ flat = next;
542
+ this._flatMeasurements = flat;
543
+ }
544
+ let runningStart;
545
+ if (min === 0) {
546
+ runningStart = paddingStart + scrollMargin;
547
+ } else {
548
+ const prevIdx = min - 1;
549
+ runningStart = flat[prevIdx * 2] + flat[prevIdx * 2 + 1] + gap;
550
+ }
551
+ for (let i = min; i < count; i++) {
552
+ const key = getItemKey(i);
553
+ const measuredSize = itemSizeCache.get(key);
554
+ const size = typeof measuredSize === "number" ? measuredSize : this.options.estimateSize(i);
555
+ flat[i * 2] = runningStart;
556
+ flat[i * 2 + 1] = size;
557
+ runningStart += size + gap;
558
+ }
559
+ const view = lazyMeasurements.createLazyMeasurementsView(count, flat, getItemKey);
560
+ this.measurementsCache = view;
561
+ return view;
562
+ }
474
563
  const measurements = this.measurementsCache.slice(0, min);
475
564
  const laneLastIndex = new Array(lanes).fill(
476
565
  void 0
@@ -486,6 +575,7 @@ class Virtualizer {
486
575
  const cachedLane = this.laneAssignments.get(i);
487
576
  let lane;
488
577
  let start;
578
+ const shouldCacheLane = laneAssignmentMode === "estimate" || itemSizeCache.has(key);
489
579
  if (cachedLane !== void 0 && this.options.lanes > 1) {
490
580
  lane = cachedLane;
491
581
  const prevIndex = laneLastIndex[lane];
@@ -495,7 +585,7 @@ class Virtualizer {
495
585
  const furthestMeasurement = this.options.lanes === 1 ? measurements[i - 1] : this.getFurthestMeasurement(measurements, i);
496
586
  start = furthestMeasurement ? furthestMeasurement.end + this.options.gap : paddingStart + scrollMargin;
497
587
  lane = furthestMeasurement ? furthestMeasurement.lane : i % this.options.lanes;
498
- if (this.options.lanes > 1) {
588
+ if (this.options.lanes > 1 && shouldCacheLane) {
499
589
  this.laneAssignments.set(i, lane);
500
590
  }
501
591
  }
@@ -532,7 +622,11 @@ class Virtualizer {
532
622
  measurements,
533
623
  outerSize,
534
624
  scrollOffset,
535
- lanes
625
+ lanes,
626
+ // Pass the typed array so binary search + forward-walk can
627
+ // read start/end directly from Float64Array, skipping the
628
+ // Proxy traps that materialize a full VirtualItem per probe.
629
+ flat: lanes === 1 && this._flatMeasurements != null ? this._flatMeasurements : null
536
630
  }) : null;
537
631
  },
538
632
  {
@@ -628,22 +722,64 @@ class Virtualizer {
628
722
  };
629
723
  this.resizeItem = (index, size) => {
630
724
  var _a;
631
- const item = this.measurementsCache[index];
632
- if (!item) return;
633
- const itemSize = this.itemSizeCache.get(item.key) ?? item.size;
725
+ if (index < 0 || index >= this.options.count) return;
726
+ let cachedSize;
727
+ let itemStart;
728
+ let key;
729
+ const flat = this._flatMeasurements;
730
+ if (this.options.lanes === 1 && flat !== null) {
731
+ key = this.options.getItemKey(index);
732
+ itemStart = flat[index * 2];
733
+ cachedSize = flat[index * 2 + 1];
734
+ } else {
735
+ const item = this.measurementsCache[index];
736
+ if (!item) return;
737
+ key = item.key;
738
+ itemStart = item.start;
739
+ cachedSize = item.size;
740
+ }
741
+ const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
634
742
  const delta = size - itemSize;
635
743
  if (delta !== 0) {
636
- if (((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(item, delta, this) : item.start < this.getScrollOffset() + this.scrollAdjustments)) {
744
+ if (((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
745
+ // The callback expects a VirtualItem; build one lazily only
746
+ // when the consumer actually supplied a custom predicate.
747
+ this.measurementsCache[index] ?? {
748
+ index,
749
+ key,
750
+ start: itemStart,
751
+ size: cachedSize,
752
+ end: itemStart + cachedSize,
753
+ lane: 0
754
+ },
755
+ delta,
756
+ this
757
+ ) : (
758
+ // Default: adjust scrollTop only when the resize is an above-
759
+ // viewport item AND we're not actively scrolling backward.
760
+ // Adjusting during backward scroll fights the user's scroll
761
+ // direction and produces the "items jump while scrolling up"
762
+ // jank reported across many issues. Users who want the old
763
+ // behavior can pass shouldAdjustScrollPositionOnItemSizeChange.
764
+ itemStart < this.getScrollOffset() + this.scrollAdjustments && this.scrollDirection !== "backward"
765
+ ))) {
637
766
  if (process.env.NODE_ENV !== "production" && this.options.debug) {
638
767
  console.info("correction", delta);
639
768
  }
640
- this._scrollToOffset(this.getScrollOffset(), {
641
- adjustments: this.scrollAdjustments += delta,
642
- behavior: void 0
643
- });
769
+ if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
770
+ this._iosDeferredAdjustment += delta;
771
+ } else {
772
+ this._scrollToOffset(this.getScrollOffset(), {
773
+ adjustments: this.scrollAdjustments += delta,
774
+ behavior: void 0
775
+ });
776
+ }
644
777
  }
645
- this.pendingMeasuredCacheIndexes.push(item.index);
646
- this.itemSizeCache = new Map(this.itemSizeCache.set(item.key, size));
778
+ if (this.pendingMin === null || index < this.pendingMin) {
779
+ this.pendingMin = index;
780
+ }
781
+ this.itemSizeCache.set(key, size);
782
+ this.itemSizeCacheVersion++;
647
783
  this.notify(false);
648
784
  }
649
785
  };
@@ -668,14 +804,15 @@ class Virtualizer {
668
804
  if (measurements.length === 0) {
669
805
  return void 0;
670
806
  }
671
- return utils.notUndefined(
672
- measurements[findNearestBinarySearch(
673
- 0,
674
- measurements.length - 1,
675
- (index) => utils.notUndefined(measurements[index]).start,
676
- offset
677
- )]
807
+ const flat = this._flatMeasurements;
808
+ const useFlat = this.options.lanes === 1 && flat != null;
809
+ const idx = findNearestBinarySearch(
810
+ 0,
811
+ measurements.length - 1,
812
+ useFlat ? (i) => flat[i * 2] : (i) => utils.notUndefined(measurements[i]).start,
813
+ offset
678
814
  );
815
+ return utils.notUndefined(measurements[idx]);
679
816
  };
680
817
  this.getMaxScrollOffset = () => {
681
818
  if (!this.scrollElement) return 0;
@@ -782,7 +919,13 @@ class Virtualizer {
782
919
  if (measurements.length === 0) {
783
920
  end = this.options.paddingStart;
784
921
  } else if (this.options.lanes === 1) {
785
- end = ((_a = measurements[measurements.length - 1]) == null ? void 0 : _a.end) ?? 0;
922
+ const lastIdx = measurements.length - 1;
923
+ const flat = this._flatMeasurements;
924
+ if (flat != null) {
925
+ end = flat[lastIdx * 2] + flat[lastIdx * 2 + 1];
926
+ } else {
927
+ end = ((_a = measurements[lastIdx]) == null ? void 0 : _a.end) ?? 0;
928
+ }
786
929
  } else {
787
930
  const endByLane = Array(this.options.lanes).fill(null);
788
931
  let endIndex = measurements.length - 1;
@@ -800,15 +943,36 @@ class Virtualizer {
800
943
  0
801
944
  );
802
945
  };
946
+ this.takeSnapshot = () => {
947
+ const snapshot = [];
948
+ if (this.itemSizeCache.size === 0) return snapshot;
949
+ const m = this.getMeasurements();
950
+ for (const item of m) {
951
+ if (item && this.itemSizeCache.has(item.key)) {
952
+ snapshot.push({
953
+ index: item.index,
954
+ key: item.key,
955
+ start: item.start,
956
+ size: item.size,
957
+ end: item.end,
958
+ lane: item.lane
959
+ });
960
+ }
961
+ }
962
+ return snapshot;
963
+ };
803
964
  this._scrollToOffset = (offset, {
804
965
  adjustments,
805
966
  behavior
806
967
  }) => {
968
+ this._intendedScrollOffset = offset + (adjustments ?? 0);
807
969
  this.options.scrollToFn(offset, { behavior, adjustments }, this);
808
970
  };
809
971
  this.measure = () => {
810
- this.itemSizeCache = /* @__PURE__ */ new Map();
811
- this.laneAssignments = /* @__PURE__ */ new Map();
972
+ this.pendingMin = null;
973
+ this.itemSizeCache.clear();
974
+ this.laneAssignments.clear();
975
+ this.itemSizeCacheVersion++;
812
976
  this.notify(false);
813
977
  };
814
978
  this.setOptions(opts);
@@ -840,17 +1004,28 @@ class Virtualizer {
840
1004
  if (!targetChanged && utils.approxEqual(targetOffset, this.getScrollOffset())) {
841
1005
  this.scrollState.stableFrames++;
842
1006
  if (this.scrollState.stableFrames >= STABLE_FRAMES) {
1007
+ if (this.getScrollOffset() !== targetOffset) {
1008
+ this._scrollToOffset(targetOffset, {
1009
+ adjustments: void 0,
1010
+ behavior: "auto"
1011
+ });
1012
+ }
843
1013
  this.scrollState = null;
844
1014
  return;
845
1015
  }
846
1016
  } else {
847
1017
  this.scrollState.stableFrames = 0;
848
1018
  if (targetChanged) {
1019
+ const viewport = this.getSize() || 600;
1020
+ const distance = Math.abs(targetOffset - this.getScrollOffset());
1021
+ const keepSmooth = this.scrollState.behavior === "smooth" && distance > viewport;
849
1022
  this.scrollState.lastTargetOffset = targetOffset;
850
- this.scrollState.behavior = "auto";
1023
+ if (!keepSmooth) {
1024
+ this.scrollState.behavior = "auto";
1025
+ }
851
1026
  this._scrollToOffset(targetOffset, {
852
1027
  adjustments: void 0,
853
- behavior: "auto"
1028
+ behavior: keepSmooth ? "smooth" : "auto"
854
1029
  });
855
1030
  }
856
1031
  }
@@ -879,25 +1054,22 @@ function calculateRange({
879
1054
  measurements,
880
1055
  outerSize,
881
1056
  scrollOffset,
882
- lanes
1057
+ lanes,
1058
+ flat
883
1059
  }) {
884
1060
  const lastIndex = measurements.length - 1;
885
- const getOffset = (index) => measurements[index].start;
1061
+ const getStart = flat ? (index) => flat[index * 2] : (index) => measurements[index].start;
1062
+ const getEnd = flat ? (index) => flat[index * 2] + flat[index * 2 + 1] : (index) => measurements[index].end;
886
1063
  if (measurements.length <= lanes) {
887
1064
  return {
888
1065
  startIndex: 0,
889
1066
  endIndex: lastIndex
890
1067
  };
891
1068
  }
892
- let startIndex = findNearestBinarySearch(
893
- 0,
894
- lastIndex,
895
- getOffset,
896
- scrollOffset
897
- );
1069
+ let startIndex = findNearestBinarySearch(0, lastIndex, getStart, scrollOffset);
898
1070
  let endIndex = startIndex;
899
1071
  if (lanes === 1) {
900
- while (endIndex < lastIndex && measurements[endIndex].end < scrollOffset + outerSize) {
1072
+ while (endIndex < lastIndex && getEnd(endIndex) < scrollOffset + outerSize) {
901
1073
  endIndex++;
902
1074
  }
903
1075
  } else if (lanes > 1) {
@@ -923,6 +1095,7 @@ exports.debounce = utils.debounce;
923
1095
  exports.memo = utils.memo;
924
1096
  exports.notUndefined = utils.notUndefined;
925
1097
  exports.Virtualizer = Virtualizer;
1098
+ exports._resetIOSDetectionForTests = _resetIOSDetectionForTests;
926
1099
  exports.defaultKeyExtractor = defaultKeyExtractor;
927
1100
  exports.defaultRangeExtractor = defaultRangeExtractor;
928
1101
  exports.elementScroll = elementScroll;