react-anchorlist 0.2.1 → 0.2.3

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,6 +1,6 @@
1
- import { jsx as C, jsxs as G } from "react/jsx-runtime";
2
- import * as D from "react";
3
- import { useCallback as K, useRef as x, useState as k, useEffect as N, useLayoutEffect as $, forwardRef as U, useImperativeHandle as W } from "react";
1
+ import { jsx as B, jsxs as $ } from "react/jsx-runtime";
2
+ import * as U from "react";
3
+ import { useCallback as L, useRef as y, useState as V, useEffect as N, useLayoutEffect as D, forwardRef as Q, useImperativeHandle as W } from "react";
4
4
  class X {
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);
@@ -25,7 +25,7 @@ class X {
25
25
  }
26
26
  append(e) {
27
27
  const t = this.sizes.length;
28
- for (let i = 0; i < e; i++)
28
+ for (let n = 0; n < e; n++)
29
29
  this.sizes.push(this.defaultSize), this.offsets.push(0);
30
30
  this._recalcFrom(t);
31
31
  }
@@ -69,521 +69,552 @@ class Z {
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 [i, r] of this.cache) {
73
- const n = t.get(i);
74
- n !== void 0 && e.setSize(n, r);
72
+ for (const [n, i] of this.cache) {
73
+ const r = t.get(n);
74
+ r !== void 0 && e.setSize(r, i);
75
75
  }
76
76
  }
77
77
  }
