Skip to content

Commit aee978a

Browse files
improve: src/useKeyboardShortcuts.ts (#11)
1 parent b2b125e commit aee978a

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

src/useKeyboardShortcuts.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { useEffect } from "react";
2+
3+
interface Shortcuts {
4+
onNew?: () => void;
5+
onSearch?: () => void;
6+
onClearDone?: () => void;
7+
}
8+
9+
/**
10+
* Global keyboard shortcuts:
11+
* - Ctrl+N / Cmd+N: New task
12+
* - Ctrl+K / Cmd+K: Focus search
13+
* - Ctrl+Shift+D: Clear done
14+
*/
15+
export function useKeyboardShortcuts({ onNew, onSearch, onClearDone }: Shortcuts) {
16+
useEffect(() => {
17+
function handler(e: KeyboardEvent) {
18+
const mod = e.metaKey || e.ctrlKey;
19+
20+
if (mod && e.key === "n") {
21+
e.preventDefault();
22+
onNew?.();
23+
}
24+
25+
if (mod && e.key === "k") {
26+
e.preventDefault();
27+
onSearch?.();
28+
}
29+
30+
if (mod && e.shiftKey && e.key === "D") {
31+
e.preventDefault();
32+
onClearDone?.();
33+
}
34+
}
35+
36+
document.addEventListener("keydown", handler);
37+
return () => document.removeEventListener("keydown", handler);
38+
}, [onNew, onSearch, onClearDone]);
39+
}

0 commit comments

Comments
 (0)