@tanstack/virtual-core 3.15.0 → 3.16.1

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.
@@ -166,6 +166,7 @@ class Virtualizer {
166
166
  this.prevLanes = void 0;
167
167
  this.lanesChangedFlag = false;
168
168
  this.lanesSettling = false;
169
+ this.pendingScrollAnchor = null;
169
170
  this.scrollRect = null;
170
171
  this.scrollOffset = null;
171
172
  this.scrollDirection = null;
@@ -233,6 +234,7 @@ class Virtualizer {
233
234
  })();
234
235
  this.range = null;
235
236
  this.setOptions = (opts2) => {
237
+ var _a, _b;
236
238
  const merged = {
237
239
  debug: false,
238
240
  initialOffset: 0,
@@ -253,6 +255,9 @@ class Virtualizer {
253
255
  indexAttribute: "data-index",
254
256
  initialMeasurementsCache: [],
255
257
  lanes: 1,
258
+ anchorTo: "start",
259
+ followOnAppend: false,
260
+ scrollEndThreshold: 1,
256
261
  isScrollingResetDelay: 150,
257
262
  enabled: true,
258
263
  isRtl: false,
@@ -264,7 +269,65 @@ class Virtualizer {
264
269
  const v = opts2[key];
265
270
  if (v !== void 0) merged[key] = v;
266
271
  }
272
+ const prevOptions = this.options;
273
+ let anchor = null;
274
+ let followOnAppend = null;
275
+ let edgeKeysChanged = false;
276
+ if (prevOptions !== void 0 && prevOptions.enabled && merged.enabled && merged.anchorTo === "end" && this.scrollElement !== null) {
277
+ const prevCount = prevOptions.count;
278
+ const nextCount = merged.count;
279
+ const measurements = this.getMeasurements();
280
+ const prevFirstKey = prevCount > 0 ? ((_a = measurements[0]) == null ? void 0 : _a.key) ?? prevOptions.getItemKey(0) : null;
281
+ const prevLastKey = prevCount > 0 ? ((_b = measurements[prevCount - 1]) == null ? void 0 : _b.key) ?? prevOptions.getItemKey(prevCount - 1) : null;
282
+ const didCountChange = nextCount !== prevCount;
283
+ const didEdgeKeysChange = didCountChange || prevCount > 0 && nextCount > 0 && (merged.getItemKey(0) !== prevFirstKey || merged.getItemKey(nextCount - 1) !== prevLastKey);
284
+ if (didEdgeKeysChange) {
285
+ edgeKeysChanged = true;
286
+ const item = prevCount > 0 ? this.getVirtualItemForOffset(this.getScrollOffset()) ?? measurements[0] : null;
287
+ if (item) {
288
+ anchor = [item.key, this.getScrollOffset() - item.start];
289
+ }
290
+ const behavior = merged.followOnAppend === true ? "auto" : merged.followOnAppend || null;
291
+ if (behavior && nextCount > prevCount && this.isAtEnd(prevOptions.scrollEndThreshold) && (prevCount === 0 || merged.getItemKey(nextCount - 1) !== prevLastKey)) {
292
+ followOnAppend = behavior;
293
+ }
294
+ }
295
+ }
267
296
  this.options = merged;
297
+ if (edgeKeysChanged) {
298
+ this.pendingMin = 0;
299
+ this.itemSizeCacheVersion++;
300
+ }
301
+ let anchorResolved = false;
302
+ let anchorDelta = 0;
303
+ if (anchor && this.scrollOffset !== null) {
304
+ const [anchorKey, anchorOffset] = anchor;
305
+ const newMeasurements = this.getMeasurements();
306
+ const { count, getItemKey } = this.options;
307
+ let idx = 0;
308
+ while (idx < count && getItemKey(idx) !== anchorKey) {
309
+ idx++;
310
+ }
311
+ if (idx < count) {
312
+ const anchorItem = newMeasurements[idx];
313
+ if (anchorItem) {
314
+ const newOffset = anchorItem.start + anchorOffset;
315
+ if (newOffset !== this.scrollOffset) {
316
+ anchorDelta = newOffset - this.scrollOffset;
317
+ this.scrollOffset = newOffset;
318
+ anchorResolved = true;
319
+ }
320
+ }
321
+ }
322
+ }
323
+ if (anchorResolved || followOnAppend) {
324
+ this.pendingScrollAnchor = [
325
+ anchorResolved ? anchor[0] : null,
326
+ anchorResolved ? anchor[1] : 0,
327
+ followOnAppend,
328
+ anchorDelta
329
+ ];
330
+ }
268
331
  };
269
332
  this.notify = (sync) => {
270
333
  var _a, _b;
@@ -396,6 +459,26 @@ class Virtualizer {
396
459
  behavior: void 0
397
460
  });
398
461
  }
462
+ const anchor = this.pendingScrollAnchor;
463
+ this.pendingScrollAnchor = null;
464
+ if (anchor && this.scrollElement && this.options.enabled) {
465
+ const [key, _offset, followOnAppend, anchorDelta] = anchor;
466
+ if (key !== null && !followOnAppend) {
467
+ if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
468
+ if (anchorDelta !== 0) {
469
+ this._iosDeferredAdjustment += anchorDelta;
470
+ }
471
+ } else {
472
+ this._scrollToOffset(this.getScrollOffset(), {
473
+ adjustments: void 0,
474
+ behavior: void 0
475
+ });
476
+ }
477
+ }
478
+ if (followOnAppend) {
479
+ this.scrollToEnd({ behavior: followOnAppend });
480
+ }
481
+ }
399
482
  };
