@stanfordspezi/spezi-web-design-system
Version:
Stanford Biodesign Digital Health Spezi Web Design System
351 lines (350 loc) • 11.9 kB
JavaScript
import { jsx as d, jsxs as H } from "react/jsx-runtime";
import { c as V } from "./index-2NvaPZWc.mjs";
import * as s from "react";
import { c as Y, u as q, a as g } from "./index-CBSFK5oB.mjs";
import { u as N, a as X } from "./index-CB1Wkg0G.mjs";
import { D as K } from "./index-e9WAJWP9.mjs";
import { u as W } from "./index-D59fTEgK.mjs";
import { c as S, R as J, A as Q, C as Z, a as ee } from "./index-BiEE5p2P.mjs";
import { P as te } from "./index-dgd0uIiN.mjs";
import { P as oe } from "./index-8-FPuQXg.mjs";
import { R as re } from "./index-C0WUJIpG.mjs";
var [_, Ge] = Y("Tooltip", [
S
]), D = S(), G = "TooltipProvider", ne = 700, A = "tooltip.open", [se, k] = _(G), j = (t) => {
const {
__scopeTooltip: o,
delayDuration: e = ne,
skipDelayDuration: r = 300,
disableHoverableContent: n = !1,
children: l
} = t, i = s.useRef(!0), v = s.useRef(!1), a = s.useRef(0);
return s.useEffect(() => {
const p = a.current;
return () => window.clearTimeout(p);
}, []), /* @__PURE__ */ d(
se,
{
scope: o,
isOpenDelayedRef: i,
delayDuration: e,
onOpen: s.useCallback(() => {
window.clearTimeout(a.current), i.current = !1;
}, []),
onClose: s.useCallback(() => {
window.clearTimeout(a.current), a.current = window.setTimeout(
() => i.current = !0,
r
);
}, [r]),
isPointerInTransitRef: v,
onPointerInTransitChange: s.useCallback((p) => {
v.current = p;
}, []),
disableHoverableContent: n,
children: l
}
);
};
j.displayName = G;
var E = "Tooltip", [ae, O] = _(E), F = (t) => {
const {
__scopeTooltip: o,
children: e,
open: r,
defaultOpen: n,
onOpenChange: l,
disableHoverableContent: i,
delayDuration: v
} = t, a = k(E, t.__scopeTooltip), p = D(o), [c, f] = s.useState(null), m = W(), u = s.useRef(0), h = i ?? a.disableHoverableContent, y = v ?? a.delayDuration, T = s.useRef(!1), [x, C] = q({
prop: r,
defaultProp: n ?? !1,
onChange: (M) => {
M ? (a.onOpen(), document.dispatchEvent(new CustomEvent(A))) : a.onClose(), l == null || l(M);
},
caller: E
}), w = s.useMemo(() => x ? T.current ? "delayed-open" : "instant-open" : "closed", [x]), P = s.useCallback(() => {
window.clearTimeout(u.current), u.current = 0, T.current = !1, C(!0);
}, [C]), R = s.useCallback(() => {
window.clearTimeout(u.current), u.current = 0, C(!1);
}, [C]), I = s.useCallback(() => {
window.clearTimeout(u.current), u.current = window.setTimeout(() => {
T.current = !0, C(!0), u.current = 0;
}, y);
}, [y, C]);
return s.useEffect(() => () => {
u.current && (window.clearTimeout(u.current), u.current = 0);
}, []), /* @__PURE__ */ d(J, { ...p, children: /* @__PURE__ */ d(
ae,
{
scope: o,
contentId: m,
open: x,
stateAttribute: w,
trigger: c,
onTriggerChange: f,
onTriggerEnter: s.useCallback(() => {
a.isOpenDelayedRef.current ? I() : P();
}, [a.isOpenDelayedRef, I, P]),
onTriggerLeave: s.useCallback(() => {
h ? R() : (window.clearTimeout(u.current), u.current = 0);
}, [R, h]),
onOpen: P,
onClose: R,
disableHoverableContent: h,
children: e
}
) });
};
F.displayName = E;
var L = "TooltipTrigger", B = s.forwardRef(
(t, o) => {
const { __scopeTooltip: e, ...r } = t, n = O(L, e), l = k(L, e), i = D(e), v = s.useRef(null), a = N(o, v, n.onTriggerChange), p = s.useRef(!1), c = s.useRef(!1), f = s.useCallback(() => p.current = !1, []);
return s.useEffect(() => () => document.removeEventListener("pointerup", f), [f]), /* @__PURE__ */ d(Q, { asChild: !0, ...i, children: /* @__PURE__ */ d(
oe.button,
{
"aria-describedby": n.open ? n.contentId : void 0,
"data-state": n.stateAttribute,
...r,
ref: a,
onPointerMove: g(t.onPointerMove, (m) => {
m.pointerType !== "touch" && !c.current && !l.isPointerInTransitRef.current && (n.onTriggerEnter(), c.current = !0);
}),
onPointerLeave: g(t.onPointerLeave, () => {
n.onTriggerLeave(), c.current = !1;
}),
onPointerDown: g(t.onPointerDown, () => {
n.open && n.onClose(), p.current = !0, document.addEventListener("pointerup", f, { once: !0 });
}),
onFocus: g(t.onFocus, () => {
p.current || n.onOpen();
}),
onBlur: g(t.onBlur, n.onClose),
onClick: g(t.onClick, n.onClose)
}
) });
}
);
B.displayName = L;
var ie = "TooltipPortal", [je, le] = _(ie, {
forceMount: void 0
}), b = "TooltipContent", $ = s.forwardRef(
(t, o) => {
const e = le(b, t.__scopeTooltip), { forceMount: r = e.forceMount, side: n = "top", ...l } = t, i = O(b, t.__scopeTooltip);
return /* @__PURE__ */ d(te, { present: r || i.open, children: i.disableHoverableContent ? /* @__PURE__ */ d(z, { side: n, ...l, ref: o }) : /* @__PURE__ */ d(ce, { side: n, ...l, ref: o }) });
}
), ce = s.forwardRef((t, o) => {
const e = O(b, t.__scopeTooltip), r = k(b, t.__scopeTooltip), n = s.useRef(null), l = N(o, n), [i, v] = s.useState(null), { trigger: a, onClose: p } = e, c = n.current, { onPointerInTransitChange: f } = r, m = s.useCallback(() => {
v(null), f(!1);
}, [f]), u = s.useCallback(
(h, y) => {
const T = h.currentTarget, x = { x: h.clientX, y: h.clientY }, C = ve(x, T.getBoundingClientRect()), w = me(x, C), P = he(y.getBoundingClientRect()), R = ye([...w, ...P]);
v(R), f(!0);
},
[f]
);
return s.useEffect(() => () => m(), [m]), s.useEffect(() => {
if (a && c) {
const h = (T) => u(T, c), y = (T) => u(T, a);
return a.addEventListener("pointerleave", h), c.addEventListener("pointerleave", y), () => {
a.removeEventListener("pointerleave", h), c.removeEventListener("pointerleave", y);
};
}
}, [a, c, u, m]), s.useEffect(() => {
if (i) {
const h = (y) => {
const T = y.target, x = { x: y.clientX, y: y.clientY }, C = (a == null ? void 0 : a.contains(T)) || (c == null ? void 0 : c.contains(T)), w = !Te(x, i);
C ? m() : w && (m(), p());
};
return document.addEventListener("pointermove", h), () => document.removeEventListener("pointermove", h);
}
}, [a, c, i, p, m]), /* @__PURE__ */ d(z, { ...t, ref: l });
}), [ue, de] = _(E, { isInside: !1 }), pe = X("TooltipContent"), z = s.forwardRef(
(t, o) => {
const {
__scopeTooltip: e,
children: r,
"aria-label": n,
onEscapeKeyDown: l,
onPointerDownOutside: i,
...v
} = t, a = O(b, e), p = D(e), { onClose: c } = a;
return s.useEffect(() => (document.addEventListener(A, c), () => document.removeEventListener(A, c)), [c]), s.useEffect(() => {
if (a.trigger) {
const f = (m) => {
const u = m.target;
u != null && u.contains(a.trigger) && c();
};
return window.addEventListener("scroll", f, { capture: !0 }), () => window.removeEventListener("scroll", f, { capture: !0 });
}
}, [a.trigger, c]), /* @__PURE__ */ d(
K,
{
asChild: !0,
disableOutsidePointerEvents: !1,
onEscapeKeyDown: l,
onPointerDownOutside: i,
onFocusOutside: (f) => f.preventDefault(),
onDismiss: c,
children: /* @__PURE__ */ H(
Z,
{
"data-state": a.stateAttribute,
...p,
...v,
ref: o,
style: {
...v.style,
"--radix-tooltip-content-transform-origin": "var(--radix-popper-transform-origin)",
"--radix-tooltip-content-available-width": "var(--radix-popper-available-width)",
"--radix-tooltip-content-available-height": "var(--radix-popper-available-height)",
"--radix-tooltip-trigger-width": "var(--radix-popper-anchor-width)",
"--radix-tooltip-trigger-height": "var(--radix-popper-anchor-height)"
},
children: [
/* @__PURE__ */ d(pe, { children: r }),
/* @__PURE__ */ d(ue, { scope: e, isInside: !0, children: /* @__PURE__ */ d(re, { id: a.contentId, role: "tooltip", children: n || r }) })
]
}
)
}
);
}
);
$.displayName = b;
var U = "TooltipArrow", fe = s.forwardRef(
(t, o) => {
const { __scopeTooltip: e, ...r } = t, n = D(e);
return de(
U,
e
).isInside ? null : /* @__PURE__ */ d(ee, { ...n, ...r, ref: o });
}
);
fe.displayName = U;
function ve(t, o) {
const e = Math.abs(o.top - t.y), r = Math.abs(o.bottom - t.y), n = Math.abs(o.right - t.x), l = Math.abs(o.left - t.x);
switch (Math.min(e, r, n, l)) {
case l:
return "left";
case n:
return "right";
case e:
return "top";
case r:
return "bottom";
default:
throw new Error("unreachable");
}
}
function me(t, o, e = 5) {
const r = [];
switch (o) {
case "top":
r.push(
{ x: t.x - e, y: t.y + e },
{ x: t.x + e, y: t.y + e }
);
break;
case "bottom":
r.push(
{ x: t.x - e, y: t.y - e },
{ x: t.x + e, y: t.y - e }
);
break;
case "left":
r.push(
{ x: t.x + e, y: t.y - e },
{ x: t.x + e, y: t.y + e }
);
break;
case "right":
r.push(
{ x: t.x - e, y: t.y - e },
{ x: t.x - e, y: t.y + e }
);
break;
}
return r;
}
function he(t) {
const { top: o, right: e, bottom: r, left: n } = t;
return [
{ x: n, y: o },
{ x: e, y: o },
{ x: e, y: r },
{ x: n, y: r }
];
}
function Te(t, o) {
const { x: e, y: r } = t;
let n = !1;
for (let l = 0, i = o.length - 1; l < o.length; i = l++) {
const v = o[l], a = o[i], p = v.x, c = v.y, f = a.x, m = a.y;
c > r != m > r && e < (f - p) * (r - c) / (m - c) + p && (n = !n);
}
return n;
}
function ye(t) {
const o = t.slice();
return o.sort((e, r) => e.x < r.x ? -1 : e.x > r.x ? 1 : e.y < r.y ? -1 : e.y > r.y ? 1 : 0), Ce(o);
}
function Ce(t) {
if (t.length <= 1) return t.slice();
const o = [];
for (let r = 0; r < t.length; r++) {
const n = t[r];
for (; o.length >= 2; ) {
const l = o[o.length - 1], i = o[o.length - 2];
if ((l.x - i.x) * (n.y - i.y) >= (l.y - i.y) * (n.x - i.x)) o.pop();
else break;
}
o.push(n);
}
o.pop();
const e = [];
for (let r = t.length - 1; r >= 0; r--) {
const n = t[r];
for (; e.length >= 2; ) {
const l = e[e.length - 1], i = e[e.length - 2];
if ((l.x - i.x) * (n.y - i.y) >= (l.y - i.y) * (n.x - i.x)) e.pop();
else break;
}
e.push(n);
}
return e.pop(), o.length === 1 && e.length === 1 && o[0].x === e[0].x && o[0].y === e[0].y ? o : o.concat(e);
}
var xe = j, ge = F, be = B, we = $;
const Pe = xe, Re = ge, Ee = be, _e = ({
className: t,
sideOffset: o = 4,
...e
}) => /* @__PURE__ */ d(
we,
{
sideOffset: o,
className: V(
"animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 bg-popover text-foreground z-50 w-max overflow-hidden rounded-sm border px-2 py-0.5",
t
),
...e
}
), Fe = ({
children: t,
tooltip: o,
className: e,
sideOffset: r,
side: n,
delayDuration: l = 0,
...i
}) => /* @__PURE__ */ d(Pe, { children: /* @__PURE__ */ H(Re, { ...i, delayDuration: l, children: [
t && /* @__PURE__ */ d(Ee, { asChild: !0, children: t }),
/* @__PURE__ */ d(_e, { side: n, sideOffset: r, className: e, children: o })
] }) });
export {
Pe as T,
Re as a,
Ee as b,
_e as c,
Fe as d
};