import React, { useEffect } from "react"; export type SearchProps = { id: string; inputValue: string; onChangeInputValue: (value: string) => void; isFocused: boolean; onChangeIsFocused: (isFocused: boolean) => void; placeholder?: string; }; export function useFocusViaKeyboard( inputRef: React.RefObject ) { useEffect(() => { function focusOnSearchMaybe(event: KeyboardEvent) { const input = inputRef.current; const target = event.composedPath()?.[0] || event.target; const keyPressed = event.key; const ctrlOrMetaPressed = event.ctrlKey || event.metaKey; const isSlash = keyPressed === "/" && !ctrlOrMetaPressed; const isCtrlK = keyPressed === "k" && ctrlOrMetaPressed && !event.shiftKey; const isTextField = target instanceof HTMLElement && (["TEXTAREA", "INPUT"].includes(target.tagName) || target.isContentEditable); if ((isSlash || isCtrlK) && !isTextField) { if (input && document.activeElement !== input) { event.preventDefault(); input.focus(); } } } document.addEventListener("keydown", focusOnSearchMaybe); return () => { document.removeEventListener("keydown", focusOnSearchMaybe); }; }, [inputRef]); }