@tanstack/virtual-core 3.19.11 → 3.19.12

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,4 +1,4 @@
1
- type ScrollEventKind = 'spurious' | 'readBack' | 'clampedReadBack' | 'clampedEcho' | 'external';
1
+ type ScrollEventKind = 'spurious' | 'readBack' | 'clampedReadBack' | 'clampedEcho' | 'restEcho' | 'external';
2
2
  export interface ScrollEventInput {
3
3
  offset: number;
4
4
  isScrolling: boolean;
@@ -11,8 +11,9 @@ export interface ScrollEventInput {
11
11
  lowMaxes?: Array<number>;
12
12
  } | null;
13
13
  clampedMaxAtWrite: number | null;
14
+ ownWriteAt: number | null;
14
15
  }
15
- export declare function classifyScrollEvent({ offset, isScrolling, elementMax, holdOffset, scrollOffset, pendingWrite, clampedMaxAtWrite, }: ScrollEventInput): ScrollEventKind;
16
+ export declare function classifyScrollEvent({ offset, isScrolling, elementMax, holdOffset, scrollOffset, pendingWrite, clampedMaxAtWrite, ownWriteAt, }: ScrollEventInput): ScrollEventKind;
16
17
  export type ScrollPhase = 'queued' | 'smooth' | 'smoothLanded' | 'smoothFinishing' | 'instant';
