UNPKG

welcome-ui

Version:

Customizable design system with react, typescript, tailwindcss and ariakit.

309 lines (308 loc) 9.56 kB
"use client"; import { H as S, G as C, S as Q, I as L, s as X, g as Z, K as p } from "./OE2EFRVA-C3721cM3.js"; import { t as ee, c as O, s as R, i as te, f as G, d as h, a as ne } from "./RTNCFSKZ-P4icqVD0.js"; function re(t) { const i = []; for (const e of t) i.push(...e); return i; } function U(t) { return t.slice().reverse(); } function ie(t) { var i; const e = t.find((a) => !!a.element), o = [...t].reverse().find((a) => !!a.element); let s = (i = e == null ? void 0 : e.element) == null ? void 0 : i.parentElement; for (; s && (o != null && o.element); ) { if (o && s.contains(o.element)) return s; s = s.parentElement; } return Z(s).body; } function se(t) { return t == null ? void 0 : t.__unstablePrivateStore; } function oe(t = {}) { var i; ee(t, t.store); const e = (i = t.store) == null ? void 0 : i.getState(), o = S( t.items, e == null ? void 0 : e.items, t.defaultItems, [] ), s = new Map(o.map((r) => [r.id, r])), a = { items: o, renderedItems: S(e == null ? void 0 : e.renderedItems, []) }, d = se(t.store), u = O( { items: o, renderedItems: a.renderedItems }, d ), n = O(a, t.store), b = (r) => { const l = X(r, (c) => c.element); u.setState("renderedItems", l), n.setState("renderedItems", l); }; R(n, () => te(u)), R(u, () => G(u, ["items"], (r) => { n.setState("items", r.items); })), R(u, () => G(u, ["renderedItems"], (r) => { let l = !0, c = requestAnimationFrame(() => { const { renderedItems: m } = n.getState(); r.renderedItems !== m && b(r.renderedItems); }); if (typeof IntersectionObserver != "function") return () => cancelAnimationFrame(c); const g = () => { if (l) { l = !1; return; } cancelAnimationFrame(c), c = requestAnimationFrame(() => b(r.renderedItems)); }, F = ie(r.renderedItems), f = new IntersectionObserver(g, { root: F }); for (const m of r.renderedItems) m.element && f.observe(m.element); return () => { cancelAnimationFrame(c), f.disconnect(); }; })); const E = (r, l, c = !1) => { let g; return l((f) => { const m = f.findIndex(({ id: x }) => x === r.id), w = f.slice(); if (m !== -1) { g = f[m]; const x = L(L({}, g), r); w[m] = x, s.set(r.id, x); } else w.push(r), s.set(r.id, r); return w; }), () => { l((f) => { if (!g) return c && s.delete(r.id), f.filter(({ id: x }) => x !== r.id); const m = f.findIndex(({ id: x }) => x === r.id); if (m === -1) return f; const w = f.slice(); return w[m] = g, s.set(r.id, g), w; }); }; }, P = (r) => E( r, (l) => u.setState("items", l), !0 ); return C(L({}, n), { registerItem: P, renderItem: (r) => Q( P(r), E( r, (l) => u.setState("renderedItems", l) ) ), item: (r) => { if (!r) return null; let l = s.get(r); if (!l) { const { items: c } = u.getState(); l = c.find((g) => g.id === r), l && s.set(r, l); } return l || null; }, // @ts-expect-error Internal __unstablePrivateStore: u }); } function le(t, i, e) { return p(i, [e.store]), h(t, e, "items", "setItems"), t; } var ae = { id: null }; function _(t, i) { return t.find((e) => i ? !e.disabled && e.id !== i : !e.disabled); } function de(t, i) { return t.filter((e) => i ? !e.disabled && e.id !== i : !e.disabled); } function $(t, i) { return t.filter((e) => e.rowId === i); } function ce(t, i, e = !1) { const o = t.findIndex((s) => s.id === i); return [ ...t.slice(o + 1), ...e ? [ae] : [], ...t.slice(0, o) ]; } function H(t) { const i = []; for (const e of t) { const o = i.find((s) => { var a; return ((a = s[0]) == null ? void 0 : a.rowId) === e.rowId; }); o ? o.push(e) : i.push([e]); } return i; } function K(t) { let i = 0; for (const { length: e } of t) e > i && (i = e); return i; } function ue(t) { return { id: "__EMPTY_ITEM__", disabled: !0, rowId: t }; } function fe(t, i, e) { const o = K(t); for (const s of t) for (let a = 0; a < o; a += 1) { const d = s[a]; if (!d || e && d.disabled) { const n = a === 0 && e ? _(s) : s[a - 1]; s[a] = n && i !== n.id && e ? n : ue(n == null ? void 0 : n.rowId); } } return t; } function me(t) { const i = H(t), e = K(i), o = []; for (let s = 0; s < e; s += 1) for (const a of i) { const d = a[s]; d && o.push(C(L({}, d), { // If there's no rowId, it means that it's not a grid composite, but // a single row instead. So, instead of verticalizing it, that is, // assigning a different rowId based on the column index, we keep it // undefined so they will be part of the same row. This is useful // when using up/down on one-dimensional composites. rowId: d.rowId ? `${s}` : void 0 })); } return o; } function Se(t = {}) { var i; const e = (i = t.store) == null ? void 0 : i.getState(), o = oe(t), s = S( t.activeId, e == null ? void 0 : e.activeId, t.defaultActiveId ), a = C(L({}, o.getState()), { id: S( t.id, e == null ? void 0 : e.id, `id-${Math.random().toString(36).slice(2, 8)}` ), activeId: s, baseElement: S(e == null ? void 0 : e.baseElement, null), includesBaseElement: S( t.includesBaseElement, e == null ? void 0 : e.includesBaseElement, s === null ), moves: S(e == null ? void 0 : e.moves, 0), orientation: S( t.orientation, e == null ? void 0 : e.orientation, "both" ), rtl: S(t.rtl, e == null ? void 0 : e.rtl, !1), virtualFocus: S( t.virtualFocus, e == null ? void 0 : e.virtualFocus, !1 ), focusLoop: S(t.focusLoop, e == null ? void 0 : e.focusLoop, !1), focusWrap: S(t.focusWrap, e == null ? void 0 : e.focusWrap, !1), focusShift: S(t.focusShift, e == null ? void 0 : e.focusShift, !1) }), d = O(a, o, t.store); R( d, () => ne(d, ["renderedItems", "activeId"], (n) => { d.setState("activeId", (b) => { var E; return b !== void 0 ? b : (E = _(n.renderedItems)) == null ? void 0 : E.id; }); }) ); const u = (n = "next", b = {}) => { var E, P; const r = d.getState(), { skip: l = 0, activeId: c = r.activeId, focusShift: g = r.focusShift, focusLoop: F = r.focusLoop, focusWrap: f = r.focusWrap, includesBaseElement: m = r.includesBaseElement, renderedItems: w = r.renderedItems, rtl: x = r.rtl } = b, y = n === "up" || n === "down", z = n === "next" || n === "down", Y = z ? x && !y : !x || y, j = g && !l; let v = y ? re( fe(H(w), c, j) ) : w; if (v = Y ? U(v) : v, v = y ? me(v) : v, c == null) return (E = _(v)) == null ? void 0 : E.id; const M = v.find((I) => I.id === c); if (!M) return (P = _(v)) == null ? void 0 : P.id; const D = v.some((I) => I.rowId), J = v.indexOf(M), N = v.slice(J + 1), W = $(N, M.rowId); if (l) { const I = de(W, c), B = I.slice(l)[0] || // If we can't find an item, just return the last one. I[I.length - 1]; return B == null ? void 0 : B.id; } const T = F && (y ? F !== "horizontal" : F !== "vertical"), V = D && f && (y ? f !== "horizontal" : f !== "vertical"), A = z ? (!D || y) && T && m : y ? m : !1; if (T) { const I = V && !A ? v : $(v, M.rowId), B = ce(I, c, A), q = _(B, c); return q == null ? void 0 : q.id; } if (V) { const I = _( // We can use nextItems, which contains all the next items, including // items from other rows, to wrap between rows. However, if there is a // null item (the composite container), we'll only use the next items in // the row. So moving next from the last item will focus on the // composite container. On grid composites, horizontal navigation never // focuses on the composite container, only vertical. A ? W : N, c ); return A ? (I == null ? void 0 : I.id) || null : I == null ? void 0 : I.id; } const k = _(W, c); return !k && A ? null : k == null ? void 0 : k.id; }; return C(L(L({}, o), d), { setBaseElement: (n) => d.setState("baseElement", n), setActiveId: (n) => d.setState("activeId", n), move: (n) => { n !== void 0 && (d.setState("activeId", n), d.setState("moves", (b) => b + 1)); }, first: () => { var n; return (n = _(d.getState().renderedItems)) == null ? void 0 : n.id; }, last: () => { var n; return (n = _(U(d.getState().renderedItems))) == null ? void 0 : n.id; }, next: (n) => (n !== void 0 && typeof n == "number" && (n = { skip: n }), u("next", n)), previous: (n) => (n !== void 0 && typeof n == "number" && (n = { skip: n }), u("previous", n)), down: (n) => (n !== void 0 && typeof n == "number" && (n = { skip: n }), u("down", n)), up: (n) => (n !== void 0 && typeof n == "number" && (n = { skip: n }), u("up", n)) }); } function ge(t, i, e) { return t = le(t, i, e), h(t, e, "activeId", "setActiveId"), h(t, e, "includesBaseElement"), h(t, e, "virtualFocus"), h(t, e, "orientation"), h(t, e, "rtl"), h(t, e, "focusLoop"), h(t, e, "focusWrap"), h(t, e, "focusShift"), t; } export { oe as a, Se as c, re as f, U as r, ge as u };