vue-hooks-plus
Version:
Vue hooks library
38 lines (37 loc) • 977 B
JavaScript
import useEventListener from "../useEventListener";
import useBoolean from "../useBoolean";
function useFocusWithin(target, options) {
const [isFocusWithin, { set: setIsFocusWithin }] = useBoolean(false);
const { onFocus, onBlur, onChange } = options || {};
useEventListener(
"focusin",
(e) => {
if (!isFocusWithin.value) {
onFocus == null ? void 0 : onFocus(e);
onChange == null ? void 0 : onChange(true);
setIsFocusWithin(true);
}
},
{
target
}
);
useEventListener(
"focusout",
(e) => {
var _a, _b;
if (isFocusWithin.value && !((_b = (_a = e.currentTarget) == null ? void 0 : _a.contains) == null ? void 0 : _b.call(_a, e.relatedTarget))) {
onBlur == null ? void 0 : onBlur(e);
onChange == null ? void 0 : onChange(false);
setIsFocusWithin(false);
}
},
{
target
}
);
return isFocusWithin;
}
export {
useFocusWithin as default
};