@tanstack/virtual-core 3.15.0 → 3.16.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.
@@ -4,12 +4,15 @@ export type { NoInfer, PartialKeys } from './utils.cjs';
4
4
  type ScrollDirection = 'forward' | 'backward';
5
5
  type ScrollAlignment = 'start' | 'center' | 'end' | 'auto';
6
6
  type ScrollBehavior = 'auto' | 'smooth' | 'instant';
7
+ type ScrollAnchor = 'start' | 'end';
8
+ type FollowOnAppend = boolean | ScrollBehavior;
7
9
  export interface ScrollToOptions {
8
10
  align?: ScrollAlignment;
9
11
  behavior?: ScrollBehavior;
10
12
  }
11
13
  type ScrollToOffsetOptions = ScrollToOptions;
12
14
  type ScrollToIndexOptions = ScrollToOptions;
15
+ type ScrollToEndOptions = Pick<ScrollToOptions, 'behavior'>;
13
16
  export interface Range {
14
17
  startIndex: number;
15
18
  endIndex: number;
@@ -74,6 +77,9 @@ export interface VirtualizerOptions<TScrollElement extends Element | Window, TIt
74
77
  indexAttribute?: string;
75
78
  initialMeasurementsCache?: Array<VirtualItem>;
76
79
  lanes?: number;
80
+ anchorTo?: ScrollAnchor;
81
+ followOnAppend?: FollowOnAppend;
82
+ scrollEndThreshold?: number;
77
83
  isScrollingResetDelay?: number;
78
84
  useScrollendEvent?: boolean;
79
85
  enabled?: boolean;
@@ -97,6 +103,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
97
103
  private prevLanes;
98
104
  private lanesChangedFlag;
99
105
  private lanesSettling;
106
+ private pendingScrollAnchor;
100
107
  scrollRect: Rect | null;
101
108
  scrollOffset: number | null;
102
109
  scrollDirection: ScrollDirection | null;
@@ -117,6 +124,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
117
124
  constructor(opts: VirtualizerOptions<TScrollElement, TItemElement>);
118
125
  setOptions: (opts: VirtualizerOptions<TScrollElement, TItemElement>) => void;
119
126
  private notify;
127
+ private applyScrollAdjustment;
120
128
  private maybeNotify;
121
129
  private cleanup;
122
130
  _didMount: () => () => void;
@@ -156,11 +164,15 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
156
164
  };
157
165
  getVirtualItemForOffset: (offset: number) => VirtualItem | undefined;
158
166
  private getMaxScrollOffset;
167
+ private getVirtualDistanceFromEnd;
168
+ getDistanceFromEnd: () => number;
169
+ isAtEnd: (threshold?: number) => boolean;
159
170
  getOffsetForAlignment: (toOffset: number, align: ScrollAlignment, itemSize?: number) => number;
160
171
  getOffsetForIndex: (index: number, align?: ScrollAlignment) => readonly [number, "auto"] | readonly [number, "start" | "center" | "end"] | undefined;
161
172
  scrollToOffset: (toOffset: number, { align, behavior }?: ScrollToOffsetOptions) => void;
162
173
  scrollToIndex: (index: number, { align: initialAlign, behavior, }?: ScrollToIndexOptions) => void;
163
174
  scrollBy: (delta: number, { behavior }?: ScrollToOffsetOptions) => void;
175
+ scrollToEnd: ({ behavior }?: ScrollToEndOptions) => void;
164
176
  getTotalSize: () => number;