400
483
  this._flushIosDeferredIfReady = () => {
401
484
  if (this._iosDeferredAdjustment === 0) return;
@@ -721,7 +804,7 @@ class Virtualizer {
721
804
  }
722
805
  };
723
806
  this.resizeItem = (index, size) => {
724
- var _a;
807
+ var _a, _b;
725
808
  if (index < 0 || index >= this.options.count) return;
726
809
  let cachedSize;
727
810
  let itemStart;
@@ -741,7 +824,9 @@ class Virtualizer {
741
824
  const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
742
825
  const delta = size - itemSize;
743
826
  if (delta !== 0) {
744
- if (((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
827
+ const wasAtEnd = this.options.anchorTo === "end" && ((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
828
+ const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
829
+ const shouldAdjustScroll = ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
745
830
  // The callback expects a VirtualItem; build one lazily only
746
831
  // when the consumer actually supplied a custom predicate.
747
832
  this.measurementsCache[index] ?? {
@@ -762,24 +847,17 @@ class Virtualizer {
762
847
  // jank reported across many issues. Users who want the old
763
848
  // behavior can pass shouldAdjustScrollPositionOnItemSizeChange.
764
849
  itemStart < this.getScrollOffset() + this.scrollAdjustments && this.scrollDirection !== "backward"
765
- ))) {
766
- if (process.env.NODE_ENV !== "production" && this.options.debug) {
767
- console.info("correction", delta);
768
- }
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
- }
777
- }
850
+ ));
778
851
  if (this.pendingMin === null || index < this.pendingMin) {
779
852
  this.pendingMin = index;
780
853
  }
781
854
  this.itemSizeCache.set(key, size);
782
855
  this.itemSizeCacheVersion++;
856
+ if (wasAtEnd) {
857
+ this.applyScrollAdjustment(this.getTotalSize() - prevTotalSize);
858
+ } else if (shouldAdjustScroll) {
859
+ this.applyScrollAdjustment(delta);
860
+ }
783
861
  this.notify(false);
784
862
  }
785
863
  };
@@ -823,6 +901,18 @@ class Virtualizer {
823
901
  return this.options.horizontal ? doc.scrollWidth - this.scrollElement.innerWidth : doc.scrollHeight - this.scrollElement.innerHeight;
824
902
  }
825
903
  };
904
+ this.getVirtualDistanceFromEnd = () => {
905
+ return Math.max(
906
+ this.getTotalSize() - this.getSize() - this.getScrollOffset(),
907
+ 0
908
+ );
909
+ };
910
+ this.getDistanceFromEnd = () => {
911
+ return Math.max(this.getMaxScrollOffset() - this.getScrollOffset(), 0);
912
+ };
913
+ this.isAtEnd = (threshold = this.options.scrollEndThreshold) => {
914
+ return this.getDistanceFromEnd() <= threshold;
915
+ };
826
916
  this.getOffsetForAlignment = (toOffset, align, itemSize = 0) => {
827
917
  if (!this.scrollElement) return 0;
828
918
  const size = this.getSize();
@@ -912,6 +1002,18 @@ class Virtualizer {
912
1002
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
913
1003
  this.scheduleScrollReconcile();
914
1004
  };
1005
+ this.scrollToEnd = ({ behavior = "auto" } = {}) => {
1006
+ if (this.options.count > 0) {
1007
+ this.scrollToIndex(this.options.count - 1, {
1008
+ align: "end",
1009
+ behavior
1010
+ });
1011
+ return;
1012
+ }
1013
+ this.scrollToOffset(Math.max(this.getTotalSize() - this.getSize(), 0), {
1014
+ behavior
1015
+ });
1016
+ };
915
1017
  this.getTotalSize = () => {
916
1018
  var _a;
917
1019
  const measurements = this.getMeasurements();
@@ -977,6 +1079,20 @@ class Virtualizer {
977
1079
  };
978
1080
  this.setOptions(opts);
979
1081
  }
1082
+ applyScrollAdjustment(delta, behavior) {
1083
+ if (delta === 0) return;
1084
+ if (process.env.NODE_ENV !== "production" && this.options.debug) {
1085
+ console.info("correction", delta);
1086
+ }
1087
+ if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
1088
+ this._iosDeferredAdjustment += delta;
1089
+ } else {
1090
+ this._scrollToOffset(this.getScrollOffset(), {
1091
+ adjustments: this.scrollAdjustments += delta,
1092
+ behavior
1093
+ });
1094
+ }
1095
+ }
980
1096
  scheduleScrollReconcile() {
981
1097
  if (!this.targetWindow) {
982
1098
  this.scrollState = null;