UNPKG

@stanfordspezi/spezi-web-design-system

Version:

Stanford Biodesign Digital Health Spezi Web Design System

196 lines (195 loc) 6.52 kB
import * as i from "react"; import { c as y, u as M } from "./index-hURUNS5V.mjs"; import * as U from "react-dom"; import $ from "react-dom"; import { c as g, u as S } from "./index-D2LZVjSn.mjs"; import { jsx as E } from "react/jsx-runtime"; import { u as b } from "./index-0ioNhtNM.mjs"; var z = [ "a", "button", "div", "form", "h2", "h3", "img", "input", "label", "li", "nav", "ol", "p", "select", "span", "svg", "ul" ], N = z.reduce((t, e) => { const n = g(`Primitive.${e}`), s = i.forwardRef((r, o) => { const { asChild: c, ...d } = r, f = c ? n : e; return typeof window < "u" && (window[/* @__PURE__ */ Symbol.for("radix-ui")] = !0), /* @__PURE__ */ E(f, { ...d, ref: o }); }); return s.displayName = `Primitive.${e}`, { ...t, [e]: s }; }, {}); function H(t, e) { t && U.flushSync(() => t.dispatchEvent(e)); } function K(t, e = globalThis?.document) { const n = b(t); i.useEffect(() => { const s = (r) => { r.key === "Escape" && n(r); }; return e.addEventListener("keydown", s, { capture: !0 }), () => e.removeEventListener("keydown", s, { capture: !0 }); }, [n, e]); } var j = "DismissableLayer", h = "dismissableLayer.update", X = "dismissableLayer.pointerDownOutside", Y = "dismissableLayer.focusOutside", L, T = i.createContext({ layers: /* @__PURE__ */ new Set(), layersWithOutsidePointerEventsDisabled: /* @__PURE__ */ new Set(), branches: /* @__PURE__ */ new Set() }), q = i.forwardRef( (t, e) => { const { disableOutsidePointerEvents: n = !1, onEscapeKeyDown: s, onPointerDownOutside: r, onFocusOutside: o, onInteractOutside: c, onDismiss: d, ...f } = t, u = i.useContext(T), [l, B] = i.useState(null), v = l?.ownerDocument ?? globalThis?.document, [, F] = i.useState({}), I = S(e, (a) => B(a)), P = Array.from(u.layers), [A] = [...u.layersWithOutsidePointerEventsDisabled].slice(-1), W = P.indexOf(A), D = l ? P.indexOf(l) : -1, _ = u.layersWithOutsidePointerEventsDisabled.size > 0, O = D >= W, k = Q((a) => { const p = a.target, C = [...u.branches].some((m) => m.contains(p)); !O || C || (r?.(a), c?.(a), a.defaultPrevented || d?.()); }, v), w = V((a) => { const p = a.target; [...u.branches].some((m) => m.contains(p)) || (o?.(a), c?.(a), a.defaultPrevented || d?.()); }, v); return K((a) => { D === u.layers.size - 1 && (s?.(a), !a.defaultPrevented && d && (a.preventDefault(), d())); }, v), i.useEffect(() => { if (l) return n && (u.layersWithOutsidePointerEventsDisabled.size === 0 && (L = v.body.style.pointerEvents, v.body.style.pointerEvents = "none"), u.layersWithOutsidePointerEventsDisabled.add(l)), u.layers.add(l), R(), () => { n && u.layersWithOutsidePointerEventsDisabled.size === 1 && (v.body.style.pointerEvents = L); }; }, [l, v, n, u]), i.useEffect(() => () => { l && (u.layers.delete(l), u.layersWithOutsidePointerEventsDisabled.delete(l), R()); }, [l, u]), i.useEffect(() => { const a = () => F({}); return document.addEventListener(h, a), () => document.removeEventListener(h, a); }, []), /* @__PURE__ */ E( N.div, { ...f, ref: I, style: { pointerEvents: _ ? O ? "auto" : "none" : void 0, ...t.style }, onFocusCapture: y(t.onFocusCapture, w.onFocusCapture), onBlurCapture: y(t.onBlurCapture, w.onBlurCapture), onPointerDownCapture: y( t.onPointerDownCapture, k.onPointerDownCapture ) } ); } ); q.displayName = j; var G = "DismissableLayerBranch", J = i.forwardRef((t, e) => { const n = i.useContext(T), s = i.useRef(null), r = S(e, s); return i.useEffect(() => { const o = s.current; if (o) return n.branches.add(o), () => { n.branches.delete(o); }; }, [n.branches]), /* @__PURE__ */ E(N.div, { ...t, ref: r }); }); J.displayName = G; function Q(t, e = globalThis?.document) { const n = b(t), s = i.useRef(!1), r = i.useRef(() => { }); return i.useEffect(() => { const o = (d) => { if (d.target && !s.current) { let f = function() { x( X, n, u, { discrete: !0 } ); }; const u = { originalEvent: d }; d.pointerType === "touch" ? (e.removeEventListener("click", r.current), r.current = f, e.addEventListener("click", r.current, { once: !0 })) : f(); } else e.removeEventListener("click", r.current); s.current = !1; }, c = window.setTimeout(() => { e.addEventListener("pointerdown", o); }, 0); return () => { window.clearTimeout(c), e.removeEventListener("pointerdown", o), e.removeEventListener("click", r.current); }; }, [e, n]), { // ensures we check React component tree (not just DOM tree) onPointerDownCapture: () => s.current = !0 }; } function V(t, e = globalThis?.document) { const n = b(t), s = i.useRef(!1); return i.useEffect(() => { const r = (o) => { o.target && !s.current && x(Y, n, { originalEvent: o }, { discrete: !1 }); }; return e.addEventListener("focusin", r), () => e.removeEventListener("focusin", r); }, [e, n]), { onFocusCapture: () => s.current = !0, onBlurCapture: () => s.current = !1 }; } function R() { const t = new CustomEvent(h); document.dispatchEvent(t); } function x(t, e, n, { discrete: s }) { const r = n.originalEvent.target, o = new CustomEvent(t, { bubbles: !1, cancelable: !0, detail: n }); e && r.addEventListener(t, e, { once: !0 }), s ? H(r, o) : r.dispatchEvent(o); } var Z = [ "a", "button", "div", "form", "h2", "h3", "img", "input", "label", "li", "nav", "ol", "p", "select", "span", "svg", "ul" ], ee = Z.reduce((t, e) => { const n = g(`Primitive.${e}`), s = i.forwardRef((r, o) => { const { asChild: c, ...d } = r, f = c ? n : e; return typeof window < "u" && (window[/* @__PURE__ */ Symbol.for("radix-ui")] = !0), /* @__PURE__ */ E(f, { ...d, ref: o }); }); return s.displayName = `Primitive.${e}`, { ...t, [e]: s }; }, {}), te = "Portal", ne = i.forwardRef((t, e) => { const { container: n, ...s } = t, [r, o] = i.useState(!1); M(() => o(!0), []); const c = n || r && globalThis?.document?.body; return c ? $.createPortal(/* @__PURE__ */ E(ee.div, { ...s, ref: e }), c) : null; }); ne.displayName = te; export { q as D, ne as P };