UNPKG

fui-fancyui

Version:
28 lines (27 loc) 1.38 kB
"use client"; import { jsxs as v, jsx as x } from "react/jsx-runtime"; import { useRef as p, useEffect as h } from "react"; import { RefContainer as y, ContentContainer as E } from "./FancyPopover.style.js"; function O(a) { const { refComponent: g, contentComponent: w, offsetX: c = 0, offsetY: r = 0, isOpen: i, onOutsideClick: s, ...C } = a, o = p(null), e = p(null); return h(() => { function t(n) { o.current && !o.current.contains(n.target) && i && s && s(n); } return document.addEventListener("mousedown", t), () => document.removeEventListener("mousedown", t); }, [s, i]), h(() => { const t = () => { if (!e.current || !o.current) return; const n = o.current.getBoundingClientRect(), d = e.current.getBoundingClientRect(), R = window.innerWidth / 2, l = n.left < R, u = l ? c : "auto", f = l ? "auto" : c; let m = n.height + r; n.bottom + d.height + r > window.innerHeight && (m = -d.height - r), e.current.style.left = typeof u == "number" ? `${u}px` : u, e.current.style.right = typeof f == "number" ? `${f}px` : f, e.current.style.top = `${m}px`; }; return t(), window.addEventListener("resize", t), () => window.removeEventListener("resize", t); }, [i, c, r]), /* @__PURE__ */ v(y, { ref: o, ...C, children: [ g, i && /* @__PURE__ */ x(E, { ref: e, children: w }) ] }); } export { O as default };