Skip to content

Commit

Permalink
feat: Add useHotkey
Browse files Browse the repository at this point in the history
Co-authored-by: Morten Kolstad <morten.kolstad@bekk.no>
  • Loading branch information
sivertschou and morteako committed Jan 5, 2025
1 parent 8bf90d0 commit 766c48c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions apps/frontend/src/hooks/useHotkey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useEffect } from 'react';

export const useHotkey = (keyCode: string, callback: () => void) => {
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.code === keyCode && document.activeElement === document.body) {
event.preventDefault();
callback();
}
};

window.addEventListener('keydown', handleKeyDown);

return () => {
window.removeEventListener('keydown', handleKeyDown);
};
}, [callback, keyCode]);
};

0 comments on commit 766c48c

Please sign in to comment.