@konstructio/ui
Version:
A set of reusable and customizable React components built for konstruct.io
480 lines (477 loc) • 16.7 kB
JavaScript
import * as i from "react";
import { P as O, c as Ee, b as _, a as ye, e as he } from "./index-DKfEnhKr.js";
import { u as F, c as Ce } from "./index-DLcqcWxM.js";
import { u as w } from "./index-Dy59FQl5.js";
import { D as De } from "./index-BlSRBdPy.js";
import { u as $ } from "./index-0ioNhtNM.js";
import { jsx as u, jsxs as j, Fragment as B } from "react/jsx-runtime";
import { P as be } from "./index-DMb4KD0b.js";
import { P as U } from "./index-AV6ZtGhy.js";
import { R as Re } from "./Combination-BtmnusWq.js";
var x = "focusScope.autoFocusOnMount", W = "focusScope.autoFocusOnUnmount", q = { bubbles: !1, cancelable: !0 }, Ae = "FocusScope", Q = i.forwardRef((e, t) => {
const {
loop: n = !1,
trapped: o = !1,
onMountAutoFocus: r,
onUnmountAutoFocus: a,
...l
} = e, [s, m] = i.useState(null), p = $(r), E = $(a), c = i.useRef(null), d = F(t, (f) => m(f)), v = i.useRef({
paused: !1,
pause() {
this.paused = !0;
},
resume() {
this.paused = !1;
}
}).current;
i.useEffect(() => {
if (o) {
let f = function(C) {
if (v.paused || !s) return;
const h = C.target;
s.contains(h) ? c.current = h : b(c.current, { select: !0 });
}, D = function(C) {
if (v.paused || !s) return;
const h = C.relatedTarget;
h !== null && (s.contains(h) || b(c.current, { select: !0 }));
}, g = function(C) {
if (document.activeElement === document.body)
for (const H of C)
H.removedNodes.length > 0 && b(s);
};
document.addEventListener("focusin", f), document.addEventListener("focusout", D);
const N = new MutationObserver(g);
return s && N.observe(s, { childList: !0, subtree: !0 }), () => {
document.removeEventListener("focusin", f), document.removeEventListener("focusout", D), N.disconnect();
};
}
}, [o, s, v.paused]), i.useEffect(() => {
if (s) {
Y.add(v);
const f = document.activeElement;
if (!s.contains(f)) {
const g = new CustomEvent(x, q);
s.addEventListener(x, p), s.dispatchEvent(g), g.defaultPrevented || (Oe(Ie(X(s)), { select: !0 }), document.activeElement === f && b(s));
}
return () => {
s.removeEventListener(x, p), setTimeout(() => {
const g = new CustomEvent(W, q);
s.addEventListener(W, E), s.dispatchEvent(g), g.defaultPrevented || b(f ?? document.body, { select: !0 }), s.removeEventListener(W, E), Y.remove(v);
}, 0);
};
}
}, [s, p, E, v]);
const P = i.useCallback(
(f) => {
if (!n && !o || v.paused) return;
const D = f.key === "Tab" && !f.altKey && !f.ctrlKey && !f.metaKey, g = document.activeElement;
if (D && g) {
const N = f.currentTarget, [C, h] = Ne(N);
C && h ? !f.shiftKey && g === h ? (f.preventDefault(), n && b(C, { select: !0 })) : f.shiftKey && g === C && (f.preventDefault(), n && b(h, { select: !0 })) : g === N && f.preventDefault();
}
},
[n, o, v.paused]
);
return /* @__PURE__ */ u(O.div, { tabIndex: -1, ...l, ref: d, onKeyDown: P });
});
Q.displayName = Ae;
function Oe(e, { select: t = !1 } = {}) {
const n = document.activeElement;
for (const o of e)
if (b(o, { select: t }), document.activeElement !== n) return;
}
function Ne(e) {
const t = X(e), n = Z(t, e), o = Z(t.reverse(), e);
return [n, o];
}
function X(e) {
const t = [], n = document.createTreeWalker(e, NodeFilter.SHOW_ELEMENT, {
acceptNode: (o) => {
const r = o.tagName === "INPUT" && o.type === "hidden";
return o.disabled || o.hidden || r ? NodeFilter.FILTER_SKIP : o.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
}
});
for (; n.nextNode(); ) t.push(n.currentNode);
return t;
}
function Z(e, t) {
for (const n of e)
if (!_e(n, { upTo: t })) return n;
}
function _e(e, { upTo: t }) {
if (getComputedStyle(e).visibility === "hidden") return !0;
for (; e; ) {
if (t !== void 0 && e === t) return !1;
if (getComputedStyle(e).display === "none") return !0;
e = e.parentElement;
}
return !1;
}
function Pe(e) {
return e instanceof HTMLInputElement && "select" in e;
}
function b(e, { select: t = !1 } = {}) {
if (e && e.focus) {
const n = document.activeElement;
e.focus({ preventScroll: !0 }), e !== n && Pe(e) && t && e.select();
}
}
var Y = Se();
function Se() {
let e = [];
return {
add(t) {
const n = e[0];
t !== n && n?.pause(), e = z(e, t), e.unshift(t);
},
remove(t) {
e = z(e, t), e[0]?.resume();
}
};
}
function z(e, t) {
const n = [...e], o = n.indexOf(t);
return o !== -1 && n.splice(o, 1), n;
}
function Ie(e) {
return e.filter((t) => t.tagName !== "A");
}
var L = 0;
function Te() {
i.useEffect(() => {
const e = document.querySelectorAll("[data-radix-focus-guard]");
return document.body.insertAdjacentElement("afterbegin", e[0] ?? J()), document.body.insertAdjacentElement("beforeend", e[1] ?? J()), L++, () => {
L === 1 && document.querySelectorAll("[data-radix-focus-guard]").forEach((t) => t.remove()), L--;
};
}, []);
}
function J() {
const e = document.createElement("span");
return e.setAttribute("data-radix-focus-guard", ""), e.tabIndex = 0, e.style.outline = "none", e.style.opacity = "0", e.style.position = "fixed", e.style.pointerEvents = "none", e;
}
var Fe = function(e) {
if (typeof document > "u")
return null;
var t = Array.isArray(e) ? e[0] : e;
return t.ownerDocument.body;
}, A = /* @__PURE__ */ new WeakMap(), S = /* @__PURE__ */ new WeakMap(), I = {}, k = 0, ee = function(e) {
return e && (e.host || ee(e.parentNode));
}, Me = function(e, t) {
return t.map(function(n) {
if (e.contains(n))
return n;
var o = ee(n);
return o && e.contains(o) ? o : (console.error("aria-hidden", n, "in not contained inside", e, ". Doing nothing"), null);
}).filter(function(n) {
return !!n;
});
}, we = function(e, t, n, o) {
var r = Me(t, Array.isArray(e) ? e : [e]);
I[n] || (I[n] = /* @__PURE__ */ new WeakMap());
var a = I[n], l = [], s = /* @__PURE__ */ new Set(), m = new Set(r), p = function(c) {
!c || s.has(c) || (s.add(c), p(c.parentNode));
};
r.forEach(p);
var E = function(c) {
!c || m.has(c) || Array.prototype.forEach.call(c.children, function(d) {
if (s.has(d))
E(d);
else
try {
var v = d.getAttribute(o), P = v !== null && v !== "false", f = (A.get(d) || 0) + 1, D = (a.get(d) || 0) + 1;
A.set(d, f), a.set(d, D), l.push(d), f === 1 && P && S.set(d, !0), D === 1 && d.setAttribute(n, "true"), P || d.setAttribute(o, "true");
} catch (g) {
console.error("aria-hidden: cannot operate on ", d, g);
}
});
};
return E(t), s.clear(), k++, function() {
l.forEach(function(c) {
var d = A.get(c) - 1, v = a.get(c) - 1;
A.set(c, d), a.set(c, v), d || (S.has(c) || c.removeAttribute(o), S.delete(c)), v || c.removeAttribute(n);
}), k--, k || (A = /* @__PURE__ */ new WeakMap(), A = /* @__PURE__ */ new WeakMap(), S = /* @__PURE__ */ new WeakMap(), I = {});
};
}, xe = function(e, t, n) {
n === void 0 && (n = "data-aria-hidden");
var o = Array.from(Array.isArray(e) ? e : [e]), r = Fe(e);
return r ? (o.push.apply(o, Array.from(r.querySelectorAll("[aria-live]"))), we(o, r, n, "aria-hidden")) : function() {
return null;
};
};
// @__NO_SIDE_EFFECTS__
function We(e) {
const t = /* @__PURE__ */ Le(e), n = i.forwardRef((o, r) => {
const { children: a, ...l } = o, s = i.Children.toArray(a), m = s.find(Ue);
if (m) {
const p = m.props.children, E = s.map((c) => c === m ? i.Children.count(p) > 1 ? i.Children.only(null) : i.isValidElement(p) ? p.props.children : null : c);
return /* @__PURE__ */ u(t, { ...l, ref: r, children: i.isValidElement(p) ? i.cloneElement(p, void 0, E) : null });
}
return /* @__PURE__ */ u(t, { ...l, ref: r, children: a });
});
return n.displayName = `${e}.Slot`, n;
}
// @__NO_SIDE_EFFECTS__
function Le(e) {
const t = i.forwardRef((n, o) => {
const { children: r, ...a } = n;
if (i.isValidElement(r)) {
const l = Ge(r), s = Ke(a, r.props);
return r.type !== i.Fragment && (s.ref = o ? Ce(o, l) : l), i.cloneElement(r, s);
}
return i.Children.count(r) > 1 ? i.Children.only(null) : null;
});
return t.displayName = `${e}.SlotClone`, t;
}
var ke = /* @__PURE__ */ Symbol("radix.slottable");
function Ue(e) {
return i.isValidElement(e) && typeof e.type == "function" && "__radixId" in e.type && e.type.__radixId === ke;
}
function Ke(e, t) {
const n = { ...t };
for (const o in t) {
const r = e[o], a = t[o];
/^on[A-Z]/.test(o) ? r && a ? n[o] = (...s) => {
const m = a(...s);
return r(...s), m;
} : r && (n[o] = r) : o === "style" ? n[o] = { ...r, ...a } : o === "className" && (n[o] = [r, a].filter(Boolean).join(" "));
}
return { ...e, ...n };
}
function Ge(e) {
let t = Object.getOwnPropertyDescriptor(e.props, "ref")?.get, n = t && "isReactWarning" in t && t.isReactWarning;
return n ? e.ref : (t = Object.getOwnPropertyDescriptor(e, "ref")?.get, n = t && "isReactWarning" in t && t.isReactWarning, n ? e.props.ref : e.props.ref || e.ref);
}
var M = "Dialog", [te, st] = Ee(M), [Ve, y] = te(M), ne = (e) => {
const {
__scopeDialog: t,
children: n,
open: o,
defaultOpen: r,
onOpenChange: a,
modal: l = !0
} = e, s = i.useRef(null), m = i.useRef(null), [p, E] = ye({
prop: o,
defaultProp: r ?? !1,
onChange: a,
caller: M
});
return /* @__PURE__ */ u(
Ve,
{
scope: t,
triggerRef: s,
contentRef: m,
contentId: w(),
titleId: w(),
descriptionId: w(),
open: p,
onOpenChange: E,
onOpenToggle: i.useCallback(() => E((c) => !c), [E]),
modal: l,
children: n
}
);
};
ne.displayName = M;
var oe = "DialogTrigger", re = i.forwardRef(
(e, t) => {
const { __scopeDialog: n, ...o } = e, r = y(oe, n), a = F(t, r.triggerRef);
return /* @__PURE__ */ u(
O.button,
{
type: "button",
"aria-haspopup": "dialog",
"aria-expanded": r.open,
"aria-controls": r.contentId,
"data-state": V(r.open),
...o,
ref: a,
onClick: _(e.onClick, r.onOpenToggle)
}
);
}
);
re.displayName = oe;
var K = "DialogPortal", [He, ae] = te(K, {
forceMount: void 0
}), se = (e) => {
const { __scopeDialog: t, forceMount: n, children: o, container: r } = e, a = y(K, t);
return /* @__PURE__ */ u(He, { scope: t, forceMount: n, children: i.Children.map(o, (l) => /* @__PURE__ */ u(U, { present: n || a.open, children: /* @__PURE__ */ u(be, { asChild: !0, container: r, children: l }) })) });
};
se.displayName = K;
var T = "DialogOverlay", ie = i.forwardRef(
(e, t) => {
const n = ae(T, e.__scopeDialog), { forceMount: o = n.forceMount, ...r } = e, a = y(T, e.__scopeDialog);
return a.modal ? /* @__PURE__ */ u(U, { present: o || a.open, children: /* @__PURE__ */ u(je, { ...r, ref: t }) }) : null;
}
);
ie.displayName = T;
var $e = /* @__PURE__ */ We("DialogOverlay.RemoveScroll"), je = i.forwardRef(
(e, t) => {
const { __scopeDialog: n, ...o } = e, r = y(T, n);
return (
// Make sure `Content` is scrollable even when it doesn't live inside `RemoveScroll`
// ie. when `Overlay` and `Content` are siblings
/* @__PURE__ */ u(Re, { as: $e, allowPinchZoom: !0, shards: [r.contentRef], children: /* @__PURE__ */ u(
O.div,
{
"data-state": V(r.open),
...o,
ref: t,
style: { pointerEvents: "auto", ...o.style }
}
) })
);
}
), R = "DialogContent", ce = i.forwardRef(
(e, t) => {
const n = ae(R, e.__scopeDialog), { forceMount: o = n.forceMount, ...r } = e, a = y(R, e.__scopeDialog);
return /* @__PURE__ */ u(U, { present: o || a.open, children: a.modal ? /* @__PURE__ */ u(Be, { ...r, ref: t }) : /* @__PURE__ */ u(qe, { ...r, ref: t }) });
}
);
ce.displayName = R;
var Be = i.forwardRef(
(e, t) => {
const n = y(R, e.__scopeDialog), o = i.useRef(null), r = F(t, n.contentRef, o);
return i.useEffect(() => {
const a = o.current;
if (a) return xe(a);
}, []), /* @__PURE__ */ u(
ue,
{
...e,
ref: r,
trapFocus: n.open,
disableOutsidePointerEvents: !0,
onCloseAutoFocus: _(e.onCloseAutoFocus, (a) => {
a.preventDefault(), n.triggerRef.current?.focus();
}),
onPointerDownOutside: _(e.onPointerDownOutside, (a) => {
const l = a.detail.originalEvent, s = l.button === 0 && l.ctrlKey === !0;
(l.button === 2 || s) && a.preventDefault();
}),
onFocusOutside: _(
e.onFocusOutside,
(a) => a.preventDefault()
)
}
);
}
), qe = i.forwardRef(
(e, t) => {
const n = y(R, e.__scopeDialog), o = i.useRef(!1), r = i.useRef(!1);
return /* @__PURE__ */ u(
ue,
{
...e,
ref: t,
trapFocus: !1,
disableOutsidePointerEvents: !1,
onCloseAutoFocus: (a) => {
e.onCloseAutoFocus?.(a), a.defaultPrevented || (o.current || n.triggerRef.current?.focus(), a.preventDefault()), o.current = !1, r.current = !1;
},
onInteractOutside: (a) => {
e.onInteractOutside?.(a), a.defaultPrevented || (o.current = !0, a.detail.originalEvent.type === "pointerdown" && (r.current = !0));
const l = a.target;
n.triggerRef.current?.contains(l) && a.preventDefault(), a.detail.originalEvent.type === "focusin" && r.current && a.preventDefault();
}
}
);
}
), ue = i.forwardRef(
(e, t) => {
const { __scopeDialog: n, trapFocus: o, onOpenAutoFocus: r, onCloseAutoFocus: a, ...l } = e, s = y(R, n), m = i.useRef(null), p = F(t, m);
return Te(), /* @__PURE__ */ j(B, { children: [
/* @__PURE__ */ u(
Q,
{
asChild: !0,
loop: !0,
trapped: o,
onMountAutoFocus: r,
onUnmountAutoFocus: a,
children: /* @__PURE__ */ u(
De,
{
role: "dialog",
id: s.contentId,
"aria-describedby": s.descriptionId,
"aria-labelledby": s.titleId,
"data-state": V(s.open),
...l,
ref: p,
onDismiss: () => s.onOpenChange(!1)
}
)
}
),
/* @__PURE__ */ j(B, { children: [
/* @__PURE__ */ u(Ze, { titleId: s.titleId }),
/* @__PURE__ */ u(ze, { contentRef: m, descriptionId: s.descriptionId })
] })
] });
}
), G = "DialogTitle", le = i.forwardRef(
(e, t) => {
const { __scopeDialog: n, ...o } = e, r = y(G, n);
return /* @__PURE__ */ u(O.h2, { id: r.titleId, ...o, ref: t });
}
);
le.displayName = G;
var fe = "DialogDescription", de = i.forwardRef(
(e, t) => {
const { __scopeDialog: n, ...o } = e, r = y(fe, n);
return /* @__PURE__ */ u(O.p, { id: r.descriptionId, ...o, ref: t });
}
);
de.displayName = fe;
var pe = "DialogClose", me = i.forwardRef(
(e, t) => {
const { __scopeDialog: n, ...o } = e, r = y(pe, n);
return /* @__PURE__ */ u(
O.button,
{
type: "button",
...o,
ref: t,
onClick: _(e.onClick, () => r.onOpenChange(!1))
}
);
}
);
me.displayName = pe;
function V(e) {
return e ? "open" : "closed";
}
var ve = "DialogTitleWarning", [it, ge] = he(ve, {
contentName: R,
titleName: G,
docsSlug: "dialog"
}), Ze = ({ titleId: e }) => {
const t = ge(ve), n = `\`${t.contentName}\` requires a \`${t.titleName}\` for the component to be accessible for screen reader users.
If you want to hide the \`${t.titleName}\`, you can wrap it with our VisuallyHidden component.
For more information, see https://radix-ui.com/primitives/docs/components/${t.docsSlug}`;
return i.useEffect(() => {
e && (document.getElementById(e) || console.error(n));
}, [n, e]), null;
}, Ye = "DialogDescriptionWarning", ze = ({ contentRef: e, descriptionId: t }) => {
const o = `Warning: Missing \`Description\` or \`aria-describedby={undefined}\` for {${ge(Ye).contentName}}.`;
return i.useEffect(() => {
const r = e.current?.getAttribute("aria-describedby");
t && r && (document.getElementById(t) || console.warn(o));
}, [o, e, t]), null;
}, ct = ne, ut = re, lt = se, ft = ie, dt = ce, pt = le, mt = de, vt = me;
export {
dt as C,
le as D,
ft as O,
lt as P,
ct as R,
ut as T,
it as W,
se as a,
vt as b,
st as c,
pt as d,
mt as e
};