react-anchorlist 0.3.3 → 0.3.5

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.
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { jsx as W, jsxs as le } from "react/jsx-runtime";
2
- import * as te from "react";
3
- import { useRef as g, useCallback as C, useState as U, useEffect as X, useLayoutEffect as Q, forwardRef as me, useImperativeHandle as de } from "react";
4
- class ge {
1
+ import { jsx as J, jsxs as ae } from "react/jsx-runtime";
2
+ import * as oe from "react";
3
+ import { useRef as m, useCallback as k, useState as X, useEffect as $, useLayoutEffect as Z, forwardRef as ge, useImperativeHandle as ve } from "react";
4
+ class Te {
5
5
  constructor(e, t) {
6
6
  this.defaultSize = t, this.sizes = e > 0 ? Array(e).fill(t) : [], this.offsets = e > 0 ? Array(e).fill(0) : [], e > 0 && this._recalcFrom(0);
7
7
  }
@@ -25,7 +25,7 @@ class ge {
25
25
  }
26
26
  append(e) {
27
27
  const t = this.sizes.length;
28
- for (let n = 0; n < e; n++)
28
+ for (let s = 0; s < e; s++)
29
29
  this.sizes.push(this.defaultSize), this.offsets.push(0);
30
30
  this._recalcFrom(t);
31
31
  }
@@ -48,7 +48,7 @@ class ge {
48
48
  return this.sizes;
49
49
  }
50
50
  }
51
- class pe {
51
+ class ye {
52
52
  constructor() {
53
53
  this.cache = /* @__PURE__ */ new Map();
54
54
  }
@@ -69,452 +69,485 @@ class pe {
69
69
  }
70
70
  /** Re-applies all cached sizes to the OffsetMap using a key→index map */
71
71
  applyToOffsetMap(e, t) {
72
- for (const [n, c] of this.cache) {
73
- const o = t.get(n);
74
- o !== void 0 && e.setSize(o, c);
72
+ for (const [s, c] of this.cache) {
73
+ const n = t.get(s);
74
+ n !== void 0 && e.setSize(n, c);
75
75
  }
76
76
  }
77
77
  }
78
- function ce(a, e) {
78
+ function ue(a, e) {
79
79
  if (a.length === 0) return 0;
80
- let t = 0, n = a.length - 1;
81
- for (; t < n; ) {
82
- const c = t + n >> 1;
83
- (a[c] ?? 0) < e ? t = c + 1 : n = c;
80
+ let t = 0, s = a.length - 1;
81
+ for (; t < s; ) {
82
+ const c = t + s >> 1;
83
+ (a[c] ?? 0) < e ? t = c + 1 : s = c;
84
84
  }
85
85
  return Math.max(0, t > 0 && (a[t] ?? 0) > e ? t - 1 : t);
86
86
  }
87
- function ve(a, e, t) {
87
+ function Se(a, e, t) {
88
88
  if (a.length === 0) return 0;
89
- let n = 0, c = a.length - 1, o = 0;
90
- for (; n <= c; ) {
91
- const r = n + c >> 1;
92
- (a[r] ?? 0) < t ? (o = r, n = r + 1) : c = r - 1;
89
+ let s = 0, c = a.length - 1, n = 0;
90
+ for (; s <= c; ) {
91
+ const r = s + c >> 1;
92
+ (a[r] ?? 0) < t ? (n = r, s = r + 1) : c = r - 1;
93
93
  }
94
- return o;
94
+ return n;
95
95
  }
96
- function Te(a) {
97
- const { firstVisible: e, lastVisible: t, itemCount: n, overscan: c } = a;
98
- return n === 0 ? { start: 0, end: -1 } : {
96
+ function Ae(a) {
97
+ const { firstVisible: e, lastVisible: t, itemCount: s, overscan: c } = a;
98
+ return s === 0 ? { start: 0, end: -1 } : {
99
99
  start: Math.max(0, e - c),
100
- end: Math.min(n - 1, t + c)
100
+ end: Math.min(s - 1, t + c)
101
101
  };
102
102
  }
103
- function ye(a, e, t, n) {
104
- const c = n == null ? void 0 : n.reconcile, o = g(null);
105
- return C(
106
- (r, S) => {
107
- const l = a.current, f = e.current;
108
- if (!l || !f) return;
109
- o.current !== null && (cancelAnimationFrame(o.current), o.current = null);
110
- let y, u;
111
- if (typeof r == "object" && r !== null ? (y = r.index, u = {
103
+ function ze(a, e, t, s) {
104
+ const c = s == null ? void 0 : s.reconcile, n = m(null);
105
+ return k(
106
+ (r, y) => {
107
+ const l = a.current, h = e.current;
108
+ if (!l || !h) return;
109
+ n.current !== null && (cancelAnimationFrame(n.current), n.current = null);
110
+ let v, f;
111
+ if (typeof r == "object" && r !== null ? (v = r.index, f = {
112
112
  align: r.align,
113
113
  behavior: r.behavior,
114
114
  offset: r.offset
115
- }) : (y = r, u = S), !Number.isFinite(y)) return;
116
- const m = Math.max(0, Math.min(Math.floor(y), f.count - 1));
117
- f.getOffset(m), f.getSize(m);
118
- const b = (u == null ? void 0 : u.align) ?? "start", A = (u == null ? void 0 : u.behavior) ?? "auto", K = (u == null ? void 0 : u.offset) ?? 0, h = () => {
119
- var p;
120
- const H = e.current, i = a.current;
121
- if (!H || !i) return 0;
122
- const d = ((p = t == null ? void 0 : t.current) == null ? void 0 : p.offsetTop) ?? 0, R = H.getOffset(m), w = H.getSize(m);
123
- return b === "start" ? d + R + K : b === "center" ? d + R - i.clientHeight / 2 + w / 2 + K : d + R - i.clientHeight + w + K;
124
- }, E = h();
125
- ((H, i) => {
126
- typeof l.scrollTo == "function" ? l.scrollTo({ top: H, behavior: i }) : l.scrollTop = H;
127
- })(Math.max(0, E), A);
128
- const M = performance.now();
129
- let z = 0;
130
- const L = 12, q = 300, V = 1, k = () => {
131
- o.current = null;
115
+ }) : (v = r, f = y), !Number.isFinite(v)) return;
116
+ const p = Math.max(0, Math.min(Math.floor(v), h.count - 1));
117
+ h.getOffset(p), h.getSize(p);
118
+ const F = (f == null ? void 0 : f.align) ?? "start", M = (f == null ? void 0 : f.behavior) ?? "auto", K = (f == null ? void 0 : f.offset) ?? 0, i = () => {
119
+ var B;
120
+ const H = e.current, u = a.current;
121
+ if (!H || !u) return 0;
122
+ const A = ((B = t == null ? void 0 : t.current) == null ? void 0 : B.offsetTop) ?? 0, T = H.getOffset(p), w = H.getSize(p);
123
+ return F === "start" ? A + T + K : F === "center" ? A + T - u.clientHeight / 2 + w / 2 + K : A + T - u.clientHeight + w + K;
124
+ }, E = i();
125
+ ((H, u) => {
126
+ typeof l.scrollTo == "function" ? l.scrollTo({ top: H, behavior: u }) : l.scrollTop = H;
127
+ })(Math.max(0, E), M);
128
+ const b = performance.now();
129
+ let S = 0;
130
+ const C = 12, j = 300, U = 1, L = () => {
131
+ n.current = null;
132
132
  const H = a.current;
133
133
  if (!H) return;
134
- const i = Math.max(0, h()), d = Math.abs(H.scrollTop - i), R = performance.now() - M >= q, w = z >= L;
135
- d <= V || R || w || (typeof H.scrollTo == "function" ? H.scrollTo({ top: i, behavior: "auto" }) : H.scrollTop = i, z += 1, o.current = requestAnimationFrame(k));
134
+ const u = Math.max(0, i()), A = Math.abs(H.scrollTop - u), T = performance.now() - b >= j, w = S >= C;
135
+ A <= U || T || w || (typeof H.scrollTo == "function" ? H.scrollTo({ top: u, behavior: "auto" }) : H.scrollTop = u, S += 1, n.current = requestAnimationFrame(L));
136
136
  };
137
- o.current = requestAnimationFrame(k);
137
+ n.current = requestAnimationFrame(L);
138
138
  },
139
139
  [a, e, t, c]
140
140
  );
141
141
  }
142
- function ie(a) {
143
- var se;
144
- const { items: e, getKey: t, estimatedItemSize: n, overscan: c, initialAlignment: o } = a, r = g(null), S = g(null), l = g(null), f = g(new pe()), y = g([]), u = g(/* @__PURE__ */ new Map()), m = g(e.length), b = g(null), A = g(null), K = g(!1), h = g(0), E = g(0), T = g(null), M = g(!1), z = g(null), L = g(0), q = g(0), V = g(0), k = g(null), H = g(!1), [, i] = U(0), d = C(() => i((s) => s + 1), []);
145
- l.current || (l.current = new ge(e.length, n));
146
- const R = e.length > 0 ? t(e[0], 0) : null, w = e.length > 0 ? t(e[e.length - 1], e.length - 1) : null;
147
- if (e.length !== m.current || R !== b.current || w !== A.current) {
148
- const s = l.current, v = e.map((j, _) => t(j, _)), x = y.current, I = x.length, P = v.length;
142
+ function fe(a) {
143
+ var ie;
144
+ const { items: e, getKey: t, estimatedItemSize: s, overscan: c, initialAlignment: n } = a, r = m(null), y = m(null), l = m(null), h = m(new ye()), v = m([]), f = m(/* @__PURE__ */ new Map()), p = m(e.length), F = m(null), M = m(null), K = m(!1), i = m(0), E = m(0), g = m(null), b = m(!1), S = m(null), C = m(0), j = m(0), U = m(0), L = m(null), H = m(!1), u = m(!1), [, A] = X(0), T = k(() => A((o) => o + 1), []);
145
+ l.current || (l.current = new Te(e.length, s));
146
+ const w = e.length > 0 ? t(e[0], 0) : null, B = e.length > 0 ? t(e[e.length - 1], e.length - 1) : null;
147
+ if (e.length !== p.current || w !== F.current || B !== M.current) {
148
+ const o = l.current, d = e.map((V, W) => t(V, W)), x = v.current, I = x.length, P = d.length;
149
149
  if (P === 0)
150
- s.resize(0);
150
+ o.resize(0);
151
151
  else if (I === 0)
152
- s.resize(P);
152
+ o.resize(P);
153
153
  else if (P > I) {
154
- const j = P - I;
155
- x.length > 0 && j >= 0 && v[j] === x[0] ? s.prepend(j) : s.resize(P);
156
- } else P < I, s.resize(P);
157
- const F = /* @__PURE__ */ new Map();
158
- v.forEach((j, _) => F.set(j, _)), u.current = F, f.current.applyToOffsetMap(s, F), y.current = v, m.current = e.length, b.current = R, A.current = w;
154
+ const V = P - I;
155
+ x.length > 0 && V >= 0 && d[V] === x[0] ? o.prepend(V) : o.resize(P);
156
+ } else P < I, o.resize(P);
157
+ const R = /* @__PURE__ */ new Map();
158
+ d.forEach((V, W) => R.set(V, W)), f.current = R, h.current.applyToOffsetMap(o, R), v.current = d, p.current = e.length, F.current = w, M.current = B;
159
159
  }
160
- const B = C(() => {
161
- k.current === null && (k.current = requestAnimationFrame(() => {
162
- k.current = null;
163
- const s = r.current;
164
- if (!s) return;
165
- const v = L.current;
166
- Math.abs(v) < 0.01 || (L.current = 0, q.current += v, V.current += v, s.scrollTop += v, h.current = s.scrollTop, H.current = !0, d());
160
+ const z = k(() => {
161
+ L.current === null && (L.current = requestAnimationFrame(() => {
162
+ L.current = null;
163
+ const o = r.current;
164
+ if (!o) return;
165
+ const d = C.current;
166
+ Math.abs(d) < 0.01 || (C.current = 0, j.current += d, U.current += d, o.scrollTop += d, i.current = o.scrollTop, H.current = !0, T());
167
167
  }));
168
- }, [d]);
169
- X(() => {
170
- const s = r.current;
171
- if (!s) return;
172
- const v = () => {
173
- T.current === null && (T.current = requestAnimationFrame(() => {
174
- T.current = null, h.current = s.scrollTop, E.current = s.clientHeight, H.current && (H.current = !1, q.current = 0, V.current = 0), d();
168
+ }, [T]);
169
+ $(() => {
170
+ const o = r.current;
171
+ if (!o) return;
172
+ const d = () => {
173
+ g.current === null && (g.current = requestAnimationFrame(() => {
174
+ g.current = null, i.current = o.scrollTop, E.current = o.clientHeight, H.current && (H.current = !1, j.current = 0, U.current = 0), T();
175
175
  }));
176
176
  };
177
- return s.addEventListener("scroll", v, { passive: !0 }), () => {
178
- s.removeEventListener("scroll", v), T.current !== null && (cancelAnimationFrame(T.current), T.current = null), k.current !== null && (cancelAnimationFrame(k.current), k.current = null);
177
+ return o.addEventListener("scroll", d, { passive: !0 }), () => {
178
+ o.removeEventListener("scroll", d), g.current !== null && (cancelAnimationFrame(g.current), g.current = null), L.current !== null && (cancelAnimationFrame(L.current), L.current = null);
179
179
  };
180
- }, [d]), X(() => {
181
- const s = r.current;
182
- if (!s) return;
183
- E.current = s.clientHeight, h.current = s.scrollTop;
184
- const v = new ResizeObserver(([x]) => {
185
- x && (E.current = x.contentRect.height, d());
180
+ }, [T]), $(() => {
181
+ const o = r.current;
182
+ if (!o) return;
183
+ E.current = o.clientHeight, i.current = o.scrollTop;
184
+ const d = new ResizeObserver(([x]) => {
185
+ x && (E.current = x.contentRect.height, T());
186
186
  });
187
- return v.observe(s), () => v.disconnect();
188
- }, [d]), Q(() => {
187
+ return d.observe(o), () => d.disconnect();
188
+ }, [T]), Z(() => {
189
189
  if (K.current || e.length === 0) return;
190
- const s = r.current;
191
- if (s) {
192
- if (o === "bottom") {
193
- s.scrollTop = s.scrollHeight, h.current = s.scrollTop, E.current = s.clientHeight, M.current = !0, z.current !== null && cancelAnimationFrame(z.current);
194
- const v = performance.now();
195
- let x = 0, I = s.scrollHeight;
190
+ const o = r.current;
191
+ if (o) {
192
+ if (n === "bottom") {
193
+ o.scrollTop = o.scrollHeight, i.current = o.scrollTop, E.current = o.clientHeight, b.current = !0, S.current !== null && cancelAnimationFrame(S.current);
194
+ const d = performance.now();
195
+ let x = 0, I = o.scrollHeight;
196
196
  const P = () => {
197
- if (!M.current) {
198
- z.current = null;
197
+ if (!b.current) {
198
+ S.current = null;
199
199
  return;
200
200
  }
201
- const F = s.scrollHeight, j = Math.abs(F - I);
202
- I = F, j < 1 ? x += 1 : x = 0, s.scrollTop = s.scrollHeight, h.current = s.scrollTop;
203
- const _ = performance.now() - v;
204
- if (x >= 3 || _ >= 500) {
205
- M.current = !1, z.current = null;
201
+ const R = o.scrollHeight, V = Math.abs(R - I);
202
+ I = R, V < 1 ? x += 1 : x = 0, o.scrollTop = o.scrollHeight, i.current = o.scrollTop;
203
+ const W = performance.now() - d;
204
+ if (x >= 3 || W >= 500) {
205
+ b.current = !1, S.current = null;
206
206
  return;
207
207
  }
208
- z.current = requestAnimationFrame(P);
208
+ S.current = requestAnimationFrame(P);
209
209
  };
210
- z.current = requestAnimationFrame(P);
210
+ S.current = requestAnimationFrame(P);
211
211
  }
212
212
  K.current = !0;
213
213
  }
214
- }, [o, e.length]), X(() => {
215
- e.length === 0 && (K.current = !1, M.current = !1, z.current !== null && (cancelAnimationFrame(z.current), z.current = null));
214
+ }, [n, e.length]), $(() => {
215
+ e.length === 0 && (K.current = !1, b.current = !1, S.current !== null && (cancelAnimationFrame(S.current), S.current = null));
216
216
  }, [e.length]);
217
- const N = C((s, v) => {
218
- var oe;
217
+ const O = k((o, d) => {
218
+ var Q;
219
219
  const x = l.current;
220
- if (!x || f.current.get(s) === v) return;
221
- f.current.set(s, v);
222
- const P = u.current.get(s);
220
+ if (!x || h.current.get(o) === d) return;
221
+ h.current.set(o, d);
222
+ const P = f.current.get(o);
223
223
  if (P === void 0) return;
224
- const F = x.getSize(P), j = v - F, _ = r.current;
225
- _ && !M.current && j !== 0 && x.getOffset(P) + F + (((oe = S.current) == null ? void 0 : oe.offsetTop) ?? 0) < _.scrollTop && (L.current += j, B()), x.setSize(P, v) && d();
226
- }, [B, d]), D = C(
227
- (s, v = "auto") => {
224
+ const R = x.getSize(P), V = d - R, W = r.current;
225
+ W && !b.current && !u.current && V !== 0 && x.getOffset(P) + R + (((Q = y.current) == null ? void 0 : Q.offsetTop) ?? 0) < W.scrollTop && (C.current += V, z()), x.setSize(P, d) && T();
226
+ }, [z, T]), N = k(
227
+ (o, d = "auto") => {
228
228
  var x;
229
- (x = r.current) == null || x.scrollTo({ top: s, behavior: v });
229
+ (x = r.current) == null || x.scrollTo({ top: o, behavior: d });
230
230
  },
231
231
  []
232
- ), $ = ye(r, l, S, {
232
+ ), G = ze(r, l, y, {
233
233
  reconcile: !0
234
- }), G = C(() => {
235
- var _;
236
- const s = r.current, v = l.current;
237
- if (!s || !v || v.count === 0) return null;
238
- const x = ((_ = S.current) == null ? void 0 : _.offsetTop) ?? 0, I = Math.max(0, s.scrollTop - x), P = v.getOffsets(), F = ce(P, I);
234
+ }), q = k(() => {
235
+ var ne;
236
+ const o = r.current, d = l.current;
237
+ if (!o || !d || d.count === 0) return null;
238
+ const x = ((ne = y.current) == null ? void 0 : ne.offsetTop) ?? 0, I = Math.max(0, o.scrollTop - x), P = d.getOffsets(), R = ue(P, I), V = v.current[R] ?? null, W = [];
239
+ for (let Q = R; Q < Math.min(d.count, R + 6); Q++) {
240
+ const se = v.current[Q] ?? null;
241
+ se !== null && W.push({
242
+ key: se,
243
+ offsetWithinItem: I - d.getOffset(Q)
244
+ });
245
+ }
239
246
  return {
240
- key: y.current[F] ?? null,
241
- offsetWithinItem: I - v.getOffset(F),
242
- scrollTop: s.scrollTop,
243
- scrollHeight: s.scrollHeight
247
+ key: V,
248
+ offsetWithinItem: I - d.getOffset(R),
249
+ candidates: W,
250
+ scrollTop: o.scrollTop,
251
+ scrollHeight: o.scrollHeight
244
252
  };
245
- }, []), J = C(
246
- (s, v) => {
247
- var F;
253
+ }, []), ee = k(
254
+ (o, d) => {
255
+ var R;
248
256
  const x = l.current;
249
257
  if (!x) return null;
250
- const I = u.current.get(s);
251
- return I === void 0 ? null : (((F = S.current) == null ? void 0 : F.offsetTop) ?? 0) + x.getOffset(I) + v;
258
+ const I = f.current.get(o);
259
+ return I === void 0 ? null : (((R = y.current) == null ? void 0 : R.offsetTop) ?? 0) + x.getOffset(I) + d;
252
260
  },
253
261
  []
254
- ), O = l.current, ae = O ? O.totalSize() : 0, Y = r.current, Z = (Y == null ? void 0 : Y.scrollTop) ?? h.current, re = (Y == null ? void 0 : Y.clientHeight) ?? E.current, fe = ((se = S.current) == null ? void 0 : se.offsetTop) ?? 0, ne = Math.max(0, Z - fe), ee = [];
255
- if (O && O.count > 0 && re > 0) {
256
- const s = O.getOffsets(), v = O.getSizes(), x = ce(s, ne), I = ve(
257
- s,
258
- v,
259
- ne + re
260
- ), P = Te({
262
+ ), _ = l.current, me = _ ? _.totalSize() : 0, Y = r.current, te = (Y == null ? void 0 : Y.scrollTop) ?? i.current, ce = (Y == null ? void 0 : Y.clientHeight) ?? E.current, de = ((ie = y.current) == null ? void 0 : ie.offsetTop) ?? 0, le = Math.max(0, te - de), re = [];
263
+ if (_ && _.count > 0 && ce > 0) {
264
+ const o = _.getOffsets(), d = _.getSizes(), x = ue(o, le), I = Se(
265
+ o,
266
+ d,
267
+ le + ce
268
+ ), P = Ae({
261
269
  firstVisible: x,
262
270
  lastVisible: I,
263
- itemCount: O.count,
271
+ itemCount: _.count,
264
272
  overscan: c
265
273
  });
266
- for (let F = P.start; F <= P.end && F < e.length; F++)
267
- ee.push({
268
- key: y.current[F] ?? t(e[F], F),
269
- index: F,
270
- start: O.getOffset(F),
271
- size: O.getSize(F),
272
- data: e[F]
274
+ for (let R = P.start; R <= P.end && R < e.length; R++)
275
+ re.push({
276
+ key: v.current[R] ?? t(e[R], R),
277
+ index: R,
278
+ start: _.getOffset(R),
279
+ size: _.getSize(R),
280
+ data: e[R]
273
281
  });
274
- } else if (O && O.count > 0) {
275
- const s = Math.min(e.length, c * 2 + 1), v = o === "bottom" ? Math.max(0, e.length - s) : 0, x = v + s - 1;
276
- for (let I = v; I <= x; I++)
277
- ee.push({
278
- key: y.current[I] ?? t(e[I], I),
282
+ } else if (_ && _.count > 0) {
283
+ const o = Math.min(e.length, c * 2 + 1), d = n === "bottom" ? Math.max(0, e.length - o) : 0, x = d + o - 1;
284
+ for (let I = d; I <= x; I++)
285
+ re.push({
286
+ key: v.current[I] ?? t(e[I], I),
279
287
  index: I,
280
- start: O.getOffset(I),
281
- size: O.getSize(I),
288
+ start: _.getOffset(I),
289
+ size: _.getSize(I),
282
290
  data: e[I]
283
291
  });
284
292
  }
285
- const he = Y ? Y.scrollHeight - Y.scrollTop - Y.clientHeight : 1 / 0;
293
+ const pe = Y ? Y.scrollHeight - Y.scrollTop - Y.clientHeight : 1 / 0;
286
294
  return {
287
295
  scrollerRef: r,
288
- innerRef: S,
289
- virtualItems: ee,
290
- totalSize: ae,
291
- measureItem: N,
292
- scrollToIndex: $,
293
- scrollToOffset: D,
294
- captureAnchorSnapshot: G,
295
- resolveAnchorTop: J,
296
- isAtTop: Z <= 1,
297
- isAtBottom: he <= 1,
298
- scrollTop: Z
296
+ innerRef: y,
297
+ virtualItems: re,
298
+ totalSize: me,
299
+ measureItem: O,
300
+ scrollToIndex: G,
301
+ scrollToOffset: N,
302
+ captureAnchorSnapshot: q,
303
+ resolveAnchorTop: ee,
304
+ isAtTop: te <= 1,
305
+ isAtBottom: pe <= 1,
306
+ scrollTop: te,
307
+ jumpSuppressedRef: u
299
308
  };
300
309
  }
301
- function ze(a) {
310
+ function Re(a) {
311
+ var c;
312
+ const { snapshot: e, currentScrollHeight: t, resolveAnchorTop: s } = a;
313
+ if (e.key !== null) {
314
+ const n = s(e.key, e.offsetWithinItem);
315
+ if (n !== null) return n;
316
+ }
317
+ if ((c = e.candidates) != null && c.length)
318
+ for (const n of e.candidates) {
319
+ const r = s(n.key, n.offsetWithinItem);
320
+ if (r !== null) return r;
321
+ }
322
+ return e.scrollTop + (t - e.scrollHeight);
323
+ }
324
+ function xe(a) {
302
325
  const {
303
326
  scrollerRef: e,
304
327
  itemCount: t,
305
- captureAnchorSnapshot: n,
328
+ captureAnchorSnapshot: s,
306
329
  resolveAnchorTop: c,
307
- onRestored: o
308
- } = a, r = g(null), S = g(!1), l = g({
330
+ onRestored: n
331
+ } = a, r = m(null), y = m(!1), l = m({
309
332
  first: null,
310
333
  second: null,
311
334
  timeout: null
312
- }), f = C(() => {
313
- const { first: u, second: m, timeout: b } = l.current;
314
- u && cancelAnimationFrame(u), m && cancelAnimationFrame(m), b && clearTimeout(b), l.current = { first: null, second: null, timeout: null };
315
- }, []), y = C(() => {
316
- const u = e.current;
317
- if (!u) return;
318
- const m = n();
319
- r.current = m ?? {
335
+ }), h = k(() => {
336
+ const { first: f, second: p, timeout: F } = l.current;
337
+ f && cancelAnimationFrame(f), p && cancelAnimationFrame(p), F && clearTimeout(F), l.current = { first: null, second: null, timeout: null };
338
+ }, []), v = k(() => {
339
+ const f = e.current;
340
+ if (!f) return;
341
+ const p = s();
342
+ r.current = p ?? {
320
343
  key: null,
321
344
  offsetWithinItem: 0,
322
- scrollTop: u.scrollTop,
323
- scrollHeight: u.scrollHeight
324
- }, S.current = !0;
325
- }, [e, n]);
326
- return Q(() => {
327
- if (!S.current) return;
328
- const u = e.current, m = r.current;
329
- if (!u || !m) return;
330
- S.current = !1;
331
- const b = () => {
332
- let A = null;
333
- m.key !== null && (A = c(m.key, m.offsetWithinItem)), A === null && (A = m.scrollTop + (u.scrollHeight - m.scrollHeight)), Number.isFinite(A) && Math.abs(u.scrollTop - A) > 1 && (u.scrollTop = A);
345
+ scrollTop: f.scrollTop,
346
+ scrollHeight: f.scrollHeight
347
+ }, y.current = !0;
348
+ }, [e, s]);
349
+ return Z(() => {
350
+ if (!y.current) return;
351
+ const f = e.current, p = r.current;
352
+ if (!f || !p) return;
353
+ y.current = !1;
354
+ const F = () => {
355
+ const M = Re({
356
+ snapshot: p,
357
+ currentScrollHeight: f.scrollHeight,
358
+ resolveAnchorTop: c
359
+ });
360
+ Number.isFinite(M) && Math.abs(f.scrollTop - M) > 1 && (f.scrollTop = M);
334
361
  };
335
- return f(), b(), o == null || o(), l.current.first = requestAnimationFrame(() => {
336
- l.current.first = null, b(), l.current.second = requestAnimationFrame(() => {
337
- l.current.second = null, b();
362
+ return h(), F(), n == null || n(), l.current.first = requestAnimationFrame(() => {
363
+ l.current.first = null, F(), l.current.second = requestAnimationFrame(() => {
364
+ l.current.second = null, F();
338
365
  });
339
366
  }), l.current.timeout = setTimeout(() => {
340
- l.current.timeout = null, b();
341
- }, 90), () => f();
342
- }, [t, e, c, f, o]), { prepareAnchor: y };
367
+ l.current.timeout = null, F();
368
+ }, 90), () => h();
369
+ }, [t, e, c, h, n]), { prepareAnchor: v };
343
370
  }
344
- function Se(a) {
345
- const { previous: e, distanceFromBottom: t, threshold: n, hysteresis: c } = a;
346
- if (!c) return t <= n;
347
- const o = Math.max(0, c.enter), r = Math.max(o, c.leave);
348
- return e ? t <= r : t <= o;
371
+ function Ie(a) {
372
+ const { previous: e, distanceFromBottom: t, threshold: s, hysteresis: c } = a;
373
+ if (!c) return t <= s;
374
+ const n = Math.max(0, c.enter), r = Math.max(n, c.leave);
375
+ return e ? t <= r : t <= n;
349
376
  }
350
- function Ae(a, e) {
351
- const t = typeof e == "number" ? e : e.threshold ?? 200, n = typeof e == "number" ? void 0 : e.hysteresis, [c, o] = U(!0), r = g(null), S = g(!0);
352
- return X(() => {
377
+ function Fe(a, e) {
378
+ const t = typeof e == "number" ? e : e.threshold ?? 200, s = typeof e == "number" ? void 0 : e.hysteresis, [c, n] = X(!0), r = m(null), y = m(!0);
379
+ return $(() => {
353
380
  const l = a.current;
354
381
  if (!l) return;
355
- const f = () => {
356
- const u = l.scrollHeight - l.scrollTop - l.clientHeight, m = Se({
357
- previous: S.current,
358
- distanceFromBottom: u,
382
+ const h = () => {
383
+ const f = l.scrollHeight - l.scrollTop - l.clientHeight, p = Ie({
384
+ previous: y.current,
385
+ distanceFromBottom: f,
359
386
  threshold: t,
360
- hysteresis: n
387
+ hysteresis: s
361
388
  });
362
- S.current = m, o(m);
363
- }, y = () => {
364
- r.current !== null && cancelAnimationFrame(r.current), r.current = requestAnimationFrame(f);
389
+ y.current = p, n(p);
390
+ }, v = () => {
391
+ r.current !== null && cancelAnimationFrame(r.current), r.current = requestAnimationFrame(h);
365
392
  };
366
- return l.addEventListener("scroll", y, { passive: !0 }), f(), () => {
367
- l.removeEventListener("scroll", y), r.current !== null && cancelAnimationFrame(r.current);
393
+ return l.addEventListener("scroll", v, { passive: !0 }), h(), () => {
394
+ l.removeEventListener("scroll", v), r.current !== null && cancelAnimationFrame(r.current);
368
395
  };
369
- }, [a, t, n == null ? void 0 : n.enter, n == null ? void 0 : n.leave]), c;
396
+ }, [a, t, s == null ? void 0 : s.enter, s == null ? void 0 : s.leave]), c;
370
397
  }
371
- function Re(a) {
372
- const { itemCount: e, firstKey: t, lastKey: n, isAtBottom: c, scrollToIndex: o, mode: r } = a, S = g(e), l = g(t), f = g(n);
373
- Q(() => {
398
+ function be(a) {
399
+ const { itemCount: e, firstKey: t, lastKey: s, isAtBottom: c, scrollToIndex: n, mode: r } = a, y = m(e), l = m(t), h = m(s);
400
+ Z(() => {
374
401
  if (!r) {
375
- S.current = e, l.current = t, f.current = n;
402
+ y.current = e, l.current = t, h.current = s;
376
403
  return;
377
404
  }
378
- const y = S.current, u = l.current, m = f.current;
379
- e > y && t === u && n !== m && c && e > 0 && o(e - 1, {
405
+ const v = y.current, f = l.current, p = h.current;
406
+ e > v && t === f && s !== p && c && e > 0 && n(e - 1, {
380
407
  align: "end",
381
408
  behavior: r === "smooth" ? "smooth" : "auto"
382
- }), S.current = e, l.current = t, f.current = n;
383
- }, [e, t, n, c, o, r]);
409
+ }), y.current = e, l.current = t, h.current = s;
410
+ }, [e, t, s, c, n, r]);
384
411
  }
385
- function xe(a) {
412
+ function He(a) {
386
413
  const {
387
414
  items: e,
388
415
  getKey: t,
389
- estimatedItemSize: n = 80,
416
+ estimatedItemSize: s = 80,
390
417
  overscan: c = 20,
391
- atBottomThreshold: o = 200,
418
+ atBottomThreshold: n = 200,
392
419
  atBottomHysteresis: r,
393
- followOutput: S = "auto",
420
+ followOutput: y = "auto",
394
421
  initialAlignment: l = "bottom",
395
- scrollModifier: f = null,
396
- onStartReached: y,
397
- onEndReached: u,
398
- startReachedThreshold: m = 300,
399
- endReachedThreshold: b = 300,
400
- scrollToMessageKey: A,
422
+ scrollModifier: h = null,
423
+ onStartReached: v,
424
+ onEndReached: f,
425
+ startReachedThreshold: p = 300,
426
+ endReachedThreshold: F = 300,
427
+ scrollToMessageKey: M,
401
428
  onScrollToMessageComplete: K
402
- } = a, h = ie({
429
+ } = a, i = fe({
403
430
  items: e,
404
431
  getKey: t,
405
- estimatedItemSize: n,
432
+ estimatedItemSize: s,
406
433
  overscan: c,
407
434
  initialAlignment: l
408
- }), E = Ae(h.scrollerRef, {
409
- threshold: o,
435
+ }), E = Fe(i.scrollerRef, {
436
+ threshold: n,
410
437
  hysteresis: r ?? { enter: 80, leave: 160 }
411
- }), [, T] = U(0), M = C(() => T((p) => p + 1), []), { prepareAnchor: z } = ze({
412
- scrollerRef: h.scrollerRef,
438
+ }), [, g] = X(0), b = m(null), S = k(() => {
439
+ g((z) => z + 1), b.current && clearTimeout(b.current), b.current = setTimeout(() => {
440
+ i.jumpSuppressedRef.current = !1, b.current = null;
441
+ }, 200);
442
+ }, [i.jumpSuppressedRef]), { prepareAnchor: C } = xe({
443
+ scrollerRef: i.scrollerRef,
413
444
  itemCount: e.length,
414
- captureAnchorSnapshot: h.captureAnchorSnapshot,
415
- resolveAnchorTop: h.resolveAnchorTop,
416
- onRestored: M
417
- }), L = e.length > 0 ? t(e[0], 0) : null, q = e.length > 0 ? t(e[e.length - 1], e.length - 1) : null;
418
- Re({
445
+ captureAnchorSnapshot: i.captureAnchorSnapshot,
446
+ resolveAnchorTop: i.resolveAnchorTop,
447
+ onRestored: S
448
+ }), j = k(() => {
449
+ i.jumpSuppressedRef.current = !0, C();
450
+ }, [C, i.jumpSuppressedRef]), U = e.length > 0 ? t(e[0], 0) : null, L = e.length > 0 ? t(e[e.length - 1], e.length - 1) : null;
451
+ be({
419
452
  itemCount: e.length,
420
- firstKey: L,
421
- lastKey: q,
453
+ firstKey: U,
454
+ lastKey: L,
422
455
  isAtBottom: E,
423
- scrollToIndex: h.scrollToIndex,
424
- mode: S ?? !1
456
+ scrollToIndex: i.scrollToIndex,
457
+ mode: y ?? !1
425
458
  });
426
- const V = C(
427
- (p = "auto") => {
428
- e.length !== 0 && h.scrollToIndex(e.length - 1, { align: "end", behavior: p });
459
+ const H = k(
460
+ (z = "auto") => {
461
+ e.length !== 0 && i.scrollToIndex(e.length - 1, { align: "end", behavior: z });
429
462
  },
430
- [e.length, h]
431
- ), k = C(
432
- (p, B) => {
433
- const N = e.findIndex((D, $) => t(D, $) === p);
434
- N !== -1 && h.scrollToIndex(N, B);
463
+ [e.length, i]
464
+ ), u = k(
465
+ (z, O) => {
466
+ const N = e.findIndex((G, q) => t(G, q) === z);
467
+ N !== -1 && i.scrollToIndex(N, O);
435
468
  },
436
- [e, t, h]
437
- ), H = g(null);
438
- Q(() => {
439
- if (f && H.current !== f.id) {
440
- if (H.current = f.id, f.type === "prepend") {
441
- z();
469
+ [e, t, i]
470
+ ), A = m(null);
471
+ Z(() => {
472
+ if (h && A.current !== h.id) {
473
+ if (A.current = h.id, h.type === "prepend") {
474
+ j();
442
475
  return;
443
476
  }
444
- if (f.type === "append") {
445
- if (f.ifAtBottomOnly && !E) return;
446
- V(f.behavior ?? "auto");
477
+ if (h.type === "append") {
478
+ if (h.ifAtBottomOnly && !E) return;
479
+ H(h.behavior ?? "auto");
447
480
  return;
448
481
  }
449
- if (f.type === "items-change") {
450
- E && V("auto");
482
+ if (h.type === "items-change") {
483
+ E && H("auto");
451
484
  return;
452
485
  }
453
- k(f.key, {
454
- align: f.align ?? "center",
455
- behavior: f.behavior ?? "auto"
486
+ u(h.key, {
487
+ align: h.align ?? "center",
488
+ behavior: h.behavior ?? "auto"
456
489
  });
457
490
  }
458
- }, [f, E, z, V, k]);
459
- const i = g(!1), d = g(l === "top");
460
- X(() => {
461
- const p = h.scrollerRef.current;
462
- if (!p || !y) return;
463
- const B = () => {
464
- const N = p.scrollTop;
465
- d.current || (p.scrollHeight <= p.clientHeight + m || N > m) && (d.current = !0), d.current && N <= m && !i.current && (i.current = !0, Promise.resolve(y()).finally(() => {
466
- i.current = !1;
491
+ }, [h, E, j, H, u]);
492
+ const T = m(!1), w = m(l === "top");
493
+ $(() => {
494
+ const z = i.scrollerRef.current;
495
+ if (!z || !v) return;
496
+ const O = () => {
497
+ const N = z.scrollTop;
498
+ w.current || (z.scrollHeight <= z.clientHeight + p || N > p) && (w.current = !0), w.current && N <= p && !T.current && (T.current = !0, Promise.resolve(v()).finally(() => {
499
+ T.current = !1;
467
500
  }));
468
501
  };
469
- return p.addEventListener("scroll", B, { passive: !0 }), B(), () => p.removeEventListener("scroll", B);
470
- }, [h.scrollerRef, y, m, l]);
471
- const R = g(!1);
472
- X(() => {
473
- const p = h.scrollerRef.current;
474
- if (!p || !u) return;
475
- const B = () => {
476
- p.scrollHeight - p.scrollTop - p.clientHeight <= b && !R.current && (R.current = !0, Promise.resolve(u()).finally(() => {
477
- R.current = !1;
502
+ return z.addEventListener("scroll", O, { passive: !0 }), O(), () => z.removeEventListener("scroll", O);
503
+ }, [i.scrollerRef, v, p, l]);
504
+ const B = m(!1);
505
+ $(() => {
506
+ const z = i.scrollerRef.current;
507
+ if (!z || !f) return;
508
+ const O = () => {
509
+ z.scrollHeight - z.scrollTop - z.clientHeight <= F && !B.current && (B.current = !0, Promise.resolve(f()).finally(() => {
510
+ B.current = !1;
478
511
  }));
479
512
  };
480
- return p.addEventListener("scroll", B, { passive: !0 }), () => p.removeEventListener("scroll", B);
481
- }, [h.scrollerRef, u, b]);
482
- const w = g(null);
483
- return X(() => {
484
- if (!A || w.current === A) return;
485
- const p = e.findIndex((B, N) => t(B, N) === A);
486
- p !== -1 && (w.current = A, h.scrollToIndex(p, { align: "center", behavior: "auto" }), K == null || K());
487
- }, [A, e, t, h, K]), {
488
- scrollerRef: h.scrollerRef,
489
- innerRef: h.innerRef,
490
- virtualItems: h.virtualItems,
491
- totalSize: h.totalSize,
492
- measureItem: h.measureItem,
493
- scrollToIndex: h.scrollToIndex,
494
- scrollToBottom: V,
495
- scrollToKey: k,
513
+ return z.addEventListener("scroll", O, { passive: !0 }), () => z.removeEventListener("scroll", O);
514
+ }, [i.scrollerRef, f, F]);
515
+ const D = m(null);
516
+ return $(() => {
517
+ if (!M || D.current === M) return;
518
+ const z = e.findIndex((O, N) => t(O, N) === M);
519
+ z !== -1 && (D.current = M, i.scrollToIndex(z, { align: "center", behavior: "auto" }), K == null || K());
520
+ }, [M, e, t, i, K]), {
521
+ scrollerRef: i.scrollerRef,
522
+ innerRef: i.innerRef,
523
+ virtualItems: i.virtualItems,
524
+ totalSize: i.totalSize,
525
+ measureItem: i.measureItem,
526
+ scrollToIndex: i.scrollToIndex,
527
+ scrollToBottom: H,
528
+ scrollToKey: u,
496
529
  isAtBottom: E,
497
- prepareAnchor: z
530
+ prepareAnchor: j
498
531
  };
499
532
  }
500
- function ue({
533
+ function he({
501
534
  virtualItem: a,
502
535
  measureItem: e,
503
536
  children: t
504
537
  }) {
505
- const n = g(null), c = g(!1);
506
- return X(() => {
507
- const o = n.current;
508
- if (!o) return;
538
+ const s = m(null), c = m(!1);
539
+ return $(() => {
540
+ const n = s.current;
541
+ if (!n) return;
509
542
  c.current = !1;
510
- const r = new ResizeObserver(([S]) => {
511
- S && (c.current = !0, e(a.key, S.contentRect.height));
543
+ const r = new ResizeObserver(([y]) => {
544
+ y && (c.current = !0, e(a.key, y.contentRect.height));
512
545
  });
513
- return r.observe(o), () => r.disconnect();
514
- }, [a.key, e]), /* @__PURE__ */ W(
546
+ return r.observe(n), () => r.disconnect();
547
+ }, [a.key, e]), /* @__PURE__ */ J(
515
548
  "div",
516
549
  {
517
- ref: n,
550
+ ref: s,
518
551
  style: {
519
552
  position: "absolute",
520
553
  top: 0,
@@ -532,241 +565,246 @@ function ue({
532
565
  }
533
566
  );
534
567
  }
535
- function Ie(a, e) {
568
+ function Me(a, e) {
536
569
  const {
537
570
  data: t,
538
- itemContent: n,
571
+ itemContent: s,
539
572
  computeItemKey: c,
540
- estimatedItemSize: o = 80,
573
+ estimatedItemSize: n = 80,
541
574
  overscan: r = 20,
542
- followOutput: S = "auto",
575
+ followOutput: y = "auto",
543
576
  atBottomThreshold: l = 200,
544
- atBottomHysteresis: f,
545
- initialAlignment: y = "bottom",
546
- scrollModifier: u,
547
- onStartReached: m,
548
- onEndReached: b,
549
- startReachedThreshold: A = 300,
577
+ atBottomHysteresis: h,
578
+ initialAlignment: v = "bottom",
579
+ scrollModifier: f,
580
+ onStartReached: p,
581
+ onEndReached: F,
582
+ startReachedThreshold: M = 300,
550
583
  endReachedThreshold: K = 300,
551
- scrollToMessageKey: h,
584
+ scrollToMessageKey: i,
552
585
  onScrollToMessageComplete: E,
553
- onAtBottomChange: T,
554
- components: M = {},
555
- className: z,
556
- style: L
586
+ onAtBottomChange: g,
587
+ components: b = {},
588
+ className: S,
589
+ style: C
557
590
  } = a, {
558
- scrollerRef: q,
559
- innerRef: V,
560
- virtualItems: k,
591
+ scrollerRef: j,
592
+ innerRef: U,
593
+ virtualItems: L,
561
594
  totalSize: H,
562
- measureItem: i,
563
- scrollToIndex: d,
564
- scrollToBottom: R,
595
+ measureItem: u,
596
+ scrollToIndex: A,
597
+ scrollToBottom: T,
565
598
  scrollToKey: w,
566
- isAtBottom: p,
567
- prepareAnchor: B
568
- } = xe({
599
+ isAtBottom: B,
600
+ prepareAnchor: D
601
+ } = He({
569
602
  items: t,
570
- getKey: (J, O) => c(O, J),
571
- estimatedItemSize: o,
603
+ getKey: (q, ee) => c(ee, q),
604
+ estimatedItemSize: n,
572
605
  overscan: r,
573
606
  atBottomThreshold: l,
574
- atBottomHysteresis: f,
575
- followOutput: S,
576
- initialAlignment: y,
577
- scrollModifier: u,
578
- onStartReached: m,
579
- onEndReached: b,
580
- startReachedThreshold: A,
607
+ atBottomHysteresis: h,
608
+ followOutput: y,
609
+ initialAlignment: v,
610
+ scrollModifier: f,
611
+ onStartReached: p,
612
+ onEndReached: F,
613
+ startReachedThreshold: M,
581
614
  endReachedThreshold: K,
582
- scrollToMessageKey: h,
615
+ scrollToMessageKey: i,
583
616
  onScrollToMessageComplete: E
584
- }), N = te.useRef(p);
585
- te.useEffect(() => {
586
- N.current !== p && (N.current = p, T == null || T(p));
587
- }, [p, T]), de(
617
+ }), z = oe.useRef(B);
618
+ oe.useEffect(() => {
619
+ z.current !== B && (z.current = B, g == null || g(B));
620
+ }, [B, g]), ve(
588
621
  e,
589
622
  () => ({
590
- scrollToBottom: R,
591
- scrollToIndex: d,
623
+ scrollToBottom: T,
624
+ scrollToIndex: A,
592
625
  scrollToKey: w,
593
626
  getScrollTop: () => {
594
- var J;
595
- return ((J = q.current) == null ? void 0 : J.scrollTop) ?? 0;
627
+ var q;
628
+ return ((q = j.current) == null ? void 0 : q.scrollTop) ?? 0;
596
629
  },
597
- isAtBottom: () => p,
598
- prepareAnchor: B
630
+ isAtBottom: () => B,
631
+ prepareAnchor: D
599
632
  }),
600
- [R, d, w, q, p, B]
633
+ [T, A, w, j, B, D]
601
634
  );
602
- const { Header: D, Footer: $, EmptyPlaceholder: G } = M;
603
- return t.length === 0 && G ? /* @__PURE__ */ W(G, {}) : /* @__PURE__ */ le(
635
+ const { Header: O, Footer: N, EmptyPlaceholder: G } = b;
636
+ return t.length === 0 && G ? /* @__PURE__ */ J(G, {}) : /* @__PURE__ */ ae(
604
637
  "div",
605
638
  {
606
- ref: q,
607
- className: z,
639
+ ref: j,
640
+ className: S,
608
641
  style: {
609
642
  overflow: "auto",
610
643
  height: "100%",
611
644
  position: "relative",
645
+ // Prevent browser native scroll anchoring from fighting
646
+ // the library prepend compensation logic.
647
+ overflowAnchor: "none",
612
648
  overscrollBehaviorY: "contain",
613
- ...L
649
+ ...C
614
650
  },
615
651
  children: [
616
- D && /* @__PURE__ */ W(D, {}),
617
- /* @__PURE__ */ W("div", { ref: V, style: { height: H, position: "relative", width: "100%" }, children: k.map((J) => /* @__PURE__ */ W(
618
- ue,
652
+ O && /* @__PURE__ */ J(O, {}),
653
+ /* @__PURE__ */ J("div", { ref: U, style: { height: H, position: "relative", width: "100%" }, children: L.map((q) => /* @__PURE__ */ J(
654
+ he,
619
655
  {
620
- virtualItem: J,
621
- measureItem: i,
622
- children: n(J.index, J.data)
656
+ virtualItem: q,
657
+ measureItem: u,
658
+ children: s(q.index, q.data)
623
659
  },
624
- J.key
660
+ q.key
625
661
  )) }),
626
- $ && /* @__PURE__ */ W($, {})
662
+ N && /* @__PURE__ */ J(N, {})
627
663
  ]
628
664
  }
629
665
  );
630
666
  }
631
- const we = me(Ie);
632
- function Pe({
667
+ const Ke = ge(Me);
668
+ function Ee({
633
669
  data: a,
634
670
  itemContent: e,
635
671
  computeItemKey: t,
636
- estimatedItemSize: n = 60,
672
+ estimatedItemSize: s = 60,
637
673
  overscan: c = 20,
638
- onEndReached: o,
674
+ onEndReached: n,
639
675
  endReachedThreshold: r = 300,
640
- components: S = {},
676
+ components: y = {},
641
677
  className: l,
642
- style: f
678
+ style: h
643
679
  }) {
644
- const { scrollerRef: y, innerRef: u, virtualItems: m, totalSize: b, measureItem: A } = ie({
680
+ const { scrollerRef: v, innerRef: f, virtualItems: p, totalSize: F, measureItem: M } = fe({
645
681
  items: a,
646
- getKey: (T, M) => t(M, T),
647
- estimatedItemSize: n,
682
+ getKey: (g, b) => t(b, g),
683
+ estimatedItemSize: s,
648
684
  overscan: c,
649
685
  initialAlignment: "top"
650
686
  });
651
- te.useEffect(() => {
652
- const T = y.current;
653
- if (!T || !o) return;
654
- let M = !1;
655
- const z = () => {
656
- T.scrollHeight - T.scrollTop - T.clientHeight <= r && !M && (M = !0, Promise.resolve(o()).finally(() => {
657
- M = !1;
687
+ oe.useEffect(() => {
688
+ const g = v.current;
689
+ if (!g || !n) return;
690
+ let b = !1;
691
+ const S = () => {
692
+ g.scrollHeight - g.scrollTop - g.clientHeight <= r && !b && (b = !0, Promise.resolve(n()).finally(() => {
693
+ b = !1;
658
694
  }));
659
695
  };
660
- return T.addEventListener("scroll", z, { passive: !0 }), () => T.removeEventListener("scroll", z);
661
- }, [y, o, r]);
662
- const { Header: K, Footer: h, EmptyPlaceholder: E } = S;
663
- return a.length === 0 && E ? /* @__PURE__ */ W(E, {}) : /* @__PURE__ */ le(
696
+ return g.addEventListener("scroll", S, { passive: !0 }), () => g.removeEventListener("scroll", S);
697
+ }, [v, n, r]);
698
+ const { Header: K, Footer: i, EmptyPlaceholder: E } = y;
699
+ return a.length === 0 && E ? /* @__PURE__ */ J(E, {}) : /* @__PURE__ */ ae(
664
700
  "div",
665
701
  {
666
- ref: y,
702
+ ref: v,
667
703
  className: l,
668
704
  style: {
669
705
  overflow: "auto",
670
706
  height: "100%",
671
707
  position: "relative",
708
+ // Keep scrolling behavior deterministic when items are inserted above.
709
+ overflowAnchor: "none",
672
710
  overscrollBehaviorY: "contain",
673
- ...f
711
+ ...h
674
712
  },
675
713
  children: [
676
- K && /* @__PURE__ */ W(K, {}),
677
- /* @__PURE__ */ W("div", { ref: u, style: { height: b, position: "relative", width: "100%" }, children: m.map((T) => /* @__PURE__ */ W(
678
- ue,
714
+ K && /* @__PURE__ */ J(K, {}),
715
+ /* @__PURE__ */ J("div", { ref: f, style: { height: F, position: "relative", width: "100%" }, children: p.map((g) => /* @__PURE__ */ J(
716
+ he,
679
717
  {
680
- virtualItem: T,
681
- measureItem: A,
682
- children: e(T.index, T.data)
718
+ virtualItem: g,
719
+ measureItem: M,
720
+ children: e(g.index, g.data)
683
721
  },
684
- T.key
722
+ g.key
685
723
  )) }),
686
- h && /* @__PURE__ */ W(h, {})
724
+ i && /* @__PURE__ */ J(i, {})
687
725
  ]
688
726
  }
689
727
  );
690
728
  }
691
- function Ke(a) {
729
+ function Be(a) {
692
730
  const {
693
731
  fetcher: e,
694
732
  initialPage: t = 1,
695
- direction: n = "append",
733
+ direction: s = "append",
696
734
  getKey: c,
697
- onPageLoaded: o,
735
+ onPageLoaded: n,
698
736
  onError: r
699
- } = a, [S, l] = U([]), [f, y] = U(t), [u, m] = U(!0), [b, A] = U(!1), [K, h] = U(!1), [E, T] = U(!1), M = g(/* @__PURE__ */ new Set()), z = g(!1), L = C(
700
- (i) => c ? i.filter((d) => {
701
- const R = c(d);
702
- return M.current.has(R) ? !1 : (M.current.add(R), !0);
703
- }) : i,
737
+ } = a, [y, l] = X([]), [h, v] = X(t), [f, p] = X(!0), [F, M] = X(!1), [K, i] = X(!1), [E, g] = X(!1), b = m(/* @__PURE__ */ new Set()), S = m(!1), C = k(
738
+ (u) => c ? u.filter((A) => {
739
+ const T = c(A);
740
+ return b.current.has(T) ? !1 : (b.current.add(T), !0);
741
+ }) : u,
704
742
  [c]
705
- ), q = C(async () => {
706
- if (!(z.current || !u)) {
707
- z.current = !0, T(!0);
743
+ ), j = k(async () => {
744
+ if (!(S.current || !f)) {
745
+ S.current = !0, g(!0);
708
746
  try {
709
- const i = f + 1, d = await e(i), R = L(d.data);
747
+ const u = h + 1, A = await e(u), T = C(A.data);
710
748
  l(
711
- (w) => n === "prepend" ? [...R, ...w] : [...w, ...R]
712
- ), y(i), m(d.hasNextPage), A(d.hasPrevPage), o == null || o(i, R);
713
- } catch (i) {
714
- r == null || r(i instanceof Error ? i : new Error(String(i)));
749
+ (w) => s === "prepend" ? [...T, ...w] : [...w, ...T]
750
+ ), v(u), p(A.hasNextPage), M(A.hasPrevPage), n == null || n(u, T);
751
+ } catch (u) {
752
+ r == null || r(u instanceof Error ? u : new Error(String(u)));
715
753
  } finally {
716
- T(!1), z.current = !1;
754
+ g(!1), S.current = !1;
717
755
  }
718
756
  }
719
- }, [f, u, e, L, n, o, r]), V = C(async () => {
720
- if (!(z.current || !b)) {
721
- z.current = !0, T(!0);
757
+ }, [h, f, e, C, s, n, r]), U = k(async () => {
758
+ if (!(S.current || !F)) {
759
+ S.current = !0, g(!0);
722
760
  try {
723
- const i = f - 1, d = await e(i), R = L(d.data);
724
- l((w) => [...R, ...w]), y(i), A(d.hasPrevPage), m(d.hasNextPage), o == null || o(i, R);
725
- } catch (i) {
726
- r == null || r(i instanceof Error ? i : new Error(String(i)));
761
+ const u = h - 1, A = await e(u), T = C(A.data);
762
+ l((w) => [...T, ...w]), v(u), M(A.hasPrevPage), p(A.hasNextPage), n == null || n(u, T);
763
+ } catch (u) {
764
+ r == null || r(u instanceof Error ? u : new Error(String(u)));
727
765
  } finally {
728
- T(!1), z.current = !1;
766
+ g(!1), S.current = !1;
729
767
  }
730
768
  }
731
- }, [f, b, e, L, o, r]), k = C(async () => {
732
- if (!z.current) {
733
- z.current = !0, h(!0);
769
+ }, [h, F, e, C, n, r]), L = k(async () => {
770
+ if (!S.current) {
771
+ S.current = !0, i(!0);
734
772
  try {
735
- const i = await e(t), d = i.data;
773
+ const u = await e(t), A = u.data;
736
774
  if (c) {
737
- const R = new Set(d.map(c));
738
- d.forEach((w) => M.current.add(c(w))), l((w) => {
739
- const p = w.filter((B) => !R.has(c(B)));
740
- return n === "prepend" ? [...d, ...p] : [...p, ...d];
775
+ const T = new Set(A.map(c));
776
+ A.forEach((w) => b.current.add(c(w))), l((w) => {
777
+ const B = w.filter((D) => !T.has(c(D)));
778
+ return s === "prepend" ? [...A, ...B] : [...B, ...A];
741
779
  });
742
780
  } else
743
- l(d);
744
- y(t), m(i.hasNextPage), A(i.hasPrevPage), o == null || o(t, d);
745
- } catch (i) {
746
- r == null || r(i instanceof Error ? i : new Error(String(i)));
781
+ l(A);
782
+ v(t), p(u.hasNextPage), M(u.hasPrevPage), n == null || n(t, A);
783
+ } catch (u) {
784
+ r == null || r(u instanceof Error ? u : new Error(String(u)));
747
785
  } finally {
748
- h(!1), z.current = !1;
786
+ i(!1), S.current = !1;
749
787
  }
750
788
  }
751
- }, [e, t, c, n, o, r]), H = C(() => {
752
- l([]), y(t), m(!0), A(!1), h(!1), T(!1), M.current.clear(), z.current = !1;
789
+ }, [e, t, c, s, n, r]), H = k(() => {
790
+ l([]), v(t), p(!0), M(!1), i(!1), g(!1), b.current.clear(), S.current = !1;
753
791
  }, [t]);
754
792
  return {
755
- items: S,
756
- loadNextPage: q,
757
- loadPrevPage: V,
758
- hasNextPage: u,
759
- hasPrevPage: b,
793
+ items: y,
794
+ loadNextPage: j,
795
+ loadPrevPage: U,
796
+ hasNextPage: f,
797
+ hasPrevPage: F,
760
798
  loading: K,
761
799
  loadingMore: E,
762
- refresh: k,
800
+ refresh: L,
763
801
  reset: H,
764
- currentPage: f
802
+ currentPage: h
765
803
  };
766
804
  }
767
805
  export {
768
- we as ChatVirtualList,
769
- Pe as VirtualList,
770
- xe as useChatVirtualizer,
771
- Ke as usePagination
806
+ Ke as ChatVirtualList,
807
+ Ee as VirtualList,
808
+ He as useChatVirtualizer,
809
+ Be as usePagination
772
810
  };