@tanstack/virtual-core 3.19.13 → 3.19.15

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.13",
3
+ "version": "3.19.15",
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
@@ -540,6 +547,12 @@ export class Virtualizer<
540
547
  // `_flushHoldIfReady`: the rows re-render in layout coordinates and the
541
548
  // element moves by the same amount in one paint.
542
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
543
556
  // Whether the element's last scroll event reported a position past either
544
557
  // edge (the rubber-band bounce). Judged at report time against the
545
558
  // element's own max, with a 1px tolerance for a fractional scrollTop: a
@@ -666,6 +679,8 @@ export class Virtualizer<
666
679
  // Set while a ResizeObserver callback measures its entries; `resizeItem`
667
680
  // records the pending notify here instead of firing it per entry.
668
681
  private _resizeBatch: { sync: boolean; notify?: boolean } | null = null
682
+ // Set while a microtask waits to run `dropDisconnected` again.
683
+ private _dropQueued = false
669
684
  private now = () => this.targetWindow?.performance?.now?.() ?? Date.now()
670
685
  private observer = (() => {
671
686
  let _ro: ResizeObserver | null = null
@@ -963,10 +978,11 @@ export class Virtualizer<
963
978
  if (this._canWriteScroll()) {
964
979
  this.pendingScrollAnchor = { kind: 'anchor', delta: anchorDelta }
965
980
  } else {
966
- // The element cannot take the write (a touch owns it on iOS, or it is
967
- // overscrolled). Hold the delta instead: this render already exposes
968
- // the rows and the total shifted back by it, so the reader's row
969
- // stays put, and the flush writes it once a write is possible.
981
+ // The element cannot take the write (a touch owns it on iOS, it is
982
+ // overscrolled, or wheel input runs in WebKit). Hold the delta
983
+ // instead: this render already exposes the rows and the total
984
+ // shifted back by it, so the reader's row stays put, and the flush
985
+ // writes it once a write is possible.
970
986
  this._holdOffset += anchorDelta
971
987
  this._followAnchor(anchorDelta)
972
988
  }
@@ -1087,6 +1103,16 @@ export class Virtualizer<
1087
1103
  // just removed), deferring every adjustment on the new element until
1088
1104
  // its next touch cycle.
1089
1105
  this._holdOffset = 0
1106
+ if (this.targetWindow) {
1107
+ if (this._wheelTimer !== null) {
1108
+ this.targetWindow.clearTimeout(this._wheelTimer)
1109
+ }
1110
+ if (this._edgeFlushFrame !== null) {
1111
+ this.targetWindow.cancelAnimationFrame(this._edgeFlushFrame)
1112
+ }
1113
+ }
1114
+ this._wheelTimer = null
1115
+ this._edgeFlushFrame = null
1090
1116
  this._reportedOverscroll = false
1091
1117
  this._trailing = 0
1092
1118
  // (The gesture was ended with its listeners, in the unsubs above.)
@@ -1276,6 +1302,7 @@ export class Virtualizer<
1276
1302
 
1277
1303
  // A held correction is written once a write is possible.
1278
1304
  this._flushHoldIfReady(true)
1305
+ this._flushHoldAtTop()
1279
1306
 
1280
1307
  if (this.scrollState) {
1281
1308
  this.scheduleScrollReconcile()
@@ -1286,9 +1313,24 @@ export class Virtualizer<
1286
1313
 
1287
1314
  // Touch event listeners (iOS-aware deferral).
1288
1315
  if ('addEventListener' in this.scrollElement) {
1289
- this.unsubs.push(
1290
- this._iosGesture.listen(this.scrollElement as unknown as EventTarget),
1291
- )
1316
+ const target = this.scrollElement as unknown as EventTarget
1317
+ this.unsubs.push(this._iosGesture.listen(target))
1318
+ if (isWebKit()) {
1319
+ const onWheel = () => {
1320
+ const win = this.targetWindow
1321
+ if (win === null) return
1322
+ if (this._wheelTimer !== null) win.clearTimeout(this._wheelTimer)
1323
+ this._wheelTimer = win.setTimeout(() => {
1324
+ this._wheelTimer = null
1325
+ this._flushHoldIfReady(true)
1326
+ this._retryClampedAdjustment()
1327
+ }, 150)
1328
+ // At the top the element stops moving, so no scroll event comes.
1329
+ this._flushHoldAtTop()
1330
+ }
1331
+ target.addEventListener('wheel', onWheel, addEventListenerOptions)
1332
+ this.unsubs.push(() => target.removeEventListener('wheel', onWheel))
1333
+ }
1292
1334
  }
1293
1335
 
1294
1336
  this._scrollToOffset(this.getScrollOffset())
@@ -1443,19 +1485,82 @@ export class Virtualizer<
1443
1485
  // #1307), so a correction is held through the tail on every version;
1444
1486
  // - not while the element is overscrolled past either edge (the
1445
1487
  // rubber-band bounce): WebKit discards the write and the bounce can
1446
- // stall where it was (#1287).
1488
+ // stall where it was (#1287);
1489
+ // - not in WebKit within 150ms of a wheel event: the write lands, but
1490
+ // WebKit then discards the distance of the next wheel event, and a list
1491
+ // of rows measured on the way up writes on most frames. In Playwright
1492
+ // WebKit 26.0 a wheel event per frame lost about a third of its
1493
+ // distance that way (#1398, #1400); a prepend's anchor write could be
1494
+ // undone whole. With the hold, Safari 26.5.2 itself lost nothing on
1495
+ // trackpad momentum and about 2.5% on a continuous mouse wheel, at the
1496
+ // writes at the top (#1400); without it only Playwright is measured.
1447
1497
  // A write that is unwanted for another reason (a travelling smooth scroll
1448
1498
  // owns the position) is a separate check at the call sites.
1449
1499
  private _canWriteScroll = () =>
1450
- !(isIOSWebKit() && this._iosGesture.inMotion()) && !this._reportedOverscroll
1500
+ !(isIOSWebKit() && this._iosGesture.inMotion()) &&
1501
+ !this._reportedOverscroll &&
1502
+ this._wheelTimer === null
1503
+
1504
+ // A hold shifts the rows and leaves the element where it is, so the
1505
+ // element reaches its top while the layout has `_holdOffset` further to
1506
+ // go (or, for a negative hold, the layout reaches the top first). Wheel
1507
+ // input that never pauses would park the reader there, so there the wheel
1508
+ // hold ends early and the hold is written, two frames later (a prepend's
1509
+ // anchor written at once can be undone, #1398). The next wheel event
1510
+ // starts the wheel hold again. Not before the top: while the element
1511
+ // still moves, WebKit applies the write about two frames after the rows
1512
+ // have gone back to layout coordinates, and they show `_holdOffset` away
1513
+ // for those frames (measured with a 1200px and a one-viewport margin in
1514
+ // Playwright WebKit 26.0; not seen in 27.2, #1400). The list's top is
1515
+ // `scrollMargin` down the element, where held rows would be drawn over
1516
+ // what is above the list; the element still moves there, so those frames
1517
+ // are the lesser evil. Past the top (a bounce) the write waits: the next
1518
+ // wheel or scroll event tries again.
1519
+ // The end needs none of this: the total size is exposed short by the
1520
+ // hold, so the element and the layout reach it together.
1521
+ private _holdAtTop = () => {
1522
+ if (this._holdOffset === 0) return false
1523
+ const offset = this._readElementOffset()
1524
+ // 1px for a fractional scrollTop.
1525
+ return (
1526
+ offset !== null &&
1527
+ Math.min(offset, offset + this._holdOffset) <
1528
+ this.options.scrollMargin + 1
1529
+ )
1530
+ }
1531
+ private _flushHoldAtTop = () => {
1532
+ const win = this.targetWindow
1533
+ if (
1534
+ win === null ||
1535
+ this._wheelTimer === null ||
1536
+ this._edgeFlushFrame !== null ||
1537
+ !this._holdAtTop()
1538
+ ) {
1539
+ return
1540
+ }
1541
+ this._edgeFlushFrame = win.requestAnimationFrame(() => {
1542
+ this._edgeFlushFrame = win.requestAnimationFrame(() => {
1543
+ this._edgeFlushFrame = null
1544
+ // The reader can have turned back within the two frames.
1545
+ if (this._wheelTimer === null || !this._holdAtTop()) return
1546
+ win.clearTimeout(this._wheelTimer)
1547
+ this._wheelTimer = null
1548
+ this._flushHoldIfReady(true)
1549
+ this._retryClampedAdjustment()
1550
+ })
1551
+ })
1552
+ }
1451
1553
 
1452
1554
  // Write a held correction once a write is possible, from the scroll
1453
- // handler, the touch tail timer, `_willUpdate` and a resize notify. The
1454
- // rows go back to layout coordinates and the total grows by the hold in a
1455
- // notify issued before the write, so the sizer can take the position and
1456
- // both commit in the paint the write lands in. `sync` is false only where
1457
- // the caller already runs inside the consumer's commit (`_willUpdate`),
1458
- // where an update it schedules commits before paint anyway.
1555
+ // handler, the touch tail timer, the wheel timer, the frame at the top
1556
+ // (`_flushHoldAtTop`), a command's start, `_willUpdate` and a resize
1557
+ // notify. The rows go back to layout coordinates and the total grows by
1558
+ // the hold in a notify issued before the write, so the sizer can take the
1559
+ // position and both commit in the paint the write lands in. `sync` is
1560
+ // false only where the caller already runs inside the consumer's commit
1561
+ // (`_willUpdate`), where an update it schedules commits before paint
1562
+ // anyway. A command's start passes true, although `_willUpdate` can reach
1563
+ // it too (a follow on append).
1459
1564
  private _flushHoldIfReady = (sync: boolean) => {
1460
1565
  if (this._holdOffset === 0) return
1461
1566
  if (!this.scrollElement || !this.options.enabled) return
@@ -1623,7 +1728,7 @@ export class Virtualizer<
1623
1728
  this.scrollOffset =
1624
1729
  this.scrollOffset ??
1625
1730
  (typeof this.options.initialOffset === 'function'
1626
- ? this.options.initialOffset()
1731
+ ? this.options.initialOffset(this)
1627
1732
  : this.options.initialOffset)
1628
1733
 
1629
1734
  return this.scrollOffset
@@ -2032,14 +2137,31 @@ export class Virtualizer<
2032
2137
  return true
2033
2138
  }
2034
2139
 
2140
+ private dropDisconnected = () => {
2141
+ this.elementsCache.forEach((cached, key) => {
2142
+ if (!cached.isConnected) {
2143
+ this.observer.unobserve(cached)
2144
+ this.elementsCache.delete(key)
2145
+ }
2146
+ })
2147
+ }
2148
+
2035
2149
  measureElement = (node: TItemElement | null) => {
2036
2150
  if (!node) {
2037
- this.elementsCache.forEach((cached, key) => {
2038
- if (!cached.isConnected) {
2039
- this.observer.unobserve(cached)
2040
- this.elementsCache.delete(key)
2041
- }
2042
- })
2151
+ this.dropDisconnected()
2152
+ // A framework clears the ref before it detaches the element, so the row
2153
+ // that is unmounting is still connected here. A render in a microtask of
2154
+ // a ResizeObserver delivery (Vue does this) detaches it before the
2155
+ // browser gathers the next observations, and a detached row that is
2156
+ // still observed at that point makes the browser report a loop error.
2157
+ // Check again once the render is done.
2158
+ if (!this._dropQueued) {
2159
+ this._dropQueued = true
2160
+ queueMicrotask(() => {
2161
+ this._dropQueued = false
2162
+ this.dropDisconnected()
2163
+ })
2164
+ }
2043
2165
  return
2044
2166
  }
2045
2167
 
@@ -2086,6 +2208,13 @@ export class Virtualizer<
2086
2208
  resizeItem = (index: number, size: number) => {
2087
2209
  if (!this.isIndexInRange(index)) return
2088
2210
 
2211
+ // Options can change before the next render rebuilds the measurements.
2212
+ // Use the current item position and estimate when deciding whether this
2213
+ // resize needs a scroll correction.
2214
+ if (this.getMeasurementOptions() !== this.builtMeasurementOptions) {
2215
+ this.getMeasurements()
2216
+ }
2217
+
2089
2218
  // An earlier entry in this ResizeObserver batch moved `scrollOffset`.
2090
2219
  // Outside a batch its sync notify would have re-rendered and rebuilt the
2091
2220
  // caches read below; rebuild them here so the anchoring check compares
@@ -2578,10 +2707,12 @@ export class Virtualizer<
2578
2707
  // Every scroll command starts here: it becomes the in-flight command,
2579
2708
  // writes its first target and leaves reconcile to retarget and retire it.
2580
2709
  //
2581
- // Targets are layout offsets. A held correction is not dropped by a
2582
- // command: the write lands the element `_holdOffset` short of the target,
2583
- // under the rows exposed shifted by the same amount, and the flush moves
2584
- // both together once a write is possible.
2710
+ // Targets are layout offsets. A correction held for the wheel is written
2711
+ // before the command (below). One held for a touch or a bounce is not
2712
+ // dropped by a command: the write lands the element `_holdOffset` short
2713
+ // of the target, under the rows exposed shifted by the same amount, and
2714
+ // the flush moves both together once a write is possible. Unless the
2715
+ // target is less than the hold (below).
2585
2716
  //
2586
2717
  // An index or end target that is already at its offset starts a smooth
2587
2718
  // scroll as landed; an offset target never does, so its first scroll
@@ -2592,6 +2723,18 @@ export class Virtualizer<
2592
2723
  behavior: ScrollBehavior,
2593
2724
  kind: 'absolute' | 'relative',
2594
2725
  ) {
2726
+ // The command takes over from the wheel: end the wheel hold and write
2727
+ // what it held first. The command is written `_holdOffset` short of its
2728
+ // target, so under a hold larger than the target the element clamps at
2729
+ // 0, the command never sees its target and the flush after it writes
2730
+ // the old position. The command this one replaces must not keep the
2731
+ // hold back (`_flushHoldIfReady` waits for a smooth one).
2732
+ if (this._wheelTimer !== null && this.targetWindow) {
2733
+ this.targetWindow.clearTimeout(this._wheelTimer)
2734
+ this._wheelTimer = null
2735
+ this.scrollState = null
2736
+ this._flushHoldIfReady(true)
2737
+ }
2595
2738
  // A new explicit command supersedes layout-driven work queued before it.
2596
2739
  // Clear both before any iOS deferral so a later commit/retry cannot
2597
2740
  // overwrite the user's newer scroll intent.
@@ -2650,6 +2793,21 @@ export class Virtualizer<
2650
2793
  // scroll: leave it.
2651
2794
  this._iosGesture.end()
2652
2795
  }
2796
+ // A target less than the hold is out of the element's reach: written
2797
+ // the hold short, the write clamps at the element's 0, and the read-back
2798
+ // looks like a scroll away from the target (#1405). Write the hold first
2799
+ // where a write is possible now (the command has closed a tap's tail),
2800
+ // as for the wheel above. Its clamp retry would write over the command.
2801
+ // The target was clamped to a max that included the hold; a consumer
2802
+ // that commits the sizer after the notify leaves the element its old
2803
+ // max, so clamp it again, as for a command issued now. Reconcile
2804
+ // retargets an index or end target once the sizer grows.
2805
+ if (offset < this._holdOffset && this._canWriteScroll()) {
2806
+ this.scrollState = null
2807
+ this._flushHoldIfReady(true)
2808
+ this._clampedAdjustment = null
2809
+ offset = Math.min(offset, Math.max(this.getMaxScrollOffset(), 0))
2810
+ }
2653
2811
  this.scrollState = {
2654
2812
  ...target,
2655
2813
  startedAt: this.now(),
@@ -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