@tanstack/virtual-core 3.14.0 → 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.
package/dist/esm/index.js CHANGED
@@ -1,4 +1,16 @@
1
- import { debounce, memo, notUndefined, approxEqual } from "./utils.js";
1
+ import { createLazyMeasurementsView } from "./lazy-measurements.js";
2
+ import { memo, notUndefined, approxEqual, debounce } from "./utils.js";
3
+ let _isIOSResult;
4
+ const isIOSWebKit = () => {
5
+ if (_isIOSResult !== void 0) return _isIOSResult;
6
+ if (typeof navigator === "undefined") return _isIOSResult = false;
7
+ if (/iP(hone|od|ad)/.test(navigator.userAgent)) return _isIOSResult = true;
8
+ const mtp = navigator.maxTouchPoints;
9
+ return _isIOSResult = navigator.platform === "MacIntel" && mtp !== void 0 && mtp > 0;
10
+ };
11
+ const _resetIOSDetectionForTests = () => {
12
+ _isIOSResult = void 0;
13
+ };
2
14
  const getRect = (element) => {
3
15
  const { offsetWidth, offsetHeight } = element;
4
16
  return { width: offsetWidth, height: offsetHeight };
@@ -7,9 +19,10 @@ const defaultKeyExtractor = (index) => index;
7
19
  const defaultRangeExtractor = (range) => {
8
20
  const start = Math.max(range.startIndex - range.overscan, 0);
9
21
  const end = Math.min(range.endIndex + range.overscan, range.count - 1);
10
- const arr = [];
11
- for (let i = start; i <= end; i++) {
12
- arr.push(i);
22
+ const len = end - start + 1;
23
+ const arr = new Array(len);
24
+ for (let i = 0; i < len; i++) {
25
+ arr[i] = start + i;
13
26
  }
14
27
  return arr;
15
28
  };
@@ -68,7 +81,7 @@ const observeWindowRect = (instance, cb) => {
68
81
  };
69
82
  };
70
83
  const supportsScrollend = typeof window == "undefined" ? true : "onscrollend" in window;
71
- const observeElementOffset = (instance, cb) => {
84
+ const observeOffset = (instance, cb, readOffset) => {
72
85
  const element = instance.scrollElement;
73
86
  if (!element) {
74
87
  return;
@@ -77,60 +90,21 @@ const observeElementOffset = (instance, cb) => {
77
90
  if (!targetWindow) {
78
91
  return;
79
92
  }
80
- let offset = 0;
81
- const fallback = instance.options.useScrollendEvent && supportsScrollend ? () => void 0 : debounce(
82
- targetWindow,
83
- () => {
84
- cb(offset, false);
85
- },
86
- instance.options.isScrollingResetDelay
87
- );
88
- const createHandler = (isScrolling) => () => {
89
- const { horizontal, isRtl } = instance.options;
90
- offset = horizontal ? element["scrollLeft"] * (isRtl && -1 || 1) : element["scrollTop"];
91
- fallback();
92
- cb(offset, isScrolling);
93
- };
94
- const handler = createHandler(true);
95
- const endHandler = createHandler(false);
96
- element.addEventListener("scroll", handler, addEventListenerOptions);
97
93
  const registerScrollendEvent = instance.options.useScrollendEvent && supportsScrollend;
98
- if (registerScrollendEvent) {
99
- element.addEventListener("scrollend", endHandler, addEventListenerOptions);
100
- }
101
- return () => {
102
- element.removeEventListener("scroll", handler);
103
- if (registerScrollendEvent) {
104
- element.removeEventListener("scrollend", endHandler);
105
- }
106
- };
107
- };
108
- const observeWindowOffset = (instance, cb) => {
109
- const element = instance.scrollElement;
110
- if (!element) {
111
- return;
112
- }
113
- const targetWindow = instance.targetWindow;
114
- if (!targetWindow) {
115
- return;
116
- }
117
94
  let offset = 0;
118
- const fallback = instance.options.useScrollendEvent && supportsScrollend ? () => void 0 : debounce(
95
+ const fallback = registerScrollendEvent ? null : debounce(
119
96
  targetWindow,
120
- () => {
121
- cb(offset, false);
122
- },
97
+ () => cb(offset, false),
123
98
  instance.options.isScrollingResetDelay
124
99
  );
125
100
  const createHandler = (isScrolling) => () => {
126
- offset = element[instance.options.horizontal ? "scrollX" : "scrollY"];
127
- fallback();
101
+ offset = readOffset(element);
102
+ fallback == null ? void 0 : fallback();
128
103
  cb(offset, isScrolling);
129
104
  };
130
105
  const handler = createHandler(true);
131
106
  const endHandler = createHandler(false);
132
107
  element.addEventListener("scroll", handler, addEventListenerOptions);
133
- const registerScrollendEvent = instance.options.useScrollendEvent && supportsScrollend;
134
108
  if (registerScrollendEvent) {
135
109
  element.addEventListener("scrollend", endHandler, addEventListenerOptions);
136
110
  }
@@ -141,6 +115,15 @@ const observeWindowOffset = (instance, cb) => {
141
115
  }
142
116
  };
143
117
  };
118
+ const observeElementOffset = (instance, cb) => observeOffset(instance, cb, (el) => {
119
+ const { horizontal, isRtl } = instance.options;
120
+ return horizontal ? el.scrollLeft * (isRtl && -1 || 1) : el.scrollTop;
121
+ });
122
+ const observeWindowOffset = (instance, cb) => observeOffset(
123
+ instance,
124
+ cb,
125
+ (win) => instance.options.horizontal ? win.scrollX : win.scrollY
126
+ );
144
127
  const measureElement = (element, entry, instance) => {
145
128
  if (entry == null ? void 0 : entry.borderBoxSize) {
146
129
  const box = entry.borderBoxSize[0];
@@ -153,28 +136,18 @@ const measureElement = (element, entry, instance) => {
153
136
  }
154
137
  return element[instance.options.horizontal ? "offsetWidth" : "offsetHeight"];
155
138
  };
156
- const windowScroll = (offset, {
157
- adjustments = 0,
158
- behavior
159
- }, instance) => {
160
- var _a, _b;
161
- const toOffset = offset + adjustments;
162
- (_b = (_a = instance.scrollElement) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, {
163
- [instance.options.horizontal ? "left" : "top"]: toOffset,
164
- behavior
165
- });
166
- };
167
- const elementScroll = (offset, {
139
+ const scrollWithAdjustments = (offset, {
168
140
  adjustments = 0,
169
141
  behavior
170
142
  }, instance) => {
171
143
  var _a, _b;
172
- const toOffset = offset + adjustments;
173
144
  (_b = (_a = instance.scrollElement) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, {
174
- [instance.options.horizontal ? "left" : "top"]: toOffset,
145
+ [instance.options.horizontal ? "left" : "top"]: offset + adjustments,
175
146
  behavior
176
147
  });
177
148
  };
149
+ const windowScroll = scrollWithAdjustments;
150
+ const elementScroll = scrollWithAdjustments;
178
151
  class Virtualizer {
179
152
  constructor(opts) {
180
153
  this.unsubs = [];
@@ -183,9 +156,11 @@ class Virtualizer {
183
156
  this.isScrolling = false;
184
157
  this.scrollState = null;
185
158
  this.measurementsCache = [];
159
+ this._flatMeasurements = null;
186
160
  this.itemSizeCache = /* @__PURE__ */ new Map();
161
+ this.itemSizeCacheVersion = 0;
187
162
  this.laneAssignments = /* @__PURE__ */ new Map();
188
- this.pendingMeasuredCacheIndexes = [];
163
+ this.pendingMin = null;
189
164
  this.prevLanes = void 0;
190
165
  this.lanesChangedFlag = false;
191
166
  this.lanesSettling = false;
@@ -193,6 +168,11 @@ class Virtualizer {
193
168
  this.scrollOffset = null;
194
169
  this.scrollDirection = null;
195
170
  this.scrollAdjustments = 0;
171
+ this._iosDeferredAdjustment = 0;
172
+ this._iosTouching = false;
173
+ this._iosJustTouchEnded = false;
174
+ this._iosTouchEndTimerId = null;
175
+ this._intendedScrollOffset = null;
196
176
  this.elementsCache = /* @__PURE__ */ new Map();
197
177
  this.now = () => {
198
178
  var _a, _b, _c;
@@ -214,6 +194,12 @@ class Virtualizer {
214
194
  const index = this.indexFromElement(node);
215
195
  if (!node.isConnected) {
216
196
  this.observer.unobserve(node);
197
+ for (const [cacheKey, cachedNode] of this.elementsCache) {
198
+ if (cachedNode === node) {
199
+ this.elementsCache.delete(cacheKey);
200
+ break;
201
+ }
202
+ }
217
203
  return;
218
204
  }
219
205
  if (this.shouldMeasureDuringScroll(index)) {
@@ -245,10 +231,7 @@ class Virtualizer {
245
231
  })();
246
232
  this.range = null;
247
233
  this.setOptions = (opts2) => {
248
- Object.entries(opts2).forEach(([key, value]) => {
249
- if (typeof value === "undefined") delete opts2[key];
250
- });
251
- this.options = {
234
+ const merged = {
252
235
  debug: false,
253
236
  initialOffset: 0,
254
237
  overscan: 1,
@@ -273,9 +256,13 @@ class Virtualizer {
273
256
  isRtl: false,
274
257
  useScrollendEvent: false,
275
258
  useAnimationFrameWithResizeObserver: false,
276
- laneAssignmentMode: "estimate",
277
- ...opts2
259
+ laneAssignmentMode: "estimate"
278
260
  };
261
+ for (const key in opts2) {
262
+ const v = opts2[key];
263
+ if (v !== void 0) merged[key] = v;
264
+ }
265
+ this.options = merged;
279
266
  };
280
267
  this.notify = (sync) => {
281
268
  var _a, _b;
@@ -346,22 +333,83 @@ class Virtualizer {
346
333
  );
347
334
  this.unsubs.push(
348
335
  this.options.observeElementOffset(this, (offset, isScrolling) => {
336
+ if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
337
+ offset = this._intendedScrollOffset;
338
+ }
339
+ this._intendedScrollOffset = null;
349
340
  this.scrollAdjustments = 0;
350
341
  this.scrollDirection = isScrolling ? this.getScrollOffset() < offset ? "forward" : "backward" : null;
351
342
  this.scrollOffset = offset;
352
343
  this.isScrolling = isScrolling;
344
+ this._flushIosDeferredIfReady();
353
345
  if (this.scrollState) {
354
346
  this.scheduleScrollReconcile();
355
347
  }
356
348
  this.maybeNotify();
357
349
  })
358
350
  );
351
+ if ("addEventListener" in this.scrollElement) {
352
+ const scrollEl = this.scrollElement;
353
+ const onTouchStart = () => {
354
+ this._iosTouching = true;
355
+ this._iosJustTouchEnded = false;
356
+ if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
357
+ this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
358
+ this._iosTouchEndTimerId = null;
359
+ }
360
+ };
361
+ const onTouchEnd = () => {
362
+ this._iosTouching = false;
363
+ if (!isIOSWebKit() || this.targetWindow == null) {
364
+ return;
365
+ }
366
+ this._iosJustTouchEnded = true;
367
+ this._iosTouchEndTimerId = this.targetWindow.setTimeout(() => {
368
+ this._iosJustTouchEnded = false;
369
+ this._iosTouchEndTimerId = null;
370
+ this._flushIosDeferredIfReady();
371
+ }, 150);
372
+ };
373
+ scrollEl.addEventListener(
374
+ "touchstart",
375
+ onTouchStart,
376
+ addEventListenerOptions
377
+ );
378
+ scrollEl.addEventListener(
379
+ "touchend",
380
+ onTouchEnd,
381
+ addEventListenerOptions
382
+ );
383
+ this.unsubs.push(() => {
384
+ scrollEl.removeEventListener("touchstart", onTouchStart);
385
+ scrollEl.removeEventListener("touchend", onTouchEnd);
386
+ if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
387
+ this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
388
+ this._iosTouchEndTimerId = null;
389
+ }
390
+ });
391
+ }
359
392
  this._scrollToOffset(this.getScrollOffset(), {
360
393
  adjustments: void 0,
361
394
  behavior: void 0
362
395
  });
363
396
  }
364
397
  };
398
+ this._flushIosDeferredIfReady = () => {
399
+ if (this._iosDeferredAdjustment === 0) return;
400
+ if (this.isScrolling) return;
401
+ if (this._iosTouching) return;
402
+ if (this._iosJustTouchEnded) return;
403
+ const cur = this.getScrollOffset();
404
+ const max = this.getMaxScrollOffset();
405
+ if (cur < 0 || cur > max) return;
406
+ const delta = this._iosDeferredAdjustment;
407
+ this._iosDeferredAdjustment = 0;
408
+ this._scrollToOffset(cur, {
409
+ adjustments: this.scrollAdjustments += delta,
410
+ behavior: void 0
411
+ });
412
+ };
365
413
  this.rafId = null;
366
414
  this.getSize = () => {
367
415
  if (!this.options.enabled) {
@@ -422,7 +470,7 @@ class Virtualizer {
422
470
  this.lanesChangedFlag = true;
423
471
  }
424
472
  this.prevLanes = lanes;
425
- this.pendingMeasuredCacheIndexes = [];
473
+ this.pendingMin = null;
426
474
  return {
427
475
  count,
428
476
  paddingStart,
@@ -438,7 +486,7 @@ class Virtualizer {
438
486
  }
439
487
  );
440
488
  this.getMeasurements = memo(
441
- () => [this.getMeasurementOptions(), this.itemSizeCache],
489
+ () => [this.getMeasurementOptions(), this.itemSizeCacheVersion],
442
490
  ({
443
491
  count,
444
492
  paddingStart,
@@ -447,7 +495,8 @@ class Virtualizer {
447
495
  enabled,
448
496
  lanes,
449
497
  laneAssignmentMode
450
- }, itemSizeCache) => {
498
+ }, _itemSizeCacheVersion) => {
499
+ const itemSizeCache = this.itemSizeCache;
451
500
  if (!enabled) {
452
501
  this.measurementsCache = [];
453
502
  this.itemSizeCache.clear();
@@ -467,7 +516,7 @@ class Virtualizer {
467
516
  this.measurementsCache = [];
468
517
  this.itemSizeCache.clear();
469
518
  this.laneAssignments.clear();
470
- this.pendingMeasuredCacheIndexes = [];
519
+ this.pendingMin = null;
471
520
  }
472
521
  if (this.measurementsCache.length === 0 && !this.lanesSettling) {
473
522
  this.measurementsCache = this.options.initialMeasurementsCache;
@@ -475,11 +524,40 @@ class Virtualizer {
475
524
  this.itemSizeCache.set(item.key, item.size);
476
525
  });
477
526
  }
478
- const min = this.lanesSettling ? 0 : this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
479
- this.pendingMeasuredCacheIndexes = [];
527
+ const min = this.lanesSettling ? 0 : this.pendingMin ?? 0;
528
+ this.pendingMin = null;
480
529
  if (this.lanesSettling && this.measurementsCache.length === count) {
481
530
  this.lanesSettling = false;
482
531
  }
532
+ if (lanes === 1) {
533
+ const gap = this.options.gap;
534
+ const need = count * 2;
535
+ let flat = this._flatMeasurements;
536
+ if (!flat || flat.length < need) {
537
+ const next = new Float64Array(need);
538
+ if (flat && min > 0) next.set(flat.subarray(0, min * 2));
539
+ flat = next;
540
+ this._flatMeasurements = flat;
541
+ }
542
+ let runningStart;
543
+ if (min === 0) {
544
+ runningStart = paddingStart + scrollMargin;
545
+ } else {
546
+ const prevIdx = min - 1;
547
+ runningStart = flat[prevIdx * 2] + flat[prevIdx * 2 + 1] + gap;
548
+ }
549
+ for (let i = min; i < count; i++) {
550
+ const key = getItemKey(i);
551
+ const measuredSize = itemSizeCache.get(key);
552
+ const size = typeof measuredSize === "number" ? measuredSize : this.options.estimateSize(i);
553
+ flat[i * 2] = runningStart;
554
+ flat[i * 2 + 1] = size;
555
+ runningStart += size + gap;
556
+ }
557
+ const view = createLazyMeasurementsView(count, flat, getItemKey);
558
+ this.measurementsCache = view;
559
+ return view;
560
+ }
483
561
  const measurements = this.measurementsCache.slice(0, min);
484
562
  const laneLastIndex = new Array(lanes).fill(
485
563
  void 0
@@ -542,7 +620,11 @@ class Virtualizer {
542
620
  measurements,
543
621
  outerSize,
544
622
  scrollOffset,
545
- lanes
623
+ lanes,
624
+ // Pass the typed array so binary search + forward-walk can
625
+ // read start/end directly from Float64Array, skipping the
626
+ // Proxy traps that materialize a full VirtualItem per probe.
627
+ flat: lanes === 1 && this._flatMeasurements != null ? this._flatMeasurements : null
546
628
  }) : null;
547
629
  },
548
630
  {
@@ -638,22 +720,64 @@ class Virtualizer {
638
720
  };
639
721
  this.resizeItem = (index, size) => {
640
722
  var _a;
641
- const item = this.measurementsCache[index];
642
- if (!item) return;
643
- const itemSize = this.itemSizeCache.get(item.key) ?? item.size;
723
+ if (index < 0 || index >= this.options.count) return;
724
+ let cachedSize;
725
+ let itemStart;
726
+ let key;
727
+ const flat = this._flatMeasurements;
728
+ if (this.options.lanes === 1 && flat !== null) {
729
+ key = this.options.getItemKey(index);
730
+ itemStart = flat[index * 2];
731
+ cachedSize = flat[index * 2 + 1];
732
+ } else {
733
+ const item = this.measurementsCache[index];
734
+ if (!item) return;
735
+ key = item.key;
736
+ itemStart = item.start;
737
+ cachedSize = item.size;
738
+ }
739
+ const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
644
740
  const delta = size - itemSize;
645
741
  if (delta !== 0) {
646
- 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)) {
742
+ if (((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
743
+ // The callback expects a VirtualItem; build one lazily only
744
+ // when the consumer actually supplied a custom predicate.
745
+ this.measurementsCache[index] ?? {
746
+ index,
747
+ key,
748
+ start: itemStart,
749
+ size: cachedSize,
750
+ end: itemStart + cachedSize,
751
+ lane: 0
752
+ },
753
+ delta,
754
+ this
755
+ ) : (
756
+ // Default: adjust scrollTop only when the resize is an above-
757
+ // viewport item AND we're not actively scrolling backward.
758
+ // Adjusting during backward scroll fights the user's scroll
759
+ // direction and produces the "items jump while scrolling up"
760
+ // jank reported across many issues. Users who want the old
761
+ // behavior can pass shouldAdjustScrollPositionOnItemSizeChange.
762
+ itemStart < this.getScrollOffset() + this.scrollAdjustments && this.scrollDirection !== "backward"
763
+ ))) {
647
764
  if (process.env.NODE_ENV !== "production" && this.options.debug) {
648
765
  console.info("correction", delta);
649
766
  }
650
- this._scrollToOffset(this.getScrollOffset(), {
651
- adjustments: this.scrollAdjustments += delta,
652
- behavior: void 0
653
- });
767
+ if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
768
+ this._iosDeferredAdjustment += delta;
769
+ } else {
770
+ this._scrollToOffset(this.getScrollOffset(), {
771
+ adjustments: this.scrollAdjustments += delta,
772
+ behavior: void 0
773
+ });
774
+ }
654
775
  }
655
- this.pendingMeasuredCacheIndexes.push(item.index);
656
- this.itemSizeCache = new Map(this.itemSizeCache.set(item.key, size));
776
+ if (this.pendingMin === null || index < this.pendingMin) {
777
+ this.pendingMin = index;
778
+ }
779
+ this.itemSizeCache.set(key, size);
780
+ this.itemSizeCacheVersion++;
657
781
  this.notify(false);
658
782
  }
659
783
  };
@@ -678,14 +802,15 @@ class Virtualizer {
678
802
  if (measurements.length === 0) {
679
803
  return void 0;
680
804
  }
681
- return notUndefined(
682
- measurements[findNearestBinarySearch(
683
- 0,
684
- measurements.length - 1,
685
- (index) => notUndefined(measurements[index]).start,
686
- offset
687
- )]
805
+ const flat = this._flatMeasurements;
806
+ const useFlat = this.options.lanes === 1 && flat != null;
807
+ const idx = findNearestBinarySearch(
808
+ 0,
809
+ measurements.length - 1,
810
+ useFlat ? (i) => flat[i * 2] : (i) => notUndefined(measurements[i]).start,
811
+ offset
688
812
  );
813
+ return notUndefined(measurements[idx]);
689
814
  };
690
815
  this.getMaxScrollOffset = () => {
691
816
  if (!this.scrollElement) return 0;
@@ -792,7 +917,13 @@ class Virtualizer {
792
917
  if (measurements.length === 0) {
793
918
  end = this.options.paddingStart;
794
919
  } else if (this.options.lanes === 1) {
795
- end = ((_a = measurements[measurements.length - 1]) == null ? void 0 : _a.end) ?? 0;
920
+ const lastIdx = measurements.length - 1;
921
+ const flat = this._flatMeasurements;
922
+ if (flat != null) {
923
+ end = flat[lastIdx * 2] + flat[lastIdx * 2 + 1];
924
+ } else {
925
+ end = ((_a = measurements[lastIdx]) == null ? void 0 : _a.end) ?? 0;
926
+ }
796
927
  } else {
797
928
  const endByLane = Array(this.options.lanes).fill(null);
798
929
  let endIndex = measurements.length - 1;
@@ -810,15 +941,36 @@ class Virtualizer {
810
941
  0
811
942
  );
812
943
  };
944
+ this.takeSnapshot = () => {
945
+ const snapshot = [];
946
+ if (this.itemSizeCache.size === 0) return snapshot;
947
+ const m = this.getMeasurements();
948
+ for (const item of m) {
949
+ if (item && this.itemSizeCache.has(item.key)) {
950
+ snapshot.push({
951
+ index: item.index,
952
+ key: item.key,
953
+ start: item.start,
954
+ size: item.size,
955
+ end: item.end,
956
+ lane: item.lane
957
+ });
958
+ }
959
+ }
960
+ return snapshot;
961
+ };
813
962
  this._scrollToOffset = (offset, {
814
963
  adjustments,
815
964
  behavior
816
965
  }) => {
966
+ this._intendedScrollOffset = offset + (adjustments ?? 0);
817
967
  this.options.scrollToFn(offset, { behavior, adjustments }, this);
818
968
  };
819
969
  this.measure = () => {
820
- this.itemSizeCache = /* @__PURE__ */ new Map();
821
- this.laneAssignments = /* @__PURE__ */ new Map();
970
+ this.pendingMin = null;
971
+ this.itemSizeCache.clear();
972
+ this.laneAssignments.clear();
973
+ this.itemSizeCacheVersion++;
822
974
  this.notify(false);
823
975
  };
824
976
  this.setOptions(opts);
@@ -850,17 +1002,28 @@ class Virtualizer {
850
1002
  if (!targetChanged && approxEqual(targetOffset, this.getScrollOffset())) {
851
1003
  this.scrollState.stableFrames++;
852
1004
  if (this.scrollState.stableFrames >= STABLE_FRAMES) {
1005
+ if (this.getScrollOffset() !== targetOffset) {
1006
+ this._scrollToOffset(targetOffset, {
1007
+ adjustments: void 0,
1008
+ behavior: "auto"
1009
+ });
1010
+ }
853
1011
  this.scrollState = null;
854
1012
  return;
855
1013
  }
856
1014
  } else {
857
1015
  this.scrollState.stableFrames = 0;
858
1016
  if (targetChanged) {
1017
+ const viewport = this.getSize() || 600;
1018
+ const distance = Math.abs(targetOffset - this.getScrollOffset());
1019
+ const keepSmooth = this.scrollState.behavior === "smooth" && distance > viewport;
859
1020
  this.scrollState.lastTargetOffset = targetOffset;
860
- this.scrollState.behavior = "auto";
1021
+ if (!keepSmooth) {
1022
+ this.scrollState.behavior = "auto";
1023
+ }
861
1024
  this._scrollToOffset(targetOffset, {
862
1025
  adjustments: void 0,
863
- behavior: "auto"
1026
+ behavior: keepSmooth ? "smooth" : "auto"
864
1027
  });
865
1028
  }
866
1029
  }
@@ -889,25 +1052,22 @@ function calculateRange({
889
1052
  measurements,
890
1053
  outerSize,
891
1054
  scrollOffset,
892
- lanes
1055
+ lanes,
1056
+ flat
893
1057
  }) {
894
1058
  const lastIndex = measurements.length - 1;
895
- const getOffset = (index) => measurements[index].start;
1059
+ const getStart = flat ? (index) => flat[index * 2] : (index) => measurements[index].start;
1060
+ const getEnd = flat ? (index) => flat[index * 2] + flat[index * 2 + 1] : (index) => measurements[index].end;
896
1061
  if (measurements.length <= lanes) {
897
1062
  return {
898
1063
  startIndex: 0,
899
1064
  endIndex: lastIndex
900
1065
  };
901
1066
  }
902
- let startIndex = findNearestBinarySearch(
903
- 0,
904
- lastIndex,
905
- getOffset,
906
- scrollOffset
907
- );
1067
+ let startIndex = findNearestBinarySearch(0, lastIndex, getStart, scrollOffset);
908
1068
  let endIndex = startIndex;
909
1069
  if (lanes === 1) {
910
- while (endIndex < lastIndex && measurements[endIndex].end < scrollOffset + outerSize) {
1070
+ while (endIndex < lastIndex && getEnd(endIndex) < scrollOffset + outerSize) {
911
1071
  endIndex++;
912
1072
  }
913
1073
  } else if (lanes > 1) {
@@ -930,6 +1090,7 @@ function calculateRange({
930
1090
  }
931
1091
  export {
932
1092
  Virtualizer,
1093
+ _resetIOSDetectionForTests,
933
1094
  approxEqual,
934
1095
  debounce,
935
1096
  defaultKeyExtractor,