78
- function ee(o, e) {
79
- if (o.length === 0) return 0;
80
- let t = 0, i = o.length - 1;
81
- for (; t < i; ) {
82
- const r = t + i >> 1;
83
- (o[r] ?? 0) < e ? t = r + 1 : i = r;
78
+ function ee(l, e) {
79
+ if (l.length === 0) return 0;
80
+ let t = 0, n = l.length - 1;
81
+ for (; t < n; ) {
82
+ const i = t + n >> 1;
83
+ (l[i] ?? 0) < e ? t = i + 1 : n = i;
84
84
  }
85
- return Math.max(0, t > 0 && (o[t] ?? 0) > e ? t - 1 : t);
85
+ return Math.max(0, t > 0 && (l[t] ?? 0) > e ? t - 1 : t);
86
86
  }
87
- function te(o, e, t) {
88
- if (o.length === 0) return 0;
89
- for (let i = o.length - 1; i >= 0; i--)
90
- if ((o[i] ?? 0) < t) return i;
87
+ function te(l, e, t) {
88
+ if (l.length === 0) return 0;
89
+ for (let n = l.length - 1; n >= 0; n--)
90
+ if ((l[n] ?? 0) < t) return n;
91
91
  return 0;
92
92
  }
93
- function re(o) {
94
- const { firstVisible: e, lastVisible: t, itemCount: i, overscan: r } = o;
95
- return i === 0 ? { start: 0, end: -1 } : {
96
- start: Math.max(0, e - r),
97
- end: Math.min(i - 1, t + r)
93
+ function re(l) {
94
+ const { firstVisible: e, lastVisible: t, itemCount: n, overscan: i } = l;
95
+ return n === 0 ? { start: 0, end: -1 } : {
96
+ start: Math.max(0, e - i),
97
+ end: Math.min(n - 1, t + i)
98
98
  };
99
99
  }
100
- function ne(o, e, t) {
101
- return K(
102
- (i, r) => {
103
- var l;
104
- const n = o.current, s = e.current;
105
- if (!n || !s) return;
106
- const y = ((l = t == null ? void 0 : t.current) == null ? void 0 : l.offsetTop) ?? 0, h = s.getOffset(i), I = s.getSize(i), m = (r == null ? void 0 : r.align) ?? "start", H = (r == null ? void 0 : r.behavior) ?? "auto", p = (r == null ? void 0 : r.offset) ?? 0;
100
+ function ne(l, e, t) {
101
+ return L(
102
+ (n, i) => {
103
+ var K;
104
+ const r = l.current, o = e.current;
105
+ if (!r || !o) return;
106
+ let m, s;
107
+ if (typeof n == "object" && n !== null ? (m = n.index, s = {
108
+ align: n.align,
109
+ behavior: n.behavior,
110
+ offset: n.offset
111
+ }) : (m = n, s = i), !Number.isFinite(m)) return;
112
+ const d = Math.max(0, Math.min(Math.floor(m), o.count - 1)), f = ((K = t == null ? void 0 : t.current) == null ? void 0 : K.offsetTop) ?? 0, A = o.getOffset(d), T = o.getSize(d), R = (s == null ? void 0 : s.align) ?? "start", u = (s == null ? void 0 : s.behavior) ?? "auto", H = (s == null ? void 0 : s.offset) ?? 0;
107
113
  let v;
108
- m === "start" ? v = y + h + p : m === "center" ? v = y + h - n.clientHeight / 2 + I / 2 + p : v = y + h - n.clientHeight + I + p, n.scrollTo({ top: Math.max(0, v), behavior: H });
114
+ R === "start" ? v = f + A + H : R === "center" ? v = f + A - r.clientHeight / 2 + T / 2 + H : v = f + A - r.clientHeight + T + H, r.scrollTo({ top: Math.max(0, v), behavior: u });
109
115
  },
110
- [o, e, t]
116
+ [l, e, t]
111
117
  );
112
118
  }
113
- function J(o) {
114
- var j;
115
- const { items: e, getKey: t, estimatedItemSize: i, overscan: r, initialAlignment: n } = o, s = x(null), y = x(null), h = x(null), I = x(new Z()), m = x([]), H = x(!1), p = x(0), v = x(0), l = x(null), [, B] = k(0), R = K(() => B((c) => c + 1), []);
116
- h.current || (h.current = new X(e.length, i));
117
- const F = x(e.length);
118
- if (e.length !== F.current || e.some((c, f) => {
119
- const g = t(c, f);
120
- return m.current[f] !== g;
119
+ function G(l) {
120
+ var q;
121
+ const { items: e, getKey: t, estimatedItemSize: n, overscan: i, initialAlignment: r } = l, o = y(null), m = y(null), s = y(null), d = y(new Z()), f = y([]), A = y(!1), T = y(0), R = y(0), u = y(null), [, H] = V(0), v = L(() => H((a) => a + 1), []);
122
+ s.current || (s.current = new X(e.length, n));
123
+ const K = y(e.length);
124
+ if (e.length !== K.current || e.some((a, h) => {
125
+ const z = t(a, h);
126
+ return f.current[h] !== z;
121
127
  })) {
122
- const c = h.current, f = e.map((Y, q) => t(Y, q)), g = m.current, V = g.length, E = f.length;
123
- E === 0 ? c.resize(0) : V === 0 ? c.resize(E) : E > V ? g.length > 0 && f[E - V] === g[0] ? c.prepend(E - V) : c.resize(E) : E < V && c.resize(E);
124
- const O = /* @__PURE__ */ new Map();
125
- f.forEach((Y, q) => O.set(Y, q)), I.current.applyToOffsetMap(c, O), m.current = f, F.current = e.length;
128
+ const a = s.current, h = e.map((O, Y) => t(O, Y)), z = f.current, k = z.length, E = h.length;
129
+ E === 0 ? a.resize(0) : k === 0 ? a.resize(E) : E > k ? z.length > 0 && h[E - k] === z[0] ? a.prepend(E - k) : a.resize(E) : E < k && a.resize(E);
130
+ const M = /* @__PURE__ */ new Map();
131
+ h.forEach((O, Y) => M.set(O, Y)), d.current.applyToOffsetMap(a, M), f.current = h, K.current = e.length;
126
132
  }
127
133
  N(() => {
128
- const c = s.current;
129
- if (!c) return;
130
- const f = () => {
131
- l.current === null && (l.current = requestAnimationFrame(() => {
132
- l.current = null, p.current = c.scrollTop, v.current = c.clientHeight, R();
134
+ const a = o.current;
135
+ if (!a) return;
136
+ const h = () => {
137
+ u.current === null && (u.current = requestAnimationFrame(() => {
138
+ u.current = null, T.current = a.scrollTop, R.current = a.clientHeight, v();
133
139
  }));
134
140
  };
135
- return c.addEventListener("scroll", f, { passive: !0 }), () => {
136
- c.removeEventListener("scroll", f), l.current !== null && (cancelAnimationFrame(l.current), l.current = null);
141
+ return a.addEventListener("scroll", h, { passive: !0 }), () => {
142
+ a.removeEventListener("scroll", h), u.current !== null && (cancelAnimationFrame(u.current), u.current = null);
137
143
  };
138
- }, [R]), N(() => {
139
- const c = s.current;
140
- if (!c) return;
141
- v.current = c.clientHeight, p.current = c.scrollTop;
142
- const f = new ResizeObserver(([g]) => {
143
- g && (v.current = g.contentRect.height, R());
144
+ }, [v]), N(() => {
145
+ const a = o.current;
146
+ if (!a) return;
147
+ R.current = a.clientHeight, T.current = a.scrollTop;
148
+ const h = new ResizeObserver(([z]) => {
149
+ z && (R.current = z.contentRect.height, v());
144
150
  });
145
- return f.observe(c), () => f.disconnect();
146
- }, [R]), $(() => {
147
- if (H.current || e.length === 0) return;
148
- const c = s.current;
149
- c && (n === "bottom" && (c.scrollTop = c.scrollHeight, p.current = c.scrollTop), H.current = !0);
150
- }, [n, e.length]), N(() => {
151
- e.length === 0 && (H.current = !1);
151
+ return h.observe(a), () => h.disconnect();
152
+ }, [v]), D(() => {
153
+ if (A.current || e.length === 0) return;
154
+ const a = o.current;
155
+ a && (r === "bottom" && (a.scrollTop = a.scrollHeight, T.current = a.scrollTop), A.current = !0);
156
+ }, [r, e.length]), N(() => {
157
+ e.length === 0 && (A.current = !1);
152
158
  }, [e.length]);
153
- const A = K((c, f) => {
154
- const g = h.current;
155
- if (!g || I.current.get(c) === f) return;
156
- I.current.set(c, f);
157
- const E = m.current.indexOf(c);
159
+ const F = L((a, h) => {
160
+ const z = s.current;
161
+ if (!z || d.current.get(a) === h) return;
162
+ d.current.set(a, h);
163
+ const E = f.current.indexOf(a);
158
164
  if (E === -1) return;
159
- g.setSize(E, f) && R();
160
- }, [R]), T = K(
161
- (c, f = "auto") => {
162
- var g;
163
- (g = s.current) == null || g.scrollTo({ top: c, behavior: f });
165
+ z.setSize(E, h) && v();
166
+ }, [v]), I = L(
167
+ (a, h = "auto") => {
168
+ var z;
169
+ (z = o.current) == null || z.scrollTo({ top: a, behavior: h });
164
170
  },
165
171
  []
166
- ), M = ne(s, h, y), a = h.current, w = a ? a.totalSize() : 0, P = s.current, L = (P == null ? void 0 : P.scrollTop) ?? p.current, u = (P == null ? void 0 : P.clientHeight) ?? v.current, S = ((j = y.current) == null ? void 0 : j.offsetTop) ?? 0, z = Math.max(0, L - S);
167
- let b = [];
168
- if (a && a.count > 0 && u > 0) {
169
- const c = a.getOffsets(), f = a.getSizes(), g = ee(c, z), V = te(
170
- c,
171
- f,
172
- z + u
172
+ ), C = ne(o, s, m), b = s.current, _ = b ? b.totalSize() : 0, w = o.current, p = (w == null ? void 0 : w.scrollTop) ?? T.current, c = (w == null ? void 0 : w.clientHeight) ?? R.current, g = ((q = m.current) == null ? void 0 : q.offsetTop) ?? 0, x = Math.max(0, p - g);
173
+ let P = [];
174
+ if (b && b.count > 0 && c > 0) {
175
+ const a = b.getOffsets(), h = b.getSizes(), z = ee(a, x), k = te(
176
+ a,
177
+ h,
178
+ x + c
173
179
  ), E = re({
174
- firstVisible: g,
175
- lastVisible: V,
176
- itemCount: a.count,
177
- overscan: r
180
+ firstVisible: z,
181
+ lastVisible: k,
182
+ itemCount: b.count,
183
+ overscan: i
178
184
  });
179
- for (let O = E.start; O <= E.end && O < e.length; O++)
180
- b.push({
181
- key: m.current[O] ?? t(e[O], O),
182
- index: O,
183
- start: a.getOffset(O),
184
- size: a.getSize(O),
185
- data: e[O]
185
+ for (let M = E.start; M <= E.end && M < e.length; M++)
186
+ P.push({
187
+ key: f.current[M] ?? t(e[M], M),
188
+ index: M,
189
+ start: b.getOffset(M),
190
+ size: b.getSize(M),
191
+ data: e[M]
186
192
  });
187
- } else if (a && a.count > 0) {
188
- const c = Math.min(e.length - 1, r * 2);
189
- for (let f = 0; f <= c; f++)
190
- b.push({
191
- key: m.current[f] ?? t(e[f], f),
192
- index: f,
193
- start: a.getOffset(f),
194
- size: a.getSize(f),
195
- data: e[f]
193
+ } else if (b && b.count > 0) {
194
+ const a = Math.min(e.length - 1, i * 2);
195
+ for (let h = 0; h <= a; h++)
196
+ P.push({
197
+ key: f.current[h] ?? t(e[h], h),
198
+ index: h,
199
+ start: b.getOffset(h),
200
+ size: b.getSize(h),
201
+ data: e[h]
196
202
  });
197
203
  }
198
- const _ = P ? P.scrollHeight - P.scrollTop - P.clientHeight : 1 / 0;
204
+ const j = w ? w.scrollHeight - w.scrollTop - w.clientHeight : 1 / 0;
199
205
  return {
200
- scrollerRef: s,
201
- innerRef: y,
202
- virtualItems: b,
203
- totalSize: w,
204
- measureItem: A,
205
- scrollToIndex: M,
206
- scrollToOffset: T,
207
- isAtTop: L <= 1,
208
- isAtBottom: _ <= 1,
209
- scrollTop: L
206
+ scrollerRef: o,
207
+ innerRef: m,
208
+ virtualItems: P,
209
+ totalSize: _,
210
+ measureItem: F,
211
+ scrollToIndex: C,
212
+ scrollToOffset: I,
213
+ isAtTop: p <= 1,
214
+ isAtBottom: j <= 1,
215
+ scrollTop: p
210
216
  };
211
217
  }
212
- function se(o, e) {
213
- const t = x(0), i = x(0), r = x(!1), n = K(() => {
214
- const s = o.current;
215
- s && (t.current = s.scrollTop, i.current = s.scrollHeight, r.current = !0);
216
- }, [o]);
217
- return $(() => {
218
- if (!r.current) return;
219
- const s = o.current;
218
+ function se(l, e) {
219
+ const t = y(0), n = y(0), i = y(!1), r = y({
220
+ first: null,
221
+ second: null,
222
+ timeout: null
223
+ }), o = L(() => {
224
+ const { first: s, second: d, timeout: f } = r.current;
225
+ s && cancelAnimationFrame(s), d && cancelAnimationFrame(d), f && clearTimeout(f), r.current = { first: null, second: null, timeout: null };
226
+ }, []), m = L(() => {
227
+ const s = l.current;
228
+ s && (t.current = s.scrollTop, n.current = s.scrollHeight, i.current = !0);
229
+ }, [l]);
230
+ return D(() => {
231
+ if (!i.current) return;
232
+ const s = l.current;
220
233
  if (!s) return;
221
- const y = s.scrollHeight - i.current;
222
- y > 0 && (s.scrollTop = t.current + y), r.current = !1;
223
- }, [e, o]), { prepareAnchor: n };
234
+ i.current = !1;
235
+ const d = () => {
236
+ const f = t.current + (s.scrollHeight - n.current);
237
+ Number.isFinite(f) && Math.abs(s.scrollTop - f) > 1 && (s.scrollTop = f);
238
+ };
239
+ return o(), d(), r.current.first = requestAnimationFrame(() => {
240
+ r.current.first = null, d(), r.current.second = requestAnimationFrame(() => {
241
+ r.current.second = null, d();
242
+ });
243
+ }), r.current.timeout = setTimeout(() => {
244
+ r.current.timeout = null, d();
245
+ }, 90), () => o();
246
+ }, [e, l, o]), { prepareAnchor: m };
224
247
  }
225
- function ie(o, e) {
226
- const [t, i] = k(!0), r = x(null);
248
+ function oe(l, e) {
249
+ const [t, n] = V(!0), i = y(null);
227
250
  return N(() => {
228
- const n = o.current;
229
- if (!n) return;
230
- const s = () => {
231
- const h = n.scrollHeight - n.scrollTop - n.clientHeight;
232
- i(h <= e);
233
- }, y = () => {
234
- r.current !== null && cancelAnimationFrame(r.current), r.current = requestAnimationFrame(s);
251
+ const r = l.current;
252
+ if (!r) return;
253
+ const o = () => {
254
+ const s = r.scrollHeight - r.scrollTop - r.clientHeight;
255
+ n(s <= e);
256
+ }, m = () => {
257
+ i.current !== null && cancelAnimationFrame(i.current), i.current = requestAnimationFrame(o);
235
258
  };
236
- return n.addEventListener("scroll", y, { passive: !0 }), s(), () => {
237
- n.removeEventListener("scroll", y), r.current !== null && cancelAnimationFrame(r.current);
259
+ return r.addEventListener("scroll", m, { passive: !0 }), o(), () => {
260
+ r.removeEventListener("scroll", m), i.current !== null && cancelAnimationFrame(i.current);
238
261
  };
239
- }, [o, e]), t;
262
+ }, [l, e]), t;
240
263
  }
241
- function oe(o) {
242
- const { itemCount: e, isAtBottom: t, scrollToIndex: i, mode: r } = o, n = x(e);
243
- $(() => {
244
- r && (e > n.current && t && e > 0 && i(e - 1, {
264
+ function ie(l) {
265
+ const { itemCount: e, firstKey: t, lastKey: n, isAtBottom: i, scrollToIndex: r, mode: o } = l, m = y(e), s = y(t), d = y(n);
266
+ D(() => {
267
+ if (!o) {
268
+ m.current = e, s.current = t, d.current = n;
269
+ return;
270
+ }
271
+ const f = m.current, A = s.current, T = d.current;
272
+ e > f && t === A && n !== T && i && e > 0 && r(e - 1, {
245
273
  align: "end",
246
- behavior: r === "smooth" ? "smooth" : "auto"
247
- }), n.current = e);
248
- }, [e, t, i, r]);
274
+ behavior: o === "smooth" ? "smooth" : "auto"
275
+ }), m.current = e, s.current = t, d.current = n;
276
+ }, [e, t, n, i, r, o]);
249
277
  }
250
- function ce(o) {
278
+ function ce(l) {
251
279
  const {
252
280
  items: e,
253
281
  getKey: t,
254
- estimatedItemSize: i = 80,
255
- overscan: r = 20,
256
- atBottomThreshold: n = 200,
257
- followOutput: s = "auto",
258
- initialAlignment: y = "bottom",
259
- onStartReached: h,
260
- onEndReached: I,
261
- startReachedThreshold: m = 300,
262
- endReachedThreshold: H = 300,
263
- scrollToMessageKey: p,
264
- onScrollToMessageComplete: v
265
- } = o, l = J({
282
+ estimatedItemSize: n = 80,
283
+ overscan: i = 20,
284
+ atBottomThreshold: r = 200,
285
+ followOutput: o = "auto",
286
+ initialAlignment: m = "bottom",
287
+ onStartReached: s,
288
+ onEndReached: d,
289
+ startReachedThreshold: f = 300,
290
+ endReachedThreshold: A = 300,
291
+ scrollToMessageKey: T,
292
+ onScrollToMessageComplete: R
293
+ } = l, u = G({
266
294
  items: e,
267
295
  getKey: t,
268
- estimatedItemSize: i,
269
- overscan: r,
270
- initialAlignment: y
271
- }), B = ie(l.scrollerRef, n), { prepareAnchor: R } = se(l.scrollerRef, e.length);
272
- oe({
296
+ estimatedItemSize: n,
297
+ overscan: i,
298
+ initialAlignment: m
299
+ }), H = oe(u.scrollerRef, r), { prepareAnchor: v } = se(u.scrollerRef, e.length), K = e.length > 0 ? t(e[0], 0) : null, S = e.length > 0 ? t(e[e.length - 1], e.length - 1) : null;
300
+ ie({
273
301
  itemCount: e.length,
274
- isAtBottom: B,
275
- scrollToIndex: l.scrollToIndex,
276
- mode: s ?? !1
302
+ firstKey: K,
303
+ lastKey: S,
304
+ isAtBottom: H,
305
+ scrollToIndex: u.scrollToIndex,
306
+ mode: o ?? !1
277
307
  });
278
- const F = x(!1);
308
+ const F = y(!1), I = y(m === "top");
279
309
  N(() => {
280
- const a = l.scrollerRef.current;
281
- if (!a || !h) return;
282
- const w = () => {
283
- a.scrollTop <= m && !F.current && (F.current = !0, Promise.resolve(h()).finally(() => {
310
+ const p = u.scrollerRef.current;
311
+ if (!p || !s) return;
312
+ const c = () => {
313
+ const g = p.scrollTop;
314
+ !I.current && g > f && (I.current = !0), I.current && g <= f && !F.current && (F.current = !0, Promise.resolve(s()).finally(() => {
284
315
  F.current = !1;
285
316
  }));
286
317
  };
287
- return a.addEventListener("scroll", w, { passive: !0 }), () => a.removeEventListener("scroll", w);
288
- }, [l.scrollerRef, h, m]);
289
- const d = x(!1);
318
+ return p.addEventListener("scroll", c, { passive: !0 }), () => p.removeEventListener("scroll", c);
319
+ }, [u.scrollerRef, s, f, m]);
320
+ const C = y(!1);
290
321
  N(() => {
291
- const a = l.scrollerRef.current;
292
- if (!a || !I) return;
293
- const w = () => {
294
- a.scrollHeight - a.scrollTop - a.clientHeight <= H && !d.current && (d.current = !0, Promise.resolve(I()).finally(() => {
295
- d.current = !1;
322
+ const p = u.scrollerRef.current;
323
+ if (!p || !d) return;
324
+ const c = () => {
325
+ p.scrollHeight - p.scrollTop - p.clientHeight <= A && !C.current && (C.current = !0, Promise.resolve(d()).finally(() => {
326
+ C.current = !1;
296
327
  }));
297
328
  };
298
- return a.addEventListener("scroll", w, { passive: !0 }), () => a.removeEventListener("scroll", w);
299
- }, [l.scrollerRef, I, H]);
300
- const A = x(null);
329
+ return p.addEventListener("scroll", c, { passive: !0 }), () => p.removeEventListener("scroll", c);
330
+ }, [u.scrollerRef, d, A]);
331
+ const b = y(null);
301
332
  N(() => {
302
- if (!p || A.current === p) return;
303
- const a = e.findIndex((w, P) => t(w, P) === p);
304
- a !== -1 && (A.current = p, l.scrollToIndex(a, { align: "center", behavior: "smooth" }), v == null || v());
305
- }, [p, e, t, l, v]);
306
- const T = K(
307
- (a = "auto") => {
308
- e.length !== 0 && l.scrollToIndex(e.length - 1, { align: "end", behavior: a });
333
+ if (!T || b.current === T) return;
334
+ const p = e.findIndex((c, g) => t(c, g) === T);
335
+ p !== -1 && (b.current = T, u.scrollToIndex(p, { align: "center", behavior: "smooth" }), R == null || R());
336
+ }, [T, e, t, u, R]);
337
+ const _ = L(
338
+ (p = "auto") => {
339
+ e.length !== 0 && u.scrollToIndex(e.length - 1, { align: "end", behavior: p });
309
340
  },
310
- [e.length, l]
311
- ), M = K(
312
- (a, w) => {
313
- const P = e.findIndex((L, u) => t(L, u) === a);
314
- P !== -1 && l.scrollToIndex(P, w);
341
+ [e.length, u]
342
+ ), w = L(
343
+ (p, c) => {
344
+ const g = e.findIndex((x, P) => t(x, P) === p);
345
+ g !== -1 && u.scrollToIndex(g, c);
315
346
  },
316
- [e, t, l]
347
+ [e, t, u]
317
348
  );
318
349
  return {
319
- scrollerRef: l.scrollerRef,
320
- innerRef: l.innerRef,
321
- virtualItems: l.virtualItems,
322
- totalSize: l.totalSize,
323
- measureItem: l.measureItem,
324
- scrollToIndex: l.scrollToIndex,
325
- scrollToBottom: T,
326
- scrollToKey: M,
327
- isAtBottom: B,
328
- prepareAnchor: R
350
+ scrollerRef: u.scrollerRef,
351
+ innerRef: u.innerRef,
352
+ virtualItems: u.virtualItems,
353
+ totalSize: u.totalSize,
354
+ measureItem: u.measureItem,
355
+ scrollToIndex: u.scrollToIndex,
356
+ scrollToBottom: _,
357
+ scrollToKey: w,
358
+ isAtBottom: H,
359
+ prepareAnchor: v
329
360
  };
330
361
  }
331
- function Q({
332
- virtualItem: o,
362
+ function J({
363
+ virtualItem: l,
333
364
  measureItem: e,
334
365
  children: t
335
366
  }) {
336
- const i = x(null);
367
+ const n = y(null);
337
368
  return N(() => {
338
- const r = i.current;
339
- if (!r) return;
340
- const n = new ResizeObserver(([s]) => {
341
- s && e(o.key, s.contentRect.height);
369
+ const i = n.current;
370
+ if (!i) return;
371
+ const r = new ResizeObserver(([o]) => {
372
+ o && e(l.key, o.contentRect.height);
342
373
  });
343
- return n.observe(r), () => n.disconnect();
344
- }, [o.key, e]), /* @__PURE__ */ C(
374
+ return r.observe(i), () => r.disconnect();
375
+ }, [l.key, e]), /* @__PURE__ */ B(
345
376
  "div",
346
377
  {
347
- ref: i,
378
+ ref: n,
348
379
  style: {
349
380
  position: "absolute",
350
381
  top: 0,
351
382
  // transform instead of top: avoids reflow, uses GPU compositor layer
352
- transform: `translateY(${o.start}px)`,
383
+ transform: `translateY(${l.start}px)`,
353
384
  width: "100%",
354
385
  // Reserve estimated height to prevent layout collapse before first measure
355
- minHeight: o.size
386
+ minHeight: l.size
356
387
  },
357
388
  children: t
358
389
  }
359
390
  );
360
391
  }
361
- function le(o, e) {
392
+ function le(l, e) {
362
393
  const {
363
394
  data: t,
364
- itemContent: i,
365
- computeItemKey: r,
366
- estimatedItemSize: n = 80,
367
- overscan: s = 20,
368
- followOutput: y = "auto",
369
- atBottomThreshold: h = 200,
370
- initialAlignment: I = "bottom",
371
- onStartReached: m,
372
- onEndReached: H,
373
- startReachedThreshold: p = 300,
374
- endReachedThreshold: v = 300,
375
- scrollToMessageKey: l,
376
- onScrollToMessageComplete: B,
377
- onAtBottomChange: R,
378
- components: F = {},
379
- className: d,
380
- style: A
381
- } = o, {
382
- scrollerRef: T,
383
- innerRef: M,
384
- virtualItems: a,
385
- totalSize: w,
386
- measureItem: P,
387
- scrollToIndex: L,
388
- scrollToBottom: u,
389
- scrollToKey: S,
390
- isAtBottom: z,
391
- prepareAnchor: b
395
+ itemContent: n,
396
+ computeItemKey: i,
397
+ estimatedItemSize: r = 80,
398
+ overscan: o = 20,
399
+ followOutput: m = "auto",
400
+ atBottomThreshold: s = 200,
401
+ initialAlignment: d = "bottom",
402
+ onStartReached: f,
403
+ onEndReached: A,
404
+ startReachedThreshold: T = 300,
405
+ endReachedThreshold: R = 300,
406
+ scrollToMessageKey: u,
407
+ onScrollToMessageComplete: H,
408
+ onAtBottomChange: v,
409
+ components: K = {},
410
+ className: S,
411
+ style: F
412
+ } = l, {
413
+ scrollerRef: I,
414
+ innerRef: C,
415
+ virtualItems: b,
416
+ totalSize: _,
417
+ measureItem: w,
418
+ scrollToIndex: p,
419
+ scrollToBottom: c,
420
+ scrollToKey: g,
421
+ isAtBottom: x,
422
+ prepareAnchor: P
392
423
  } = ce({
393
424
  items: t,
394
- getKey: (g, V) => r(V, g),
395
- estimatedItemSize: n,
396
- overscan: s,
397
- atBottomThreshold: h,
398
- followOutput: y,
399
- initialAlignment: I,
400
- onStartReached: m,
401
- onEndReached: H,
402
- startReachedThreshold: p,
403
- endReachedThreshold: v,
404
- scrollToMessageKey: l,
405
- onScrollToMessageComplete: B
406
- }), _ = D.useRef(z);
407
- D.useEffect(() => {
408
- _.current !== z && (_.current = z, R == null || R(z));
409
- }, [z, R]), W(
425
+ getKey: (z, k) => i(k, z),
426
+ estimatedItemSize: r,
427
+ overscan: o,
428
+ atBottomThreshold: s,
429
+ followOutput: m,
430
+ initialAlignment: d,
431
+ onStartReached: f,
432
+ onEndReached: A,
433
+ startReachedThreshold: T,
434
+ endReachedThreshold: R,
435
+ scrollToMessageKey: u,
436
+ onScrollToMessageComplete: H
437
+ }), j = U.useRef(x);
438
+ U.useEffect(() => {
439
+ j.current !== x && (j.current = x, v == null || v(x));
440
+ }, [x, v]), W(
410
441
  e,
411
442
  () => ({
412
- scrollToBottom: u,
413
- scrollToIndex: L,
414
- scrollToKey: S,
443
+ scrollToBottom: c,
444
+ scrollToIndex: p,
445
+ scrollToKey: g,
415
446
  getScrollTop: () => {
416
- var g;
417
- return ((g = T.current) == null ? void 0 : g.scrollTop) ?? 0;
447
+ var z;
448
+ return ((z = I.current) == null ? void 0 : z.scrollTop) ?? 0;
418
449
  },
419
- isAtBottom: () => z,
420
- prepareAnchor: b
450
+ isAtBottom: () => x,
451
+ prepareAnchor: P
421
452
  }),
422
- [u, L, S, T, z, b]
453
+ [c, p, g, I, x, P]
423
454
  );
424
- const { Header: j, Footer: c, EmptyPlaceholder: f } = F;
425
- return t.length === 0 && f ? /* @__PURE__ */ C(f, {}) : /* @__PURE__ */ G(
455
+ const { Header: q, Footer: a, EmptyPlaceholder: h } = K;
456
+ return t.length === 0 && h ? /* @__PURE__ */ B(h, {}) : /* @__PURE__ */ $(
426
457
  "div",
427
458
  {
428
- ref: T,
429
- className: d,
459
+ ref: I,
460
+ className: S,
430
461
  style: {
431
462
  overflow: "auto",
432
463
  height: "100%",
433
464
  position: "relative",
434
465
  overscrollBehaviorY: "contain",
435
- ...A
466
+ ...F
436
467
  },
437
468
  children: [
438
- j && /* @__PURE__ */ C(j, {}),
439
- /* @__PURE__ */ C("div", { ref: M, style: { height: w, position: "relative", width: "100%" }, children: a.map((g) => /* @__PURE__ */ C(
440
- Q,
469
+ q && /* @__PURE__ */ B(q, {}),
470
+ /* @__PURE__ */ B("div", { ref: C, style: { height: _, position: "relative", width: "100%" }, children: b.map((z) => /* @__PURE__ */ B(
471
+ J,
441
472
  {
442
- virtualItem: g,
443
- measureItem: P,
444
- children: i(g.index, g.data)
473
+ virtualItem: z,
474
+ measureItem: w,
475
+ children: n(z.index, z.data)
445
476
  },
446
- g.key
477
+ z.key
447
478
  )) }),
448
- c && /* @__PURE__ */ C(c, {})
479
+ a && /* @__PURE__ */ B(a, {})
449
480
  ]
450
481
  }
451
482
  );
452
483
  }
453
- const fe = U(le);
484
+ const fe = Q(le);
454
485
  function he({
455
- data: o,
486
+ data: l,
456
487
  itemContent: e,
457
488
  computeItemKey: t,
458
- estimatedItemSize: i = 60,
459
- overscan: r = 20,
460
- onEndReached: n,
461
- endReachedThreshold: s = 300,
462
- components: y = {},
463
- className: h,
464
- style: I
489
+ estimatedItemSize: n = 60,
490
+ overscan: i = 20,
491
+ onEndReached: r,
492
+ endReachedThreshold: o = 300,
493
+ components: m = {},
494
+ className: s,
495
+ style: d
465
496
  }) {
466
- const { scrollerRef: m, innerRef: H, virtualItems: p, totalSize: v, measureItem: l } = J({
467
- items: o,
468
- getKey: (d, A) => t(A, d),
469
- estimatedItemSize: i,
470
- overscan: r,
497
+ const { scrollerRef: f, innerRef: A, virtualItems: T, totalSize: R, measureItem: u } = G({
498
+ items: l,
499
+ getKey: (S, F) => t(F, S),
500
+ estimatedItemSize: n,
501
+ overscan: i,
471
502
  initialAlignment: "top"
472
503
  });
473
- D.useEffect(() => {
474
- const d = m.current;
475
- if (!d || !n) return;
476
- let A = !1;
477
- const T = () => {
478
- d.scrollHeight - d.scrollTop - d.clientHeight <= s && !A && (A = !0, Promise.resolve(n()).finally(() => {
479
- A = !1;
504
+ U.useEffect(() => {
505
+ const S = f.current;
506
+ if (!S || !r) return;
507
+ let F = !1;
508
+ const I = () => {
509
+ S.scrollHeight - S.scrollTop - S.clientHeight <= o && !F && (F = !0, Promise.resolve(r()).finally(() => {
510
+ F = !1;
480
511
  }));
481
512
  };
482
- return d.addEventListener("scroll", T, { passive: !0 }), () => d.removeEventListener("scroll", T);
483
- }, [m, n, s]);
484
- const { Header: B, Footer: R, EmptyPlaceholder: F } = y;
485
- return o.length === 0 && F ? /* @__PURE__ */ C(F, {}) : /* @__PURE__ */ G(
513
+ return S.addEventListener("scroll", I, { passive: !0 }), () => S.removeEventListener("scroll", I);
514
+ }, [f, r, o]);
515
+ const { Header: H, Footer: v, EmptyPlaceholder: K } = m;
516
+ return l.length === 0 && K ? /* @__PURE__ */ B(K, {}) : /* @__PURE__ */ $(
486
517
  "div",
487
518
  {
488
- ref: m,
489
- className: h,
519
+ ref: f,
520
+ className: s,
490
521
  style: {
491
522
  overflow: "auto",
492
523
  height: "100%",
493
524
  position: "relative",
494
525
  overscrollBehaviorY: "contain",
495
- ...I
526
+ ...d
496
527
  },
497
528
  children: [
498
- B && /* @__PURE__ */ C(B, {}),
499
- /* @__PURE__ */ C("div", { ref: H, style: { height: v, position: "relative", width: "100%" }, children: p.map((d) => /* @__PURE__ */ C(
500
- Q,
529
+ H && /* @__PURE__ */ B(H, {}),
530
+ /* @__PURE__ */ B("div", { ref: A, style: { height: R, position: "relative", width: "100%" }, children: T.map((S) => /* @__PURE__ */ B(
531
+ J,
501
532
  {
502
- virtualItem: d,
503
- measureItem: l,
504
- children: e(d.index, d.data)
533
+ virtualItem: S,
534
+ measureItem: u,
535
+ children: e(S.index, S.data)
505
536
  },
506
- d.key
537
+ S.key
507
538
  )) }),
508
- R && /* @__PURE__ */ C(R, {})
539
+ v && /* @__PURE__ */ B(v, {})
509
540
  ]
510
541
  }
511
542
  );
512
543
  }
513
- function de(o) {
544
+ function de(l) {
514
545
  const {
515
546
  fetcher: e,
516
547
  initialPage: t = 1,
517
- direction: i = "append",
518
- getKey: r,
519
- onPageLoaded: n,
520
- onError: s
521
- } = o, [y, h] = k([]), [I, m] = k(t), [H, p] = k(!0), [v, l] = k(!1), [B, R] = k(!1), [F, d] = k(!1), A = x(/* @__PURE__ */ new Set()), T = x(!1), M = K(
522
- (u) => r ? u.filter((S) => {
523
- const z = r(S);
524
- return A.current.has(z) ? !1 : (A.current.add(z), !0);
525
- }) : u,
526
- [r]
527
- ), a = K(async () => {
528
- if (!(T.current || !H)) {
529
- T.current = !0, d(!0);
548
+ direction: n = "append",
549
+ getKey: i,
550
+ onPageLoaded: r,
551
+ onError: o
552
+ } = l, [m, s] = V([]), [d, f] = V(t), [A, T] = V(!0), [R, u] = V(!1), [H, v] = V(!1), [K, S] = V(!1), F = y(/* @__PURE__ */ new Set()), I = y(!1), C = L(
553
+ (c) => i ? c.filter((g) => {
554
+ const x = i(g);
555
+ return F.current.has(x) ? !1 : (F.current.add(x), !0);
556
+ }) : c,
557
+ [i]
558
+ ), b = L(async () => {
559
+ if (!(I.current || !A)) {
560
+ I.current = !0, S(!0);
530
561
  try {
531
- const u = I + 1, S = await e(u), z = M(S.data);
532
- h(
533
- (b) => i === "prepend" ? [...z, ...b] : [...b, ...z]
534
- ), m(u), p(S.hasNextPage), l(S.hasPrevPage), n == null || n(u, z);
535
- } catch (u) {
536
- s == null || s(u instanceof Error ? u : new Error(String(u)));
562
+ const c = d + 1, g = await e(c), x = C(g.data);
563
+ s(
564
+ (P) => n === "prepend" ? [...x, ...P] : [...P, ...x]
565
+ ), f(c), T(g.hasNextPage), u(g.hasPrevPage), r == null || r(c, x);
566
+ } catch (c) {
567
+ o == null || o(c instanceof Error ? c : new Error(String(c)));
537
568
  } finally {
538
- d(!1), T.current = !1;
569
+ S(!1), I.current = !1;
539
570
  }
540
571
  }
541
- }, [I, H, e, M, i, n, s]), w = K(async () => {
542
- if (!(T.current || !v)) {
543
- T.current = !0, d(!0);
572
+ }, [d, A, e, C, n, r, o]), _ = L(async () => {
573
+ if (!(I.current || !R)) {
574
+ I.current = !0, S(!0);
544
575
  try {
545
- const u = I - 1, S = await e(u), z = M(S.data);
546
- h((b) => [...z, ...b]), m(u), l(S.hasPrevPage), p(S.hasNextPage), n == null || n(u, z);
547
- } catch (u) {
548
- s == null || s(u instanceof Error ? u : new Error(String(u)));
576
+ const c = d - 1, g = await e(c), x = C(g.data);
577
+ s((P) => [...x, ...P]), f(c), u(g.hasPrevPage), T(g.hasNextPage), r == null || r(c, x);
578
+ } catch (c) {
579
+ o == null || o(c instanceof Error ? c : new Error(String(c)));
549
580
  } finally {
550
- d(!1), T.current = !1;
581
+ S(!1), I.current = !1;
551
582
  }
552
583
  }
553
- }, [I, v, e, M, n, s]), P = K(async () => {
554
- if (!T.current) {
555
- T.current = !0, R(!0);
584
+ }, [d, R, e, C, r, o]), w = L(async () => {
585
+ if (!I.current) {
586
+ I.current = !0, v(!0);
556
587
  try {
557
- const u = await e(t), S = u.data;
558
- if (r) {
559
- const z = new Set(S.map(r));
560
- S.forEach((b) => A.current.add(r(b))), h((b) => {
561
- const _ = b.filter((j) => !z.has(r(j)));
562
- return i === "prepend" ? [...S, ..._] : [..._, ...S];
588
+ const c = await e(t), g = c.data;
589
+ if (i) {
590
+ const x = new Set(g.map(i));
591
+ g.forEach((P) => F.current.add(i(P))), s((P) => {
592
+ const j = P.filter((q) => !x.has(i(q)));
593
+ return n === "prepend" ? [...g, ...j] : [...j, ...g];
563
594
  });
564
595
  } else
565
- h(S);
566
- m(t), p(u.hasNextPage), l(u.hasPrevPage), n == null || n(t, S);
567
- } catch (u) {
568
- s == null || s(u instanceof Error ? u : new Error(String(u)));
596
+ s(g);
597
+ f(t), T(c.hasNextPage), u(c.hasPrevPage), r == null || r(t, g);
598
+ } catch (c) {
599
+ o == null || o(c instanceof Error ? c : new Error(String(c)));
569
600
  } finally {
570
- R(!1), T.current = !1;
601
+ v(!1), I.current = !1;
571
602
  }
572
603
  }
573
- }, [e, t, r, i, n, s]), L = K(() => {
574
- h([]), m(t), p(!0), l(!1), R(!1), d(!1), A.current.clear(), T.current = !1;
604
+ }, [e, t, i, n, r, o]), p = L(() => {
605
+ s([]), f(t), T(!0), u(!1), v(!1), S(!1), F.current.clear(), I.current = !1;
575
606
  }, [t]);
576
607
  return {
577
- items: y,
578
- loadNextPage: a,
579
- loadPrevPage: w,
580
- hasNextPage: H,
581
- hasPrevPage: v,
582
- loading: B,
583
- loadingMore: F,
584
- refresh: P,
585
- reset: L,
586
- currentPage: I
608
+ items: m,
609
+ loadNextPage: b,
610
+ loadPrevPage: _,
611
+ hasNextPage: A,
612
+ hasPrevPage: R,
613
+ loading: H,
614
+ loadingMore: K,
615
+ refresh: w,
616
+ reset: p,
617
+ currentPage: d
587
618
  };
588
619
  }
589
620
  export {