welcome-ui
Version:
Customizable design system with react, typescript, tailwindcss and ariakit.
967 lines (966 loc) • 25.6 kB
JavaScript
"use client";
import { createContext as xe, useRef as H, useContext as De, useState as X, useEffect as D, useCallback as de, useMemo as mt, isValidElement as Et } from "react";
import { c as J, d as F, a as S, f as j, b as K, _ as Fe, p as Je, M as N, a0 as We, g as P, x as U, a1 as te, a2 as ge, a3 as je, a4 as yt, a5 as St, a6 as Pt, a7 as Oe, S as W, q as R, a8 as wt, u as Qe, a9 as xt, $ as Dt, aa as et, ab as Ft, ac as Ot, T as he, ad as Tt, ae as At, w as Ct, af as tt, O as Rt, l as be, ag as _t, ah as re, W as Lt, Z as Ht, ai as Nt, aj as Mt } from "./OE2EFRVA-C3721cM3.js";
import { createPortal as Ye, flushSync as Vt } from "react-dom";
import { jsx as E, jsxs as le, Fragment as fe } from "react/jsx-runtime";
import { u as q, a as $t, b as It } from "./RTNCFSKZ-P4icqVD0.js";
import { u as kt, d as rt, e as Bt, f as qt, b as Te, i as Wt, g as jt, h as Yt, j as Xt } from "./T22PY7TE-BxbmImwX.js";
var Xe = xe(null), zt = "span", nt = J(
function(t) {
return t = F(S({}, t), {
style: S({
border: 0,
clip: "rect(0 0 0 0)",
height: "1px",
margin: "-1px",
overflow: "hidden",
padding: 0,
position: "absolute",
whiteSpace: "nowrap",
width: "1px"
}, t.style)
}), t;
}
);
j(function(t) {
const r = nt(t);
return K(zt, r);
});
var Kt = "span", Gt = J(
function(t) {
return t = F(S({
"data-focus-trap": "",
tabIndex: 0,
"aria-hidden": !0
}, t), {
style: S({
// Prevents unintended scroll jumps.
position: "fixed",
top: 0,
left: 0
}, t.style)
}), t = nt(t), t;
}
), ce = j(function(t) {
const r = Gt(t);
return K(Kt, r);
}), Ut = "div";
function Zt(e) {
return P(e).body;
}
function Jt(e, t) {
return t ? typeof t == "function" ? t(e) : t : P(e).createElement("div");
}
function Qt(e = "id") {
return `${e ? `${e}-` : ""}${Math.random().toString(36).slice(2, 8)}`;
}
function B(e) {
queueMicrotask(() => {
e == null || e.focus();
});
}
var ot = J(function(t) {
var r = t, {
preserveTabOrder: n,
preserveTabOrderAnchor: o,
portalElement: s,
portalRef: i,
portal: u = !0
} = r, c = Fe(r, [
"preserveTabOrder",
"preserveTabOrderAnchor",
"portalElement",
"portalRef",
"portal"
]);
const h = H(null), m = Je(h, c.ref), O = De(Xe), [f, V] = X(null), [$, I] = X(
null
), k = H(null), M = H(null), G = H(null), T = H(null);
return N(() => {
const l = h.current;
if (!l || !u) {
V(null);
return;
}
const b = Jt(l, s);
if (!b) {
V(null);
return;
}
const g = b.isConnected;
if (g || (O || Zt(l)).appendChild(b), b.id || (b.id = l.id ? `portal/${l.id}` : Qt()), V(b), We(i, b), !g)
return () => {
b.remove(), We(i, null);
};
}, [u, s, O, i]), N(() => {
if (!u || !n || !o) return;
const b = P(o).createElement("span");
return b.style.position = "fixed", o.insertAdjacentElement("afterend", b), I(b), () => {
b.remove(), I(null);
};
}, [u, n, o]), D(() => {
if (!f || !n) return;
let l = 0;
const b = (g) => {
if (!te(g)) return;
const d = g.type === "focusin";
if (cancelAnimationFrame(l), d)
return yt(f);
l = requestAnimationFrame(() => {
St(f, !0);
});
};
return f.addEventListener("focusin", b, !0), f.addEventListener("focusout", b, !0), () => {
cancelAnimationFrame(l), f.removeEventListener("focusin", b, !0), f.removeEventListener("focusout", b, !0);
};
}, [f, n]), c = U(
c,
(l) => {
if (l = // While the portal node is not in the DOM, we need to pass the
// current context to the portal context, otherwise it's going to
// reset to the body element on nested portals.
/* @__PURE__ */ E(Xe.Provider, { value: f || O, children: l }), !u) return l;
if (!f)
return /* @__PURE__ */ E(
"span",
{
ref: m,
id: c.id,
style: { position: "fixed" },
hidden: !0
}
);
l = /* @__PURE__ */ le(fe, { children: [
n && f && /* @__PURE__ */ E(
ce,
{
ref: M,
"data-focus-trap": c.id,
className: "__focus-trap-inner-before",
onFocus: (g) => {
te(g, f) ? B(ge()) : B(k.current);
}
}
),
l,
n && f && /* @__PURE__ */ E(
ce,
{
ref: G,
"data-focus-trap": c.id,
className: "__focus-trap-inner-after",
onFocus: (g) => {
te(g, f) ? B(je()) : B(T.current);
}
}
)
] }), f && (l = Ye(l, f));
let b = /* @__PURE__ */ le(fe, { children: [
n && f && /* @__PURE__ */ E(
ce,
{
ref: k,
"data-focus-trap": c.id,
className: "__focus-trap-outer-before",
onFocus: (g) => {
!(g.relatedTarget === T.current) && te(g, f) ? B(M.current) : B(je());
}
}
),
n && // We're using position: fixed here so that the browser doesn't
// add margin to the element when setting gap on a parent element.
/* @__PURE__ */ E("span", { "aria-owns": f == null ? void 0 : f.id, style: { position: "fixed" } }),
n && f && /* @__PURE__ */ E(
ce,
{
ref: T,
"data-focus-trap": c.id,
className: "__focus-trap-outer-after",
onFocus: (g) => {
if (te(g, f))
B(G.current);
else {
const d = ge();
if (d === M.current) {
requestAnimationFrame(() => {
var oe;
return (oe = ge()) == null ? void 0 : oe.focus();
});
return;
}
B(d);
}
}
}
)
] });
return $ && n && (b = Ye(
b,
$
)), /* @__PURE__ */ le(fe, { children: [
b,
l
] });
},
[f, O, u, c.id, n, $]
), c = F(S({}, c), {
ref: m
}), c;
});
j(function(t) {
const r = ot(t);
return K(Ut, r);
});
var ze = xe(0);
function er({ level: e, children: t }) {
const r = De(ze), n = Math.max(
Math.min(e || r + 1, 6),
1
);
return /* @__PURE__ */ E(ze.Provider, { value: n, children: t });
}
var tr = "div", st = J(function(t) {
var r = t, { autoFocusOnShow: n = !0 } = r, o = Fe(r, ["autoFocusOnShow"]);
return o = U(
o,
(s) => /* @__PURE__ */ E(Pt.Provider, { value: n, children: s }),
[n]
), o;
});
j(function(t) {
const r = st(t);
return K(tr, r);
});
function rr(e, t) {
const n = P(e).createElement("button");
return n.type = "button", n.tabIndex = -1, n.textContent = "Dismiss popup", Object.assign(n.style, {
border: "0px",
clip: "rect(0 0 0 0)",
height: "1px",
margin: "-1px",
overflow: "hidden",
padding: "0px",
position: "absolute",
whiteSpace: "nowrap",
width: "1px"
}), n.addEventListener("click", t), e.prepend(n), () => {
n.removeEventListener("click", t), n.remove();
};
}
function nr(e) {
const t = H();
return D(() => {
if (!e) {
t.current = null;
return;
}
return Oe("mousedown", (n) => {
t.current = n.target;
}, !0);
}, [e]), t;
}
var me = /* @__PURE__ */ new WeakMap();
function ne(e, t, r) {
me.has(e) || me.set(e, /* @__PURE__ */ new Map());
const n = me.get(e), o = n.get(t);
if (!o)
return n.set(t, r()), () => {
var u;
(u = n.get(t)) == null || u(), n.delete(t);
};
const s = r(), i = () => {
s(), o(), n.delete(t);
};
return n.set(t, i), () => {
n.get(t) === i && (s(), n.set(t, o));
};
}
function Ae(e, t, r) {
return ne(e, t, () => {
const o = e.getAttribute(t);
return e.setAttribute(t, r), () => {
o == null ? e.removeAttribute(t) : e.setAttribute(t, o);
};
});
}
function z(e, t, r) {
return ne(e, t, () => {
const o = t in e, s = e[t];
return e[t] = r, () => {
o ? e[t] = s : delete e[t];
};
});
}
function Se(e, t) {
return e ? ne(e, "style", () => {
const n = e.style.cssText;
return Object.assign(e.style, t), () => {
e.style.cssText = n;
};
}) : () => {
};
}
function or(e, t, r) {
return e ? ne(e, t, () => {
const o = e.style.getPropertyValue(t);
return e.style.setProperty(t, r), () => {
o ? e.style.setProperty(t, o) : e.style.removeProperty(t);
};
}) : () => {
};
}
var sr = ["SCRIPT", "STYLE"];
function Pe(e) {
return `__ariakit-dialog-snapshot-${e}`;
}
function ur(e, t) {
const r = P(t), n = Pe(e);
if (!r.body[n]) return !0;
do {
if (t === r.body) return !1;
if (t[n]) return !0;
if (!t.parentElement) return !1;
t = t.parentElement;
} while (!0);
}
function ir(e, t, r) {
return sr.includes(t.tagName) || !ur(e, t) ? !1 : !r.some(
(n) => n && R(t, n)
);
}
function Ce(e, t, r, n) {
for (let o of t) {
if (!(o != null && o.isConnected)) continue;
const s = t.some((c) => !c || c === o ? !1 : c.contains(o)), i = P(o), u = o;
for (; o.parentElement && o !== i.body; ) {
if (n == null || n(o.parentElement, u), !s)
for (const c of o.parentElement.children)
ir(e, c, t) && r(c, u);
o = o.parentElement;
}
}
}
function ar(e, t) {
const { body: r } = P(t[0]), n = [];
return Ce(e, t, (s) => {
n.push(z(s, Pe(e), !0));
}), W(z(r, Pe(e), !0), () => {
for (const s of n)
s();
});
}
function ut(e, ...t) {
if (!e) return !1;
const r = e.getAttribute("data-backdrop");
return r == null ? !1 : r === "" || r === "true" || !t.length ? !0 : t.some((n) => r === n);
}
function Z(e = "", t = !1) {
return `__ariakit-dialog-${t ? "ancestor" : "outside"}${e ? `-${e}` : ""}`;
}
function cr(e, t = "") {
return W(
z(e, Z(), !0),
z(e, Z(t), !0)
);
}
function it(e, t = "") {
return W(
z(e, Z("", !0), !0),
z(e, Z(t, !0), !0)
);
}
function Re(e, t) {
const r = Z(t, !0);
if (e[r]) return !0;
const n = Z(t);
do {
if (e[n]) return !0;
if (!e.parentElement) return !1;
e = e.parentElement;
} while (!0);
}
function Ke(e, t) {
const r = [], n = t.map((s) => s == null ? void 0 : s.id);
return Ce(
e,
t,
(s) => {
ut(s, ...n) || r.unshift(cr(s, e));
},
(s, i) => {
i.hasAttribute("data-dialog") && i.id !== e || r.unshift(it(s, e));
}
), () => {
for (const s of r)
s();
};
}
function lr(e) {
return e.tagName === "HTML" ? !0 : R(P(e).body, e);
}
function fr(e, t) {
if (!e) return !1;
if (R(e, t)) return !0;
const r = t.getAttribute("aria-activedescendant");
if (r) {
const n = P(e).getElementById(r);
if (n)
return R(e, n);
}
return !1;
}
function dr(e, t) {
if (!("clientY" in e)) return !1;
const r = t.getBoundingClientRect();
return r.width === 0 || r.height === 0 ? !1 : r.top <= e.clientY && e.clientY <= r.top + r.height && r.left <= e.clientX && e.clientX <= r.left + r.width;
}
function Ee({
store: e,
type: t,
listener: r,
capture: n,
domReady: o
}) {
const s = Qe(r), i = q(e, "open"), u = H(!1);
N(() => {
if (!i || !o) return;
const { contentElement: c } = e.getState();
if (!c) return;
const h = () => {
u.current = !0;
};
return c.addEventListener("focusin", h, !0), () => c.removeEventListener("focusin", h, !0);
}, [e, i, o]), D(() => i ? Oe(t, (h) => {
const { contentElement: m, disclosureElement: O } = e.getState(), f = h.target;
!m || !f || !lr(f) || R(m, f) || fr(O, f) || f.hasAttribute("data-focus-trap") || dr(h, m) || u.current && !Re(f, m.id) || xt(f) || s(h);
}, n) : void 0, [i, n]);
}
function ye(e, t) {
return typeof e == "function" ? e(t) : !!e;
}
function pr(e, t, r) {
const n = q(e, "open"), o = nr(n), s = { store: e, domReady: r, capture: !0 };
Ee(F(S({}, s), {
type: "click",
listener: (i) => {
const { contentElement: u } = e.getState(), c = o.current;
c && wt(c) && Re(c, u == null ? void 0 : u.id) && ye(t, i) && e.hide();
}
})), Ee(F(S({}, s), {
type: "focusin",
listener: (i) => {
const { contentElement: u } = e.getState();
u && i.target !== P(u) && ye(t, i) && e.hide();
}
})), Ee(F(S({}, s), {
type: "contextmenu",
listener: (i) => {
ye(t, i) && e.hide();
}
}));
}
var Ge = xe({});
function vr(e) {
const t = De(Ge), [r, n] = X([]), o = de(
(u) => {
var c;
return n((h) => [...h, u]), W((c = t.add) == null ? void 0 : c.call(t, u), () => {
n((h) => h.filter((m) => m !== u));
});
},
[t]
);
N(() => $t(e, ["open", "contentElement"], (u) => {
var c;
if (u.open && u.contentElement)
return (c = t.add) == null ? void 0 : c.call(t, e);
}), [e, t]);
const s = mt(() => ({ store: e, add: o }), [e, o]);
return { wrapElement: de(
(u) => /* @__PURE__ */ E(Ge.Provider, { value: s, children: u }),
[s]
), nestedDialogs: r };
}
function gr({
attribute: e,
contentId: t,
contentElement: r,
enabled: n
}) {
const [o, s] = Dt(), i = de(() => {
if (!n || !r) return !1;
const { body: u } = P(r), c = u.getAttribute(e);
return !c || c === t;
}, [o, n, r, e, t]);
return D(() => {
if (!n || !t || !r) return;
const { body: u } = P(r);
if (i())
return u.setAttribute(e, t), () => u.removeAttribute(e);
const c = new MutationObserver(() => Vt(s));
return c.observe(u, { attributeFilter: [e] }), () => c.disconnect();
}, [o, n, t, r, i, e]), i;
}
function hr(e) {
const t = e.getBoundingClientRect().left;
return Math.round(t) + e.scrollLeft ? "paddingLeft" : "paddingRight";
}
function br(e, t, r) {
const n = gr({
attribute: "data-dialog-prevent-body-scroll",
contentElement: e,
contentId: t,
enabled: r
});
D(() => {
if (!n() || !e) return;
const o = P(e), s = et(e), { documentElement: i, body: u } = o, c = i.style.getPropertyValue("--scrollbar-width"), h = c ? Number.parseInt(c, 10) : s.innerWidth - i.clientWidth, m = () => or(
i,
"--scrollbar-width",
`${h}px`
), O = hr(i), f = () => Se(u, {
overflow: "hidden",
[O]: `${h}px`
}), V = () => {
var I, k;
const { scrollX: M, scrollY: G, visualViewport: T } = s, l = (I = T == null ? void 0 : T.offsetLeft) != null ? I : 0, b = (k = T == null ? void 0 : T.offsetTop) != null ? k : 0, g = Se(u, {
position: "fixed",
overflow: "hidden",
top: `${-(G - Math.floor(b))}px`,
left: `${-(M - Math.floor(l))}px`,
right: "0",
[O]: `${h}px`
});
return () => {
g(), process.env.NODE_ENV !== "test" && s.scrollTo({ left: M, top: G, behavior: "instant" });
};
}, $ = Ft() && !Ot();
return W(
m(),
$ ? V() : f()
);
}, [n, e]);
}
function mr(e, ...t) {
if (!e) return !1;
const r = e.getAttribute("data-focus-trap");
return r == null ? !1 : t.length ? r === "" ? !1 : t.some((n) => r === n) : !0;
}
function at() {
return "inert" in HTMLElement.prototype;
}
function Er(e) {
return Ae(e, "aria-hidden", "true");
}
function ct(e, t) {
if (!("style" in e)) return he;
if (at())
return z(e, "inert", !0);
const n = Tt(e, !0).map((o) => {
if (t != null && t.some((i) => i && R(i, o))) return he;
const s = ne(o, "focus", () => (o.focus = he, () => {
delete o.focus;
}));
return W(Ae(o, "tabindex", "-1"), s);
});
return W(
...n,
Er(e),
Se(e, {
pointerEvents: "none",
userSelect: "none",
cursor: "default"
})
);
}
function yr(e, t) {
const r = [], n = t.map((s) => s == null ? void 0 : s.id);
return Ce(
e,
t,
(s) => {
ut(s, ...n) || mr(s, ...n) || r.unshift(ct(s, t));
},
(s) => {
s.hasAttribute("role") && (t.some((i) => i && R(i, s)) || r.unshift(Ae(s, "role", "none")));
}
), () => {
for (const s of r)
s();
};
}
var Sr = "div", Pr = [
"a",
"button",
"details",
"dialog",
"div",
"form",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"header",
"img",
"input",
"label",
"li",
"nav",
"ol",
"p",
"section",
"select",
"span",
"summary",
"textarea",
"ul",
"svg"
];
J(
function(t) {
return t;
}
);
var we = j(
// @ts-expect-error
function(t) {
return K(Sr, t);
}
);
Object.assign(
we,
Pr.reduce((e, t) => (e[t] = j(function(n) {
return K(t, n);
}), e), {})
);
function wr({
store: e,
backdrop: t,
alwaysVisible: r,
hidden: n
}) {
const o = H(null), s = kt({ disclosure: e }), i = q(e, "contentElement");
D(() => {
const h = o.current, m = i;
h && m && (h.style.zIndex = getComputedStyle(m).zIndex);
}, [i]), N(() => {
const h = i == null ? void 0 : i.id;
if (!h) return;
const m = o.current;
if (m)
return it(m, h);
}, [i]);
const u = rt({
ref: o,
store: s,
role: "presentation",
"data-backdrop": (i == null ? void 0 : i.id) || "",
alwaysVisible: r,
hidden: n ?? void 0,
style: {
position: "fixed",
top: 0,
right: 0,
bottom: 0,
left: 0
}
});
if (!t) return null;
if (Et(t))
return /* @__PURE__ */ E(we, F(S({}, u), { render: t }));
const c = typeof t != "boolean" ? t : "div";
return /* @__PURE__ */ E(we, F(S({}, u), { render: /* @__PURE__ */ E(c, {}) }));
}
function xr(e = {}) {
return Bt(e);
}
function Dr(e, t, r) {
return qt(e, t, r);
}
function Fr(e = {}) {
const [t, r] = It(xr, e);
return Dr(t, r, e);
}
var Or = "div", Ue = Mt();
function Tr(e) {
const t = tt();
return !t || e && R(e, t) ? !1 : !!re(t);
}
function Ze(e, t = !1) {
if (!e) return null;
const r = "current" in e ? e.current : e;
return r ? t ? re(r) ? r : null : r : null;
}
var Ar = J(function(t) {
var r = t, {
store: n,
open: o,
onClose: s,
focusable: i = !0,
modal: u = !0,
portal: c = !!u,
backdrop: h = !!u,
hideOnEscape: m = !0,
hideOnInteractOutside: O = !0,
getPersistentElements: f,
preventBodyScroll: V = !!u,
autoFocusOnShow: $ = !0,
autoFocusOnHide: I = !0,
initialFocus: k,
finalFocus: M,
unmountOnHide: G,
unstable_treeSnapshotKey: T
} = r, l = Fe(r, [
"store",
"open",
"onClose",
"focusable",
"modal",
"portal",
"backdrop",
"hideOnEscape",
"hideOnInteractOutside",
"getPersistentElements",
"preventBodyScroll",
"autoFocusOnShow",
"autoFocusOnHide",
"initialFocus",
"finalFocus",
"unmountOnHide",
"unstable_treeSnapshotKey"
]);
const b = Te(), g = H(null), d = Fr({
store: n || b,
open: o,
setOpen(a) {
if (a) return;
const v = g.current;
if (!v) return;
const y = new Event("close", { bubbles: !1, cancelable: !0 });
s && v.addEventListener("close", s, { once: !0 }), v.dispatchEvent(y), y.defaultPrevented && d.setOpen(!0);
}
}), { portalRef: oe, domReady: w } = At(c, l.portalRef), lt = l.preserveTabOrder, pe = q(
d,
(a) => lt && !u && a.mounted
), _ = Ct(l.id), A = q(d, "open"), C = q(d, "mounted"), Y = q(d, "contentElement"), ft = Wt(C, l.hidden, l.alwaysVisible);
br(Y, _, V && !ft), pr(d, O, w);
const { wrapElement: _e, nestedDialogs: Le } = vr(d);
l = U(l, _e, [_e]), N(() => {
if (!A) return;
const a = g.current, v = tt(a, !0);
v && v.tagName !== "BODY" && (a && R(a, v) || d.setDisclosureElement(v));
}, [d, A]), Ue && D(() => {
if (!C) return;
const { disclosureElement: a } = d.getState();
if (!a || !Rt(a)) return;
const v = () => {
let y = !1;
const p = () => {
y = !0;
}, x = { capture: !0, once: !0 };
a.addEventListener("focusin", p, x), Ht(a, "mouseup", () => {
a.removeEventListener("focusin", p, !0), !y && Nt(a);
});
};
return a.addEventListener("mousedown", v), () => {
a.removeEventListener("mousedown", v);
};
}, [d, C]), D(() => {
if (!C || !w) return;
const a = g.current;
if (!a) return;
const v = et(a), y = v.visualViewport || v, p = () => {
var x, L;
const ee = (L = (x = v.visualViewport) == null ? void 0 : x.height) != null ? L : v.innerHeight;
a.style.setProperty("--dialog-viewport-height", `${ee}px`);
};
return p(), y.addEventListener("resize", p), () => {
y.removeEventListener("resize", p);
};
}, [C, w]), D(() => {
if (!u || !C || !w) return;
const a = g.current;
if (!(!a || a.querySelector("[data-dialog-dismiss]")))
return rr(a, d.hide);
}, [d, u, C, w]), N(() => {
if (!at() || A || !C || !w) return;
const a = g.current;
if (a)
return ct(a);
}, [A, C, w]);
const se = A && w;
N(() => {
if (!_ || !se) return;
const a = g.current;
return ar(_, [a]);
}, [_, se, T]);
const He = Qe(f);
N(() => {
if (!_ || !se) return;
const { disclosureElement: a } = d.getState(), v = g.current, y = He() || [], p = [
v,
...y,
...Le.map((x) => x.getState().contentElement)
];
return u ? W(
Ke(_, p),
yr(_, p)
) : Ke(_, [a, ...p]);
}, [
_,
d,
se,
He,
Le,
u,
T
]);
const Ne = !!$, Me = be($), [dt, pt] = X(!1);
D(() => {
if (!A || !Ne || !w || !(Y != null && Y.isConnected)) return;
const a = Ze(k, !0) || // If no initial focus is specified, we try to focus the first element
// with the autofocus attribute. If it's an Ariakit component, the
// Focusable component will consume the autoFocus prop and add the
// data-autofocus attribute to the element instead.
Y.querySelector(
"[data-autofocus=true],[autofocus]"
) || // We have to fallback to the first focusable element otherwise portaled
// dialogs with preserveTabOrder set to true will not receive focus
// properly because the elements aren't tabbable until the dialog receives
// focus.
_t(Y, !0, c && pe) || // Finally, we fallback to the dialog element itself.
Y, v = re(a);
Me(v ? a : null) && (pt(!0), queueMicrotask(() => {
a.focus(), Ue && v && a.scrollIntoView({ block: "nearest", inline: "nearest" });
}));
}, [
A,
Ne,
w,
Y,
k,
c,
pe,
Me
]);
const ue = !!I, Ve = be(I), [ie, $e] = X(!1);
D(() => {
if (A)
return $e(!0), () => $e(!1);
}, [A]);
const Q = de(
(a, v = !0) => {
const { disclosureElement: y } = d.getState();
if (Tr(a)) return;
let p = Ze(M) || y;
if (p != null && p.id) {
const L = P(p), ee = `[aria-activedescendant="${p.id}"]`, ae = L.querySelector(ee);
ae && (p = ae);
}
if (p && !re(p)) {
const L = p.closest("[data-dialog]");
if (L != null && L.id) {
const ee = P(L), ae = `[aria-controls~="${L.id}"]`, qe = ee.querySelector(ae);
qe && (p = qe);
}
}
const x = p && re(p);
if (!x && v) {
requestAnimationFrame(() => Q(a, !1));
return;
}
Ve(x ? p : null) && x && (p == null || p.focus({ preventScroll: !0 }));
},
[d, M, Ve]
), ve = H(!1);
N(() => {
if (A || !ie || !ue) return;
const a = g.current;
ve.current = !0, Q(a);
}, [A, ie, w, ue, Q]), D(() => {
if (!ie || !ue) return;
const a = g.current;
return () => {
if (ve.current) {
ve.current = !1;
return;
}
Q(a);
};
}, [ie, ue, Q]);
const Ie = be(m);
D(() => !w || !C ? void 0 : Oe("keydown", (v) => {
if (v.key !== "Escape" || v.defaultPrevented) return;
const y = g.current;
if (!y || Re(y)) return;
const p = v.target;
if (!p) return;
const { disclosureElement: x } = d.getState();
!!(p.tagName === "BODY" || R(y, p) || !x || R(x, p)) && Ie(v) && d.hide();
}, !0), [d, w, C, Ie]), l = U(
l,
(a) => /* @__PURE__ */ E(er, { level: u ? 1 : void 0, children: a }),
[u]
);
const ke = l.hidden, Be = l.alwaysVisible;
l = U(
l,
(a) => h ? /* @__PURE__ */ le(fe, { children: [
/* @__PURE__ */ E(
wr,
{
store: d,
backdrop: h,
hidden: ke,
alwaysVisible: Be
}
),
a
] }) : a,
[d, h, ke, Be]
);
const [vt, gt] = X(), [ht, bt] = X();
return l = U(
l,
(a) => /* @__PURE__ */ E(jt, { value: d, children: /* @__PURE__ */ E(Yt.Provider, { value: gt, children: /* @__PURE__ */ E(Xt.Provider, { value: bt, children: a }) }) }),
[d]
), l = F(S({
id: _,
"data-dialog": "",
role: "dialog",
tabIndex: i ? -1 : void 0,
"aria-labelledby": vt,
"aria-describedby": ht
}, l), {
ref: Je(g, l.ref)
}), l = st(F(S({}, l), {
autoFocusOnShow: dt
})), l = rt(S({ store: d }, l)), l = Lt(F(S({}, l), { focusable: i })), l = ot(F(S({ portal: c }, l), { portalRef: oe, preserveTabOrder: pe })), l;
});
function Cr(e, t = Te) {
return j(function(n) {
const o = t(), s = n.store || o;
return q(
s,
(u) => !n.unmountOnHide || (u == null ? void 0 : u.mounted) || !!n.open
) ? /* @__PURE__ */ E(e, S({}, n)) : null;
});
}
var Vr = Cr(
j(function(t) {
const r = Ar(t);
return K(Or, r);
}),
Te
);
export {
Vr as D,
we as R,
Ar as a,
xr as b,
Cr as c,
Dr as d,
Fr as u
};