feat: add keyboard shortcut to navigate home on 'h' key press
This commit is contained in:
parent
f854c6ea67
commit
2809436d89
1 changed files with 24 additions and 3 deletions
|
|
@ -7,7 +7,7 @@ export default function KeyboardShortcuts() {
|
|||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
function handleKeyDown(event: KeyboardEvent) {
|
||||
function handleAddKeyDown(event: KeyboardEvent) {
|
||||
// input, textarea, contenteditable에서는 무시
|
||||
const target = event.target as HTMLElement;
|
||||
if (
|
||||
|
|
@ -23,8 +23,29 @@ export default function KeyboardShortcuts() {
|
|||
}
|
||||
}
|
||||
|
||||
document.addEventListener("keydown", handleKeyDown);
|
||||
return () => document.removeEventListener("keydown", handleKeyDown);
|
||||
document.addEventListener("keydown", handleAddKeyDown);
|
||||
return () => document.removeEventListener("keydown", handleAddKeyDown);
|
||||
}, [router]);
|
||||
|
||||
useEffect(() => {
|
||||
function handleHomeKeyDown(event: KeyboardEvent) {
|
||||
// input, textarea, contenteditable에서는 무시
|
||||
const target = event.target as HTMLElement;
|
||||
if (
|
||||
target.tagName === "INPUT" ||
|
||||
target.tagName === "TEXTAREA" ||
|
||||
target.isContentEditable
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.key === "h" || event.key === "H") {
|
||||
router.push("/");
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("keydown", handleHomeKeyDown);
|
||||
return () => document.removeEventListener("keydown", handleHomeKeyDown);
|
||||
}, [router]);
|
||||
|
||||
return null;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue