welcome-ui
Version:
Customizable design system with react, typescript, tailwindcss and ariakit.
167 lines (166 loc) • 5.8 kB
JavaScript
"use client";
import { e as W, g as v, s as X, i as w, a as J, h as Q, j as Y } from "./WZWDIE3S-CGqoM1Fy.js";
import { c as Z, i as $, al as ee, am as te, k as B, an as E, p as ne, ab as re, u as F, e as I, m as ie, d as H, h as ae, q as ue, X as oe, ao as ce, f as le, a as se } from "./U6HHPQDW-CzxyP5H7.js";
import { f as fe, r as de } from "./7PRQYBBV-D9IY4qtU.js";
import { useRef as R, useEffect as M, useState as me, useCallback as ve } from "react";
import { jsx as ge } from "react/jsx-runtime";
var ye = "div";
function Ee(n) {
return n.some((e) => !!e.rowId);
}
function Ie(n) {
const e = n.target;
return e && !H(e) ? !1 : n.key.length === 1 && !n.ctrlKey && !n.metaKey;
}
function pe(n) {
return n.key === "Shift" || n.key === "Control" || n.key === "Alt" || n.key === "Meta";
}
function _(n, e, a) {
return F((u) => {
var s;
if (e == null || e(u), u.defaultPrevented || u.isPropagationStopped() || !I(u) || pe(u) || Ie(u)) return;
const r = n.getState(), d = (s = v(n, r.activeId)) == null ? void 0 : s.element;
if (!d) return;
const { view: h, ...l } = u, P = a == null ? void 0 : a.current;
d !== P && d.focus(), ce(d, u.type, l) || u.preventDefault(), u.currentTarget.contains(d) && u.stopPropagation();
});
}
function Fe(n) {
return Q(
fe(de(Y(n)))
);
}
function Se(n) {
const [e, a] = me(!1), u = ve(() => a(!0), []), s = n.useState(
(r) => v(n, r.activeId)
);
return M(() => {
const r = s == null ? void 0 : s.element;
e && r && (a(!1), r.focus({ preventScroll: !0 }));
}, [s, e]), u;
}
var Ce = Z(
function({
store: e,
composite: a = !0,
focusOnMove: u = a,
moveOnKeyPress: s = !0,
...r
}) {
const d = W();
e = e || d, $(
e,
process.env.NODE_ENV !== "production" && "Composite must receive a `store` prop or be wrapped in a CompositeProvider component."
);
const h = R(null), l = R(null), P = Se(e), S = e.useState("moves"), [, z] = ee(
a ? e.setBaseElement : null
);
M(() => {
var t;
if (!e || !S || !a || !u) return;
const { activeId: i } = e.getState(), o = (t = v(e, i)) == null ? void 0 : t.element;
o && te(o);
}, [e, S, a, u]), B(() => {
if (!e || !S || !a) return;
const { baseElement: t, activeId: i } = e.getState();
if (!(i === null) || !t) return;
const c = l.current;
l.current = null, c && E(c, { relatedTarget: t }), ne(t) || t.focus();
}, [e, S, a]);
const k = e.useState("activeId"), A = e.useState("virtualFocus");
B(() => {
var t;
if (!e || !a || !A) return;
const i = l.current;
if (l.current = null, !i) return;
const c = ((t = v(e, k)) == null ? void 0 : t.element) || re(i);
c !== i && E(i, { relatedTarget: c });
}, [e, k, A, a]);
const L = _(
e,
r.onKeyDownCapture,
l
), U = _(
e,
r.onKeyUpCapture,
l
), K = r.onFocusCapture, N = F((t) => {
if (K == null || K(t), t.defaultPrevented || !e) return;
const { virtualFocus: i } = e.getState();
if (!i) return;
const o = t.relatedTarget, c = X(t.currentTarget);
I(t) && c && (t.stopPropagation(), l.current = o);
}), D = r.onFocus, V = F((t) => {
if (D == null || D(t), t.defaultPrevented || !a || !e) return;
const { relatedTarget: i } = t, { virtualFocus: o } = e.getState();
o ? I(t) && !w(e, i) && queueMicrotask(P) : I(t) && e.setActiveId(null);
}), T = r.onBlurCapture, j = F((t) => {
var i;
if (T == null || T(t), t.defaultPrevented || !e) return;
const { virtualFocus: o, activeId: c } = e.getState();
if (!o) return;
const m = (i = v(e, c)) == null ? void 0 : i.element, g = t.relatedTarget, C = w(e, g), f = l.current;
l.current = null, I(t) && C ? (g === m ? f && f !== g && E(f, t) : m ? E(m, t) : f && E(f, t), t.stopPropagation()) : !w(e, t.target) && m && E(m, t);
}), b = r.onKeyDown, q = ie(s), G = F((t) => {
var i;
if (b == null || b(t), t.nativeEvent.isComposing || t.defaultPrevented || !e || !I(t)) return;
const { orientation: o, renderedItems: c, activeId: m } = e.getState(), g = v(e, m);
if ((i = g == null ? void 0 : g.element) != null && i.isConnected) return;
const C = o !== "horizontal", f = o !== "vertical", y = Ee(c);
if ((t.key === "ArrowLeft" || t.key === "ArrowRight" || t.key === "Home" || t.key === "End") && H(t.currentTarget)) return;
const x = {
ArrowUp: (y || C) && (() => {
if (y) {
const p = Fe(c);
return p == null ? void 0 : p.id;
}
return e == null ? void 0 : e.last();
}),
ArrowRight: (y || f) && e.first,
ArrowDown: (y || C) && e.first,
ArrowLeft: (y || f) && e.last,
Home: e.first,
End: e.last,
PageUp: e.first,
PageDown: e.last
}[t.key];
if (x) {
const p = x();
if (p !== void 0) {
if (!q(t)) return;
t.preventDefault(), e.move(p);
}
}
});
r = ae(
r,
(t) => /* @__PURE__ */ ge(J, { value: e, children: t }),
[e]
), r = {
"aria-activedescendant": e.useState((t) => {
var i;
if (e && a && t.virtualFocus)
return (i = v(e, t.activeId)) == null ? void 0 : i.id;
}),
...r,
ref: ue(h, z, r.ref),
onKeyDownCapture: L,
onKeyUpCapture: U,
onFocusCapture: N,
onFocus: V,
onBlurCapture: j,
onKeyDown: G
};
const O = e.useState(
(t) => a && (t.virtualFocus || t.activeId === null)
);
return r = oe({ focusable: O, ...r }), r;
}
);
le(function(e) {
const a = Ce(e);
return se(ye, a);
});
export {
Ce as u
};