@uva-glass/component-library
Version:
React components UvA
70 lines (69 loc) • 2.95 kB
JavaScript
import { useState as L, useEffect as p } from "react";
import { useDebounce as F } from "./useDebounce.js";
const a = 10, y = 45, r = 300, B = 0, k = 768, D = 2;
function S(d, e, n) {
const [m, H] = L(!1), w = {
inset: "0 auto auto 0",
opacity: 0,
maxHeight: "100%",
maxWidth: "100%",
minWidth: r,
overflowX: "hidden",
overflowY: "auto",
margin: "0rem"
}, [h, v] = L(w), l = (t) => {
const o = parseFloat(getComputedStyle(document.documentElement).fontSize);
return t.indexOf("rem") !== -1 ? parseFloat(t) * o : parseFloat(t);
}, b = (t, o) => {
if (!t) return 0;
const i = o === "bottom" ? window.innerHeight - t.bottom - n.offset - a : t.top - n.offset - a;
return !n.maxFixedHeight || l(n.maxFixedHeight) > i ? i : l(n.maxFixedHeight);
}, W = (t, o) => {
if (!t) return 0;
const i = o === "left" ? window.innerWidth - t.left - a : t.right - a;
return i <= r ? r : i;
}, P = (t, o, i) => i ? `${t.left - Math.abs(o.width - t.width) / D}px` : `${t.left}px`, x = () => {
const t = d == null ? void 0 : d.getBoundingClientRect(), o = e == null ? void 0 : e.getBoundingClientRect();
let i = "bottom", u = "left";
const f = window.matchMedia(
`(max-width: ${n.mobileBreakpoint ? l(n.mobileBreakpoint) : k}px)`
).matches;
return !o || !t ? w : (window.innerHeight - o.bottom - a < y && (i = "top"), (window.innerWidth - o.left - a < r || n.horizontalPosition === "right") && !f && (u = "right"), {
inset: `${i === "bottom" ? o[i] + "px" : "auto"}
${u === "right" ? window.innerWidth - o[u] + "px" : "auto"}
${i === "top" ? window.innerHeight - o[i] + "px" : "auto"}
${u === "left" ? P(o, t, f) : "auto"}`,
maxHeight: `${b(o, i)}px`,
maxWidth: `${W(o, u)}px`,
minWidth: n.fullWidth ? o.width : r,
overflowX: "hidden",
overflowY: "auto",
opacity: 1,
margin: `${n.offset}px 0`
});
}, c = () => {
v({
...h,
...x()
}), H(!0);
}, $ = () => {
m && v({
...h,
...x()
});
}, s = (t) => {
!e || e.contains(t.target) || !d || d.contains(t.target);
}, g = F($, B);
return p(() => (e !== null && (n.mouseEvent === "click" ? e.addEventListener("click", c) : (e.addEventListener("mouseenter", c), e.addEventListener("mouseleave", s)), document.addEventListener("mousedown", s), document.addEventListener("touchstart", s), window.addEventListener("resize", g)), () => {
e !== null && (n.mouseEvent === "click" ? e.removeEventListener("click", c) : (e.removeEventListener("mouseenter", c), e.removeEventListener("mouseleave", s)), document.removeEventListener("mousedown", s), document.removeEventListener("touchstart", s), window.removeEventListener("resize", g));
})), {
style: {
style: h
},
status: m
};
}
export {
S as usePositionedFloaters
};
//# sourceMappingURL=usePositionedFloaters.js.map