vuestic-ui
Version:
Vue 3 UI Framework
48 lines (47 loc) • 1.23 kB
JavaScript
import { unref } from "vue";
import { u as useEvent } from "./useEvent.js";
import { u as unwrapEl } from "../utils/unwrapEl.js";
const checkIfElementChild = (parent, child) => {
if (!child) {
return false;
}
if (child instanceof Window) {
return false;
}
if (child.parentElement === parent) {
return true;
}
return parent.contains(child);
};
const safeArray = (a) => Array.isArray(a) ? a : [a];
const useFocusOutside = (elements, cb, options = {}) => {
let previouslyClicked = false;
if (options.onlyKeyboard) {
useEvent("mousedown", (e) => {
previouslyClicked = true;
setTimeout(() => {
previouslyClicked = false;
}, 200);
}, true);
}
useEvent("focus", (event) => {
if (options.onlyKeyboard && previouslyClicked) {
return;
}
const focusTarget = event.target;
if (event.target.shadowRoot) {
return;
}
const isFocusInside = safeArray(elements).some((element) => {
const el = unwrapEl(unref(element));
return el && checkIfElementChild(el, focusTarget);
});
if (!isFocusInside) {
cb(focusTarget);
}
}, true);
};
export {
useFocusOutside as u
};
//# sourceMappingURL=useFocusOutside.js.map