17
18
  export interface RetireInput {
18
19
  kind: ScrollEventKind;
@@ -6,7 +6,8 @@ function classifyScrollEvent({
6
6
  holdOffset,
7
7
  scrollOffset,
8
8
  pendingWrite,
9
- clampedMaxAtWrite
9
+ clampedMaxAtWrite,
10
+ ownWriteAt
10
11
  }) {
11
12
  var _a;
12
13
  if (isScrolling && pendingWrite === null && offset === scrollOffset) {
@@ -24,6 +25,9 @@ function classifyScrollEvent({
24
25
  if (clampedMaxAtWrite !== null && Math.abs(offset - clampedMaxAtWrite) < 1.5) {
25
26
  return "clampedEcho";
26
27
  }
28
+ if (!isScrolling && ownWriteAt !== null && Math.abs(offset - ownWriteAt) < 1.5) {
29
+ return "restEcho";
30
+ }
27
31
  return "external";
28
32
  }
29
33
  const isAnimating = (phase) => phase === "smooth" || phase === "smoothFinishing";
@@ -33,6 +37,8 @@ function externalRetiresCommand({
33
37
  command,
34
38
  target
35
39
  }) {
40
+ let fresh;
41
+ const freshTarget = () => fresh ?? (fresh = target());
36
42
  return kind !== "spurious" && kind !== "readBack" && // Our own write, clamped (`clampedReadBack`, #1310): the command is not
37
43
  // retired by it, and reconcile retargets below the new max.
38
44
  kind !== "clampedReadBack" && (command == null ? void 0 : command.index) != null && // Nothing has been written for a command waiting for the tail; these
@@ -45,7 +51,13 @@ function externalRetiresCommand({
45
51
  // keeps it reconciling (#1294). While a smooth scroll's animation may
46
52
  // still report, its events look like a user's, so it yields only once it
47
53
  // has landed.
48
- !isAnimating(command.phase) && !approxEqual(offset, command.lastTargetOffset) && !approxEqual(offset, target()) && // A compensation write's clamped echo (#1258) keeps the command. The
54
+ !isAnimating(command.phase) && !approxEqual(offset, command.lastTargetOffset) && !approxEqual(offset, freshTarget()) && // Our own write reported again at rest (`restEcho`): nothing else has
55
+ // moved the element, but reconcile writes only a target that moved. Once
56
+ // the target has moved since the command wrote it, reconcile lands the
57
+ // command there (with `scrollend`, a correction's echo arrives before
58
+ // reconcile has run on the measurements behind it); while it has not,
59
+ // nothing will bring the element back, and the command ends here.
60
+ (kind !== "restEcho" || freshTarget() === command.lastTargetOffset) && // A compensation write's clamped echo (#1258) keeps the command. The
49
61
  // order is deliberate, not tidiness: this clause stays after `target()`
50
62
  // (and `clampedReadBack` before it) because `target()` can rebuild the
51
63
  // measurements, so it must run for exactly the events it always did.
@@ -1 +1 @@
1
- {"version":3,"file":"scroll-classifier.js","sources":["../../src/scroll-classifier.ts"],"sourcesContent":["import { approxEqual } from './utils'\n\n// Why a scroll event arrived, decided from the virtualizer's state as it was\n// before the event, before the scroll handler mutates anything. The first\n// match in this order wins:\n// spurious Safari/Firefox re-emit at the position we hold, nothing\n// pending\n// readBack our last write reporting back (within 1.5px of its\n// target)\n// clampedReadBack our last non-smooth write, clamped at the element's\n// current max or at a smaller max the layout passed\n// through since the write\n// clampedEcho a compensation write the browser clamped (#1258)\n// reporting at the max seen at write time\n// external anything else: the user, a fling, a smooth animation\n// frame\ntype ScrollEventKind =\n | 'spurious'\n | 'readBack'\n | 'clampedReadBack'\n | 'clampedEcho'\n | 'external'\n\nexport interface ScrollEventInput {\n // The reported position in layout coordinates (the element's offset plus\n // the held correction and a pending prepend anchor's delta).\n offset: number\n isScrolling: boolean\n // The element's own scroll max, read only while scrolling (null otherwise\n // or where it cannot be read).\n elementMax: number | null\n // How far the element sits behind the layout while a correction is held.\n holdOffset: number\n // The layout position the virtualizer holds.\n scrollOffset: number | null\n // Our last write to the element, until a scroll event reports it, with\n // the scroll maxima below its target the element had since (see\n // `lowMaxes` on `pendingWrite` in the virtualizer).\n pendingWrite: {\n target: number\n smooth: boolean\n lowMaxes?: Array<number>\n } | null\n // The element's max when a clamped compensation write was made (#1258),\n // or null when none is pending.\n clampedMaxAtWrite: number | null\n}\n\nexport function classifyScrollEvent({\n offset,\n isScrolling,\n elementMax,\n holdOffset,\n scrollOffset,\n pendingWrite,\n clampedMaxAtWrite,\n}: ScrollEventInput): ScrollEventKind {\n // A scroll event that reports movement but lands on the offset we already\n // hold — and isn't a self-write read-back — is a spurious no-op re-emit\n // that Safari/Firefox fire after a re-render's layout (Chrome doesn't).\n // Treating it as scrolling re-arms `isScrolling`, which forces a render\n // that triggers another such event: an infinite re-render loop.\n if (isScrolling && pendingWrite === null && offset === scrollOffset) {\n return 'spurious'\n }\n if (pendingWrite !== null && Math.abs(offset - pendingWrite.target) < 1.5) {\n return 'readBack'\n }\n // A non-smooth write past the element's max reads back at the max: ours,\n // clamped because the sizer shrank between the write and the report (rows\n // above the target measured smaller in a same-task render, #1310). The\n // element is where it says. A finger that drags to the max while such a\n // write is pending (iOS ignored it) is read the same way; the command then\n // retires at the next frame, its target clamped to the same max.\n if (\n pendingWrite !== null &&\n !pendingWrite.smooth &&\n elementMax !== null &&\n pendingWrite.target > elementMax + holdOffset + 0.5 &&\n Math.abs(offset - (elementMax + holdOffset)) < 1.5\n ) {\n return 'clampedReadBack'\n }\n // The same, clamped to a smaller max than the element's current one: the\n // sizer shrank below the target after the write (rows measured smaller,\n // a count change) and the browser clamped there, then it grew back, all\n // before the report. The clamp lands on one of those maxima; a position\n // between them is not one.\n if (\n pendingWrite !== null &&\n !pendingWrite.smooth &&\n pendingWrite.lowMaxes?.some((max) => Math.abs(offset - max) < 1.5)\n ) {\n return 'clampedReadBack'\n }\n if (\n clampedMaxAtWrite !== null &&\n Math.abs(offset - clampedMaxAtWrite) < 1.5\n ) {\n return 'clampedEcho'\n }\n return 'external'\n}\n\n// Where an in-flight scroll command is in its lifecycle, which decides how\n// its scroll events and measurements are read:\n// queued the first write waits for the momentum tail to close\n// (iOS 27, see `_startScroll`). Nothing has been written:\n// the fling's scroll events do not retire the command, a\n// drag does, and reconcile only keeps the safety valve\n// smooth a smooth animation is travelling to the target\n// smoothLanded a smooth scroll is at its target, waiting to retire\n// smoothFinishing a smooth scroll switched to instant writes for the\n// final approach; its animation can still report positions\n// instant instant writes\nexport type ScrollPhase =\n | 'queued'\n | 'smooth'\n | 'smoothLanded'\n | 'smoothFinishing'\n | 'instant'\n\n// The smooth animation may still be reporting positions, so a scroll event\n// that isn't the read-back of our own write may not be the user.\nconst isAnimating = (phase: ScrollPhase) =>\n phase === 'smooth' || phase === 'smoothFinishing'\n\nexport interface RetireInput {\n kind: ScrollEventKind\n // The reported position in layout coordinates, as classified.\n offset: number\n // The in-flight scroll command, or null.\n command: {\n index: number | null\n phase: ScrollPhase\n lastTargetOffset: number\n } | null\n // The command's target recomputed against the current layout. Called\n // only once every cheaper clause has passed, and at most once.\n target: () => number\n}\n\n// Whether a scroll event that is not the read-back of our own write retires\n// the in-flight index command: an external scroll moved away from its\n// target. Our own measurement updates (including clamped writes) still need\n// to reconcile.\nexport function externalRetiresCommand({\n kind,\n offset,\n command,\n target,\n}: RetireInput): boolean {\n return (\n kind !== 'spurious' &&\n kind !== 'readBack' &&\n // Our own write, clamped (`clampedReadBack`, #1310): the command is not\n // retired by it, and reconcile retargets below the new max.\n kind !== 'clampedReadBack' &&\n command?.index != null &&\n // Nothing has been written for a command waiting for the tail; these\n // are the fling's events.\n command.phase !== 'queued' &&\n // A non-smooth write lands at once, so any other scroll event is the\n // user's, even before reconcile saw the target reached (the target may\n // keep moving, or iOS ignored the write under a finger). A write the\n // browser clamped because the sizer hasn't grown yet reads back at the\n // max, which is also where the fresh target clamps, so the check below\n // keeps it reconciling (#1294). While a smooth scroll's animation may\n // still report, its events look like a user's, so it yields only once it\n // has landed.\n !isAnimating(command.phase) &&\n !approxEqual(offset, command.lastTargetOffset) &&\n !approxEqual(offset, target()) &&\n // A compensation write's clamped echo (#1258) keeps the command. The\n // order is deliberate, not tidiness: this clause stays after `target()`\n // (and `clampedReadBack` before it) because `target()` can rebuild the\n // measurements, so it must run for exactly the events it always did.\n kind !== 'clampedEcho'\n )\n}\n\n// A scroll position reported at a time, in the element's own coordinates.\nexport interface ScrollSample {\n t: number\n offset: number\n}\n\nconst FLING_WINDOW_MS = 40\n\n// Whether the scroll sped up: a finger flicked again or dragged on during a\n// fling. iOS fires no touch events for a touch that lands on a fling\n// (#1307), so this, and a turn against the fling, is all that shows it.\n// Momentum only slows down. Scroll events arrive 7–26ms apart and some\n// carry two frames, so one event can read 2–3× faster than its neighbours;\n// the speed is taken over the last 40ms and compared with the 40ms before.\n// On an iPhone (27.0.1) the flicks read 2.4–6× faster than the window before\n// and at least 1.5px/ms more, momentum alone at most 1.7× (#1307).\nexport function flingSpedUp(samples: ReadonlyArray<ScrollSample>): boolean {\n const last = samples[samples.length - 1]\n if (last === undefined) return false\n let j = samples.length - 2\n while (j >= 0 && last.t - samples[j]!.t < FLING_WINDOW_MS) j--\n if (j < 0) return false\n const mid = samples[j]!\n let k = j - 1\n while (k >= 0 && mid.t - samples[k]!.t < FLING_WINDOW_MS) k--\n if (k < 0) return false\n const first = samples[k]!\n const recent = (last.offset - mid.offset) / (last.t - mid.t)\n const before = (mid.offset - first.offset) / (mid.t - first.t)\n return (\n recent * before >= 0 &&\n Math.abs(recent) >= 2 * Math.abs(before) &&\n Math.abs(recent) - Math.abs(before) >= 0.8\n )\n}\n\n// Whether a fling that sent its last scroll event was still going at\n// 0.05px/ms or more over its last 40ms: a finger caught it. A fling that\n// ends on its own fades to at most 0.015px/ms first; taps stopped one at\n// 0.21–1.43px/ms (iPhones on 27.0.1, #1307). The finger stays down with no\n// touch events, and iOS undoes a write made under it. A tap in the last\n// pixels of a fling reads as its end.\nexport function flingCaught(samples: ReadonlyArray<ScrollSample>): boolean {\n const last = samples[samples.length - 1]\n if (last === undefined) return false\n let j = samples.length - 2\n while (j >= 0 && last.t - samples[j]!.t < FLING_WINDOW_MS) j--\n if (j < 0) return false\n const from = samples[j]!\n return Math.abs(last.offset - from.offset) / (last.t - from.t) >= 0.05\n}\n"],"names":[],"mappings":";AAgDO,SAAS,oBAAoB;AAAA,EAClC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAsC;;AAMpC,MAAI,eAAe,iBAAiB,QAAQ,WAAW,cAAc;AACnE,WAAO;AAAA,EACT;AACA,MAAI,iBAAiB,QAAQ,KAAK,IAAI,SAAS,aAAa,MAAM,IAAI,KAAK;AACzE,WAAO;AAAA,EACT;AAOA,MACE,iBAAiB,QACjB,CAAC,aAAa,UACd,eAAe,QACf,aAAa,SAAS,aAAa,aAAa,OAChD,KAAK,IAAI,UAAU,aAAa,WAAW,IAAI,KAC/C;AACA,WAAO;AAAA,EACT;AAMA,MACE,iBAAiB,QACjB,CAAC,aAAa,YACd,kBAAa,aAAb,mBAAuB,KAAK,CAAC,QAAQ,KAAK,IAAI,SAAS,GAAG,IAAI,OAC9D;AACA,WAAO;AAAA,EACT;AACA,MACE,sBAAsB,QACtB,KAAK,IAAI,SAAS,iBAAiB,IAAI,KACvC;AACA,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAsBA,MAAM,cAAc,CAAC,UACnB,U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AS;AACzB,SAAO,KAAK,KAAK,KAAK,IAAI,QAAQ,CAAC,EAAG,IAAI,gBAAiB;AAC3D,MAAI,IAAI,EAAG,QAAO;AAClB,QAAM,OAAO,QAAQ,CAAC;AACtB,SAAO,KAAK,IAAI,KAAK,SAAS,KAAK,MAAM,KAAK,KAAK,IAAI,KAAK,MAAM;AACpE;"}
1
+ {"version":3,"file":"scroll-classifier.js","sources":["../../src/scroll-classifier.ts"],"sourcesContent":["import { approxEqual } from './utils'\n\n// Why a scroll event arrived, decided from the virtualizer's state as it was\n// before the event, before the scroll handler mutates anything. The first\n// match in this order wins:\n// spurious Safari/Firefox re-emit at the position we hold, nothing\n// pending\n// readBack our last write reporting back (within 1.5px of its\n// target)\n// clampedReadBack our last non-smooth write, clamped at the element's\n// current max or at a smaller max the layout passed\n// through since the write\n// clampedEcho a compensation write the browser clamped (#1258)\n// reporting at the max seen at write time\n// restEcho our own write reported again at rest, where the last\n// scroll event reported it\n// external anything else: the user, a fling, a smooth animation\n// frame\ntype ScrollEventKind =\n | 'spurious'\n | 'readBack'\n | 'clampedReadBack'\n | 'clampedEcho'\n | 'restEcho'\n | 'external'\n\nexport interface ScrollEventInput {\n // The reported position in layout coordinates (the element's offset plus\n // the held correction and a pending prepend anchor's delta).\n offset: number\n isScrolling: boolean\n // The element's own scroll max, read only while scrolling (null otherwise\n // or where it cannot be read).\n elementMax: number | null\n // How far the element sits behind the layout while a correction is held.\n holdOffset: number\n // The layout position the virtualizer holds.\n scrollOffset: number | null\n // Our last write to the element, until a scroll event reports it, with\n // the scroll maxima below its target the element had since (see\n // `lowMaxes` on `pendingWrite` in the virtualizer).\n pendingWrite: {\n target: number\n smooth: boolean\n lowMaxes?: Array<number>\n } | null\n // The element's max when a clamped compensation write was made (#1258),\n // or null when none is pending.\n clampedMaxAtWrite: number | null\n // Where the last scroll event reported our own write (read back, clamped\n // or echoed), until a report anywhere else.\n ownWriteAt: number | null\n}\n\nexport function classifyScrollEvent({\n offset,\n isScrolling,\n elementMax,\n holdOffset,\n scrollOffset,\n pendingWrite,\n clampedMaxAtWrite,\n ownWriteAt,\n}: ScrollEventInput): ScrollEventKind {\n // A scroll event that reports movement but lands on the offset we already\n // hold — and isn't a self-write read-back — is a spurious no-op re-emit\n // that Safari/Firefox fire after a re-render's layout (Chrome doesn't).\n // Treating it as scrolling re-arms `isScrolling`, which forces a render\n // that triggers another such event: an infinite re-render loop.\n if (isScrolling && pendingWrite === null && offset === scrollOffset) {\n return 'spurious'\n }\n if (pendingWrite !== null && Math.abs(offset - pendingWrite.target) < 1.5) {\n return 'readBack'\n }\n // A non-smooth write past the element's max reads back at the max: ours,\n // clamped because the sizer shrank between the write and the report (rows\n // above the target measured smaller in a same-task render, #1310). The\n // element is where it says. A finger that drags to the max while such a\n // write is pending (iOS ignored it) is read the same way; the command then\n // retires at the next frame, its target clamped to the same max.\n if (\n pendingWrite !== null &&\n !pendingWrite.smooth &&\n elementMax !== null &&\n pendingWrite.target > elementMax + holdOffset + 0.5 &&\n Math.abs(offset - (elementMax + holdOffset)) < 1.5\n ) {\n return 'clampedReadBack'\n }\n // The same, clamped to a smaller max than the element's current one: the\n // sizer shrank below the target after the write (rows measured smaller,\n // a count change) and the browser clamped there, then it grew back, all\n // before the report. The clamp lands on one of those maxima; a position\n // between them is not one.\n if (\n pendingWrite !== null &&\n !pendingWrite.smooth &&\n pendingWrite.lowMaxes?.some((max) => Math.abs(offset - max) < 1.5)\n ) {\n return 'clampedReadBack'\n }\n if (\n clampedMaxAtWrite !== null &&\n Math.abs(offset - clampedMaxAtWrite) < 1.5\n ) {\n return 'clampedEcho'\n }\n // A write is reported again when the element comes to rest: `scrollend`\n // follows its scroll event in the same frame's scroll steps, before\n // reconcile; the fallback follows it after `isScrollingResetDelay`.\n // Nothing has moved the element in between.\n if (\n !isScrolling &&\n ownWriteAt !== null &&\n Math.abs(offset - ownWriteAt) < 1.5\n ) {\n return 'restEcho'\n }\n return 'external'\n}\n\n// Where an in-flight scroll command is in its lifecycle, which decides how\n// its scroll events and measurements are read:\n// queued the first write waits for the momentum tail to close\n// (iOS 27, see `_startScroll`). Nothing has been written:\n// the fling's scroll events do not retire the command, a\n// drag does, and reconcile only keeps the safety valve\n// smooth a smooth animation is travelling to the target\n// smoothLanded a smooth scroll is at its target, waiting to retire\n// smoothFinishing a smooth scroll switched to instant writes for the\n// final approach; its animation can still report positions\n// instant instant writes\nexport type ScrollPhase =\n | 'queued'\n | 'smooth'\n | 'smoothLanded'\n | 'smoothFinishing'\n | 'instant'\n\n// The smooth animation may still be reporting positions, so a scroll event\n// that isn't the read-back of our own write may not be the user.\nconst isAnimating = (phase: ScrollPhase) =>\n phase === 'smooth' || phase === 'smoothFinishing'\n\nexport interface RetireInput {\n kind: ScrollEventKind\n // The reported position in layout coordinates, as classified.\n offset: number\n // The in-flight scroll command, or null.\n command: {\n index: number | null\n phase: ScrollPhase\n lastTargetOffset: number\n } | null\n // The command's target recomputed against the current layout. Called\n // only once every cheaper clause has passed, and at most once.\n target: () => number\n}\n\n// Whether a scroll event that is not the read-back of our own write retires\n// the in-flight index command: an external scroll moved away from its\n// target, or our own write left the element where reconcile will not move\n// it. Our own measurement updates (including clamped writes) still need to\n// reconcile.\nexport function externalRetiresCommand({\n kind,\n offset,\n command,\n target,\n}: RetireInput): boolean {\n let fresh: number | undefined\n const freshTarget = () => (fresh ??= target())\n return (\n kind !== 'spurious' &&\n kind !== 'readBack' &&\n // Our own write, clamped (`clampedReadBack`, #1310): the command is not\n // retired by it, and reconcile retargets below the new max.\n kind !== 'clampedReadBack' &&\n command?.index != null &&\n // Nothing has been written for a command waiting for the tail; these\n // are the fling's events.\n command.phase !== 'queued' &&\n // A non-smooth write lands at once, so any other scroll event is the\n // user's, even before reconcile saw the target reached (the target may\n // keep moving, or iOS ignored the write under a finger). A write the\n // browser clamped because the sizer hasn't grown yet reads back at the\n // max, which is also where the fresh target clamps, so the check below\n // keeps it reconciling (#1294). While a smooth scroll's animation may\n // still report, its events look like a user's, so it yields only once it\n // has landed.\n !isAnimating(command.phase) &&\n !approxEqual(offset, command.lastTargetOffset) &&\n !approxEqual(offset, freshTarget()) &&\n // Our own write reported again at rest (`restEcho`): nothing else has\n // moved the element, but reconcile writes only a target that moved. Once\n // the target has moved since the command wrote it, reconcile lands the\n // command there (with `scrollend`, a correction's echo arrives before\n // reconcile has run on the measurements behind it); while it has not,\n // nothing will bring the element back, and the command ends here.\n (kind !== 'restEcho' || freshTarget() === command.lastTargetOffset) &&\n // A compensation write's clamped echo (#1258) keeps the command. The\n // order is deliberate, not tidiness: this clause stays after `target()`\n // (and `clampedReadBack` before it) because `target()` can rebuild the\n // measurements, so it must run for exactly the events it always did.\n kind !== 'clampedEcho'\n )\n}\n\n// A scroll position reported at a time, in the element's own coordinates.\nexport interface ScrollSample {\n t: number\n offset: number\n}\n\nconst FLING_WINDOW_MS = 40\n\n// Whether the scroll sped up: a finger flicked again or dragged on during a\n// fling. iOS fires no touch events for a touch that lands on a fling\n// (#1307), so this, and a turn against the fling, is all that shows it.\n// Momentum only slows down. Scroll events arrive 7–26ms apart and some\n// carry two frames, so one event can read 2–3× faster than its neighbours;\n// the speed is taken over the last 40ms and compared with the 40ms before.\n// On an iPhone (27.0.1) the flicks read 2.4–6× faster than the window before\n// and at least 1.5px/ms more, momentum alone at most 1.7× (#1307).\nexport function flingSpedUp(samples: ReadonlyArray<ScrollSample>): boolean {\n const last = samples[samples.length - 1]\n if (last === undefined) return false\n let j = samples.length - 2\n while (j >= 0 && last.t - samples[j]!.t < FLING_WINDOW_MS) j--\n if (j < 0) return false\n const mid = samples[j]!\n let k = j - 1\n while (k >= 0 && mid.t - samples[k]!.t < FLING_WINDOW_MS) k--\n if (k < 0) return false\n const first = samples[k]!\n const recent = (last.offset - mid.offset) / (last.t - mid.t)\n const before = (mid.offset - first.offset) / (mid.t - first.t)\n return (\n recent * before >= 0 &&\n Math.abs(recent) >= 2 * Math.abs(before) &&\n Math.abs(recent) - Math.abs(before) >= 0.8\n )\n}\n\n// Whether a fling that sent its last scroll event was still going at\n// 0.05px/ms or more over its last 40ms: a finger caught it. A fling that\n// ends on its own fades to at most 0.015px/ms first; taps stopped one at\n// 0.21–1.43px/ms (iPhones on 27.0.1, #1307). The finger stays down with no\n// touch events, and iOS undoes a write made under it. A tap in the last\n// pixels of a fling reads as its end.\nexport function flingCaught(samples: ReadonlyArray<ScrollSample>): boolean {\n const last = samples[samples.length - 1]\n if (last === undefined) return false\n let j = samples.length - 2\n while (j >= 0 && last.t - samples[j]!.t < FLING_WINDOW_MS) j--\n if (j < 0) return false\n const from = samples[j]!\n return Math.abs(last.offset - from.offset) / (last.t - from.t) >= 0.05\n}\n"],"names":[],"mappings":";AAsDO,SAAS,oBAAoB;AAAA,EAClC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAsC;;AAMpC,MAAI,eAAe,iBAAiB,QAAQ,WAAW,cAAc;AACnE,WAAO;AAAA,EACT;AACA,MAAI,iBAAiB,QAAQ,KAAK,IAAI,SAAS,aAAa,MAAM,IAAI,KAAK;AACzE,WAAO;AAAA,EACT;AAOA,MACE,iBAAiB,QACjB,CAAC,aAAa,UACd,eAAe,QACf,aAAa,SAAS,aAAa,aAAa,OAChD,KAAK,IAAI,UAAU,aAAa,WAAW,IAAI,KAC/C;AACA,WAAO;AAAA,EACT;AAMA,MACE,iBAAiB,QACjB,CAAC,aAAa,YACd,kBAAa,aAAb,mBAAuB,KAAK,CAAC,QAAQ,KAAK,IAAI,SAAS,GAAG,IAAI,OAC9D;AACA,WAAO;AAAA,EACT;AACA,MACE,sBAAsB,QACtB,KAAK,IAAI,SAAS,iBAAiB,IAAI,KACvC;AACA,WAAO;AAAA,EACT;AAKA,MACE,CAAC,eACD,eAAe,QACf,KAAK,IAAI,SAAS,UAAU,IAAI,KAChC;AACA,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAsBA,MAAM,cAAc,CAAC,UACnB,UAAU,YAAY,U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+C;AACzE,QAAM,OAAO,QAAQ,QAAQ,SAAS,CAAC;AACvC,MAAI,SAAS,OAAW,QAAO;AAC/B,MAAI,IAAI,QAAQ,SAAS;AACzB,SAAO,KAAK,KAAK,KAAK,IAAI,QAAQ,CAAC,EAAG,IAAI,gBAAiB;AAC3D,MAAI,IAAI,EAAG,QAAO;AAClB,QAAM,OAAO,QAAQ,CAAC;AACtB,SAAO,KAAK,IAAI,KAAK,SAAS,KAAK,MAAM,KAAK,KAAK,IAAI,KAAK,MAAM;AACpE;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/virtual-core",
3
- "version": "3.19.11",
3
+ "version": "3.19.12",
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
@@ -549,6 +549,13 @@ export class Virtualizer<
549
549
  // (`followOnAppend does not follow 'trim only'`; reading the element at
550
550
  // write time still moves 49 of 1080 snapshot seeds).
551
551
  private _reportedOverscroll = false
552
+ // How far the scroll element extends past the end of the list: other
553
+ // content that shares it (#1371). Read with the element's scroll range on
554
+ // scroll reports and on attach, against the sizer the element has (the
555
+ // last `getTotalSize()` an adapter drew), not a layout that measurements
556
+ // moved since.
557
+ private _trailing = 0
558
+ private _drawnListEnd = Infinity
552
559
  // The touch gesture in progress (see `IosGesture`). Its tail closing on
553
560
  // its own makes a write possible again: the last momentum frame may have
554
561
  // come before the timer. The hold is released first, so a command that
@@ -626,6 +633,10 @@ export class Virtualizer<
626
633
  smooth: boolean
627
634
  lowMaxes?: Array<number>
628
635
  } | null = null
636
+ // Where the last scroll event reported our own write, until a report
637
+ // anywhere else. The browser reports the write again when the element
638
+ // comes to rest (`restEcho`).
639
+ private _ownWriteAt: number | null = null
629
640
  // A compensation write from `applyScrollAdjustment` whose target exceeded
630
641
  // the element's scroll max at the moment of the write. The browser clamps
631
642
  // such a write because the consumer's sizer has not grown yet: an
@@ -949,7 +960,7 @@ export class Virtualizer<
949
960
  // the rows and the total shifted back by it, so the reader's row
950
961
  // stays put, and the flush writes it once a write is possible.
951
962
  this._holdOffset += anchorDelta
952
- this._followOffsetTarget(anchorDelta)
963
+ this._followAnchor(anchorDelta)
953
964
  }
954
965
  }
955
966
  }
@@ -1069,10 +1080,12 @@ export class Virtualizer<
1069
1080
  // its next touch cycle.
1070
1081
  this._holdOffset = 0
1071
1082
  this._reportedOverscroll = false
1083
+ this._trailing = 0
1072
1084
  // (The gesture was ended with its listeners, in the unsubs above.)
1073
1085
  this._clampedAdjustment = null
1074
1086
  // A write to the old element will never be read back.
1075
1087
  this.pendingWrite = null
1088
+ this._ownWriteAt = null
1076
1089
  this.scrollElement = null
1077
1090
  this.targetWindow = null
1078
1091
  }
@@ -1121,6 +1134,7 @@ export class Virtualizer<
1121
1134
  // judged only while scrolling: at rest the element has clamped,
1122
1135
  // and a stale sizer makes no bounce.
1123
1136
  const elementMax = isScrolling ? this._elementMaxScrollOffset() : null
1137
+ this._readTrailing(elementMax)
1124
1138
  const wasOverscrolled = this._reportedOverscroll
1125
1139
  this._reportedOverscroll =
1126
1140
  offset < 0 || (elementMax !== null && offset > elementMax + 1)
@@ -1161,6 +1175,17 @@ export class Virtualizer<
1161
1175
  this.scrollState = null
1162
1176
  }
1163
1177
  this.pendingWrite = null
1178
+ // Remember where our own write was reported. Its echo at rest
1179
+ // leaves that as it is; any other report clears it.
1180
+ if (
1181
+ kind === 'readBack' ||
1182
+ kind === 'clampedReadBack' ||
1183
+ kind === 'clampedEcho'
1184
+ ) {
1185
+ this._ownWriteAt = offset
1186
+ } else if (kind !== 'restEcho') {
1187
+ this._ownWriteAt = null
1188
+ }
1164
1189
 
1165
1190
  // A pending clamped compensation write (#1258) survives only its
1166
1191
  // own read-back, which the browser reports at the scroll max we
@@ -1259,6 +1284,10 @@ export class Virtualizer<
1259
1284
  }
1260
1285
 
1261
1286
  this._scrollToOffset(this.getScrollOffset())
1287
+ // The rect observer has notified already; reading what follows the
1288
+ // list can change the range again.
1289
+ this._readTrailing(this._elementMaxScrollOffset())
1290
+ this.maybeNotify()
1262
1291
  }
