4 changed files with 121 additions and 77 deletions
@ -0,0 +1,22 @@ |
|||||||
|
import { useEffect } from "react"; |
||||||
|
|
||||||
|
export function useSlashFocus(ref: React.RefObject<HTMLInputElement>) { |
||||||
|
useEffect(() => { |
||||||
|
const listener = (e: KeyboardEvent) => { |
||||||
|
if (e.key === "/") { |
||||||
|
if ( |
||||||
|
document.activeElement && |
||||||
|
document.activeElement.tagName.toLowerCase() === "input" |
||||||
|
) |
||||||
|
return; |
||||||
|
e.preventDefault(); |
||||||
|
ref.current?.focus(); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
window.addEventListener("keydown", listener); |
||||||
|
return () => { |
||||||
|
window.removeEventListener("keydown", listener); |
||||||
|
}; |
||||||
|
}, [ref]); |
||||||
|
} |
||||||
Loading…
Reference in new issue