@adyen/adyen-platform-experience-web
Version:

45 lines (44 loc) • 1.88 kB
JavaScript
import { useRef as P, useCallback as v, useEffect as E } from "../../external/preact/hooks/dist/hooks.module.js";
import { popoverUtil as i } from "../../components/internal/Popover/utils/popoverUtil.js";
import p from "../useReflex.js";
const L = Symbol("__control.Elem.");
var R = /* @__PURE__ */ ((r) => (r.POPOVER = "POPOVER", r.DEFAULT = "DEFAULT", r))(R || {});
const O = (r) => {
r.stopImmediatePropagation();
}, k = (r, u, f, c) => {
const e = P(null), m = v(
(s) => {
const o = s.composedPath();
if (e && e.current)
if (c === "POPOVER")
i.closePopoversOutsideOfClick(o);
else {
let l = 0, d = !1, t = o[l];
for (; t instanceof Element && !(d || (d = t == null ? void 0 : t.isSameNode(e.current))); )
t = o[++l] ?? t.parentElement, (t == null ? void 0 : t[L]) instanceof Element && (t = t[L], o.length = 0);
u && !d && u(!0);
}
},
[e, u, c]
), n = P(m);
return E(() => () => {
e.current && i.remove(e.current), document.removeEventListener("click", n.current, !0);
}, []), E(() => (document.removeEventListener("click", n.current, !0), n.current = m, document.addEventListener("click", n.current, !0), c === "POPOVER" && e.current instanceof Element && i.add(e.current, u), () => {
e.current && i.remove(e.current), document.removeEventListener("click", n.current, !0);
}), [m, u, c]), E(() => {
f ? document.removeEventListener("click", n.current, !0) : document.addEventListener("click", n.current, !0);
}, [f]), p(
v(
(s, o) => {
o instanceof Element && o.removeEventListener("focusout", O, !0), s instanceof Element && (f || (s.addEventListener("focusout", O, !0), e.current = s));
},
[f, c]
),
r
);
};
export {
L as CONTROL_ELEMENT_PROPERTY,
R as ClickOutsideVariant,
k as useClickOutside
};