@konstructio/ui
Version:
A set of reusable and customizable React components built for konstruct.io
119 lines (118 loc) • 3.15 kB
JavaScript
import * as u from "react";
import { jsx as v } from "react/jsx-runtime";
function _(t, e) {
const n = u.createContext(e), s = (c) => {
const { children: r, ...i } = c, f = u.useMemo(() => i, Object.values(i));
return /* @__PURE__ */ v(n.Provider, { value: f, children: r });
};
s.displayName = t + "Provider";
function o(c) {
const r = u.useContext(n);
if (r) return r;
if (e !== void 0) return e;
throw new Error(`\`${c}\` must be used within \`${t}\``);
}
return [s, o];
}
function y(t, e = []) {
let n = [];
function s(c, r) {
const i = u.createContext(r), f = n.length;
n = [...n, r];
const a = (l) => {
var S;
const { scope: d, children: h, ...x } = l, m = ((S = d == null ? void 0 : d[t]) == null ? void 0 : S[f]) || i, p = u.useMemo(() => x, Object.values(x));
return /* @__PURE__ */ v(m.Provider, { value: p, children: h });
};
a.displayName = c + "Provider";
function C(l, d) {
var m;
const h = ((m = d == null ? void 0 : d[t]) == null ? void 0 : m[f]) || i, x = u.useContext(h);
if (x) return x;
if (r !== void 0) return r;
throw new Error(`\`${l}\` must be used within \`${c}\``);
}
return [a, C];
}
const o = () => {
const c = n.map((r) => u.createContext(r));
return function(i) {
const f = (i == null ? void 0 : i[t]) || c;
return u.useMemo(
() => ({ [`__scope${t}`]: { ...i, [t]: f } }),
[i, f]
);
};
};
return o.scopeName = t, [s, b(o, ...e)];
}
function b(...t) {
const e = t[0];
if (t.length === 1) return e;
const n = () => {
const s = t.map((o) => ({
useScope: o(),
scopeName: o.scopeName
}));
return function(c) {
const r = s.reduce((i, { useScope: f, scopeName: a }) => {
const l = f(c)[`__scope${a}`];
return { ...i, ...l };
}, {});
return u.useMemo(() => ({ [`__scope${e.scopeName}`]: r }), [r]);
};
};
return n.scopeName = e.scopeName, n;
}
function M(t, e, { checkForDefaultPrevented: n = !0 } = {}) {
return function(o) {
if (t == null || t(o), n === !1 || !o.defaultPrevented)
return e == null ? void 0 : e(o);
};
}
var R = globalThis != null && globalThis.document ? u.useLayoutEffect : () => {
};
function P(t) {
const e = u.useRef(t);
return u.useEffect(() => {
e.current = t;
}), u.useMemo(() => (...n) => {
var s;
return (s = e.current) == null ? void 0 : s.call(e, ...n);
}, []);
}
function j({
prop: t,
defaultProp: e,
onChange: n = () => {
}
}) {
const [s, o] = $({ defaultProp: e, onChange: n }), c = t !== void 0, r = c ? t : s, i = P(n), f = u.useCallback(
(a) => {
if (c) {
const l = typeof a == "function" ? a(t) : a;
l !== t && i(l);
} else
o(a);
},
[c, t, o, i]
);
return [r, f];
}
function $({
defaultProp: t,
onChange: e
}) {
const n = u.useState(t), [s] = n, o = u.useRef(s), c = P(e);
return u.useEffect(() => {
o.current !== s && (c(s), o.current = s);
}, [s, o, c]), n;
}
export {
j as a,
M as b,
y as c,
P as d,
_ as e,
R as u
};