diff --git a/lib/index.cjs b/lib/index.cjs index e02dfd0b3db60faff2cc705d7ce0aa51cb958f5a..b12677a356f748ebccf6e16cb781efe1315bb55b 100644 --- a/lib/index.cjs +++ b/lib/index.cjs @@ -35,10 +35,18 @@ const r = null, {min: n, max: s, abs: i, floor: l} = Math, c = (e, t, o) => n(o, v(e, s) <= t ? (n = s, o = s + 1) : r = s - 1; } return c(n, 0, e.l - 1); -}, w = (e, t, o) => { - const r = t - e.l; - return e.i = o ? -1 : n(t - 1, e.i), e.l = t, r > 0 ? (h(e.u, r), h(e.t, r, o), - e.o * r) : (e.u.splice(r), (o ? e.t.splice(0, -r) : e.t.splice(r)).reduce((t, o) => t - (-1 === o ? e.o : o), 0)); +}, w = (e, t, o, r) => { + const s = t - e.l; + if (e.i = o ? -1 : n(t - 1, e.i), e.l = t, s > 0) { + h(e.u, s); + if (r) { + const t = o ? r.slice(0, s) : r.slice(r.length - s); + e.t[o ? "unshift" : "push"](...t); + return t.reduce((e, t) => e + t, 0); + } + return h(e.t, s, o), e.o * s; + } + return e.u.splice(s), (o ? e.t.splice(0, -s) : e.t.splice(s)).reduce((t, o) => t - (-1 === o ? e.o : o), 0); }, S = "undefined" != typeof window, m = e => e.documentElement, $ = e => e.ownerDocument, z = e => e.defaultView, b = /*#__PURE__*/ a(() => !!/iP(hone|od|ad)/.test(navigator.userAgent) || "MacIntel" === navigator.platform && navigator.maxTouchPoints > 0), y = /*#__PURE__*/ a(() => "scrollBehavior" in m(document).style), x = e => s(e.h(), e.p()), I = (e, t = 40, o = 0, l, c = !1) => { let u = !!o, d = 1, a = 0, S = 0, m = 0, $ = 0, z = 0, y = 0, x = 0, I = 0, k = r, R = [ 0, u ? s(o - 1, 0) : -1 ], T = 0, C = !1; const M = ((e, t, o) => ({ @@ -47,7 +55,7 @@ const r = null, {min: n, max: s, abs: i, floor: l} = Math, c = (e, t, o) => n(o, l: e, i: -1, u: h([], e + 1) - }))(e, l ? l[1] : t, l && l[0]), O = new Set, E = () => m - S, H = () => E() + z + $, W = (e, t) => ((e, t, o, r) => { + }))(e, l ? l[1] : Array.isArray(t) ? t.reduce((e, t) => e + t, 0) / (t.length || 1) : t, l ? l[0] : Array.isArray(t) ? t : void 0), O = new Set, E = () => m - S, H = () => E() + z + $, W = (e, t) => ((e, t, o, r) => { if (r = n(r, e.l - 1), v(e, r) <= t) { const n = _(e, o, r); return [ _(e, t, r, n), n ]; @@ -147,7 +155,7 @@ const r = null, {min: n, max: s, abs: i, floor: l} = Math, c = (e, t, o) => n(o, break; case 5: - t[1] ? (L(w(M, t[0], !0)), I = 2, l = 1) : (w(M, t[0]), l = 1); + t[1] ? (L(w(M, t[0], !0, t[2])), I = 2, l = 1) : (w(M, t[0], !1, t[2]), I = 0, l = 1); break; case 6: @@ -160,6 +168,10 @@ const r = null, {min: n, max: s, abs: i, floor: l} = Math, c = (e, t, o) => n(o, case 8: k = W(t, t + a), l = 1; + break; + + case 9: + I = 0; } l && (d = 1 + (2147483647 & d), o && z && ($ += z, z = 0), O.forEach(([e, t]) => { l & e && t(n); @@ -186,6 +198,8 @@ const r = null, {min: n, max: s, abs: i, floor: l} = Math, c = (e, t, o) => n(o, c = l(), d && (a = !0), i && e.q(6, i()), e.q(1, n()), h(); }, p = t => { - if (f || !e.M() || t.ctrlKey) return; + if (!e.M() || t.ctrlKey) return; + e.q(9); + if (f) return; const r = l() - c; 150 > r && 50 < r && (o ? t.deltaX : t.deltaY) && (f = !0); }, v = () => { @@ -266,7 +279,7 @@ const r = null, {min: n, max: s, abs: i, floor: l} = Math, c = (e, t, o) => n(o, e[l] = t; }); } - f[i] = R(e.C() + t, s), o && u(); + f[i] = R((o ? R(f[i], s) : e.C()) + t, s), o && u(); }), n[1](!0); }, v() { @@ -519,11 +532,11 @@ const r = null, {min: n, max: s, abs: i, floor: l} = Math, c = (e, t, o) => n(o, })(e); return [ e => t[e], t.length ]; }, [ e, o ]), j = /*#__PURE__*/ t.forwardRef(({children: r, data: n, bufferSize: s, itemSize: i, shift: l, horizontal: c, keepMounted: d, cache: a, startMargin: h = 0, ssrCount: g, as: p = "div", item: v = "div", scrollRef: _, onScroll: w, onScrollEnd: S}, m) => { - const [$, z] = V(r, n), b = t.useRef(null), y = t.useRef(!!g), k = X(w), R = X(S), [T, C, O, E] = P(() => { - const e = !!c, t = I(z, i, g, a, !i); + const [$, z] = V(r, n), A = t.useMemo(() => "function" == typeof i ? Array.from({length: z}, (e, t) => i(n[t], t)) : i, [ i, n, z ]), b = t.useRef(null), y = t.useRef(!!g), k = X(w), R = X(S), [T, C, O, E] = P(() => { + const e = !!c, t = I(z, A, g, a, !A); return [ t, W(t, e), M(t, e), e ]; }); - z !== T.T() && T.q(5, [ z, l ]), h !== T.O() && T.q(6, h); + z !== T.T() && T.q(5, [ z, l, A ]), h !== T.O() && T.q(6, h); const [H, q] = t.useReducer(T._, void 0, T._), B = T.M(), L = T.h(), j = O.N(), D = [], U = t => { const o = $(t); return e.jsx(Y, { diff --git a/lib/index.js b/lib/index.js index 110ac3858a002a6cdb698da2b56350bc1bf609d2..db83efc83fb3b6d7aa07d83b7463d6ed7746287e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,7 +1,7 @@ "use client"; import { jsx as e } from "react/jsx-runtime"; -import { useLayoutEffect as t, useEffect as o, useRef as n, memo as r, useMemo as s, forwardRef as i, useReducer as l, useImperativeHandle as c } from "react"; +import { useLayoutEffect as t, useEffect as o, useRef as n, memo as r, useMemo as s, useMemo as aa, forwardRef as i, useReducer as l, useImperativeHandle as c } from "react"; import { flushSync as f } from "react-dom"; @@ -39,10 +39,18 @@ const u = null, {min: d, max: a, abs: h, floor: g} = Math, p = (e, t, o) => d(o, b(e, s) <= t ? (r = s, o = s + 1) : n = s - 1; } return p(r, 0, e.l - 1); -}, x = (e, t, o) => { +}, x = (e, t, o, r) => { const n = t - e.l; - return e.i = o ? -1 : d(t - 1, e.i), e.l = t, n > 0 ? (S(e.u, n), S(e.t, n, o), - e.o * n) : (e.u.splice(n), (o ? e.t.splice(0, -n) : e.t.splice(n)).reduce((t, o) => t - (-1 === o ? e.o : o), 0)); + if (e.i = o ? -1 : d(t - 1, e.i), e.l = t, n > 0) { + S(e.u, n); + if (r) { + const t = o ? r.slice(0, n) : r.slice(r.length - n); + e.t[o ? "unshift" : "push"](...t); + return t.reduce((e, t) => e + t, 0); + } + return S(e.t, n, o), e.o * n; + } + return e.u.splice(n), (o ? e.t.splice(0, -n) : e.t.splice(n)).reduce((t, o) => t - (-1 === o ? e.o : o), 0); }, I = "undefined" != typeof window, k = e => e.documentElement, R = e => e.ownerDocument, T = e => e.defaultView, C = /*#__PURE__*/ w(() => !!/iP(hone|od|ad)/.test(navigator.userAgent) || "MacIntel" === navigator.platform && navigator.maxTouchPoints > 0), M = /*#__PURE__*/ w(() => "scrollBehavior" in k(document).style), O = e => a(e.h(), e.p()), E = (e, t = 40, o = 0, n, r = !1) => { let s = !!o, i = 1, l = 0, c = 0, f = 0, g = 0, p = 0, m = 0, _ = 0, w = 0, I = u, k = [ 0, s ? a(o - 1, 0) : -1 ], R = 0, T = !1; const M = ((e, t, o) => ({ @@ -51,7 +59,7 @@ const u = null, {min: d, max: a, abs: h, floor: g} = Math, p = (e, t, o) => d(o, l: e, i: -1, u: S([], e + 1) - }))(e, n ? n[1] : t, n && n[0]), O = new Set, E = () => f - c, H = () => E() + p + g, W = (e, t) => ((e, t, o, n) => { + }))(e, n ? n[1] : Array.isArray(t) ? t.reduce((e, t) => e + t, 0) / (t.length || 1) : t, n ? n[0] : Array.isArray(t) ? t : void 0), O = new Set, E = () => f - c, H = () => E() + p + g, W = (e, t) => ((e, t, o, n) => { if (n = d(n, e.l - 1), b(e, n) <= t) { const r = y(e, o, n); return [ y(e, t, n, r), r ]; @@ -151,7 +159,7 @@ const u = null, {min: d, max: a, abs: h, floor: g} = Math, p = (e, t, o) => d(o, break; case 5: - t[1] ? (N(x(M, t[0], !0)), w = 2, d = 1) : (x(M, t[0]), d = 1); + t[1] ? (N(x(M, t[0], !0, t[2])), w = 2, d = 1) : (x(M, t[0], !1, t[2]), w = 0, d = 1); break; case 6: @@ -164,6 +172,10 @@ const u = null, {min: d, max: a, abs: h, floor: g} = Math, p = (e, t, o) => d(o, case 8: I = W(t, t + l), d = 1; + break; + + case 9: + w = 0; } d && (i = 1 + (2147483647 & i), o && p && (g += p, p = 0), O.forEach(([e, t]) => { d & e && t(n); @@ -190,6 +202,8 @@ const u = null, {min: d, max: a, abs: h, floor: g} = Math, p = (e, t, o) => d(o, l = i(), d && (a = !0), s && e.B(6, s()), e.B(1, n()), h(); }, p = t => { - if (c || !e.M() || t.ctrlKey) return; + if (!e.M() || t.ctrlKey) return; + e.B(9); + if (c) return; const n = i() - l; 150 > n && 50 < n && (o ? t.deltaX : t.deltaY) && (c = !0); }, v = () => { @@ -270,7 +283,7 @@ const u = null, {min: d, max: a, abs: h, floor: g} = Math, p = (e, t, o) => d(o, e[l] = t; }); } - u[i] = W(e.C() + t, s), o && f(); + u[i] = W((o ? W(u[i], s) : e.C()) + t, s), o && f(); }), r[1](!0); }, v() { @@ -523,11 +536,11 @@ const u = null, {min: d, max: a, abs: h, floor: g} = Math, p = (e, t, o) => d(o, })(e); return [ e => o[e], o.length ]; }, [ e, t ]), Z = /*#__PURE__*/ i(({children: t, data: o, bufferSize: r, itemSize: s, shift: i, horizontal: u, keepMounted: d, cache: a, startMargin: h = 0, ssrCount: g, as: p = "div", item: _ = "div", scrollRef: w, onScroll: S, onScrollEnd: $}, z) => { - const [b, y] = Q(t, o), x = n(null), I = n(!!g), k = F(S), R = F($), [T, C, M, H] = U(() => { - const e = !!u, t = E(y, s, g, a, !s); + const [b, y] = Q(t, o), bb = aa(() => "function" == typeof s ? Array.from({length: y}, (e, t) => s(o[t], t)) : s, [ s, o, y ]), x = n(null), I = n(!!g), k = F(S), R = F($), [T, C, M, H] = U(() => { + const e = !!u, t = E(y, bb, g, a, !bb); return [ t, V(t, e), A(t, e), e ]; }); - y !== T.T() && T.B(5, [ y, i ]), h !== T.O() && T.B(6, h); + y !== T.T() && T.B(5, [ y, i, bb ]), h !== T.O() && T.B(6, h); const [W, B] = l(T.m, void 0, T.m), J = T.M(), L = T.h(), N = M.P(), P = [], X = t => { const o = b(t); return e(K, { diff --git a/lib/react/Virtualizer.d.ts b/lib/react/Virtualizer.d.ts index 46c5d0765641bb1b4afeb9782b64e5fd71e6a1c8..e82091f3514fdccf064bf28c0a1b22c8615fbdf2 100644 --- a/lib/react/Virtualizer.d.ts +++ b/lib/react/Virtualizer.d.ts @@ -78,7 +78,7 @@ export interface VirtualizerProps { * - If not set, initial item sizes will be automatically estimated from measured sizes. This is recommended for most cases. * - If set, you can opt out estimation and use the value as initial item size. */ - itemSize?: number; + itemSize?: number | ((data: T, index: number) => number); /** * While true is set, scroll position will be maintained from the end not usual start when items are added to/removed from start. It's recommended to set false if you add to/remove from mid/end of the list because it can cause unexpected behavior. This prop is useful for reverse infinite scrolling. */