@tanstack/virtual-core 3.19.10 → 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
@@ -157,9 +157,12 @@ const scrollWithAdjustments = (offset, {
157
157
  adjustments = 0,
158
158
  behavior
159
159
  }, instance) => {
160
- var _a, _b;
161
- (_b = (_a = instance.scrollElement) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, {
162
- [instance.options.horizontal ? "left" : "top"]: offset + adjustments,
160
+ var _a;
161
+ const { scrollElement, options } = instance;
162
+ const adjustedOffset = offset + adjustments;
163
+ const isRtlElement = options.horizontal && options.isRtl && scrollElement !== null && "ownerDocument" in scrollElement;
164
+ (_a = scrollElement == null ? void 0 : scrollElement.scrollTo) == null ? void 0 : _a.call(scrollElement, {
165
+ [options.horizontal ? "left" : "top"]: isRtlElement ? -adjustedOffset : adjustedOffset,
163
166
  behavior
164
167
  });
165
168
  };
@@ -210,6 +213,8 @@ class Virtualizer {
210
213
  this.scrollDirection = null;
211
214
  this._holdOffset = 0;
212
215
  this._reportedOverscroll = false;
216
+ this._trailing = 0;
217
+ this._drawnListEnd = Infinity;
213
218
  this._iosGesture = new IosGesture({
214
219
  getWindow: () => this.targetWindow,
215
220
  now: () => this.now(),
@@ -244,6 +249,7 @@ class Virtualizer {
244
249
  });
245
250
  this._lastElementOffset = null;
246
251
  this.pendingWrite = null;
252
+ this._ownWriteAt = null;
247
253
  this._clampedAdjustment = null;
248
254
  this.elementsCache = /* @__PURE__ */ new Map();
249
255
  this._resizeBatch = null;
@@ -452,7 +458,7 @@ class Virtualizer {
452
458
  this.pendingScrollAnchor = { kind: "anchor", delta: anchorDelta };
453
459
  } else {
454
460
  this._holdOffset += anchorDelta;
455
- this._followOffsetTarget(anchorDelta);
461
+ this._followAnchor(anchorDelta);
456
462
  }
457
463
  }
458
464
  };
@@ -498,8 +504,10 @@ class Virtualizer {
498
504
  this.scrollDirection = null;
499
505
  this._holdOffset = 0;
500
506
  this._reportedOverscroll = false;
507
+ this._trailing = 0;
501
508
  this._clampedAdjustment = null;
502
509
  this.pendingWrite = null;
510
+ this._ownWriteAt = null;
503
511
  this.scrollElement = null;
504
512
  this.targetWindow = null;
505
513
  };
@@ -536,6 +544,7 @@ class Virtualizer {
536
544
  this.options.observeElementOffset(this, (offset, isScrolling) => {
537
545
  var _a2, _b, _c, _d;
538
546
  const elementMax = isScrolling ? this._elementMaxScrollOffset() : null;
547
+ this._readTrailing(elementMax);
539
548
  const wasOverscrolled = this._reportedOverscroll;
540
549
  this._reportedOverscroll = offset < 0 || elementMax !== null && offset > elementMax + 1;
541
550
  const elementOffset = offset;
@@ -561,6 +570,11 @@ class Virtualizer {
561
570
  this.scrollState = null;
562
571
  }
563
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
+ }
564
578
  if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
565
579
  this._clampedAdjustment = null;
566
580
  }
@@ -611,6 +625,8 @@ class Virtualizer {
611
625
  );
612
626
  }
613
627
  this._scrollToOffset(this.getScrollOffset());
628
+ this._readTrailing(this._elementMaxScrollOffset());
629
+ this.maybeNotify();
614
630
  }
615
631
  this._noteClampMax();
616
632
  const anchor = this.pendingScrollAnchor;
@@ -619,14 +635,11 @@ class Virtualizer {
619
635
  if (anchor.kind === "anchor") {
620
636
  if (!this._canWriteScroll()) {
621
637
  this._holdOffset += anchor.delta;
622
- this._followOffsetTarget(anchor.delta);
638
+ this._followAnchor(anchor.delta);
623
639
  this.notify(false);
624
- } else if (isSmoothScroll(this.scrollState) && !approxEqual(
625
- this.getScrollOffset() - anchor.delta,
626
- this.scrollState.lastTargetOffset
627
- )) ;
640
+ } else if (isSmoothScroll(this.scrollState) && !this._landedBeforeAnchor(this.scrollState, anchor.delta)) ;
628
641
  else {
629
- this._followOffsetTarget(anchor.delta);
642
+ this._followAnchor(anchor.delta);
630
643
  this._scrollToOffset(this.getScrollOffset());
631
644
  }
632
645
  } else {
@@ -645,7 +658,8 @@ class Virtualizer {
645
658
  holdOffset: this._holdOffset,
646
659
  scrollOffset: this.scrollOffset,
647
660
  pendingWrite: this.pendingWrite,
648
- 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
649
663
  });
650
664
  };
