UNPKG

@konstructio/ui

Version:

A set of reusable and customizable React components built for konstruct.io

201 lines (200 loc) 5.9 kB
import * as r from "react"; import { jsx as m } from "react/jsx-runtime"; import * as y from "react-dom"; import { c as E } from "./index-DLcqcWxM.js"; function M(e, t) { const n = r.createContext(t), s = (i) => { const { children: u, ...c } = i, l = r.useMemo(() => c, Object.values(c)); return /* @__PURE__ */ m(n.Provider, { value: l, children: u }); }; s.displayName = e + "Provider"; function o(i) { const u = r.useContext(n); if (u) return u; if (t !== void 0) return t; throw new Error(`\`${i}\` must be used within \`${e}\``); } return [s, o]; } function W(e, t = []) { let n = []; function s(i, u) { const c = r.createContext(u), l = n.length; n = [...n, u]; const f = (a) => { const { scope: v, children: C, ...p } = a, h = v?.[e]?.[l] || c, S = r.useMemo(() => p, Object.values(p)); return /* @__PURE__ */ m(h.Provider, { value: S, children: C }); }; f.displayName = i + "Provider"; function d(a, v) { const C = v?.[e]?.[l] || c, p = r.useContext(C); if (p) return p; if (u !== void 0) return u; throw new Error(`\`${a}\` must be used within \`${i}\``); } return [f, d]; } const o = () => { const i = n.map((u) => r.createContext(u)); return function(c) { const l = c?.[e] || i; return r.useMemo( () => ({ [`__scope${e}`]: { ...c, [e]: l } }), [c, l] ); }; }; return o.scopeName = e, [s, x(o, ...t)]; } function x(...e) { const t = e[0]; if (e.length === 1) return t; const n = () => { const s = e.map((o) => ({ useScope: o(), scopeName: o.scopeName })); return function(i) { const u = s.reduce((c, { useScope: l, scopeName: f }) => { const a = l(i)[`__scope${f}`]; return { ...c, ...a }; }, {}); return r.useMemo(() => ({ [`__scope${t.scopeName}`]: u }), [u]); }; }; return n.scopeName = t.scopeName, n; } function A(e, t, { checkForDefaultPrevented: n = !0 } = {}) { return function(o) { if (e?.(o), n === !1 || !o.defaultPrevented) return t?.(o); }; } var P = globalThis?.document ? r.useLayoutEffect : () => { }, b = r[" useInsertionEffect ".trim().toString()] || P; function H({ prop: e, defaultProp: t, onChange: n = () => { }, caller: s }) { const [o, i, u] = g({ defaultProp: t, onChange: n }), c = e !== void 0, l = c ? e : o; { const d = r.useRef(e !== void 0); r.useEffect(() => { const a = d.current; a !== c && console.warn( `${s} is changing from ${a ? "controlled" : "uncontrolled"} to ${c ? "controlled" : "uncontrolled"}. Components should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled value for the lifetime of the component.` ), d.current = c; }, [c, s]); } const f = r.useCallback( (d) => { if (c) { const a = w(d) ? d(e) : d; a !== e && u.current?.(a); } else i(d); }, [c, e, i, u] ); return [l, f]; } function g({ defaultProp: e, onChange: t }) { const [n, s] = r.useState(e), o = r.useRef(n), i = r.useRef(t); return b(() => { i.current = t; }, [t]), r.useEffect(() => { o.current !== n && (i.current?.(n), o.current = n); }, [n, o]), [n, s, i]; } function w(e) { return typeof e == "function"; } // @__NO_SIDE_EFFECTS__ function R(e) { const t = /* @__PURE__ */ $(e), n = r.forwardRef((s, o) => { const { children: i, ...u } = s, c = r.Children.toArray(i), l = c.find(O); if (l) { const f = l.props.children, d = c.map((a) => a === l ? r.Children.count(f) > 1 ? r.Children.only(null) : r.isValidElement(f) ? f.props.children : null : a); return /* @__PURE__ */ m(t, { ...u, ref: o, children: r.isValidElement(f) ? r.cloneElement(f, void 0, d) : null }); } return /* @__PURE__ */ m(t, { ...u, ref: o, children: i }); }); return n.displayName = `${e}.Slot`, n; } // @__NO_SIDE_EFFECTS__ function $(e) { const t = r.forwardRef((n, s) => { const { children: o, ...i } = n; if (r.isValidElement(o)) { const u = D(o), c = N(i, o.props); return o.type !== r.Fragment && (c.ref = s ? E(s, u) : u), r.cloneElement(o, c); } return r.Children.count(o) > 1 ? r.Children.only(null) : null; }); return t.displayName = `${e}.SlotClone`, t; } var _ = /* @__PURE__ */ Symbol("radix.slottable"); function O(e) { return r.isValidElement(e) && typeof e.type == "function" && "__radixId" in e.type && e.type.__radixId === _; } function N(e, t) { const n = { ...t }; for (const s in t) { const o = e[s], i = t[s]; /^on[A-Z]/.test(s) ? o && i ? n[s] = (...c) => { const l = i(...c); return o(...c), l; } : o && (n[s] = o) : s === "style" ? n[s] = { ...o, ...i } : s === "className" && (n[s] = [o, i].filter(Boolean).join(" ")); } return { ...e, ...n }; } function D(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 I = [ "a", "button", "div", "form", "h2", "h3", "img", "input", "label", "li", "nav", "ol", "p", "select", "span", "svg", "ul" ], L = I.reduce((e, t) => { const n = /* @__PURE__ */ R(`Primitive.${t}`), s = r.forwardRef((o, i) => { const { asChild: u, ...c } = o, l = u ? n : t; return typeof window < "u" && (window[/* @__PURE__ */ Symbol.for("radix-ui")] = !0), /* @__PURE__ */ m(l, { ...c, ref: i }); }); return s.displayName = `Primitive.${t}`, { ...e, [t]: s }; }, {}); function T(e, t) { e && y.flushSync(() => e.dispatchEvent(t)); } export { L as P, H as a, A as b, W as c, T as d, M as e, P as u };