165
177
  /**
166
178
  * Returns a snapshot of currently-measured items suitable for round-
@@ -4,12 +4,15 @@ export type { NoInfer, PartialKeys } from './utils.js';
4
4
  type ScrollDirection = 'forward' | 'backward';
5
5
  type ScrollAlignment = 'start' | 'center' | 'end' | 'auto';
6
6
  type ScrollBehavior = 'auto' | 'smooth' | 'instant';
7
+ type ScrollAnchor = 'start' | 'end';
8
+ type FollowOnAppend = boolean | ScrollBehavior;
7
9
  export interface ScrollToOptions {
8
10
  align?: ScrollAlignment;
9
11
  behavior?: ScrollBehavior;
10
12
  }
11
13
  type ScrollToOffsetOptions = ScrollToOptions;
12
14
  type ScrollToIndexOptions = ScrollToOptions;
15
+ type ScrollToEndOptions = Pick<ScrollToOptions, 'behavior'>;
13
16
  export interface Range {
14
17
  startIndex: number;
15
18
  endIndex: number;
@@ -74,6 +77,9 @@ export interface VirtualizerOptions<TScrollElement extends Element | Window, TIt
74
77
  indexAttribute?: string;
75
78
  initialMeasurementsCache?: Array<VirtualItem>;
76
79
  lanes?: number;
80
+ anchorTo?: ScrollAnchor;
81
+ followOnAppend?: FollowOnAppend;
82
+ scrollEndThreshold?: number;
77
83
  isScrollingResetDelay?: number;
78
84
  useScrollendEvent?: boolean;
79
85
  enabled?: boolean;
@@ -97,6 +103,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
97
103
  private prevLanes;
98
104
  private lanesChangedFlag;
99
105
  private lanesSettling;
106
+ private pendingScrollAnchor;
100
107
  scrollRect: Rect | null;
101
108
  scrollOffset: number | null;
102
109
  scrollDirection: ScrollDirection | null;
@@ -117,6 +124,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
117
124
  constructor(opts: VirtualizerOptions<TScrollElement, TItemElement>);
118
125
  setOptions: (opts: VirtualizerOptions<TScrollElement, TItemElement>) => void;
119
126
  private notify;
127
+ private applyScrollAdjustment;
120
128
  private maybeNotify;
121
129
  private cleanup;
122
130
  _didMount: () => () => void;
@@ -156,11 +164,15 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
156
164
  };
157
165
  getVirtualItemForOffset: (offset: number) => VirtualItem | undefined;
158
166
  private getMaxScrollOffset;
167
+ private getVirtualDistanceFromEnd;
168
+ getDistanceFromEnd: () => number;
169
+ isAtEnd: (threshold?: number) => boolean;
159
170
  getOffsetForAlignment: (toOffset: number, align: ScrollAlignment, itemSize?: number) => number;
160
171
  getOffsetForIndex: (index: number, align?: ScrollAlignment) => readonly [number, "auto"] | readonly [number, "start" | "center" | "end"] | undefined;
161
172
  scrollToOffset: (toOffset: number, { align, behavior }?: ScrollToOffsetOptions) => void;
162
173
  scrollToIndex: (index: number, { align: initialAlign, behavior, }?: ScrollToIndexOptions) => void;
163
174
  scrollBy: (delta: number, { behavior }?: ScrollToOffsetOptions) => void;
175
+ scrollToEnd: ({ behavior }?: ScrollToEndOptions) => void;
164
176
  getTotalSize: () => number;
165
177
  /**
166
178
  * Returns a snapshot of currently-measured items suitable for round-
package/dist/esm/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createLazyMeasurementsView } from "./lazy-measurements.js";
2
- import { memo, notUndefined, approxEqual, debounce } from "./utils.js";
2
+ import { memo, approxEqual, notUndefined, debounce } from "./utils.js";
3
3
  let _isIOSResult;
4
4
  const isIOSWebKit = () => {
5
5
  if (_isIOSResult !== void 0) return _isIOSResult;
@@ -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,36 @@ 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
+ if (prevOptions !== void 0 && prevOptions.enabled && merged.enabled && merged.anchorTo === "end" && this.scrollElement !== null) {
274
+ const prevCount = prevOptions.count;
275
+ const nextCount = merged.count;
276
+ const measurements = this.getMeasurements();
277
+ const prevFirstKey = prevCount > 0 ? ((_a = measurements[0]) == null ? void 0 : _a.key) ?? prevOptions.getItemKey(0) : null;
278
+ const prevLastKey = prevCount > 0 ? ((_b = measurements[prevCount - 1]) == null ? void 0 : _b.key) ?? prevOptions.getItemKey(prevCount - 1) : null;
279
+ const didCountChange = nextCount !== prevCount;
280
+ const didEdgeKeysChange = didCountChange || prevCount > 0 && nextCount > 0 && (merged.getItemKey(0) !== prevFirstKey || merged.getItemKey(nextCount - 1) !== prevLastKey);
281
+ if (didEdgeKeysChange) {
282
+ const item = prevCount > 0 ? this.getVirtualItemForOffset(this.getScrollOffset()) ?? measurements[0] : null;
283
+ if (item) {
284
+ anchor = [item.key, this.getScrollOffset() - item.start];
285
+ }
286
+ const behavior = merged.followOnAppend === true ? "auto" : merged.followOnAppend || null;
287
+ if (behavior && nextCount > prevCount && this.isAtEnd(prevOptions.scrollEndThreshold) && (prevCount === 0 || merged.getItemKey(nextCount - 1) !== prevLastKey)) {
288
+ followOnAppend = behavior;
289
+ }
290
+ }
291
+ }
265
292
  this.options = merged;
293
+ if (anchor || followOnAppend) {
294
+ this.pendingScrollAnchor = [
295
+ (anchor == null ? void 0 : anchor[0]) ?? null,
296
+ (anchor == null ? void 0 : anchor[1]) ?? 0,
297
+ followOnAppend
298
+ ];
299
+ }
266
300
  };
267
301
  this.notify = (sync) => {
268
302
  var _a, _b;
@@ -394,6 +428,28 @@ class Virtualizer {
394
428
  behavior: void 0
395
429
  });
396
430
  }
431
+ const anchor = this.pendingScrollAnchor;
432
+ this.pendingScrollAnchor = null;
433
+ if (anchor && this.scrollElement && this.options.enabled) {
434
+ const [key, offset, followOnAppend] = anchor;
435
+ if (key !== null) {
436
+ const { count, getItemKey } = this.options;
437
+ let index = 0;
438
+ while (index < count && getItemKey(index) !== key) {
439
+ index++;
440
+ }
441
+ const item = index < count ? this.getMeasurements()[index] : void 0;
442
+ if (item) {
443
+ const delta = item.start + offset - this.getScrollOffset();
444
+ if (!approxEqual(delta, 0)) {
445
+ this.applyScrollAdjustment(delta);
446
+ }
447
+ }
448
+ }
449
+ if (followOnAppend) {
450
+ this.scrollToEnd({ behavior: followOnAppend });
451
+ }
452
+ }
397
453
  };
398
454
  this._flushIosDeferredIfReady = () => {
399
455
  if (this._iosDeferredAdjustment === 0) return;
@@ -719,7 +775,7 @@ class Virtualizer {
719
775
  }
720
776
  };
721
777
  this.resizeItem = (index, size) => {
722
- var _a;
778
+ var _a, _b;
723
779
  if (index < 0 || index >= this.options.count) return;
724
780
  let cachedSize;
725
781
  let itemStart;
@@ -739,7 +795,9 @@ class Virtualizer {
739
795
  const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
740
796
  const delta = size - itemSize;
741
797
  if (delta !== 0) {
742
- if (((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
798
+ const wasAtEnd = this.options.anchorTo === "end" && ((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
799
+ const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
800
+ const shouldAdjustScroll = ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
743
801
  // The callback expects a VirtualItem; build one lazily only
744
802
  // when the consumer actually supplied a custom predicate.
745
803
  this.measurementsCache[index] ?? {
@@ -760,24 +818,17 @@ class Virtualizer {
760
818
  // jank reported across many issues. Users who want the old
761
819
  // behavior can pass shouldAdjustScrollPositionOnItemSizeChange.
762
820
  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
- }
821
+ ));
776
822
  if (this.pendingMin === null || index < this.pendingMin) {
777
823
  this.pendingMin = index;
778
824
  }
779
825
  this.itemSizeCache.set(key, size);
780
826
  this.itemSizeCacheVersion++;
827
+ if (wasAtEnd) {
828
+ this.applyScrollAdjustment(this.getTotalSize() - prevTotalSize);
829
+ } else if (shouldAdjustScroll) {
830
+ this.applyScrollAdjustment(delta);
831
+ }
781
832
  this.notify(false);
782
833
  }
783
834
  };
@@ -821,6 +872,18 @@ class Virtualizer {
821
872
  return this.options.horizontal ? doc.scrollWidth - this.scrollElement.innerWidth : doc.scrollHeight - this.scrollElement.innerHeight;
822
873
  }
823
874
  };
875
+ this.getVirtualDistanceFromEnd = () => {
876
+ return Math.max(
877
+ this.getTotalSize() - this.getSize() - this.getScrollOffset(),
878
+ 0
879
+ );
880
+ };
881
+ this.getDistanceFromEnd = () => {
882
+ return Math.max(this.getMaxScrollOffset() - this.getScrollOffset(), 0);
883
+ };
884
+ this.isAtEnd = (threshold = this.options.scrollEndThreshold) => {
885
+ return this.getDistanceFromEnd() <= threshold;
886
+ };
824
887
  this.getOffsetForAlignment = (toOffset, align, itemSize = 0) => {
825
888
  if (!this.scrollElement) return 0;
826
889
  const size = this.getSize();
@@ -910,6 +973,18 @@ class Virtualizer {
910
973
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
911
974
  this.scheduleScrollReconcile();
912
975
  };
976
+ this.scrollToEnd = ({ behavior = "auto" } = {}) => {
977
+ if (this.options.count > 0) {
978
+ this.scrollToIndex(this.options.count - 1, {
979
+ align: "end",
980
+ behavior
981
+ });
982
+ return;
983
+ }
984
+ this.scrollToOffset(Math.max(this.getTotalSize() - this.getSize(), 0), {
985
+ behavior
986
+ });
987
+ };
913
988
  this.getTotalSize = () => {
914
989
  var _a;
915
990
  const measurements = this.getMeasurements();
@@ -975,6 +1050,20 @@ class Virtualizer {
975
1050
  };
976
1051
  this.setOptions(opts);
977
1052
  }
1053
+ applyScrollAdjustment(delta, behavior) {
1054
+ if (delta === 0) return;
1055
+ if (process.env.NODE_ENV !== "production" && this.options.debug) {
1056
+ console.info("correction", delta);
1057
+ }
1058
+ if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
1059
+ this._iosDeferredAdjustment += delta;
1060
+ } else {
1061
+ this._scrollToOffset(this.getScrollOffset(), {
1062
+ adjustments: this.scrollAdjustments += delta,
1063
+ behavior
1064
+ });
1065
+ }
1066
+ }
978
1067
  scheduleScrollReconcile() {
979
1068
  if (!this.targetWindow) {
980
1069
  this.scrollState = null;