@tanstack/virtual-core 3.17.7 → 3.17.9

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.
@@ -114,6 +114,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
114
114
  private _iosJustTouchEnded;
115
115
  private _iosTouchEndTimerId;
116
116
  private _intendedScrollOffset;
117
+ private _clampedAdjustment;
117
118
  shouldAdjustScrollPositionOnItemSizeChange: undefined | ((item: VirtualItem, delta: number, instance: Virtualizer<TScrollElement, TItemElement>) => boolean);
118
119
  elementsCache: Map<Key, TItemElement>;
119
120
  private now;
@@ -130,6 +131,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
130
131
  private cleanup;
131
132
  _didMount: () => () => void;
132
133
  _willUpdate: () => void;
134
+ private _retryClampedAdjustment;
133
135
  private _flushIosDeferredIfReady;
134
136
  private rafId;
135
137
  private scheduleScrollReconcile;
@@ -137,6 +139,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
137
139
  private getSize;
138
140
  private getScrollOffset;
139
141
  private getMeasurementOptions;
142
+ private isIndexInRange;
140
143
  private getMeasurements;
141
144
  calculateRange: {
142
145
  (): {
@@ -62,10 +62,20 @@ function notUndefined(value, msg) {
62
62
  const approxEqual = (a, b) => Math.abs(a - b) < 1.01;
63
63
  const debounce = (targetWindow, fn, ms) => {
64
64
  let timeoutId;
65
- return function(...args) {
66
- targetWindow.clearTimeout(timeoutId);
67
- timeoutId = targetWindow.setTimeout(() => fn.apply(this, args), ms);
68
- };
65
+ return Object.assign(
66
+ function(...args) {
67
+ targetWindow.clearTimeout(timeoutId);
68
+ timeoutId = targetWindow.setTimeout(() => fn.apply(this, args), ms);
69
+ },
70
+ {
71
+ // The handle is closure-local, so a caller that has already
72
+ // unsubscribed has no way to stop a queued call. Teardown paths use
73
+ // this to drop the pending invocation instead of letting it land.
74
+ cancel: () => {
75
+ targetWindow.clearTimeout(timeoutId);
76
+ }
77
+ }
78
+ );
69
79
  };
70
80
  exports.approxEqual = approxEqual;
71
81
  exports.debounce = debounce;
@@ -1 +1 @@
1
- {"version":3,"file":"utils.cjs","sources":["../../src/utils.ts"],"sourcesContent":["export type NoInfer<A extends any> = [A][A extends any ? 0 : never]\n\nexport type PartialKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>\n\nexport function memo<TDeps extends ReadonlyArray<any>, TResult>(\n getDeps: () => [...TDeps],\n fn: (...args: NoInfer<[...TDeps]>) => TResult,\n opts: {\n key: false | string\n debug?: () => boolean\n onChange?: (result: TResult) => void\n initialDeps?: TDeps\n skipInitialOnChange?: boolean\n },\n) {\n let deps = opts.initialDeps ?? []\n let result: TResult | undefined\n let isInitial = true\n\n function memoizedFunction(): TResult {\n // Debug-only timing. In production builds, `process.env.NODE_ENV !==\n // 'production'` is constant-folded to `false` by downstream minifiers\n // (Terser/esbuild/swc with `define`), which DCEs the entire block.\n const debugEnabled =\n process.env.NODE_ENV !== 'production' && !!opts.key && !!opts.debug?.()\n let depTime = 0\n if (debugEnabled) depTime = Date.now()\n\n const newDeps = getDeps()\n\n const depsChanged =\n newDeps.length !== deps.length ||\n newDeps.some((dep: any, index: number) => deps[index] !== dep)\n\n if (!depsChanged) {\n return result!\n }\n\n deps = newDeps\n\n let resultTime = 0\n if (debugEnabled) resultTime = Date.now()\n\n result = fn(...newDeps)\n\n if (debugEnabled) {\n const depEndTime = Math.round((Date.now() - depTime) * 100) / 100\n const resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100\n const resultFpsPercentage = resultEndTime / 16\n\n const pad = (str: number | string, num: number) => {\n str = String(str)\n while (str.length < num) {\n str = ' ' + str\n }\n return str\n }\n\n console.info(\n `%c⏱ ${pad(resultEndTime, 5)} /${pad(depEndTime, 5)} ms`,\n `\n font-size: .6rem;\n font-weight: bold;\n color: hsl(${Math.max(\n 0,\n Math.min(120 - 120 * resultFpsPercentage, 120),\n )}deg 100% 31%);`,\n opts?.key,\n )\n }\n\n if (opts?.onChange && !(isInitial && opts.skipInitialOnChange)) {\n opts.onChange(result)\n }\n\n isInitial = false\n\n return result\n }\n\n // Attach updateDeps to the function itself\n memoizedFunction.updateDeps = (newDeps: [...TDeps]) => {\n deps = newDeps\n }\n\n return memoizedFunction\n}\n\nexport function notUndefined<T>(value: T | undefined, msg?: string): T {\n if (value === undefined) {\n throw new Error(`Unexpected undefined${msg ? `: ${msg}` : ''}`)\n } else {\n return value\n }\n}\n\nexport const approxEqual = (a: number, b: number) => Math.abs(a - b) < 1.01\n\nexport const debounce = (\n targetWindow: Window & typeof globalThis,\n fn: Function,\n ms: number,\n) => {\n let timeoutId: number\n return function (this: any, ...args: Array<any>) {\n targetWindow.clearTimeout(timeoutId)\n timeoutId = targetWindow.setTimeout(() => fn.apply(this, args), ms)\n }\n}\n"],"names":[],"mappings":";;AAIO,SAAS,KACd,SACA,IACA,MAOA;AACA,MAAI,OAAO,KAAK,eAAe,CAAA;AAC/B,MAAI;AACJ,MAAI,YAAY;AAEhB,WAAS,mBAA4B;;AAInC,UAAM,eACJ,QAAQ,IAAI,aAAa,gBAAgB,CAAC,CAAC,KAAK,OAAO,CAAC,GAAC,UAAK,UAAL;AAC3D,QAAI,UAAU;AACd,QAAI,aAAc,WAAU,KAAK,IAAA;AAEjC,UAAM,UAAU,QAAA;AAEhB,UAAM,cACJ,QAAQ,WAAW,KAAK,UACxB,QAAQ,KAAK,CAAC,KAAU,UAAkB,KAAK,KAAK,MAAM,GAAG;AAE/D,QAAI,CAAC,aAAa;AAChB,aAAO;AAAA,IACT;AAEA,WAAO;AAEP,QAAI,aAAa;AACjB,QAAI,aAAc,cAAa,KAAK,IAAA;AAEpC,aAAS,GAAG,GAAG,OAAO;AAEtB,QAAI,cAAc;AAChB,YAAM,aAAa,KAAK,OAAO,KAAK,QAAQ,WAAW,GAAG,IAAI;AAC9D,YAAM,gBAAgB,KAAK,OAAO,KAAK,QAAQ,cAAc,GAAG,IAAI;AACpE,YAAM,sBAAsB,gBAAgB;AAE5C,YAAM,MAAM,CAAC,KAAsB,QAAgB;AACjD,cAAM,OAAO,GAAG;AAChB,eAAO,IAAI,SAAS,KAAK;AACvB,gBAAM,MAAM;AAAA,QACd;AACA,eAAO;AAAA,MACT;AAEA,cAAQ;AAAA,QACN,OAAO,IAAI,eAAe,CAAC,CAAC,KAAK,IAAI,YAAY,CAAC,CAAC;AAAA,QACnD;AAAA;AAAA;AAAA,yBAGiB,KAAK;AAAA,UAChB;AAAA,UACA,KAAK,IAAI,MAAM,MAAM,qBAAqB,GAAG;AAAA,QAAA,CAC9C;AAAA,QACL,6BAAM;AAAA,MAAA;AAAA,IAEV;AAEA,SAAI,6BAAM,aAAY,EAAE,aAAa,KAAK,sBAAsB;AAC9D,WAAK,SAAS,MAAM;AAAA,IACtB;AAEA,gBAAY;AAEZ,WAAO;AAAA,EACT;AAGA,mBAAiB,aAAa,CAAC,YAAwB;AACrD,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAEO,SAAS,aAAgB,OAAsB,KAAiB;AACrE,MAAI,UAAU,QAAW;AACvB,UAAM,IAAI,MAAM,uBAAuB,MAAM,KAAK,GAAG,KAAK,EAAE,EAAE;AAAA,EAChE,OAAO;AACL,WAAO;AAAA,EACT;AACF;AAEO,MAAM,cAAc,CAAC,GAAW,MAAc,KAAK,IAAI,IAAI,CAAC,IAAI;AAEhE,MAAM,WAAW,CACtB,cACA,IACA,OACG;AACH,MAAI;AACJ,SAAO,YAAwB,MAAkB;AAC/C,iBAAa,aAAa,SAAS;AACnC,gBAAY,aAAa,WAAW,MAAM,GAAG,MAAM,MAAM,IAAI,GAAG,EAAE;AAAA,EACpE;AACF;;;;;"}
1
+ {"version":3,"file":"utils.cjs","sources":["../../src/utils.ts"],"sourcesContent":["export type NoInfer<A extends any> = [A][A extends any ? 0 : never]\n\nexport type PartialKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>\n\nexport function memo<TDeps extends ReadonlyArray<any>, TResult>(\n getDeps: () => [...TDeps],\n fn: (...args: NoInfer<[...TDeps]>) => TResult,\n opts: {\n key: false | string\n debug?: () => boolean\n onChange?: (result: TResult) => void\n initialDeps?: TDeps\n skipInitialOnChange?: boolean\n },\n) {\n let deps = opts.initialDeps ?? []\n let result: TResult | undefined\n let isInitial = true\n\n function memoizedFunction(): TResult {\n // Debug-only timing. In production builds, `process.env.NODE_ENV !==\n // 'production'` is constant-folded to `false` by downstream minifiers\n // (Terser/esbuild/swc with `define`), which DCEs the entire block.\n const debugEnabled =\n process.env.NODE_ENV !== 'production' && !!opts.key && !!opts.debug?.()\n let depTime = 0\n if (debugEnabled) depTime = Date.now()\n\n const newDeps = getDeps()\n\n const depsChanged =\n newDeps.length !== deps.length ||\n newDeps.some((dep: any, index: number) => deps[index] !== dep)\n\n if (!depsChanged) {\n return result!\n }\n\n deps = newDeps\n\n let resultTime = 0\n if (debugEnabled) resultTime = Date.now()\n\n result = fn(...newDeps)\n\n if (debugEnabled) {\n const depEndTime = Math.round((Date.now() - depTime) * 100) / 100\n const resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100\n const resultFpsPercentage = resultEndTime / 16\n\n const pad = (str: number | string, num: number) => {\n str = String(str)\n while (str.length < num) {\n str = ' ' + str\n }\n return str\n }\n\n console.info(\n `%c⏱ ${pad(resultEndTime, 5)} /${pad(depEndTime, 5)} ms`,\n `\n font-size: .6rem;\n font-weight: bold;\n color: hsl(${Math.max(\n 0,\n Math.min(120 - 120 * resultFpsPercentage, 120),\n )}deg 100% 31%);`,\n opts?.key,\n )\n }\n\n if (opts?.onChange && !(isInitial && opts.skipInitialOnChange)) {\n opts.onChange(result)\n }\n\n isInitial = false\n\n return result\n }\n\n // Attach updateDeps to the function itself\n memoizedFunction.updateDeps = (newDeps: [...TDeps]) => {\n deps = newDeps\n }\n\n return memoizedFunction\n}\n\nexport function notUndefined<T>(value: T | undefined, msg?: string): T {\n if (value === undefined) {\n throw new Error(`Unexpected undefined${msg ? `: ${msg}` : ''}`)\n } else {\n return value\n }\n}\n\nexport const approxEqual = (a: number, b: number) => Math.abs(a - b) < 1.01\n\nexport const debounce = (\n targetWindow: Window & typeof globalThis,\n fn: Function,\n ms: number,\n) => {\n let timeoutId: number\n return Object.assign(\n function (this: any, ...args: Array<any>) {\n targetWindow.clearTimeout(timeoutId)\n timeoutId = targetWindow.setTimeout(() => fn.apply(this, args), ms)\n },\n {\n // The handle is closure-local, so a caller that has already\n // unsubscribed has no way to stop a queued call. Teardown paths use\n // this to drop the pending invocation instead of letting it land.\n cancel: () => {\n targetWindow.clearTimeout(timeoutId)\n },\n },\n )\n}\n"],"names":[],"mappings":";;AAIO,SAAS,KACd,SACA,IACA,MAOA;AACA,MAAI,OAAO,KAAK,eAAe,CAAA;AAC/B,MAAI;AACJ,MAAI,YAAY;AAEhB,WAAS,mBAA4B;;AAInC,UAAM,eACJ,QAAQ,IAAI,aAAa,gBAAgB,CAAC,CAAC,KAAK,OAAO,CAAC,GAAC,UAAK,UAAL;AAC3D,QAAI,UAAU;AACd,QAAI,aAAc,WAAU,KAAK,IAAA;AAEjC,UAAM,UAAU,QAAA;AAEhB,UAAM,cACJ,QAAQ,WAAW,KAAK,UACxB,QAAQ,KAAK,CAAC,KAAU,UAAkB,KAAK,KAAK,MAAM,GAAG;AAE/D,QAAI,CAAC,aAAa;AAChB,aAAO;AAAA,IACT;AAEA,WAAO;AAEP,QAAI,aAAa;AACjB,QAAI,aAAc,cAAa,KAAK,IAAA;AAEpC,aAAS,GAAG,GAAG,OAAO;AAEtB,QAAI,cAAc;AAChB,YAAM,aAAa,KAAK,OAAO,KAAK,QAAQ,WAAW,GAAG,IAAI;AAC9D,YAAM,gBAAgB,KAAK,OAAO,KAAK,QAAQ,cAAc,GAAG,IAAI;AACpE,YAAM,sBAAsB,gBAAgB;AAE5C,YAAM,MAAM,CAAC,KAAsB,QAAgB;AACjD,cAAM,OAAO,GAAG;AAChB,eAAO,IAAI,SAAS,KAAK;AACvB,gBAAM,MAAM;AAAA,QACd;AACA,eAAO;AAAA,MACT;AAEA,cAAQ;AAAA,QACN,OAAO,IAAI,eAAe,CAAC,CAAC,KAAK,IAAI,YAAY,CAAC,CAAC;AAAA,QACnD;AAAA;AAAA;AAAA,yBAGiB,KAAK;AAAA,UAChB;AAAA,UACA,KAAK,IAAI,MAAM,MAAM,qBAAqB,GAAG;AAAA,QAAA,CAC9C;AAAA,QACL,6BAAM;AAAA,MAAA;AAAA,IAEV;AAEA,SAAI,6BAAM,aAAY,EAAE,aAAa,KAAK,sBAAsB;AAC9D,WAAK,SAAS,MAAM;AAAA,IACtB;AAEA,gBAAY;AAEZ,WAAO;AAAA,EACT;AAGA,mBAAiB,aAAa,CAAC,YAAwB;AACrD,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAEO,SAAS,aAAgB,OAAsB,KAAiB;AACrE,MAAI,UAAU,QAAW;AACvB,UAAM,IAAI,MAAM,uBAAuB,MAAM,KAAK,GAAG,KAAK,EAAE,EAAE;AAAA,EAChE,OAAO;AACL,WAAO;AAAA,EACT;AACF;AAEO,MAAM,cAAc,CAAC,GAAW,MAAc,KAAK,IAAI,IAAI,CAAC,IAAI;AAEhE,MAAM,WAAW,CACtB,cACA,IACA,OACG;AACH,MAAI;AACJ,SAAO,OAAO;AAAA,IACZ,YAAwB,MAAkB;AACxC,mBAAa,aAAa,SAAS;AACnC,kBAAY,aAAa,WAAW,MAAM,GAAG,MAAM,MAAM,IAAI,GAAG,EAAE;AAAA,IACpE;AAAA,IACA;AAAA;AAAA;AAAA;AAAA,MAIE,QAAQ,MAAM;AACZ,qBAAa,aAAa,SAAS;AAAA,MACrC;AAAA,IAAA;AAAA,EACF;AAEJ;;;;;"}
@@ -12,4 +12,6 @@ export declare function memo<TDeps extends ReadonlyArray<any>, TResult>(getDeps:
12
12
  };
13
13
  export declare function notUndefined<T>(value: T | undefined, msg?: string): T;
14
14
  export declare const approxEqual: (a: number, b: number) => boolean;
15
- export declare const debounce: (targetWindow: Window & typeof globalThis, fn: Function, ms: number) => (this: any, ...args: Array<any>) => void;
15
+ export declare const debounce: (targetWindow: Window & typeof globalThis, fn: Function, ms: number) => ((this: any, ...args: Array<any>) => void) & {
16
+ cancel: () => void;
17
+ };
@@ -114,6 +114,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
114
114
  private _iosJustTouchEnded;
115
115
  private _iosTouchEndTimerId;
116
116
  private _intendedScrollOffset;
117
+ private _clampedAdjustment;
117
118
  shouldAdjustScrollPositionOnItemSizeChange: undefined | ((item: VirtualItem, delta: number, instance: Virtualizer<TScrollElement, TItemElement>) => boolean);
118
119
  elementsCache: Map<Key, TItemElement>;
119
120
  private now;
@@ -130,6 +131,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
130
131
  private cleanup;
131
132
  _didMount: () => () => void;
132
133
  _willUpdate: () => void;
134
+ private _retryClampedAdjustment;
133
135
  private _flushIosDeferredIfReady;
134
136
  private rafId;
135
137
  private scheduleScrollReconcile;
@@ -137,6 +139,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
137
139
  private getSize;
138
140
  private getScrollOffset;
139
141
  private getMeasurementOptions;
142
+ private isIndexInRange;
140
143
  private getMeasurements;
141
144
  calculateRange: {
142
145
  (): {
package/dist/esm/index.js CHANGED
@@ -113,6 +113,7 @@ const observeOffset = (instance, cb, readOffset) => {
113
113
  if (registerScrollendEvent) {
114
114
  element.removeEventListener("scrollend", endHandler);
115
115
  }
116
+ fallback == null ? void 0 : fallback.cancel();
116
117
  };
117
118
  };
118
119
  const observeElementOffset = (instance, cb) => observeOffset(instance, cb, (el) => {
@@ -187,6 +188,7 @@ class Virtualizer {
187
188
  this._iosJustTouchEnded = false;
188
189
  this._iosTouchEndTimerId = null;
189
190
  this._intendedScrollOffset = null;
191
+ this._clampedAdjustment = null;
190
192
  this.elementsCache = /* @__PURE__ */ new Map();
191
193
  this.now = () => {
192
194
  var _a, _b, _c;
@@ -216,6 +218,7 @@ class Virtualizer {
216
218
  }
217
219
  return;
218
220
  }
221
+ if (!this.isIndexInRange(index)) return;
219
222
  if (this.shouldMeasureDuringScroll(index)) {
220
223
  this.resizeItem(
221
224
  index,
@@ -376,9 +379,12 @@ class Virtualizer {
376
379
  this.rafId = null;
377
380
  }
378
381
  this.scrollState = null;
382
+ this.isScrolling = false;
383
+ this.scrollDirection = null;
379
384
  this._iosDeferredAdjustment = 0;
380
385
  this._iosTouching = false;
381
386
  this._iosJustTouchEnded = false;
387
+ this._clampedAdjustment = null;
382
388
  this.scrollElement = null;
383
389
  this.targetWindow = null;
384
390
  };
@@ -420,6 +426,9 @@ class Virtualizer {
420
426
  offset = this._intendedScrollOffset;
421
427
  }
422
428
  this._intendedScrollOffset = null;
429
+ if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
430
+ this._clampedAdjustment = null;
431
+ }
423
432
  this.scrollAdjustments = 0;
424
433
  const prevOffset = this.getScrollOffset();
425
434
  this.scrollDirection = isScrolling ? prevOffset === offset ? this.scrollDirection : prevOffset < offset ? "forward" : "backward" : null;
@@ -498,6 +507,21 @@ class Virtualizer {
498
507
  this.scrollToEnd({ behavior: followOnAppend });
499
508
  }
500
509
  }
510
+ this._retryClampedAdjustment();
511
+ };
512
+ this._retryClampedAdjustment = () => {
513
+ if (this._clampedAdjustment === null || !this.scrollElement || !this.options.enabled) {
514
+ return;
515
+ }
516
+ const { target, maxAtWrite } = this._clampedAdjustment;
517
+ const max = this.getMaxScrollOffset();
518
+ if (max > maxAtWrite + 0.5) {
519
+ this._clampedAdjustment = target > max + 0.5 ? { target, maxAtWrite: max } : null;
520
+ this._scrollToOffset(target, {
521
+ adjustments: void 0,
522
+ behavior: void 0
523
+ });
524
+ }
501
525
  };
502
526
  this._flushIosDeferredIfReady = () => {
503
527
  if (this._iosDeferredAdjustment === 0) return;
@@ -568,6 +592,7 @@ class Virtualizer {
568
592
  key: false
569
593
  }
570
594
  );
595
+ this.isIndexInRange = (index) => index >= 0 && index < this.options.count;
571
596
  this.getMeasurements = memo(
572
597
  () => [this.getMeasurementOptions(), this.itemSizeCacheVersion],
573
598
  ({
@@ -814,6 +839,7 @@ class Virtualizer {
814
839
  return;
815
840
  }
816
841
  const index = this.indexFromElement(node);
842
+ if (!this.isIndexInRange(index)) return;
817
843
  const key = this.options.getItemKey(index);
818
844
  const prevNode = this.elementsCache.get(key);
819
845
  if (prevNode !== node) {
@@ -829,7 +855,7 @@ class Virtualizer {
829
855
  };
830
856
  this.resizeItem = (index, size) => {
831
857
  var _a, _b;
832
- if (index < 0 || index >= this.options.count) return;
858
+ if (!this.isIndexInRange(index)) return;
833
859
  let cachedSize;
834
860
  let itemStart;
835
861
  let key;
@@ -895,6 +921,7 @@ class Virtualizer {
895
921
  adjustedSync = this.applyScrollAdjustment(delta);
896
922
  }
897
923
  this.notify(adjustedSync);
924
+ this._retryClampedAdjustment();
898
925
  }
899
926
  };
900
927
  this.getVirtualItems = memo(
@@ -1130,6 +1157,10 @@ class Virtualizer {
1130
1157
  this._iosDeferredAdjustment += delta;
1131
1158
  return false;
1132
1159
  } else {
1160
+ const target = this.getScrollOffset() + this.scrollAdjustments + delta;
1161
+ const el = this.scrollElement;
1162
+ const maxAtWrite = el !== null && ("scrollHeight" in el || "document" in el) ? this.getMaxScrollOffset() : null;
1163
+ this._clampedAdjustment = maxAtWrite !== null && target > maxAtWrite + 0.5 ? { target, maxAtWrite } : null;
1133
1164
  this._scrollToOffset(this.getScrollOffset(), {
1134
1165
  adjustments: this.scrollAdjustments += delta,
1135
1166
  behavior