UNPKG

2.3 kBJavaScriptView Raw
1import { useCallback, useMemo, useRef } from 'react';
2import useEventCallback from './useEventCallback';
3import useMounted from './useMounted';
4/**
5 * useFocusManager provides a way to track and manage focus as it moves around
6 * a container element. An `onChange` is fired when focus enters or leaves the
7 * element, but not when it moves around inside the element, similar to
8 * `pointerenter` and `pointerleave` DOM events.
9 *
10 * ```tsx
11 * const [focused, setFocusState] = useState(false)
12 *
13 * const { onBlur, onFocus } = useFocusManager({
14 * onChange: nextFocused => setFocusState(nextFocused)
15 * })
16 *
17 * return (
18 * <div tabIndex="-1" onFocus={onFocus} onBlur={onBlur}>
19 * {String(focused)}
20 * <input />
21 * <input />
22 *
23 * <button>A button</button>
24 * </div>
25 * ```
26 *
27 * @returns a memoized FocusController containing event handlers
28 */
29export default function useFocusManager(opts) {
30 const isMounted = useMounted();
31 const lastFocused = useRef();
32 const handle = useRef();
33 const willHandle = useEventCallback(opts.willHandle);
34 const didHandle = useEventCallback(opts.didHandle);
35 const onChange = useEventCallback(opts.onChange);
36 const isDisabled = useEventCallback(opts.isDisabled);
37 const handleChange = useCallback((focused, event) => {
38 if (focused !== lastFocused.current) {
39 didHandle == null ? void 0 : didHandle(focused, event);
40
41 // only fire a change when unmounted if its a blur
42 if (isMounted() || !focused) {
43 lastFocused.current = focused;
44 onChange == null ? void 0 : onChange(focused, event);
45 }
46 }
47 }, [isMounted, didHandle, onChange, lastFocused]);
48 const handleFocusChange = useCallback((focused, event) => {
49 if (isDisabled()) return;
50 if (event && event.persist) event.persist();
51 if ((willHandle == null ? void 0 : willHandle(focused, event)) === false) {
52 return;
53 }
54 clearTimeout(handle.current);
55 if (focused) {
56 handleChange(focused, event);
57 } else {
58 handle.current = window.setTimeout(() => handleChange(focused, event));
59 }
60 }, [willHandle, handleChange]);
61 return useMemo(() => ({
62 onBlur: event => {
63 handleFocusChange(false, event);
64 },
65 onFocus: event => {
66 handleFocusChange(true, event);
67 }
68 }), [handleFocusChange]);
69}
\No newline at end of file