vuestic-ui
Version:
Vue 3 UI Framework
57 lines (56 loc) • 1.64 kB
JavaScript
import { computed } from "vue";
import { u as useHTMLElement } from "./useHTMLElement.mjs";
import { u as useCurrentElement } from "./useCurrentElement.mjs";
import { u as useActiveElement } from "./useActiveElement.mjs";
const useFocusDeep = (el) => {
const focused = useActiveElement();
const current = useCurrentElement(el ? useHTMLElement(el) : void 0);
let previouslyFocusedElement = null;
const isFocused = computed({
get() {
var _a;
if (!focused.value) {
return false;
}
if (focused.value === current.value) {
return true;
}
const isFocused2 = (_a = current.value) == null ? void 0 : _a.contains(focused.value);
if (isFocused2) {
previouslyFocusedElement = focused.value;
}
return isFocused2;
},
set(value) {
var _a;
let target = previouslyFocusedElement ?? current.value;
if (!((_a = current.value) == null ? void 0 : _a.contains(target))) {
target = current.value;
}
if (value) {
target == null ? void 0 : target.focus();
} else {
target == null ? void 0 : target.blur();
}
}
});
return Object.assign(isFocused, {
/** Focus `el` if focus is not set to any other element */
focusIfNothingIfFocused: () => {
if (focused.value === document.body) {
isFocused.value = true;
}
},
focusPreviousElement: () => {
if (previouslyFocusedElement) {
previouslyFocusedElement.focus();
} else {
document.body.focus();
}
}
});
};
export {
useFocusDeep as u
};
//# sourceMappingURL=useFocusDeep.mjs.map