UNPKG

dgz-ui-shared

Version:

Custom ui library using React.js, Shadcn/ui, TailwindCSS, Typescript, dgz-ui library

342 lines (341 loc) 11.9 kB
import { j as k } from "../../jsx-runtime-DS1N_tNq.js"; import { j as $, g as q, y as J, a as f, i as H, h as Q, b as w, R as V, M as W, e as Z } from "../../index-BL59b1Bz-C9bJE_xn.js"; import * as i from "react"; import { o as A, i as ee, r as te, s as oe, c as ne } from "../../index-ZsCkaYy6-Cw9HUeiu.js"; import { n as re } from "../../index-CbK6da4c-d_6FdYZH.js"; import { m as ie } from "../../utils-B6fNqzRf-B1_jG1K7.js"; var [_, Me] = $("Tooltip", [ A ]), P = A(), B = "TooltipProvider", se = 700, L = "tooltip.open", [ae, M] = _(B), z = (t) => { const { __scopeTooltip: o, delayDuration: e = se, skipDelayDuration: n = 300, disableHoverableContent: r = !1, children: l } = t, a = i.useRef(!0), x = i.useRef(!1), s = i.useRef(0); return i.useEffect(() => { const d = s.current; return () => window.clearTimeout(d); }, []), /* @__PURE__ */ f.jsx( ae, { scope: o, isOpenDelayedRef: a, delayDuration: e, onOpen: i.useCallback(() => { window.clearTimeout(s.current), a.current = !1; }, []), onClose: i.useCallback(() => { window.clearTimeout(s.current), s.current = window.setTimeout( () => a.current = !0, n ); }, [n]), isPointerInTransitRef: x, onPointerInTransitChange: i.useCallback((d) => { x.current = d; }, []), disableHoverableContent: r, children: l } ); }; z.displayName = B; var R = "Tooltip", [le, D] = _(R), F = (t) => { const { __scopeTooltip: o, children: e, open: n, defaultOpen: r, onOpenChange: l, disableHoverableContent: a, delayDuration: x } = t, s = M(R, t.__scopeTooltip), d = P(o), [c, p] = i.useState(null), h = q(), u = i.useRef(0), m = a ?? s.disableHoverableContent, v = x ?? s.delayDuration, g = i.useRef(!1), [b, y] = J({ prop: n, defaultProp: r ?? !1, onChange: (I) => { I ? (s.onOpen(), document.dispatchEvent(new CustomEvent(L))) : s.onClose(), l == null || l(I); }, caller: R }), C = i.useMemo(() => b ? g.current ? "delayed-open" : "instant-open" : "closed", [b]), E = i.useCallback(() => { window.clearTimeout(u.current), u.current = 0, g.current = !1, y(!0); }, [y]), j = i.useCallback(() => { window.clearTimeout(u.current), u.current = 0, y(!1); }, [y]), N = i.useCallback(() => { window.clearTimeout(u.current), u.current = window.setTimeout(() => { g.current = !0, y(!0), u.current = 0; }, v); }, [v, y]); return i.useEffect(() => () => { u.current && (window.clearTimeout(u.current), u.current = 0); }, []), /* @__PURE__ */ f.jsx(ee, { ...d, children: /* @__PURE__ */ f.jsx( le, { scope: o, contentId: h, open: b, stateAttribute: C, trigger: c, onTriggerChange: p, onTriggerEnter: i.useCallback(() => { s.isOpenDelayedRef.current ? N() : E(); }, [s.isOpenDelayedRef, N, E]), onTriggerLeave: i.useCallback(() => { m ? j() : (window.clearTimeout(u.current), u.current = 0); }, [j, m]), onOpen: E, onClose: j, disableHoverableContent: m, children: e } ) }); }; F.displayName = R; var O = "TooltipTrigger", S = i.forwardRef( (t, o) => { const { __scopeTooltip: e, ...n } = t, r = D(O, e), l = M(O, e), a = P(e), x = i.useRef(null), s = H(o, x, r.onTriggerChange), d = i.useRef(!1), c = i.useRef(!1), p = i.useCallback(() => d.current = !1, []); return i.useEffect(() => () => document.removeEventListener("pointerup", p), [p]), /* @__PURE__ */ f.jsx(te, { asChild: !0, ...a, children: /* @__PURE__ */ f.jsx( Q.button, { "aria-describedby": r.open ? r.contentId : void 0, "data-state": r.stateAttribute, ...n, ref: s, onPointerMove: w(t.onPointerMove, (h) => { h.pointerType !== "touch" && !c.current && !l.isPointerInTransitRef.current && (r.onTriggerEnter(), c.current = !0); }), onPointerLeave: w(t.onPointerLeave, () => { r.onTriggerLeave(), c.current = !1; }), onPointerDown: w(t.onPointerDown, () => { r.open && r.onClose(), d.current = !0, document.addEventListener("pointerup", p, { once: !0 }); }), onFocus: w(t.onFocus, () => { d.current || r.onOpen(); }), onBlur: w(t.onBlur, r.onClose), onClick: w(t.onClick, r.onClose) } ) }); } ); S.displayName = O; var ce = "TooltipPortal", [Ne, ue] = _(ce, { forceMount: void 0 }), T = "TooltipContent", G = i.forwardRef( (t, o) => { const e = ue(T, t.__scopeTooltip), { forceMount: n = e.forceMount, side: r = "top", ...l } = t, a = D(T, t.__scopeTooltip); return /* @__PURE__ */ f.jsx(V, { present: n || a.open, children: a.disableHoverableContent ? /* @__PURE__ */ f.jsx(K, { side: r, ...l, ref: o }) : /* @__PURE__ */ f.jsx(de, { side: r, ...l, ref: o }) }); } ), de = i.forwardRef((t, o) => { const e = D(T, t.__scopeTooltip), n = M(T, t.__scopeTooltip), r = i.useRef(null), l = H(o, r), [a, x] = i.useState(null), { trigger: s, onClose: d } = e, c = r.current, { onPointerInTransitChange: p } = n, h = i.useCallback(() => { x(null), p(!1); }, [p]), u = i.useCallback( (m, v) => { const g = m.currentTarget, b = { x: m.clientX, y: m.clientY }, y = me(b, g.getBoundingClientRect()), C = ge(b, y), E = ve(v.getBoundingClientRect()), j = be([...C, ...E]); x(j), p(!0); }, [p] ); return i.useEffect(() => () => h(), [h]), i.useEffect(() => { if (s && c) { const m = (g) => u(g, c), v = (g) => u(g, s); return s.addEventListener("pointerleave", m), c.addEventListener("pointerleave", v), () => { s.removeEventListener("pointerleave", m), c.removeEventListener("pointerleave", v); }; } }, [s, c, u, h]), i.useEffect(() => { if (a) { const m = (v) => { const g = v.target, b = { x: v.clientX, y: v.clientY }, y = (s == null ? void 0 : s.contains(g)) || (c == null ? void 0 : c.contains(g)), C = !ye(b, a); y ? h() : C && (h(), d()); }; return document.addEventListener("pointermove", m), () => document.removeEventListener("pointermove", m); } }, [s, c, a, d, h]), /* @__PURE__ */ f.jsx(K, { ...t, ref: l }); }), [pe, fe] = _(R, { isInside: !1 }), xe = Z("TooltipContent"), K = i.forwardRef( (t, o) => { const { __scopeTooltip: e, children: n, "aria-label": r, onEscapeKeyDown: l, onPointerDownOutside: a, ...x } = t, s = D(T, e), d = P(e), { onClose: c } = s; return i.useEffect(() => (document.addEventListener(L, c), () => document.removeEventListener(L, c)), [c]), i.useEffect(() => { if (s.trigger) { const p = (h) => { const u = h.target; u != null && u.contains(s.trigger) && c(); }; return window.addEventListener("scroll", p, { capture: !0 }), () => window.removeEventListener("scroll", p, { capture: !0 }); } }, [s.trigger, c]), /* @__PURE__ */ f.jsx( W, { asChild: !0, disableOutsidePointerEvents: !1, onEscapeKeyDown: l, onPointerDownOutside: a, onFocusOutside: (p) => p.preventDefault(), onDismiss: c, children: /* @__PURE__ */ f.jsxs( oe, { "data-state": s.stateAttribute, ...d, ...x, ref: o, style: { ...x.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__ */ f.jsx(xe, { children: n }), /* @__PURE__ */ f.jsx(pe, { scope: e, isInside: !0, children: /* @__PURE__ */ f.jsx(re, { id: s.contentId, role: "tooltip", children: r || n }) }) ] } ) } ); } ); G.displayName = T; var X = "TooltipArrow", he = i.forwardRef( (t, o) => { const { __scopeTooltip: e, ...n } = t, r = P(e); return fe( X, e ).isInside ? null : /* @__PURE__ */ f.jsx(ne, { ...r, ...n, ref: o }); } ); he.displayName = X; function me(t, o) { const e = Math.abs(o.top - t.y), n = Math.abs(o.bottom - t.y), r = Math.abs(o.right - t.x), l = Math.abs(o.left - t.x); switch (Math.min(e, n, r, l)) { case l: return "left"; case r: return "right"; case e: return "top"; case n: return "bottom"; default: throw new Error("unreachable"); } } function ge(t, o, e = 5) { const n = []; switch (o) { case "top": n.push( { x: t.x - e, y: t.y + e }, { x: t.x + e, y: t.y + e } ); break; case "bottom": n.push( { x: t.x - e, y: t.y - e }, { x: t.x + e, y: t.y - e } ); break; case "left": n.push( { x: t.x + e, y: t.y - e }, { x: t.x + e, y: t.y + e } ); break; case "right": n.push( { x: t.x - e, y: t.y - e }, { x: t.x - e, y: t.y + e } ); break; } return n; } function ve(t) { const { top: o, right: e, bottom: n, left: r } = t; return [ { x: r, y: o }, { x: e, y: o }, { x: e, y: n }, { x: r, y: n } ]; } function ye(t, o) { const { x: e, y: n } = t; let r = !1; for (let l = 0, a = o.length - 1; l < o.length; a = l++) { const x = o[l], s = o[a], d = x.x, c = x.y, p = s.x, h = s.y; c > n != h > n && e < (p - d) * (n - c) / (h - c) + d && (r = !r); } return r; } function be(t) { const o = t.slice(); return o.sort((e, n) => e.x < n.x ? -1 : e.x > n.x ? 1 : e.y < n.y ? -1 : e.y > n.y ? 1 : 0), we(o); } function we(t) { if (t.length <= 1) return t.slice(); const o = []; for (let n = 0; n < t.length; n++) { const r = t[n]; for (; o.length >= 2; ) { const l = o[o.length - 1], a = o[o.length - 2]; if ((l.x - a.x) * (r.y - a.y) >= (l.y - a.y) * (r.x - a.x)) o.pop(); else break; } o.push(r); } o.pop(); const e = []; for (let n = t.length - 1; n >= 0; n--) { const r = t[n]; for (; e.length >= 2; ) { const l = e[e.length - 1], a = e[e.length - 2]; if ((l.x - a.x) * (r.y - a.y) >= (l.y - a.y) * (r.x - a.x)) e.pop(); else break; } e.push(r); } 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 Te = z, Ce = F, Ee = S, Y = G; const je = Te, Re = Ce, ke = Ee, U = i.forwardRef(({ className: t, sideOffset: o = 4, ...e }, n) => /* @__PURE__ */ f.jsx( Y, { ref: n, sideOffset: o, className: ie( "bg-popover text-popover-foreground 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 z-50 overflow-hidden rounded-md border px-3 py-1.5 text-sm shadow-md", t ), ...e } )); U.displayName = Y.displayName; const Ie = ({ content: t, children: o, show: e = !0, ...n }) => e ? /* @__PURE__ */ k.jsx(je, { children: /* @__PURE__ */ k.jsxs(Re, { children: [ /* @__PURE__ */ k.jsx(U, { ...n, children: t }), /* @__PURE__ */ k.jsx(ke, { children: o }) ] }) }) : o; export { Ie as MyTooltip, Re as Tooltip, U as TooltipContent, je as TooltipProvider, ke as TooltipTrigger };