vuestic-ui
Version:
Vue 3 UI Framework
41 lines (40 loc) • 1.18 kB
JavaScript
import { unref } from "vue";
import { u as useCaptureEvent } from "./useCaptureEvent.mjs";
import { f as findTeleportedFrom } from "./useTeleported.mjs";
import { u as unwrapEl } from "../utils/unwrapEl.mjs";
const checkIfElementChild = (parent, child) => {
if (!child) {
return false;
}
if (child.parentElement === parent) {
return true;
}
return parent.contains(child);
};
const safeArray = (a) => Array.isArray(a) ? a : [a];
const useClickOutside = (elements, cb) => {
useCaptureEvent("mousedown", (event) => {
const clickTarget = event.target;
if (event.target.shadowRoot) {
return;
}
const teleportParent = findTeleportedFrom(clickTarget);
const isClickInside = safeArray(elements).some((element) => {
const el = unwrapEl(unref(element));
if (!el) {
return false;
}
if (!teleportParent) {
return checkIfElementChild(el, clickTarget);
}
return checkIfElementChild(el, clickTarget) || checkIfElementChild(el, teleportParent);
});
if (!isClickInside) {
cb(clickTarget);
}
});
};
export {
useClickOutside as u
};
//# sourceMappingURL=useClickOutside.mjs.map