welcome-ui
Version:
Customizable design system with react, typescript, tailwindcss and ariakit.
291 lines (290 loc) • 8.61 kB
JavaScript
"use client";
import { b as j, d as V, g as Ce, c as W, j as Y, S as ge, q as $, r as q, f as G, a as J, w as Ie, B as be, u as D, ag as xe, ah as F, e as p, af as Se, m as U, ai as we, aj as he, h as Pe, M as Ee, ak as Te } from "./U6HHPQDW-CzxyP5H7.js";
import { createContext as Q, useRef as A, useEffect as ye, useContext as De, useCallback as Fe, useMemo as pe } from "react";
import { u as Ae } from "./PZ3OL7I2-Cfim3UGe.js";
import { f as ke } from "./Q5W46E73-5xQblshY.js";
import { jsx as Re } from "react/jsx-runtime";
var x = j(), Le = x.useContext;
x.useScopedContext;
x.useProviderContext;
var Oe = x.ContextProvider, Ne = x.ScopedContextProvider, S = j(
[Oe],
[Ne]
), Be = S.useContext;
S.useScopedContext;
var nt = S.useProviderContext, rt = S.ContextProvider, it = S.ScopedContextProvider, Me = Q(
void 0
), ze = Q(
void 0
), He = { id: null };
function ot(n, e, r = !1) {
const u = n.findIndex((c) => c.id === e);
return [
...n.slice(u + 1),
...r ? [He] : [],
...n.slice(0, u)
];
}
function ut(n, e) {
return n.find((r) => !r.disabled);
}
function Ke(n, e) {
return e && n.item(e) || null;
}
function at(n) {
const e = [];
for (const r of n) {
const u = e.find((c) => {
var s;
return ((s = c[0]) == null ? void 0 : s.rowId) === r.rowId;
});
u ? u.push(r) : e.push([r]);
}
return e;
}
function Ue(n, e = !1) {
if (V(n))
n.setSelectionRange(
e ? n.value.length : 0,
n.value.length
);
else if (n.isContentEditable) {
const r = Ce(n).getSelection();
r == null || r.selectAllChildren(n), e && (r == null || r.collapseToEnd());
}
}
var k = Symbol("FOCUS_SILENTLY");
function _e(n) {
n[k] = !0, n.focus({ preventScroll: !0 });
}
function st(n) {
const e = n[k];
return delete n[k], e;
}
function X(n, e, r) {
return !(!e || e === r || !n.item(e.id));
}
var je = "div", Z = W(
function({
store: e,
shouldRegisterItem: r = !0,
getItem: u = ge,
// @ts-expect-error This prop may come from a collection renderer.
element: c,
...s
}) {
const m = Le();
e = e || m;
const v = Y(s.id), f = A(c);
return ye(() => {
const i = f.current;
if (!v || !i || !r) return;
const C = u({ id: v, element: i });
return e == null ? void 0 : e.renderItem(C);
}, [v, r, u, e]), s = {
...s,
ref: $(f, s.ref)
}, q(s);
}
);
G(function(e) {
const r = Z(e);
return J(je, r);
});
var Ve = "button";
function We(n) {
return F(n) ? !0 : n.tagName === "INPUT" && !Ee(n);
}
function Ye(n, e = !1) {
const r = n.clientHeight, { top: u } = n.getBoundingClientRect(), c = Math.max(r * 0.875, r - 40) * 1.5, s = e ? r - c + u : c + u;
return n.tagName === "HTML" ? s + n.scrollTop : s;
}
function $e(n, e = !1) {
const { top: r } = n.getBoundingClientRect();
return e ? r + n.clientHeight : r;
}
function _(n, e, r, u = !1) {
var c;
if (!e || !r) return;
const { renderedItems: s } = e.getState(), m = Te(n);
if (!m) return;
const v = Ye(m, u);
let f, i;
for (let C = 0; C < s.length; C += 1) {
const l = f;
if (f = r(C), !f) break;
if (f === l) continue;
const w = (c = Ke(e, f)) == null ? void 0 : c.element;
if (!w) continue;
const h = $e(w, u) - v, I = Math.abs(h);
if (u && h <= 0 || !u && h >= 0) {
i !== void 0 && i < I && (f = l);
break;
}
i = I;
}
return f;
}
function qe(n, e) {
return p(n) ? !1 : X(e, n.target);
}
var Ge = W(
function({
store: e,
rowId: r,
preventScrollOnKeyDown: u = !1,
moveOnKeyPress: c = !0,
tabbable: s = !1,
getItem: m,
"aria-setsize": v,
"aria-posinset": f,
...i
}) {
const C = Be();
e = e || C;
const l = Y(i.id), w = A(null), d = De(ze), I = be(i) && !i.accessibleWhenDisabled, {
rowId: E,
baseElement: R,
isActiveItem: ee,
ariaSetSize: te,
ariaPosInSet: ne,
isTabbable: re
} = ke(e, {
rowId(t) {
if (r) return r;
if (t && d != null && d.baseElement && d.baseElement === t.baseElement)
return d.id;
},
baseElement(t) {
return (t == null ? void 0 : t.baseElement) || void 0;
},
isActiveItem(t) {
return !!t && t.activeId === l;
},
ariaSetSize(t) {
if (v != null) return v;
if (t && d != null && d.ariaSetSize && d.baseElement === t.baseElement)
return d.ariaSetSize;
},
ariaPosInSet(t) {
if (f != null) return f;
if (!t || !(d != null && d.ariaPosInSet) || d.baseElement !== t.baseElement) return;
const o = t.renderedItems.filter(
(a) => a.rowId === E
);
return d.ariaPosInSet + o.findIndex((a) => a.id === l);
},
isTabbable(t) {
if (!(t != null && t.renderedItems.length)) return !0;
if (t.virtualFocus) return !1;
if (s) return !0;
if (t.activeId === null) return !1;
const o = e == null ? void 0 : e.item(t.activeId);
return o != null && o.disabled || !(o != null && o.element) ? !0 : t.activeId === l;
}
}), ie = Fe(
(t) => {
var o;
const a = {
...t,
id: l || t.id,
rowId: E,
disabled: !!I,
children: (o = t.element) == null ? void 0 : o.textContent
};
return m ? m(a) : a;
},
[l, E, I, m]
), L = i.onFocus, T = A(!1), oe = D((t) => {
if (L == null || L(t), t.defaultPrevented || xe(t) || !l || !e || qe(t, e)) return;
const { virtualFocus: o, baseElement: a } = e.getState();
if (e.setActiveId(l), F(t.currentTarget) && Ue(t.currentTarget), !o || !p(t) || We(t.currentTarget) || !(a != null && a.isConnected)) return;
Se() && t.currentTarget.hasAttribute("data-autofocus") && t.currentTarget.scrollIntoView({
block: "nearest",
inline: "nearest"
}), T.current = !0, t.relatedTarget === a || X(e, t.relatedTarget) ? _e(a) : a.focus();
}), O = i.onBlurCapture, ue = D((t) => {
if (O == null || O(t), t.defaultPrevented) return;
const o = e == null ? void 0 : e.getState();
o != null && o.virtualFocus && T.current && (T.current = !1, t.preventDefault(), t.stopPropagation());
}), N = i.onKeyDown, ae = U(u), se = U(c), le = D((t) => {
if (N == null || N(t), t.defaultPrevented || !p(t) || !e) return;
const { currentTarget: o } = t, a = e.getState(), y = e.item(l), g = !!(y != null && y.rowId), P = a.orientation !== "horizontal", b = a.orientation !== "vertical", M = () => !!(g || b || !a.baseElement || !V(a.baseElement)), z = {
ArrowUp: (g || P) && e.up,
ArrowRight: (g || b) && e.next,
ArrowDown: (g || P) && e.down,
ArrowLeft: (g || b) && e.previous,
Home: () => {
if (M())
return !g || t.ctrlKey ? e == null ? void 0 : e.first() : e == null ? void 0 : e.previous(-1);
},
End: () => {
if (M())
return !g || t.ctrlKey ? e == null ? void 0 : e.last() : e == null ? void 0 : e.next(-1);
},
PageUp: () => _(o, e, e == null ? void 0 : e.up, !0),
PageDown: () => _(o, e, e == null ? void 0 : e.down)
}[t.key];
if (z) {
if (F(o)) {
const K = we(o), ce = b && t.key === "ArrowLeft", fe = b && t.key === "ArrowRight", de = P && t.key === "ArrowUp", me = P && t.key === "ArrowDown";
if (fe || me) {
const { length: ve } = he(o);
if (K.end !== ve) return;
} else if ((ce || de) && K.start !== 0) return;
}
const H = z();
if (ae(t) || H !== void 0) {
if (!se(t)) return;
t.preventDefault(), e.move(H);
}
}
}), B = pe(
() => ({ id: l, baseElement: R }),
[l, R]
);
return i = Pe(
i,
(t) => /* @__PURE__ */ Re(Me.Provider, { value: B, children: t }),
[B]
), i = {
id: l,
"data-active-item": ee || void 0,
...i,
ref: $(w, i.ref),
tabIndex: re ? i.tabIndex : -1,
onFocus: oe,
onBlurCapture: ue,
onKeyDown: le
}, i = Ae(i), i = Z({
store: e,
...i,
getItem: ie,
shouldRegisterItem: l ? i.shouldRegisterItem : !1
}), q({
...i,
"aria-setsize": te,
"aria-posinset": ne
});
}
), lt = Ie(
G(function(e) {
const r = Ge(e);
return J(Ve, r);
})
);
export {
it as C,
rt as a,
Ge as b,
lt as c,
Z as d,
nt as e,
ot as f,
Ke as g,
ut as h,
X as i,
at as j,
st as s,
Be as u
};