651
665
  this._retryClampedAdjustment = () => {
@@ -911,14 +925,15 @@ class Virtualizer {
911
925
  this.options.lanes,
912
926
  // scrollOffset is measured in the scroll element and the layout total
913
927
  // from the list start, so the list ends scrollMargin further down.
914
- this._getLayoutTotalSize() + this.options.scrollMargin
928
+ this._getLayoutTotalSize() + this.options.scrollMargin,
929
+ this._trailing
915
930
  ],
916
- (measurements, outerSize, scrollOffset, lanes, listEnd) => {
931
+ (measurements, outerSize, scrollOffset, lanes, listEnd, trailing) => {
917
932
  if (measurements.length === 0 || outerSize === 0) {
918
933
  this.range = null;
919
934
  return null;
920
935
  }
921
- const maxScrollOffset = Math.max(listEnd - outerSize, 0);
936
+ const maxScrollOffset = Math.max(listEnd + trailing - outerSize, 0);
922
937
  this.range = calculateRangeImpl(
923
938
  measurements,
924
939
  outerSize,
@@ -1103,6 +1118,9 @@ class Virtualizer {
1103
1118
  this._noteClampMax();
1104
1119
  this._flushHoldIfReady(true);
1105
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);
1106
1124
  }
1107
1125
  };
1108
1126
  this.getVirtualItems = memo(
@@ -1162,6 +1180,11 @@ class Virtualizer {
1162
1180
  }
1163
1181
  return offset;
1164
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
+ };
1165
1188
  this._elementMaxScrollOffset = () => {
1166
1189
  const el = this.scrollElement;
1167
1190
  if (el === null) return null;
@@ -1254,7 +1277,7 @@ class Virtualizer {
1254
1277
  index = Math.max(0, Math.min(index, this.options.count - 1));
1255
1278
  const size = this.getSize();
1256
1279
  const scrollOffset = this.getScrollOffset();
1257
- const item = this.measurementsCache[index];
1280
+ const item = this.getMeasurements()[index];
1258
1281
  if (!item) return;
1259
1282
  if (align === "auto") {
1260
1283
  if (item.end >= scrollOffset + size - this.options.scrollPaddingEnd) {
@@ -1327,13 +1350,17 @@ class Virtualizer {
1327
1350
  }
1328
1351
  this.scrollState = null;
1329
1352
  };
1330
- 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
+ };
1331
1358
  this._getLayoutTotalSize = () => {
1332
1359
  var _a, _b;
1333
1360
  const measurements = this.getMeasurements();
1334
1361
  let end;
1335
1362
  if (measurements.length === 0) {
1336
- end = this.options.paddingStart;
1363
+ end = this.options.scrollMargin + this.options.paddingStart;
1337
1364
  } else if (this.options.lanes === 1) {
1338
1365
  const lastIdx = measurements.length - 1;
1339
1366
  const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
@@ -1403,6 +1430,14 @@ class Virtualizer {
1403
1430
  this.scrollState.lastTargetOffset += delta;
1404
1431
  }
1405
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
+ };
1406
1441
  this.measure = () => {
1407
1442
  this.pendingMin = null;
1408
1443
  this.itemSizeCache.clear();