1263
1292
 
1264
1293
  // The consumer has committed the sizer.
@@ -1279,14 +1308,11 @@ export class Virtualizer<
1279
1308
  // an update scheduled from a layout effect commits before paint.
1280
1309
  if (!this._canWriteScroll()) {
1281
1310
  this._holdOffset += anchor.delta
1282
- this._followOffsetTarget(anchor.delta)
1311
+ this._followAnchor(anchor.delta)
1283
1312
  this.notify(false)
1284
1313
  } else if (
1285
1314
  isSmoothScroll(this.scrollState) &&
1286
- !approxEqual(
1287
- this.getScrollOffset() - anchor.delta,
1288
- this.scrollState.lastTargetOffset,
1289
- )
1315
+ !this._landedBeforeAnchor(this.scrollState, anchor.delta)
1290
1316
  ) {
1291
1317
  // A smooth programmatic scroll is still travelling. Writing scrollTop
1292
1318
  // here would cancel the browser's animation, and Chromium drops a
@@ -1294,10 +1320,10 @@ export class Virtualizer<
1294
1320
  // the journey would be stranded. The target is index-based and
1295
1321
  // recomputes against the new layout in reconcileScroll, so let the
1296
1322
  // animation run; the next scroll event re-syncs the tracked offset.
1297
- // A smooth scroll that has already landed (offset at its target,
1298
- // reconcile not yet retired it) still gets the anchor sync.
1323
+ // A smooth scroll that has already landed (reconcile not yet
1324
+ // retired it) still gets the anchor sync.
1299
1325
  } else {
1300
- this._followOffsetTarget(anchor.delta)
1326
+ this._followAnchor(anchor.delta)
1301
1327
  this._scrollToOffset(this.getScrollOffset())
1302
1328
  }
