react-anchorlist 0.2.1 → 0.2.4

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