@tanstack/virtual-core 3.19.12 → 3.19.14

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.
@@ -1,6 +1,7 @@
1
1
  export declare const isIOSWebKit: () => boolean;
2
2
  export declare const iosMomentumOverridesWrites: () => boolean;
3
3
  export declare const momentumOverridesWrites: () => boolean;
4
+ export declare const isWebKit: () => boolean;
4
5
  export declare const _resetIOSDetectionForTests: () => void;
5
6
  type IosTouch = {
6
7
  phase: 'down';
@@ -14,8 +14,16 @@ const iosMomentumOverridesWrites = () => {
14
14
  return _iosMomentumOverridesWritesResult = isIOSWebKit() && typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("overflow-anchor: auto");
15
15
  };
16
16
  const momentumOverridesWrites = () => !isIOSWebKit() || iosMomentumOverridesWrites();
17
+ let _isWebKitResult;
18
+ const isWebKit = () => {
19
+ if (_isWebKitResult !== void 0) return _isWebKitResult;
20
+ if (typeof navigator === "undefined") return _isWebKitResult = false;
21
+ const ua = navigator.userAgent;
22
+ return _isWebKitResult = /AppleWebKit/.test(ua) && !/Chrome|Chromium|Android|jsdom|HappyDOM/.test(ua);
23
+ };
17
24
  const _resetIOSDetectionForTests = () => {
18
25
  _isIOSResult = void 0;
26
+ _isWebKitResult = void 0;
19
27
  _iosMomentumOverridesWritesResult = void 0;
20
28
  };
21
29
  const touchListenerOptions = {
@@ -126,6 +134,7 @@ export {
126
134
  _resetIOSDetectionForTests,
127
135
  iosMomentumOverridesWrites,
128
136
  isIOSWebKit,
137
+ isWebKit,
129
138
  momentumOverridesWrites
130
139
  };
131
140
  //# sourceMappingURL=ios-gesture.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ios-gesture.js","sources":["../../src/ios-gesture.ts"],"sourcesContent":["// Browser-aware iOS detection. Programmatic `scrollTo`/`scrollTop` writes\n// during a momentum-scroll cancel the momentum on iOS WebKit, so we defer\n// scroll-position adjustments triggered by mid-scroll resizes until the\n// scroll settles. SSR-safe (returns false when navigator is unavailable).\nlet _isIOSResult: boolean | undefined\nexport const isIOSWebKit = (): boolean => {\n if (_isIOSResult !== undefined) return _isIOSResult\n if (typeof navigator === 'undefined') return (_isIOSResult = false)\n if (/iP(hone|od|ad)/.test(navigator.userAgent)) return (_isIOSResult = true)\n // iPadOS 13+ reports as MacIntel; touch-points distinguishes it from desktop.\n const mtp = (navigator as Navigator & { maxTouchPoints?: number })\n .maxTouchPoints\n return (_isIOSResult =\n navigator.platform === 'MacIntel' && mtp !== undefined && mtp > 0)\n}\n\n// iOS 27 changed what a scrollTop write does to a fling: it no longer stops\n// the momentum, the fling carries on along its own path and the write shows\n// for one frame (measured on the 27.0 simulator in #1307, device check still\n// open). WebKit shipped `overflow-anchor` in the same release, so that is\n// the feature check; iOS 26 and earlier report false. Cached like the iOS\n// check; SSR-safe.\nlet _iosMomentumOverridesWritesResult: boolean | undefined\nexport const iosMomentumOverridesWrites = (): boolean => {\n if (_iosMomentumOverridesWritesResult !== undefined) {\n return _iosMomentumOverridesWritesResult\n }\n return (_iosMomentumOverridesWritesResult =\n isIOSWebKit() &&\n typeof CSS !== 'undefined' &&\n typeof CSS.supports === 'function' &&\n CSS.supports('overflow-anchor: auto'))\n}\n\n// Whether a touch fling survives a scrollTop write, so a command issued\n// mid-fling has to stop the fling before it writes. Only iOS 26 and earlier\n// stop the momentum on the write itself. Chrome on Android carries the fling\n// on from the written position: on a Pixel 6 (Android 16, Chrome 156) a\n// `scrollToIndex` issued 150ms into a fling wrote its target, then went on\n// ~3,200px past it, 3 runs of 3. Other non-iOS engines are not measured; on\n// one whose write does stop the fling, stopping it first costs the two\n// frames of the stop.\nexport const momentumOverridesWrites = (): boolean =>\n !isIOSWebKit() || iosMomentumOverridesWrites()\n\n// Test hook: reset the iOS detection caches. Not exported.\nexport const _resetIOSDetectionForTests = () => {\n _isIOSResult = undefined\n _iosMomentumOverridesWritesResult = undefined\n}\n\n// Touch listeners on the scroll element run in the capture phase, so a row\n// that stops a touch event's propagation cannot hide the gesture from the\n// iOS deferral.\nconst touchListenerOptions = {\n capture: true,\n passive: true,\n}\n\n// The touch gesture in progress. iOS WebKit ignores a scrollTop write while\n// a finger drags the content (one resting on it does not stop the write)\n// and cancels momentum on one after it, so adjustments are deferred through\n// the whole gesture: `down` from touchstart to touchend/touchcancel, then\n// `tail` while momentum runs. iOS fires no touch events during momentum, so\n// every scroll event re-arms the tail and it closes ~150ms after the last\n// one. Null between gestures. The tail is tracked on every platform, but\n// only iOS defers anything through it; elsewhere it only tells a command\n// that a fling may be running (`momentumOverridesWrites`). The tail record\n// owns its timer: every replacement of the state goes through `end`, which\n// clears it, so a live timer always has its record.\n//\n// `moved`: the content moved under or after the finger other than by our\n// own write's read-back. While `down`, a scroll event that changed the\n// position sets it: the finger is dragging. The tail inherits it from the\n// drag, and any other scroll event re-arming the tail sets it: a fling can\n// be running. A tap's tail has it unset.\n//\n// `awaitingFling`: the tail follows a drag whose fling has not reported\n// yet. The first frame of a fling can come 150–250ms after touchend when\n// the main thread is busy (27.0 simulator, #1307); if the tail closes\n// first, `lateFling` waits until `until` for that frame to reopen it. It\n// defers nothing: other scroll events with no touch before them must not\n// defer corrections (#1250).\ntype IosTouch =\n | { phase: 'down'; moved: boolean }\n | { phase: 'tail'; moved: boolean; awaitingFling: boolean; timerId: number }\n | { phase: 'lateFling'; until: number }\n | null\n\ntype TimerWindow = Pick<Window, 'setTimeout' | 'clearTimeout'>\n\nexport class IosGesture {\n private _touch: IosTouch = null\n\n constructor(\n private readonly host: {\n // The scroll element's window, which runs the tail timer; null when\n // there is no element.\n getWindow: () => TimerWindow | null\n now: () => number\n // Asked when the tail's timer fires: true keeps it open another\n // 150ms (the fling still moves between scroll events).\n tailStillMoving: () => boolean\n // The tail closed on its own: a write is possible again.\n onTailClosed: () => void\n },\n ) {}\n\n get touch(): IosTouch {\n return this._touch\n }\n\n // A touch gesture owns the scroll: on iOS a write now would be ignored or\n // would cancel the momentum, so adjustments wait for the tail to close.\n // `isScrolling` is not part of it: any scroll event sets it, including\n // the read-back of our own write, and a programmatic scroll has no\n // momentum to protect (#1250).\n inMotion = () => this._touch !== null && this._touch.phase !== 'lateFling'\n\n // A finger is dragging the content, so iOS ignores our writes and no\n // read-back will come. A resting finger does not: the write lands.\n dragging = () => this._touch?.phase === 'down' && this._touch.moved\n\n // The open tail, if any: after a tap, or after a drag or fling. Except on\n // iOS 26 and earlier, a fling's momentum outlasts a write (see\n // `momentumOverridesWrites`).\n tail = (): 'tap' | 'fling' | null =>\n this._touch?.phase === 'tail' ? (this._touch.moved ? 'fling' : 'tap') : null\n\n // A scroll event reached the handler. `ownReadBack`: it is our own\n // write's read-back; `moved`: it changed the layout position; `external`:\n // the user or a fling moved it (not our write nor our smooth animation).\n // Returns whether it shows a finger dragging the content.\n onScroll = ({\n isScrolling,\n ownReadBack,\n moved,\n external,\n }: {\n isScrolling: boolean\n ownReadBack: boolean\n moved: boolean\n external: boolean\n }): boolean => {\n // Momentum after touchend fires a scroll event per frame but no touch\n // events, so each one re-arms the tail to span the fling. Only an open\n // tail is extended: a scroll event with no touch before it (our own\n // write's read-back) never opens one.\n if (isScrolling && this._touch?.phase === 'tail') {\n this._openTail(this._touch.moved || !ownReadBack, false)\n } else if (this._touch?.phase === 'lateFling') {\n // A drag's fling reporting after its tail closed reopens it.\n if (this.host.now() > this._touch.until) {\n this._touch = null\n } else if (external && isScrolling && moved) {\n this._openTail(true, false)\n }\n }\n // A finger that moved the content is dragging. Our own write landing\n // under a resting finger is not, and neither is a report at rest that\n // leaves the position where it was.\n if (!ownReadBack && isScrolling && moved && this._touch?.phase === 'down') {\n this._touch.moved = true\n return true\n }\n return false\n }\n\n touchStart = () => {\n // A finger back down inside the grace window cancels its tail.\n this.end()\n this._touch = { phase: 'down', moved: false }\n }\n\n touchEnd = () => {\n const moved = this._touch?.phase === 'down' && this._touch.moved\n this.end()\n this._openTail(moved, moved)\n }\n\n // Drop the gesture, and the tail's timer with it.\n end = () => {\n const win = this.host.getWindow()\n if (this._touch?.phase === 'tail' && win != null) {\n win.clearTimeout(this._touch.timerId)\n }\n this._touch = null\n }\n\n // Follow the touch events on `target`; the returned function stops and\n // drops the gesture. Listening is unconditional: the listeners are\n // passive and cheap, and on non-touch devices they never fire. The gating\n // by `isIOSWebKit()` lives where a write is decided.\n listen = (target: EventTarget) => {\n const onTouchStart = () => this.touchStart()\n const onTouchEnd = () => this.touchEnd()\n target.addEventListener('touchstart', onTouchStart, touchListenerOptions)\n target.addEventListener('touchend', onTouchEnd, touchListenerOptions)\n // Same handler as touchend: iOS can end a touch with touchcancel (system\n // gesture, interrupted scroll) and never fire touchend.\n target.addEventListener('touchcancel', onTouchEnd, touchListenerOptions)\n return () => {\n target.removeEventListener(\n 'touchstart',\n onTouchStart,\n touchListenerOptions,\n )\n target.removeEventListener('touchend', onTouchEnd, touchListenerOptions)\n target.removeEventListener(\n 'touchcancel',\n onTouchEnd,\n touchListenerOptions,\n )\n this.end()\n }\n }\n\n // (Re)open the tail after the finger lifts, closing ~150ms after the last\n // touchend or momentum frame.\n private _openTail = (moved: boolean, awaitingFling: boolean) => {\n const win = this.host.getWindow()\n if (win == null) return\n this.end()\n const tail = {\n phase: 'tail' as const,\n moved,\n awaitingFling,\n // Only fires while this tail is current: `end` clears it otherwise.\n timerId: win.setTimeout(() => {\n if (this.host.tailStillMoving()) {\n this._openTail(tail.moved, tail.awaitingFling)\n return\n }\n this._touch = tail.awaitingFling\n ? { phase: 'lateFling', until: this.host.now() + 1000 }\n : null\n this.host.onTailClosed()\n }, 150),\n }\n this._touch = tail\n }\n}\n"],"names":[],"mappings":"AAIA,IAAI;AACG,MAAM,cAAc,MAAe;AACxC,MAAI,iBAAiB,OAAW,QAAO;AACvC,MAAI,OAAO,cAAc,YAAa,QAAQ,eAAe;AAC7D,MAAI,iBAAiB,KAAK,UAAU,SAAS,UAAW,eAAe;AAEvE,QAAM,MAAO,UACV;AACH,SAAQ,eACN,UAAU,aAAa,cAAc,QAAQ,UAAa,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AC7C;AAAA,UACF;AACA,eAAK,SAAS,KAAK,gBACf,EAAE,OAAO,aAAa,OAAO,KAAK,KAAK,IAAA,IAAQ,IAAA,IAC/C;AACJ,eAAK,KAAK,aAAA;AAAA,QACZ,GAAG,GAAG;AAAA,MAAA;AAER,WAAK,SAAS;AAAA,IAChB;AAAA,EAtIG;AAAA,EAEH,IAAI,QAAkB;AACpB,WAAO,KAAK;AAAA,EACd;AAmIF;"}
1
+ {"version":3,"file":"ios-gesture.js","sources":["../../src/ios-gesture.ts"],"sourcesContent":["// Browser-aware iOS detection. Programmatic `scrollTo`/`scrollTop` writes\n// during a momentum-scroll cancel the momentum on iOS WebKit, so we defer\n// scroll-position adjustments triggered by mid-scroll resizes until the\n// scroll settles. SSR-safe (returns false when navigator is unavailable).\nlet _isIOSResult: boolean | undefined\nexport const isIOSWebKit = (): boolean => {\n if (_isIOSResult !== undefined) return _isIOSResult\n if (typeof navigator === 'undefined') return (_isIOSResult = false)\n if (/iP(hone|od|ad)/.test(navigator.userAgent)) return (_isIOSResult = true)\n // iPadOS 13+ reports as MacIntel; touch-points distinguishes it from desktop.\n const mtp = (navigator as Navigator & { maxTouchPoints?: number })\n .maxTouchPoints\n return (_isIOSResult =\n navigator.platform === 'MacIntel' && mtp !== undefined && mtp > 0)\n}\n\n// iOS 27 changed what a scrollTop write does to a fling: it no longer stops\n// the momentum, the fling carries on along its own path and the write shows\n// for one frame (measured on the 27.0 simulator in #1307, device check still\n// open). WebKit shipped `overflow-anchor` in the same release, so that is\n// the feature check; iOS 26 and earlier report false. Cached like the iOS\n// check; SSR-safe.\nlet _iosMomentumOverridesWritesResult: boolean | undefined\nexport const iosMomentumOverridesWrites = (): boolean => {\n if (_iosMomentumOverridesWritesResult !== undefined) {\n return _iosMomentumOverridesWritesResult\n }\n return (_iosMomentumOverridesWritesResult =\n isIOSWebKit() &&\n typeof CSS !== 'undefined' &&\n typeof CSS.supports === 'function' &&\n CSS.supports('overflow-anchor: auto'))\n}\n\n// Whether a touch fling survives a scrollTop write, so a command issued\n// mid-fling has to stop the fling before it writes. Only iOS 26 and earlier\n// stop the momentum on the write itself. Chrome on Android carries the fling\n// on from the written position: on a Pixel 6 (Android 16, Chrome 156) a\n// `scrollToIndex` issued 150ms into a fling wrote its target, then went on\n// ~3,200px past it, 3 runs of 3. Other non-iOS engines are not measured; on\n// one whose write does stop the fling, stopping it first costs the two\n// frames of the stop.\nexport const momentumOverridesWrites = (): boolean =>\n !isIOSWebKit() || iosMomentumOverridesWrites()\n\n// Any WebKit: Safari, a WKWebView app, every browser on iOS. Chromium's\n// user agent names AppleWebKit too, and so do jsdom's and happy-dom's, where\n// a consumer's test must see the same writes as before. Cached like the iOS\n// check; SSR-safe.\nlet _isWebKitResult: boolean | undefined\nexport const isWebKit = (): boolean => {\n if (_isWebKitResult !== undefined) return _isWebKitResult\n if (typeof navigator === 'undefined') return (_isWebKitResult = false)\n const ua = navigator.userAgent\n return (_isWebKitResult =\n /AppleWebKit/.test(ua) &&\n !/Chrome|Chromium|Android|jsdom|HappyDOM/.test(ua))\n}\n\n// Test hook: reset the iOS detection caches. Not exported.\nexport const _resetIOSDetectionForTests = () => {\n _isIOSResult = undefined\n _isWebKitResult = undefined\n _iosMomentumOverridesWritesResult = undefined\n}\n\n// Touch listeners on the scroll element run in the capture phase, so a row\n// that stops a touch event's propagation cannot hide the gesture from the\n// iOS deferral.\nconst touchListenerOptions = {\n capture: true,\n passive: true,\n}\n\n// The touch gesture in progress. iOS WebKit ignores a scrollTop write while\n// a finger drags the content (one resting on it does not stop the write)\n// and cancels momentum on one after it, so adjustments are deferred through\n// the whole gesture: `down` from touchstart to touchend/touchcancel, then\n// `tail` while momentum runs. iOS fires no touch events during momentum, so\n// every scroll event re-arms the tail and it closes ~150ms after the last\n// one. Null between gestures. The tail is tracked on every platform, but\n// only iOS defers anything through it; elsewhere it only tells a command\n// that a fling may be running (`momentumOverridesWrites`). The tail record\n// owns its timer: every replacement of the state goes through `end`, which\n// clears it, so a live timer always has its record.\n//\n// `moved`: the content moved under or after the finger other than by our\n// own write's read-back. While `down`, a scroll event that changed the\n// position sets it: the finger is dragging. The tail inherits it from the\n// drag, and any other scroll event re-arming the tail sets it: a fling can\n// be running. A tap's tail has it unset.\n//\n// `awaitingFling`: the tail follows a drag whose fling has not reported\n// yet. The first frame of a fling can come 150–250ms after touchend when\n// the main thread is busy (27.0 simulator, #1307); if the tail closes\n// first, `lateFling` waits until `until` for that frame to reopen it. It\n// defers nothing: other scroll events with no touch before them must not\n// defer corrections (#1250).\ntype IosTouch =\n | { phase: 'down'; moved: boolean }\n | { phase: 'tail'; moved: boolean; awaitingFling: boolean; timerId: number }\n | { phase: 'lateFling'; until: number }\n | null\n\ntype TimerWindow = Pick<Window, 'setTimeout' | 'clearTimeout'>\n\nexport class IosGesture {\n private _touch: IosTouch = null\n\n constructor(\n private readonly host: {\n // The scroll element's window, which runs the tail timer; null when\n // there is no element.\n getWindow: () => TimerWindow | null\n now: () => number\n // Asked when the tail's timer fires: true keeps it open another\n // 150ms (the fling still moves between scroll events).\n tailStillMoving: () => boolean\n // The tail closed on its own: a write is possible again.\n onTailClosed: () => void\n },\n ) {}\n\n get touch(): IosTouch {\n return this._touch\n }\n\n // A touch gesture owns the scroll: on iOS a write now would be ignored or\n // would cancel the momentum, so adjustments wait for the tail to close.\n // `isScrolling` is not part of it: any scroll event sets it, including\n // the read-back of our own write, and a programmatic scroll has no\n // momentum to protect (#1250).\n inMotion = () => this._touch !== null && this._touch.phase !== 'lateFling'\n\n // A finger is dragging the content, so iOS ignores our writes and no\n // read-back will come. A resting finger does not: the write lands.\n dragging = () => this._touch?.phase === 'down' && this._touch.moved\n\n // The open tail, if any: after a tap, or after a drag or fling. Except on\n // iOS 26 and earlier, a fling's momentum outlasts a write (see\n // `momentumOverridesWrites`).\n tail = (): 'tap' | 'fling' | null =>\n this._touch?.phase === 'tail' ? (this._touch.moved ? 'fling' : 'tap') : null\n\n // A scroll event reached the handler. `ownReadBack`: it is our own\n // write's read-back; `moved`: it changed the layout position; `external`:\n // the user or a fling moved it (not our write nor our smooth animation).\n // Returns whether it shows a finger dragging the content.\n onScroll = ({\n isScrolling,\n ownReadBack,\n moved,\n external,\n }: {\n isScrolling: boolean\n ownReadBack: boolean\n moved: boolean\n external: boolean\n }): boolean => {\n // Momentum after touchend fires a scroll event per frame but no touch\n // events, so each one re-arms the tail to span the fling. Only an open\n // tail is extended: a scroll event with no touch before it (our own\n // write's read-back) never opens one.\n if (isScrolling && this._touch?.phase === 'tail') {\n this._openTail(this._touch.moved || !ownReadBack, false)\n } else if (this._touch?.phase === 'lateFling') {\n // A drag's fling reporting after its tail closed reopens it.\n if (this.host.now() > this._touch.until) {\n this._touch = null\n } else if (external && isScrolling && moved) {\n this._openTail(true, false)\n }\n }\n // A finger that moved the content is dragging. Our own write landing\n // under a resting finger is not, and neither is a report at rest that\n // leaves the position where it was.\n if (!ownReadBack && isScrolling && moved && this._touch?.phase === 'down') {\n this._touch.moved = true\n return true\n }\n return false\n }\n\n touchStart = () => {\n // A finger back down inside the grace window cancels its tail.\n this.end()\n this._touch = { phase: 'down', moved: false }\n }\n\n touchEnd = () => {\n const moved = this._touch?.phase === 'down' && this._touch.moved\n this.end()\n this._openTail(moved, moved)\n }\n\n // Drop the gesture, and the tail's timer with it.\n end = () => {\n const win = this.host.getWindow()\n if (this._touch?.phase === 'tail' && win != null) {\n win.clearTimeout(this._touch.timerId)\n }\n this._touch = null\n }\n\n // Follow the touch events on `target`; the returned function stops and\n // drops the gesture. Listening is unconditional: the listeners are\n // passive and cheap, and on non-touch devices they never fire. The gating\n // by `isIOSWebKit()` lives where a write is decided.\n listen = (target: EventTarget) => {\n const onTouchStart = () => this.touchStart()\n const onTouchEnd = () => this.touchEnd()\n target.addEventListener('touchstart', onTouchStart, touchListenerOptions)\n target.addEventListener('touchend', onTouchEnd, touchListenerOptions)\n // Same handler as touchend: iOS can end a touch with touchcancel (system\n // gesture, interrupted scroll) and never fire touchend.\n target.addEventListener('touchcancel', onTouchEnd, touchListenerOptions)\n return () => {\n target.removeEventListener(\n 'touchstart',\n onTouchStart,\n touchListenerOptions,\n )\n target.removeEventListener('touchend', onTouchEnd, touchListenerOptions)\n target.removeEventListener(\n 'touchcancel',\n onTouchEnd,\n touchListenerOptions,\n )\n this.end()\n }\n }\n\n // (Re)open the tail after the finger lifts, closing ~150ms after the last\n // touchend or momentum frame.\n private _openTail = (moved: boolean, awaitingFling: boolean) => {\n const win = this.host.getWindow()\n if (win == null) return\n this.end()\n const tail = {\n phase: 'tail' as const,\n moved,\n awaitingFling,\n // Only fires while this tail is current: `end` clears it otherwise.\n timerId: win.setTimeout(() => {\n if (this.host.tailStillMoving()) {\n this._openTail(tail.moved, tail.awaitingFling)\n return\n }\n this._touch = tail.awaitingFling\n ? { phase: 'lateFling', until: this.host.now() + 1000 }\n : null\n this.host.onTailClosed()\n }, 150),\n }\n this._touch = tail\n }\n}\n"],"names":[],"mappings":"AAIA,IAAI;AACG,MAAM,cAAc,MAAe;AACxC,MAAI,iBAAiB,OAAW,QAAO;AACvC,MAAI,OAAO,cAAc,YAAa,QAAQ,eAAe;AAC7D,MAAI,iBAAiB,KAAK,UAAU,SAAS,UAAW,eAAe;AAEvE,QAAM,MAAO,UACV;AACH,SAAQ,eACN,UAAU,aAAa,cAAc,QAAQ,UAAa,MAAM;AACpE;AAQA,IAAI;AACG,MAAM,6BAA6B,MAAe;AACvD,MAAI,sCAAsC,QAAW;AACnD,WAAO;AAAA,EACT;AACA,SAAQ,oCACN,iBACA,OAAO,QAAQ,eACf,OAAO,IAAI,aAAa,cACxB,IAAI,SAAS,uBAAuB;AACxC;AAUO,MAAM,0BAA0B,MACrC,CAAC,YAAA,KAAiB,2BAAA;AAMpB,IAAI;AACG,MAAM,WAAW,MAAe;AACrC,MAAI,oBAAoB,OAAW,QAAO;AAC1C,MAAI,OAAO,cAAc,YAAa,QAAQ,kBAAkB;AAChE,QAAM,KAAK,UAAU;AACrB,SAAQ,kBACN,cAAc,KAA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AAAA,IAChB;AAAA,EAtIG;AAAA,EAEH,IAAI,QAAkB;AACpB,WAAO,KAAK;AAAA,EACd;AAmIF;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/virtual-core",
3
- "version": "3.19.12",
3
+ "version": "3.19.14",
4
4
  "description": "Headless UI for virtualizing scrollable elements in TS/JS + Frameworks",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -9,7 +9,12 @@ import {
9
9
  flingCaught,
10
10
  flingSpedUp,
11
11
  } from './scroll-classifier'
12
- import { IosGesture, isIOSWebKit, momentumOverridesWrites } from './ios-gesture'
12
+ import {
13
+ IosGesture,
14
+ isIOSWebKit,
15
+ isWebKit,
16
+ momentumOverridesWrites,
17
+ } from './ios-gesture'
13
18
  import type { ScrollPhase, ScrollSample } from './scroll-classifier'
14
19
 
15
20
  export { _resetIOSDetectionForTests } from './ios-gesture'
@@ -382,7 +387,9 @@ export interface VirtualizerOptions<
382
387
  paddingEnd?: number
383
388
  scrollPaddingStart?: number
384
389
  scrollPaddingEnd?: number
385
- initialOffset?: number | (() => number)
390
+ initialOffset?:
391
+ | number
392
+ | ((instance: Virtualizer<TScrollElement, TItemElement>) => number)
386
393
  getItemKey?: (index: number) => Key
387
394
  rangeExtractor?: (range: Range) => Array<number>
388
395
  scrollMargin?: number
@@ -496,12 +503,20 @@ export class Virtualizer<
496
503
  } | null = null
497
504
  itemSizeCache = new Map<Key, number>()
498
505
  private itemSizeCacheVersion = 0
506
+ private builtMeasurementOptions: object | null = null
507
+ private builtItemSizeCacheVersion = -1
508
+ private pendingResizes: Array<{ index: number; delta: number }> = []
499
509
  private laneAssignments = new Map<number, number>() // index → lane cache
500
510
  // Earliest index dirtied since last getMeasurements() rebuild, or null.
501
511
  private pendingMin: number | null = null
502
512
  private prevLanes: number | undefined = undefined
503
513
  private lanesChangedFlag = false
504
514
  private lanesSettling = false
515
+ // Whether `initialMeasurementsCache` has seeded `itemSizeCache`. It is
516
+ // read once: a list that empties and refills keeps the sizes measured
517
+ // since, rather than going back to the snapshot's. `enabled: false`
518
+ // resets it along with the sizes.
519
+ private initialMeasurementsCacheApplied = false
505
520
  private pendingScrollAnchor: PendingScrollAnchor | null = null
506
521
  scrollRect: Rect | null = null
507
522
  /**
@@ -532,6 +547,12 @@ export class Virtualizer<
532
547
  // `_flushHoldIfReady`: the rows re-render in layout coordinates and the
533
548
  // element moves by the same amount in one paint.
534
549
  private _holdOffset = 0
550
+ // Set in WebKit from a wheel event until 150ms pass without one, whether
551
+ // or not that event scrolled this element. Corrections are held meanwhile
552
+ // (see `_canWriteScroll`).
553
+ private _wheelTimer: number | null = null
554
+ // The frame a hold at the top is written in (`_flushHoldAtTop`).
555
+ private _edgeFlushFrame: number | null = null
535
556
  // Whether the element's last scroll event reported a position past either
536
557
  // edge (the rubber-band bounce). Judged at report time against the
537
558
  // element's own max, with a 1px tolerance for a fractional scrollTop: a
@@ -955,10 +976,11 @@ export class Virtualizer<
955
976
  if (this._canWriteScroll()) {
956
977
  this.pendingScrollAnchor = { kind: 'anchor', delta: anchorDelta }
957
978
  } else {
958
- // The element cannot take the write (a touch owns it on iOS, or it is
959
- // overscrolled). Hold the delta instead: this render already exposes
960
- // the rows and the total shifted back by it, so the reader's row
961
- // stays put, and the flush writes it once a write is possible.
979
+ // The element cannot take the write (a touch owns it on iOS, it is
980
+ // overscrolled, or wheel input runs in WebKit). Hold the delta
981
+ // instead: this render already exposes the rows and the total
982
+ // shifted back by it, so the reader's row stays put, and the flush
983
+ // writes it once a write is possible.
962
984
  this._holdOffset += anchorDelta
963
985
  this._followAnchor(anchorDelta)
964
986
  }
@@ -1079,6 +1101,16 @@ export class Virtualizer<
1079
1101
  // just removed), deferring every adjustment on the new element until
1080
1102
  // its next touch cycle.
1081
1103
  this._holdOffset = 0
1104
+ if (this.targetWindow) {
1105
+ if (this._wheelTimer !== null) {
1106
+ this.targetWindow.clearTimeout(this._wheelTimer)
1107
+ }
1108
+ if (this._edgeFlushFrame !== null) {
1109
+ this.targetWindow.cancelAnimationFrame(this._edgeFlushFrame)
1110
+ }
1111
+ }
1112
+ this._wheelTimer = null
1113
+ this._edgeFlushFrame = null
1082
1114
  this._reportedOverscroll = false
1083
1115
  this._trailing = 0
1084
1116
  // (The gesture was ended with its listeners, in the unsubs above.)
@@ -1268,6 +1300,7 @@ export class Virtualizer<
1268
1300
 
1269
1301
  // A held correction is written once a write is possible.
1270
1302
  this._flushHoldIfReady(true)
1303
+ this._flushHoldAtTop()
1271
1304
 
1272
1305
  if (this.scrollState) {
1273
1306
  this.scheduleScrollReconcile()
@@ -1278,9 +1311,24 @@ export class Virtualizer<
1278
1311
 
1279
1312
  // Touch event listeners (iOS-aware deferral).
1280
1313
  if ('addEventListener' in this.scrollElement) {
1281
- this.unsubs.push(
1282
- this._iosGesture.listen(this.scrollElement as unknown as EventTarget),
1283
- )
1314
+ const target = this.scrollElement as unknown as EventTarget
1315
+ this.unsubs.push(this._iosGesture.listen(target))
1316
+ if (isWebKit()) {
1317
+ const onWheel = () => {
1318
+ const win = this.targetWindow
1319
+ if (win === null) return
1320
+ if (this._wheelTimer !== null) win.clearTimeout(this._wheelTimer)
1321
+ this._wheelTimer = win.setTimeout(() => {
1322
+ this._wheelTimer = null
1323
+ this._flushHoldIfReady(true)
1324
+ this._retryClampedAdjustment()
1325
+ }, 150)
1326
+ // At the top the element stops moving, so no scroll event comes.
1327
+ this._flushHoldAtTop()
1328
+ }
1329
+ target.addEventListener('wheel', onWheel, addEventListenerOptions)
1330
+ this.unsubs.push(() => target.removeEventListener('wheel', onWheel))
1331
+ }
1284
1332
  }
1285
1333
 
1286
1334
  this._scrollToOffset(this.getScrollOffset())
@@ -1435,19 +1483,82 @@ export class Virtualizer<
1435
1483
  // #1307), so a correction is held through the tail on every version;
1436
1484
  // - not while the element is overscrolled past either edge (the
1437
1485
  // rubber-band bounce): WebKit discards the write and the bounce can
1438
- // stall where it was (#1287).
1486
+ // stall where it was (#1287);
1487
+ // - not in WebKit within 150ms of a wheel event: the write lands, but
1488
+ // WebKit then discards the distance of the next wheel event, and a list
1489
+ // of rows measured on the way up writes on most frames. In Playwright
1490
+ // WebKit 26.0 a wheel event per frame lost about a third of its
1491
+ // distance that way (#1398, #1400); a prepend's anchor write could be
1492
+ // undone whole. With the hold, Safari 26.5.2 itself lost nothing on
1493
+ // trackpad momentum and about 2.5% on a continuous mouse wheel, at the
1494
+ // writes at the top (#1400); without it only Playwright is measured.
1439
1495
  // A write that is unwanted for another reason (a travelling smooth scroll
1440
1496
  // owns the position) is a separate check at the call sites.
1441
1497
  private _canWriteScroll = () =>
1442
- !(isIOSWebKit() && this._iosGesture.inMotion()) && !this._reportedOverscroll
1498
+ !(isIOSWebKit() && this._iosGesture.inMotion()) &&
1499
+ !this._reportedOverscroll &&
1500
+ this._wheelTimer === null
1501
+
1502
+ // A hold shifts the rows and leaves the element where it is, so the
1503
+ // element reaches its top while the layout has `_holdOffset` further to
1504
+ // go (or, for a negative hold, the layout reaches the top first). Wheel
1505
+ // input that never pauses would park the reader there, so there the wheel
1506
+ // hold ends early and the hold is written, two frames later (a prepend's
1507
+ // anchor written at once can be undone, #1398). The next wheel event
1508
+ // starts the wheel hold again. Not before the top: while the element
1509
+ // still moves, WebKit applies the write about two frames after the rows
1510
+ // have gone back to layout coordinates, and they show `_holdOffset` away
1511
+ // for those frames (measured with a 1200px and a one-viewport margin in
1512
+ // Playwright WebKit 26.0; not seen in 27.2, #1400). The list's top is
1513
+ // `scrollMargin` down the element, where held rows would be drawn over
1514
+ // what is above the list; the element still moves there, so those frames
1515
+ // are the lesser evil. Past the top (a bounce) the write waits: the next
1516
+ // wheel or scroll event tries again.
1517
+ // The end needs none of this: the total size is exposed short by the
1518
+ // hold, so the element and the layout reach it together.
1519
+ private _holdAtTop = () => {
1520
+ if (this._holdOffset === 0) return false
1521
+ const offset = this._readElementOffset()
1522
+ // 1px for a fractional scrollTop.
1523
+ return (
1524
+ offset !== null &&
1525
+ Math.min(offset, offset + this._holdOffset) <
1526
+ this.options.scrollMargin + 1
1527
+ )
1528
+ }
1529
+ private _flushHoldAtTop = () => {
1530
+ const win = this.targetWindow
1531
+ if (
1532
+ win === null ||
1533
+ this._wheelTimer === null ||
1534
+ this._edgeFlushFrame !== null ||
1535
+ !this._holdAtTop()
1536
+ ) {
1537
+ return
1538
+ }
1539
+ this._edgeFlushFrame = win.requestAnimationFrame(() => {
1540
+ this._edgeFlushFrame = win.requestAnimationFrame(() => {
1541
+ this._edgeFlushFrame = null
1542
+ // The reader can have turned back within the two frames.
1543
+ if (this._wheelTimer === null || !this._holdAtTop()) return
1544
+ win.clearTimeout(this._wheelTimer)
1545
+ this._wheelTimer = null
1546
+ this._flushHoldIfReady(true)
1547
+ this._retryClampedAdjustment()
1548
+ })
1549
+ })
1550
+ }
1443
1551
 
1444
1552
  // Write a held correction once a write is possible, from the scroll
1445
- // handler, the touch tail timer, `_willUpdate` and a resize notify. The
1446
- // rows go back to layout coordinates and the total grows by the hold in a
1447
- // notify issued before the write, so the sizer can take the position and
1448
- // both commit in the paint the write lands in. `sync` is false only where
1449
- // the caller already runs inside the consumer's commit (`_willUpdate`),
1450
- // where an update it schedules commits before paint anyway.
1553
+ // handler, the touch tail timer, the wheel timer, the frame at the top
1554
+ // (`_flushHoldAtTop`), a command's start, `_willUpdate` and a resize
1555
+ // notify. The rows go back to layout coordinates and the total grows by
1556
+ // the hold in a notify issued before the write, so the sizer can take the
1557
+ // position and both commit in the paint the write lands in. `sync` is
1558
+ // false only where the caller already runs inside the consumer's commit
1559
+ // (`_willUpdate`), where an update it schedules commits before paint
1560
+ // anyway. A command's start passes true, although `_willUpdate` can reach
1561
+ // it too (a follow on append).
1451
1562
  private _flushHoldIfReady = (sync: boolean) => {
1452
1563
  if (this._holdOffset === 0) return
1453
1564
  if (!this.scrollElement || !this.options.enabled) return
@@ -1615,7 +1726,7 @@ export class Virtualizer<
1615
1726
  this.scrollOffset =
1616
1727
  this.scrollOffset ??
1617
1728
  (typeof this.options.initialOffset === 'function'
1618
- ? this.options.initialOffset()
1729
+ ? this.options.initialOffset(this)
1619
1730
  : this.options.initialOffset)
1620
1731
 
1621
1732
  return this.scrollOffset
@@ -1674,8 +1785,8 @@ export class Virtualizer<
1674
1785
 
1675
1786
  private getMeasurements = memo(
1676
1787
  () => [this.getMeasurementOptions(), this.itemSizeCacheVersion],
1677
- (
1678
- {
1788
+ (measurementOptions, itemSizeCacheVersion) => {
1789
+ const {
1679
1790
  count,
1680
1791
  paddingStart,
1681
1792
  scrollMargin,
@@ -1684,15 +1795,17 @@ export class Virtualizer<
1684
1795
  lanes,
1685
1796
  laneAssignmentMode,
1686
1797
  gap,
1687
- },
1688
- _itemSizeCacheVersion,
1689
- ) => {
1798
+ } = measurementOptions
1799
+ this.builtMeasurementOptions = measurementOptions
1800
+ this.builtItemSizeCacheVersion = itemSizeCacheVersion
1801
+ this.pendingResizes = []
1690
1802
  const itemSizeCache = this.itemSizeCache
1691
1803
  if (!enabled) {
1692
1804
  this.measurementsCache = []
1693
1805
  this._singleLaneMeasurements = null
1694
1806
  this.itemSizeCache.clear()
1695
1807
  this.laneAssignments.clear()
1808
+ this.initialMeasurementsCacheApplied = false
1696
1809
  return []
1697
1810
  }
1698
1811
 
@@ -1719,7 +1832,12 @@ export class Virtualizer<
1719
1832
 
1720
1833
  // Don't restore from initialMeasurementsCache during lane changes
1721
1834
  // as it contains stale lane assignments from the previous lane count
1722
- if (this.measurementsCache.length === 0 && !this.lanesSettling) {
1835
+ if (
1836
+ !this.initialMeasurementsCacheApplied &&
1837
+ this.measurementsCache.length === 0 &&
1838
+ !this.lanesSettling
1839
+ ) {
1840
+ this.initialMeasurementsCacheApplied = true
1723
1841
  this.measurementsCache = this.options.initialMeasurementsCache
1724
1842
  this.measurementsCache.forEach((item) => {
1725
1843
  this.itemSizeCache.set(item.key, item.size)
@@ -2058,16 +2176,42 @@ export class Virtualizer<
2058
2176
  }
2059
2177
  }
2060
2178
 
2179
+ private canShiftFromPendingResizes = (): boolean =>
2180
+ this.options.lanes === 1 &&
2181
+ this._singleLaneMeasurements !== null &&
2182
+ this.shouldAdjustScrollPositionOnItemSizeChange === undefined &&
2183
+ !this.lanesChangedFlag &&
2184
+ !this.lanesSettling &&
2185
+ this.getMeasurementOptions() === this.builtMeasurementOptions &&
2186
+ this.itemSizeCacheVersion ===
2187
+ this.builtItemSizeCacheVersion + this.pendingResizes.length
2188
+
2061
2189
  resizeItem = (index: number, size: number) => {
2062
2190
  if (!this.isIndexInRange(index)) return
2063
2191
 
2192
+ // Options can change before the next render rebuilds the measurements.
2193
+ // Use the current item position and estimate when deciding whether this
2194
+ // resize needs a scroll correction.
2195
+ if (this.getMeasurementOptions() !== this.builtMeasurementOptions) {
2196
+ this.getMeasurements()
2197
+ }
2198
+
2064
2199
  // An earlier entry in this ResizeObserver batch moved `scrollOffset`.
2065
2200
  // Outside a batch its sync notify would have re-rendered and rebuilt the
2066
2201
  // caches read below; rebuild them here so the anchoring check compares
2067
2202
  // this item's current start, not a stale one, against the adjusted
2068
2203
  // offset (#1218). Without a scroll move, the caches are exactly as stale
2069
2204
  // as they were before batching, so skip the rebuild.
2070
- if (this._resizeBatch?.sync) this.getMeasurements()
2205
+ let startShift = 0
2206
+ if (this._resizeBatch?.sync) {
2207
+ if (this.canShiftFromPendingResizes()) {
2208
+ for (const resize of this.pendingResizes) {
2209
+ if (resize.index < index) startShift += resize.delta
2210
+ }
2211
+ } else {
2212
+ this.getMeasurements()
2213
+ }
2214
+ }
2071
2215
 
2072
2216
  // Fast field reads. For lanes===1 we read raw start/size from the flat
2073
2217
  // typed array, avoiding a Proxy.get + VirtualItem allocation per call.
@@ -2078,7 +2222,7 @@ export class Virtualizer<
2078
2222
  const flat = this._singleLaneMeasurements?.flat
2079
2223
  if (this.options.lanes === 1 && flat != null) {
2080
2224
  key = this.options.getItemKey(index)
2081
- itemStart = flat[index * 2]!
2225
+ itemStart = flat[index * 2]! + startShift
2082
2226
  cachedSize = flat[index * 2 + 1]!
2083
2227
  } else {
2084
2228
  const item = this.measurementsCache[index]
@@ -2139,6 +2283,7 @@ export class Virtualizer<
2139
2283
  }
2140
2284
  this.itemSizeCache.set(key, size)
2141
2285
  this.itemSizeCacheVersion++
2286
+ this.pendingResizes.push({ index, delta })
2142
2287
 
2143
2288
  // Before a command's read-back, an index or end target is retargeted
2144
2289
  // from fresh measurements by reconcileScroll next frame; an offset
@@ -2543,10 +2688,11 @@ export class Virtualizer<
2543
2688
  // Every scroll command starts here: it becomes the in-flight command,
2544
2689
  // writes its first target and leaves reconcile to retarget and retire it.
2545
2690
  //
2546
- // Targets are layout offsets. A held correction is not dropped by a
2547
- // command: the write lands the element `_holdOffset` short of the target,
2548
- // under the rows exposed shifted by the same amount, and the flush moves
2549
- // both together once a write is possible.
2691
+ // Targets are layout offsets. A correction held for the wheel is written
2692
+ // before the command (below). One held for a touch or a bounce is not
2693
+ // dropped by a command: the write lands the element `_holdOffset` short
2694
+ // of the target, under the rows exposed shifted by the same amount, and
2695
+ // the flush moves both together once a write is possible.
2550
2696
  //
2551
2697
  // An index or end target that is already at its offset starts a smooth
2552
2698
  // scroll as landed; an offset target never does, so its first scroll
@@ -2557,6 +2703,18 @@ export class Virtualizer<
2557
2703
  behavior: ScrollBehavior,
2558
2704
  kind: 'absolute' | 'relative',
2559
2705
  ) {
2706
+ // The command takes over from the wheel: end the wheel hold and write
2707
+ // what it held first. The command is written `_holdOffset` short of its
2708
+ // target, so under a hold larger than the target the element clamps at
2709
+ // 0, the command never sees its target and the flush after it writes
2710
+ // the old position. The command this one replaces must not keep the
2711
+ // hold back (`_flushHoldIfReady` waits for a smooth one).
2712
+ if (this._wheelTimer !== null && this.targetWindow) {
2713
+ this.targetWindow.clearTimeout(this._wheelTimer)
2714
+ this._wheelTimer = null
2715
+ this.scrollState = null
2716
+ this._flushHoldIfReady(true)
2717
+ }
2560
2718
  // A new explicit command supersedes layout-driven work queued before it.
2561
2719
  // Clear both before any iOS deferral so a later commit/retry cannot
2562
2720
  // overwrite the user's newer scroll intent.
@@ -43,9 +43,24 @@ export const iosMomentumOverridesWrites = (): boolean => {
43
43
  export const momentumOverridesWrites = (): boolean =>
44
44
  !isIOSWebKit() || iosMomentumOverridesWrites()
45
45
 
46
+ // Any WebKit: Safari, a WKWebView app, every browser on iOS. Chromium's
47
+ // user agent names AppleWebKit too, and so do jsdom's and happy-dom's, where
48
+ // a consumer's test must see the same writes as before. Cached like the iOS
49
+ // check; SSR-safe.
50
+ let _isWebKitResult: boolean | undefined
51
+ export const isWebKit = (): boolean => {
52
+ if (_isWebKitResult !== undefined) return _isWebKitResult
53
+ if (typeof navigator === 'undefined') return (_isWebKitResult = false)
54
+ const ua = navigator.userAgent
55
+ return (_isWebKitResult =
56
+ /AppleWebKit/.test(ua) &&
57
+ !/Chrome|Chromium|Android|jsdom|HappyDOM/.test(ua))
58
+ }
59
+
46
60
  // Test hook: reset the iOS detection caches. Not exported.
47
61
  export const _resetIOSDetectionForTests = () => {
48
62
  _isIOSResult = undefined
63
+ _isWebKitResult = undefined
49
64
  _iosMomentumOverridesWritesResult = undefined
50
65
  }
51
66