mirror of
https://github.com/p-stream/p-stream.git
synced 2026-04-11 23:10:21 +00:00
22 lines
564 B
TypeScript
22 lines
564 B
TypeScript
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]);
|
|
}
|