@stanfordspezi/spezi-web-design-system
Version:
Stanford Biodesign Digital Health Spezi Web Design System
350 lines (349 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, a as q, d as g } from "./index-DqbtYIhp.mjs";
import { u as N, a as X } from "./index-Bytw4Lqn.mjs";
import { D as K } from "./index-C9uNU7u7.mjs";
import { u as W } from "./index-BR4tIz6o.mjs";
import { c as S, R as J, A as Q, C as Z, a as ee } from "./index-D3lfN3hi.mjs";
import { P as te } from "./index-ruMUvQgL.mjs";
import { P as oe } from "./index-WWNfSPCj.mjs";
import { R as re } from "./index-D6Zqau5G.mjs";
var [E, Ge] = Y("Tooltip", [
S
]), _ = S(), G = "TooltipProvider", ne = 700, A = "tooltip.open", [se, k] = E(G), F = (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
}
);
};
F.displayName = G;
var D = "Tooltip", [ae, O] = E(D), j = (t) => {
const {
__scopeTooltip: o,
children: e,
open: r,
defaultOpen: n = !1,
onOpenChange: l,
disableHoverableContent: i,
delayDuration: v
} = t, a = k(D, t.__scopeTooltip), p = _(o), [c, f] = s.useState(null), h = W(), u = s.useRef(0), m = i ?? a.disableHoverableContent, C = v ?? a.delayDuration, T = s.useRef(!1), [y = !1, x] = q({
prop: r,
defaultProp: n,
onChange: (M) => {
M ? (a.onOpen(), document.dispatchEvent(new CustomEvent(A))) : a.onClose(), l == null || l(M);
}
}), w = s.useMemo(() => y ? T.current ? "delayed-open" : "instant-open" : "closed", [y]), P = s.useCallback(() => {
window.clearTimeout(u.current), u.current = 0, T.current = !1, x(!0);
}, [x]), R = s.useCallback(() => {
window.clearTimeout(u.current), u.current = 0, x(!1);
}, [x]), I = s.useCallback(() => {
window.clearTimeout(u.current), u.current = window.setTimeout(() => {
T.current = !0, x(!0), u.current = 0;
}, C);
}, [C, x]);
return s.useEffect(() => () => {
u.current && (window.clearTimeout(u.current), u.current = 0);
}, []), /* @__PURE__ */ d(J, { ...p, children: /* @__PURE__ */ d(
ae,
{
scope: o,
contentId: h,
open: y,
stateAttribute: w,
trigger: c,
onTriggerChange: f,
onTriggerEnter: s.useCallback(() => {
a.isOpenDelayedRef.current ? I() : P();
}, [a.isOpenDelayedRef, I, P]),
onTriggerLeave: s.useCallback(() => {
m ? R() : (window.clearTimeout(u.current), u.current = 0);
}, [R, m]),
onOpen: P,
onClose: R,
disableHoverableContent: m,
children: e
}
) });
};
j.displayName = D;
var L = "TooltipTrigger", B = s.forwardRef(
(t, o) => {
const { __scopeTooltip: e, ...r } = t, n = O(L, e), l = k(L, e), i = _(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, (h) => {
h.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", [Fe, le] = E(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, h = s.useCallback(() => {
v(null), f(!1);
}, [f]), u = s.useCallback(
(m, C) => {
const T = m.currentTarget, y = { x: m.clientX, y: m.clientY }, x = ve(y, T.getBoundingClientRect()), w = me(y, x), P = he(C.getBoundingClientRect()), R = Ce([...w, ...P]);
v(R), f(!0);
},
[f]
);
return s.useEffect(() => () => h(), [h]), s.useEffect(() => {
if (a && c) {
const m = (T) => u(T, c), C = (T) => u(T, a);
return a.addEventListener("pointerleave", m), c.addEventListener("pointerleave", C), () => {
a.removeEventListener("pointerleave", m), c.removeEventListener("pointerleave", C);
};
}
}, [a, c, u, h]), s.useEffect(() => {
if (i) {
const m = (C) => {
const T = C.target, y = { x: C.clientX, y: C.clientY }, x = (a == null ? void 0 : a.contains(T)) || (c == null ? void 0 : c.contains(T)), w = !Te(y, i);
x ? h() : w && (h(), p());
};
return document.addEventListener("pointermove", m), () => document.removeEventListener("pointermove", m);
}
}, [a, c, i, p, h]), /* @__PURE__ */ d(z, { ...t, ref: l });
}), [ue, de] = E(D, { 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 = _(e), { onClose: c } = a;
return s.useEffect(() => (document.addEventListener(A, c), () => document.removeEventListener(A, c)), [c]), s.useEffect(() => {
if (a.trigger) {
const f = (h) => {
const u = h.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 = _(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].x, a = o[l].y, p = o[i].x, c = o[i].y;
a > r != c > r && e < (p - v) * (r - a) / (c - a) + v && (n = !n);
}
return n;
}
function Ce(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), xe(o);
}
function xe(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 ye = F, ge = j, be = B, we = $;
const Pe = ye, 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
}
), je = ({
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,
je as d
};