UNPKG

@stanfordspezi/spezi-web-design-system

Version:

Stanford Biodesign Digital Health Spezi Web Design System

130 lines (129 loc) 3.63 kB
import * as u from "react"; import { jsx as h } from "react/jsx-runtime"; function M(t, r) { const o = u.createContext(r), i = (c) => { const { children: e, ...n } = c, f = u.useMemo(() => n, Object.values(n)); return /* @__PURE__ */ h(o.Provider, { value: f, children: e }); }; i.displayName = t + "Provider"; function s(c) { const e = u.useContext(o); if (e) return e; if (r !== void 0) return r; throw new Error(`\`${c}\` must be used within \`${t}\``); } return [i, s]; } function R(t, r = []) { let o = []; function i(c, e) { const n = u.createContext(e), f = o.length; o = [...o, e]; const m = (l) => { var S; const { scope: a, children: v, ...x } = l, C = ((S = a == null ? void 0 : a[t]) == null ? void 0 : S[f]) || n, P = u.useMemo(() => x, Object.values(x)); return /* @__PURE__ */ h(C.Provider, { value: P, children: v }); }; m.displayName = c + "Provider"; function d(l, a) { var C; const v = ((C = a == null ? void 0 : a[t]) == null ? void 0 : C[f]) || n, x = u.useContext(v); if (x) return x; if (e !== void 0) return e; throw new Error(`\`${l}\` must be used within \`${c}\``); } return [m, d]; } const s = () => { const c = o.map((e) => u.createContext(e)); return function(n) { const f = (n == null ? void 0 : n[t]) || c; return u.useMemo( () => ({ [`__scope${t}`]: { ...n, [t]: f } }), [n, f] ); }; }; return s.scopeName = t, [i, b(s, ...r)]; } function b(...t) { const r = t[0]; if (t.length === 1) return r; const o = () => { const i = t.map((s) => ({ useScope: s(), scopeName: s.scopeName })); return function(c) { const e = i.reduce((n, { useScope: f, scopeName: m }) => { const l = f(c)[`__scope${m}`]; return { ...n, ...l }; }, {}); return u.useMemo(() => ({ [`__scope${r.scopeName}`]: e }), [e]); }; }; return o.scopeName = r.scopeName, o; } function j(t, r, { checkForDefaultPrevented: o = !0 } = {}) { return function(s) { if (t == null || t(s), o === !1 || !s.defaultPrevented) return r == null ? void 0 : r(s); }; } var p = globalThis != null && globalThis.document ? u.useLayoutEffect : () => { }, w = u[" useInsertionEffect ".trim().toString()] || p; function k({ prop: t, defaultProp: r, onChange: o = () => { }, caller: i }) { const [s, c, e] = $({ defaultProp: r, onChange: o }), n = t !== void 0, f = n ? t : s; { const d = u.useRef(t !== void 0); u.useEffect(() => { const l = d.current; l !== n && console.warn( `${i} is changing from ${l ? "controlled" : "uncontrolled"} to ${n ? "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 = n; }, [n, i]); } const m = u.useCallback( (d) => { var l; if (n) { const a = _(d) ? d(t) : d; a !== t && ((l = e.current) == null || l.call(e, a)); } else c(d); }, [n, t, c, e] ); return [f, m]; } function $({ defaultProp: t, onChange: r }) { const [o, i] = u.useState(t), s = u.useRef(o), c = u.useRef(r); return w(() => { c.current = r; }, [r]), u.useEffect(() => { var e; s.current !== o && ((e = c.current) == null || e.call(c, o), s.current = o); }, [o, s]), [o, i, c]; } function _(t) { return typeof t == "function"; } export { j as a, M as b, R as c, p as d, k as u };