1303
1329
  } else {
@@ -1332,6 +1358,7 @@ export class Virtualizer<
1332
1358
  scrollOffset: this.scrollOffset,
1333
1359
  pendingWrite: this.pendingWrite,
1334
1360
  clampedMaxAtWrite: this._clampedAdjustment?.maxAtWrite ?? null,
1361
+ ownWriteAt: this._ownWriteAt,
1335
1362
  })
1336
1363
 
1337
1364
  // Re-issue a compensation write the browser clamped because the sizer had
@@ -1876,16 +1903,19 @@ export class Virtualizer<
1876
1903
  // scrollOffset is measured in the scroll element and the layout total
1877
1904
  // from the list start, so the list ends scrollMargin further down.
1878
1905
  this._getLayoutTotalSize() + this.options.scrollMargin,
1906
+ this._trailing,
1879
1907
  ],
1880
- (measurements, outerSize, scrollOffset, lanes, listEnd) => {
1908
+ (measurements, outerSize, scrollOffset, lanes, listEnd, trailing) => {
1881
1909
  if (measurements.length === 0 || outerSize === 0) {
1882
1910
  this.range = null
1883
1911
  return null
1884
1912
  }
1885
1913
  // Until the element reports a clamped position (the list shrank, a
1886
1914
  // rubber-band bounce), the offset can sit past the last row, which
1887
- // would leave only that row in the range.
1888
- const maxScrollOffset = Math.max(listEnd - outerSize, 0)
1915
+ // would leave only that row in the range. The element ends `trailing`
1916
+ // past the list where other content follows it, and an offset up to
1917
+ // there is where the element is.
1918
+ const maxScrollOffset = Math.max(listEnd + trailing - outerSize, 0)
1889
1919
  this.range = calculateRangeImpl(
1890
1920
  measurements,
1891
1921
  outerSize,
@@ -2149,6 +2179,17 @@ export class Virtualizer<
2149
2179
  // `_willUpdate` (#1266).
2150
2180
  this._flushHoldIfReady(true)
2151
2181
  this._retryClampedAdjustment()
2182
+ } else if (
2183
+ this.options.lanes > 1 &&
2184
+ !this.options.useCachedMeasurements &&
2185
+ !this.laneAssignments.has(index)
2186
+ ) {
2187
+ // Measured at its estimate, so no rebuild will cache its lane. A single
2188
+ // lane has none to cache, a size under useCachedMeasurements is not a
2189
+ // measurement, and a lane already cached (as always in 'estimate' mode)
2190
+ // must not change.
2191
+ const item = this.getMeasurements()[index]
2192
+ if (item) this.laneAssignments.set(index, item.lane)
2152
2193
  }
2153
2194
  }
2154
2195
 
@@ -2241,6 +2282,12 @@ export class Virtualizer<
2241
2282
  return offset
2242
2283
  }
