@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.
@@ -128,9 +128,12 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
128
128
  get scrollAdjustments(): number;
129
129
  private _holdOffset;
130
130
  private _reportedOverscroll;
131
+ private _trailing;
132
+ private _drawnListEnd;
131
133
  private _iosGesture;
132
134
  private _lastElementOffset;
133
135
  private pendingWrite;
136
+ private _ownWriteAt;
134
137
  private _clampedAdjustment;
135
138
  shouldAdjustScrollPositionOnItemSizeChange: undefined | ((item: VirtualItem, delta: number, instance: Virtualizer<TScrollElement, TItemElement>) => boolean);
136
139
  elementsCache: Map<Key, TItemElement>;
@@ -169,7 +172,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
169
172
  startIndex: number;
170
173
  endIndex: number;
171
174
  } | null;
172
- updateDeps(newDeps: [VirtualItem[], number, number, number, number]): void;
175
+ updateDeps(newDeps: [VirtualItem[], number, number, number, number, number]): void;
173
176
  };
174
177
  getVirtualIndexes: {
175
178
  (): number[];
@@ -192,6 +195,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
192
195
  private _noteClampMax;
193
196
  private getMaxScrollOffset;
194
197
  private _toLayoutOffset;
198
+ private _readTrailing;
195
199
  private _elementMaxScrollOffset;
196
200
  private _momentumStop;
197
201
  private _stopMomentum;
@@ -224,5 +228,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
224
228
  private getCommandWrittenOffset;
225
229
  private _awaitingCommandReadBack;
226
230
  private _followOffsetTarget;
231
+ private _landedBeforeAnchor;
232
+ private _followAnchor;
227
233
  measure: () => void;
228
234
  }
