All files / packages/app/hooks use-autofocus.tsx

80% Statements 8/10
66.66% Branches 4/6
100% Functions 3/3
80% Lines 8/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21          15829x 17301x 17301x 1177x     147x   17301x 9x       17301x    
import React from "react";
 
import type { InputHandler } from "~components/input";
import { emptyInputHandler } from "~components/input.base";
 
export const useAutofocus = ({ shouldFocus }: { shouldFocus: boolean }) => {
	const ref = React.useRef<InputHandler>(emptyInputHandler);
	React.useEffect(() => {
		Iif (!shouldFocus) {
			return;
		}
		ref.current.focus();
	}, [shouldFocus]);
	const onKeyDownBlur = React.useCallback((key: string) => {
		Iif (key === "Escape" || key === "Enter") {
			ref.current.blur();
		}
	}, []);
	return { ref, onKeyDownBlur };
};