2243
2284
 
2285
+ private _readTrailing = (elementMax: number | null) => {
2286
+ if (elementMax === null) return
2287
+ const trailing = elementMax + this.getSize() - this._drawnListEnd
2288
+ this._trailing = trailing > 0 ? trailing : 0
2289
+ }
2290
+
2244
2291
  // The element's own scroll range, or null where it cannot be read (no
2245
2292
  // element, or a bare test double without `scrollHeight` / `document`).
2246
2293
  private _elementMaxScrollOffset = (): number | null => {
@@ -2598,18 +2645,21 @@ export class Virtualizer<
2598
2645
  // The size the consumer gives the container: the layout's total, short by
2599
2646
  // a held correction so the rows exposed shifted by it still fill it (see
2600
2647
  // `_holdOffset`).
2601
- getTotalSize = () =>
2602
- Math.max(this._getLayoutTotalSize() - this._holdOffset, 0)
2648
+ getTotalSize = () => {
2649
+ const total = Math.max(this._getLayoutTotalSize() - this._holdOffset, 0)
2650
+ this._drawnListEnd = total + this.options.scrollMargin
2651
+ return total
2652
+ }
2603
2653
 
2604
2654
  private _getLayoutTotalSize = () => {
2605
2655
  const measurements = this.getMeasurements()
2606
2656
 
2607
2657
  let end: number
2608
- // If there are no measurements, set the end to paddingStart
2658
+ // If there are no measurements, include the scroll origin and start padding
2609
2659
  // If there is only one lane, use the last measurement's end
2610
2660
  // Otherwise find the maximum end value among all measurements
2611
2661
  if (measurements.length === 0) {
2612
- end = this.options.paddingStart
2662
+ end = this.options.scrollMargin + this.options.paddingStart
2613
2663
  } else if (this.options.lanes === 1) {
2614
2664
  // Fast path: read last item's end directly from the flat typed array
2615
2665
  // when available; avoids a Proxy.get + VirtualItem materialization
@@ -2738,6 +2788,32 @@ export class Virtualizer<
2738
2788
  }
2739
2789
  }
2740
2790
 
2791
+ // Whether a command had landed where a prepend's anchor (`delta`, already
2792
+ // in `getScrollOffset()`) found the viewport: it has written (a `queued`
2793
+ // command has not, wherever the fling is), and reconcile has seen it at
2794
+ // its target or the viewport is where it last wrote. A correction since
2795
+ // then can have moved the viewport and the target together.
2796
+ private _landedBeforeAnchor = (state: ScrollState, delta: number) =>
2797
+ state.phase !== 'queued' &&
2798
+ (state.stableFrames > 0 ||
2799
+ approxEqual(this.getScrollOffset() - delta, state.lastTargetOffset))
2800
+
2801
+ // A prepend's anchor moves the viewport with the rows it shows. An offset
2802
+ // target moves with it. An index command that had landed is done: the
2803
+ // reader stays on the rows it was aimed at, and its index now names
2804
+ // others. An end target is still the end.
2805
+ private _followAnchor = (delta: number) => {
2806
+ this._followOffsetTarget(delta)
2807
+ const state = this.scrollState
2808
+ if (
2809
+ state?.index != null &&
2810
+ !state.toEnd &&
2811
+ this._landedBeforeAnchor(state, delta)
2812
+ ) {
2813
+ this.scrollState = null
2814
+ }
2815
+ }
2816
+
2741
2817
  measure = () => {
2742
2818
  // Reset pendingMin so the next getMeasurements rebuilds from index 0.
2743
2819
  // Without this, a prior resizeItem() that left pendingMin > 0 would
@@ -12,6 +12,8 @@ import { approxEqual } from './utils'
12
12
  // through since the write
13
13
  // clampedEcho a compensation write the browser clamped (#1258)
14
14
  // reporting at the max seen at write time
15
+ // restEcho our own write reported again at rest, where the last
16
+ // scroll event reported it
15
17
  // external anything else: the user, a fling, a smooth animation
16
18
  // frame
17
19
  type ScrollEventKind =
@@ -19,6 +21,7 @@ type ScrollEventKind =
19
21
  | 'readBack'
20
22
  | 'clampedReadBack'
21
23
  | 'clampedEcho'
24
+ | 'restEcho'
22
25
  | 'external'
23
26
 
24
27
  export interface ScrollEventInput {
@@ -44,6 +47,9 @@ export interface ScrollEventInput {
44
47
  // The element's max when a clamped compensation write was made (#1258),
45
48
  // or null when none is pending.
46
49
  clampedMaxAtWrite: number | null
50
+ // Where the last scroll event reported our own write (read back, clamped
51
+ // or echoed), until a report anywhere else.
52
+ ownWriteAt: number | null
47
53
  }
48
54
 
49
55
  export function classifyScrollEvent({
@@ -54,6 +60,7 @@ export function classifyScrollEvent({
54
60
  scrollOffset,
55
61
  pendingWrite,
56
62
  clampedMaxAtWrite,
63
+ ownWriteAt,
57
64
  }: ScrollEventInput): ScrollEventKind {
58
65
  // A scroll event that reports movement but lands on the offset we already
59
66
  // hold — and isn't a self-write read-back — is a spurious no-op re-emit
@@ -99,6 +106,17 @@ export function classifyScrollEvent({
99
106
  ) {
100
107
  return 'clampedEcho'
101
108
  }
109
+ // A write is reported again when the element comes to rest: `scrollend`
110
+ // follows its scroll event in the same frame's scroll steps, before
111
+ // reconcile; the fallback follows it after `isScrollingResetDelay`.
112
+ // Nothing has moved the element in between.
113
+ if (
114
+ !isScrolling &&
115
+ ownWriteAt !== null &&
116
+ Math.abs(offset - ownWriteAt) < 1.5
117
+ ) {
118
+ return 'restEcho'
119
+ }
102
120
  return 'external'
103
121
  }
104
122
 
@@ -142,14 +160,17 @@ export interface RetireInput {
142
160
 
143
161
  // Whether a scroll event that is not the read-back of our own write retires
144
162
  // the in-flight index command: an external scroll moved away from its
145
- // target. Our own measurement updates (including clamped writes) still need
146
- // to reconcile.
163
+ // target, or our own write left the element where reconcile will not move
164
+ // it. Our own measurement updates (including clamped writes) still need to
165
+ // reconcile.
147
166
  export function externalRetiresCommand({
148
167
  kind,
149
168
  offset,
150
169
  command,
151
170
  target,
152
171
  }: RetireInput): boolean {
172
+ let fresh: number | undefined
173
+ const freshTarget = () => (fresh ??= target())
153
174
  return (
154
175
  kind !== 'spurious' &&
155
176
  kind !== 'readBack' &&
@@ -170,7 +191,14 @@ export function externalRetiresCommand({
170
191
  // has landed.
171
192
  !isAnimating(command.phase) &&
172
193
  !approxEqual(offset, command.lastTargetOffset) &&
173
- !approxEqual(offset, target()) &&
194
+ !approxEqual(offset, freshTarget()) &&
195
+ // Our own write reported again at rest (`restEcho`): nothing else has
196
+ // moved the element, but reconcile writes only a target that moved. Once
197
+ // the target has moved since the command wrote it, reconcile lands the
198
+ // command there (with `scrollend`, a correction's echo arrives before
199
+ // reconcile has run on the measurements behind it); while it has not,
200
+ // nothing will bring the element back, and the command ends here.
201
+ (kind !== 'restEcho' || freshTarget() === command.lastTargetOffset) &&
174
202
  // A compensation write's clamped echo (#1258) keeps the command. The
175
203
  // order is deliberate, not tidiness: this clause stays after `target()`
176
204
  // (and `clampedReadBack` before it) because `target()` can rebuild the