@@ -8,7 +8,8 @@ function classifyScrollEvent({
8
8
  holdOffset,
9
9
  scrollOffset,
10
10
  pendingWrite,
11
- clampedMaxAtWrite
11
+ clampedMaxAtWrite,
12
+ ownWriteAt
12
13
  }) {
13
14
  var _a;
14
15
  if (isScrolling && pendingWrite === null && offset === scrollOffset) {
@@ -26,6 +27,9 @@ function classifyScrollEvent({
26
27
  if (clampedMaxAtWrite !== null && Math.abs(offset - clampedMaxAtWrite) < 1.5) {
27
28
  return "clampedEcho";
28
29
  }
30
+ if (!isScrolling && ownWriteAt !== null && Math.abs(offset - ownWriteAt) < 1.5) {
31
+ return "restEcho";
32
+ }
29
33
  return "external";
30
34
  }
31
35
  const isAnimating = (phase) => phase === "smooth" || phase === "smoothFinishing";
@@ -35,6 +39,8 @@ function externalRetiresCommand({
35
39
  command,
36
40
  target
37
41
  }) {
42
+ let fresh;
43
+ const freshTarget = () => fresh ?? (fresh = target());
38
44
  return kind !== "spurious" && kind !== "readBack" && // Our own write, clamped (`clampedReadBack`, #1310): the command is not
39
45
  // retired by it, and reconcile retargets below the new max.
40
46
  kind !== "clampedReadBack" && (command == null ? void 0 : command.index) != null && // Nothing has been written for a command waiting for the tail; these
@@ -47,7 +53,13 @@ function externalRetiresCommand({
47
53
  // keeps it reconciling (#1294). While a smooth scroll's animation may
48
54
  // still report, its events look like a user's, so it yields only once it
49
55
  // has landed.
50
- !isAnimating(command.phase) && !utils.approxEqual(offset, command.lastTargetOffset) && !utils.approxEqual(offset, target()) && // A compensation write's clamped echo (#1258) keeps the command. The
56
+ !isAnimating(command.phase) && !utils.approxEqual(offset, command.lastTargetOffset) && !utils.approxEqual(offset, freshTarget()) && // Our own write reported again at rest (`restEcho`): nothing else has
57
+ // moved the element, but reconcile writes only a target that moved. Once
58
+ // the target has moved since the command wrote it, reconcile lands the
59
+ // command there (with `scrollend`, a correction's echo arrives before
60
+ // reconcile has run on the measurements behind it); while it has not,
61
+ // nothing will bring the element back, and the command ends here.
62
+ (kind !== "restEcho" || freshTarget() === command.lastTargetOffset) && // A compensation write's clamped echo (#1258) keeps the command. The
51
63
  // order is deliberate, not tidiness: this clause stays after `target()`
52
64
  // (and `clampedReadBack` before it) because `target()` can rebuild the
53
65
  // measurements, so it must run for exactly the events it always did.
@@ -1 +1 @@
1
- {"version":3,"file":"scroll-classifier.cjs","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":["approxEqual"],"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,I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AAK,IAAI,IAAI;AAC1D,QAAM,UAAU,IAAI,SAAS,MAAM,WAAW,IAAI,IAAI,MAAM;AAC5D,SACE,SAAS,UAAU,KACnB,KAAK,IAAI,MAAM,KAAK,IAAI,KAAK,IAAI,MAAM,KACvC,KAAK,IAAI,MAAM,IAAI,KAAK,IAAI,MAAM,KAAK;AAE3C;AAQO,SAAS,YAAY,SAA+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;;;;;"}
1
+ {"version":3,"file":"scroll-classifier.cjs","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":["approxEqual"],"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,iB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AQ,CAAC,EAAG,IAAI,gBAAiB;AAC1D,MAAI,IAAI,EAAG,QAAO;AAClB,QAAM,QAAQ,QAAQ,CAAC;AACvB,QAAM,UAAU,KAAK,SAAS,IAAI,WAAW,KAAK,IAAI,IAAI;AAC1D,QAAM,UAAU,IAAI,SAAS,MAAM,WAAW,IAAI,IAAI,MAAM;AAC5D,SACE,SAAS,UAAU,KACnB,KAAK,IAAI,MAAM,KAAK,IAAI,KAAK,IAAI,MAAM,KACvC,KAAK,IAAI,MAAM,IAAI,KAAK,IAAI,MAAM,KAAK;AAE3C;AAQO,SAAS,YAAY,SAA+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;;;;;"}
@@ -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;
@@ -128,9 +128,12 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
128
128
  get scrollAdjustments(): number;
129
129
  private _holdOffset;
130
130
  private _reportedOverscroll;
131
+ private _trailing;
132
+ private _drawnListEnd;
131
133
  private _iosGesture;
132
134
  private _lastElementOffset;
133
135
  private pendingWrite;
136
+ private _ownWriteAt;
134
137
  private _clampedAdjustment;
135
138
  shouldAdjustScrollPositionOnItemSizeChange: undefined | ((item: VirtualItem, delta: number, instance: Virtualizer<TScrollElement, TItemElement>) => boolean);
136
139
  elementsCache: Map<Key, TItemElement>;
@@ -169,7 +172,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
169
172
  startIndex: number;
170
173
  endIndex: number;
171
174
  } | null;
172
- updateDeps(newDeps: [VirtualItem[], number, number, number, number]): void;
175
+ updateDeps(newDeps: [VirtualItem[], number, number, number, number, number]): void;
173
176
  };
174
177
  getVirtualIndexes: {
175
178
  (): number[];
@@ -192,6 +195,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
192
195
  private _noteClampMax;
193
196
  private getMaxScrollOffset;
194
197
  private _toLayoutOffset;
198
+ private _readTrailing;
195
199
  private _elementMaxScrollOffset;
196
200
  private _momentumStop;
197
201
  private _stopMomentum;
@@ -224,5 +228,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
224
228
  private getCommandWrittenOffset;
225
229
  private _awaitingCommandReadBack;
226
230
  private _followOffsetTarget;
231
+ private _landedBeforeAnchor;
232
+ private _followAnchor;
227
233
  measure: () => void;
228
234
  }
package/dist/esm/index.js CHANGED
@@ -213,6 +213,8 @@ class Virtualizer {
213
213
  this.scrollDirection = null;
214
214
  this._holdOffset = 0;
215
215
  this._reportedOverscroll = false;
216
+ this._trailing = 0;
217
+ this._drawnListEnd = Infinity;
216
218
  this._iosGesture = new IosGesture({
217
219
  getWindow: () => this.targetWindow,
218
220
  now: () => this.now(),
@@ -247,6 +249,7 @@ class Virtualizer {
247
249
  });
248
250
  this._lastElementOffset = null;
249
251
  this.pendingWrite = null;
252
+ this._ownWriteAt = null;
250
253
  this._clampedAdjustment = null;
251
254
  this.elementsCache = /* @__PURE__ */ new Map();
252
255
  this._resizeBatch = null;
@@ -455,7 +458,7 @@ class Virtualizer {
455
458
  this.pendingScrollAnchor = { kind: "anchor", delta: anchorDelta };
456
459
  } else {
457
460
  this._holdOffset += anchorDelta;
458
- this._followOffsetTarget(anchorDelta);
461
+ this._followAnchor(anchorDelta);
459
462
  }
460
463
  }
461
464
  };
@@ -501,8 +504,10 @@ class Virtualizer {
501
504
  this.scrollDirection = null;
502
505
  this._holdOffset = 0;
503
506
  this._reportedOverscroll = false;
507
+ this._trailing = 0;
504
508
  this._clampedAdjustment = null;
505
509
  this.pendingWrite = null;
510
+ this._ownWriteAt = null;
506
511
  this.scrollElement = null;
507
512
  this.targetWindow = null;
508
513
  };
@@ -539,6 +544,7 @@ class Virtualizer {
539
544
  this.options.observeElementOffset(this, (offset, isScrolling) => {
540
545
  var _a2, _b, _c, _d;
541
546
  const elementMax = isScrolling ? this._elementMaxScrollOffset() : null;
547
+ this._readTrailing(elementMax);
542
548
  const wasOverscrolled = this._reportedOverscroll;
543
549
  this._reportedOverscroll = offset < 0 || elementMax !== null && offset > elementMax + 1;
544
550
  const elementOffset = offset;
@@ -564,6 +570,11 @@ class Virtualizer {
564
570
  this.scrollState = null;
565
571
  }
566
572
  this.pendingWrite = null;
573
+ if (kind === "readBack" || kind === "clampedReadBack" || kind === "clampedEcho") {
574
+ this._ownWriteAt = offset;
575
+ } else if (kind !== "restEcho") {
576
+ this._ownWriteAt = null;
577
+ }
567
578
  if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
568
579
  this._clampedAdjustment = null;
569
580
  }
@@ -614,6 +625,8 @@ class Virtualizer {
614
625
  );
615
626
  }
616
627
  this._scrollToOffset(this.getScrollOffset());
628
+ this._readTrailing(this._elementMaxScrollOffset());
629
+ this.maybeNotify();
617
630
  }
618
631
  this._noteClampMax();
619
632
  const anchor = this.pendingScrollAnchor;
@@ -622,14 +635,11 @@ class Virtualizer {
622
635
  if (anchor.kind === "anchor") {
623
636
  if (!this._canWriteScroll()) {
624
637
  this._holdOffset += anchor.delta;
625
- this._followOffsetTarget(anchor.delta);
638
+ this._followAnchor(anchor.delta);
626
639
  this.notify(false);
627
- } else if (isSmoothScroll(this.scrollState) && !approxEqual(
628
- this.getScrollOffset() - anchor.delta,
629
- this.scrollState.lastTargetOffset
630
- )) ;
640
+ } else if (isSmoothScroll(this.scrollState) && !this._landedBeforeAnchor(this.scrollState, anchor.delta)) ;
631
641
  else {
632
- this._followOffsetTarget(anchor.delta);
642
+ this._followAnchor(anchor.delta);
633
643
  this._scrollToOffset(this.getScrollOffset());
634
644
  }
635
645
  } else {
@@ -648,7 +658,8 @@ class Virtualizer {
648
658
  holdOffset: this._holdOffset,
649
659
  scrollOffset: this.scrollOffset,
650
660
  pendingWrite: this.pendingWrite,
651
- clampedMaxAtWrite: ((_a = this._clampedAdjustment) == null ? void 0 : _a.maxAtWrite) ?? null
661
+ clampedMaxAtWrite: ((_a = this._clampedAdjustment) == null ? void 0 : _a.maxAtWrite) ?? null,
662
+ ownWriteAt: this._ownWriteAt
652
663
  });
653
664
  };
654
665
  this._retryClampedAdjustment = () => {
@@ -914,14 +925,15 @@ class Virtualizer {
914
925
  this.options.lanes,
915
926
  // scrollOffset is measured in the scroll element and the layout total
916
927
  // from the list start, so the list ends scrollMargin further down.
917
- this._getLayoutTotalSize() + this.options.scrollMargin
928
+ this._getLayoutTotalSize() + this.options.scrollMargin,
929
+ this._trailing
918
930
  ],
919
- (measurements, outerSize, scrollOffset, lanes, listEnd) => {
931
+ (measurements, outerSize, scrollOffset, lanes, listEnd, trailing) => {
920
932
  if (measurements.length === 0 || outerSize === 0) {
921
933
  this.range = null;
922
934
  return null;
923
935
  }
924
- const maxScrollOffset = Math.max(listEnd - outerSize, 0);
936
+ const maxScrollOffset = Math.max(listEnd + trailing - outerSize, 0);
925
937
  this.range = calculateRangeImpl(
926
938
  measurements,
927
939
  outerSize,
@@ -1106,6 +1118,9 @@ class Virtualizer {
1106
1118
  this._noteClampMax();
1107
1119
  this._flushHoldIfReady(true);
1108
1120
  this._retryClampedAdjustment();
1121
+ } else if (this.options.lanes > 1 && !this.options.useCachedMeasurements && !this.laneAssignments.has(index)) {
1122
+ const item = this.getMeasurements()[index];
1123
+ if (item) this.laneAssignments.set(index, item.lane);
1109
1124
  }
1110
1125
  };
1111
1126
  this.getVirtualItems = memo(
@@ -1165,6 +1180,11 @@ class Virtualizer {
1165
1180
  }
1166
1181
  return offset;
1167
1182
  };
1183
+ this._readTrailing = (elementMax) => {
1184
+ if (elementMax === null) return;
1185
+ const trailing = elementMax + this.getSize() - this._drawnListEnd;
1186
+ this._trailing = trailing > 0 ? trailing : 0;
1187
+ };
1168
1188
  this._elementMaxScrollOffset = () => {
1169
1189
  const el = this.scrollElement;
1170
1190
  if (el === null) return null;
@@ -1330,13 +1350,17 @@ class Virtualizer {
1330
1350
  }
1331
1351
  this.scrollState = null;
1332
1352
  };
1333
- this.getTotalSize = () => Math.max(this._getLayoutTotalSize() - this._holdOffset, 0);
1353
+ this.getTotalSize = () => {
1354
+ const total = Math.max(this._getLayoutTotalSize() - this._holdOffset, 0);
1355
+ this._drawnListEnd = total + this.options.scrollMargin;
1356
+ return total;
1357
+ };
1334
1358
  this._getLayoutTotalSize = () => {
1335
1359
  var _a, _b;
1336
1360
  const measurements = this.getMeasurements();
1337
1361
  let end;
1338
1362
  if (measurements.length === 0) {
1339
- end = this.options.paddingStart;
1363
+ end = this.options.scrollMargin + this.options.paddingStart;
1340
1364
  } else if (this.options.lanes === 1) {
1341
1365
  const lastIdx = measurements.length - 1;
1342
1366
  const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
@@ -1406,6 +1430,14 @@ class Virtualizer {
1406
1430
  this.scrollState.lastTargetOffset += delta;
1407
1431
  }
1408
1432
  };
1433
+ this._landedBeforeAnchor = (state, delta) => state.phase !== "queued" && (state.stableFrames > 0 || approxEqual(this.getScrollOffset() - delta, state.lastTargetOffset));
1434
+ this._followAnchor = (delta) => {
1435
+ this._followOffsetTarget(delta);
1436
+ const state = this.scrollState;
1437
+ if ((state == null ? void 0 : state.index) != null && !state.toEnd && this._landedBeforeAnchor(state, delta)) {
1438
+ this.scrollState = null;
1439
+ }
1440
+ };
1409
1441
  this.measure = () => {
1410
1442
  this.pendingMin = null;
1411
1443
  this.itemSizeCache.clear();