Files
NapCatQQ/.pnpm-store/v3/files/e6/986e43f376baef0a94591525ea1e18285f1da982fb0407394f5d613e7dbcf5c88f7234c9298e55f3b8f3522ed60123b8c81a458dd0be7f3f3abbc3b691e81a
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

28 lines
1.2 KiB
Plaintext

import { __read } from "tslib";
import { useState } from 'react';
import useEventListener from '../useEventListener';
export default function useFocusWithin(target, options) {
var _a = __read(useState(false), 2), isFocusWithin = _a[0], setIsFocusWithin = _a[1];
var _b = options || {}, onFocus = _b.onFocus, onBlur = _b.onBlur, onChange = _b.onChange;
useEventListener('focusin', function (e) {
if (!isFocusWithin) {
onFocus === null || onFocus === void 0 ? void 0 : onFocus(e);
onChange === null || onChange === void 0 ? void 0 : onChange(true);
setIsFocusWithin(true);
}
}, {
target: target,
});
useEventListener('focusout', function (e) {
var _a, _b;
if (isFocusWithin && !((_b = (_a = e.currentTarget) === null || _a === void 0 ? void 0 : _a.contains) === null || _b === void 0 ? void 0 : _b.call(_a, e.relatedTarget))) {
onBlur === null || onBlur === void 0 ? void 0 : onBlur(e);
onChange === null || onChange === void 0 ? void 0 : onChange(false);
setIsFocusWithin(false);
}
}, {
target: target,
});
return isFocusWithin;
}