-
-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c96ca21
commit 64df58d
Showing
3 changed files
with
41 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useEffect, Ref, RefObject } from "react"; | ||
|
||
interface useSearchShortcutProps { | ||
inputRef : RefObject<HTMLInputElement | null>, | ||
} | ||
|
||
const useSearchShortcut = ({inputRef} : useSearchShortcutProps) => { | ||
useEffect(() => { | ||
const handleSlashKeyPress = (event: KeyboardEvent) => { | ||
// Check if the pressed key is '/' | ||
if (event.key === '/') { | ||
// Prevent the default behavior (e.g., adding '/' to an input field) | ||
event.preventDefault(); | ||
|
||
// Focus the input element | ||
if (inputRef.current) { | ||
inputRef.current.focus(); | ||
} | ||
} | ||
}; | ||
|
||
// Attach the event listener when the component mounts | ||
document.addEventListener('keydown', handleSlashKeyPress); | ||
|
||
// Detach the event listener when the component unmounts | ||
return () => { | ||
document.removeEventListener('keydown', handleSlashKeyPress); | ||
}; | ||
}, [inputRef]); | ||
} | ||
|
||
export default useSearchShortcut |