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