mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-25 10:51:26 +01:00
Auth plugins (#155)
This commit is contained in:
18
src-web/hooks/useKeyboardEvent.ts
Normal file
18
src-web/hooks/useKeyboardEvent.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { useEffect } from 'react';
|
||||
|
||||
export function useKeyboardEvent(
|
||||
event: 'keyup' | 'keydown',
|
||||
key: KeyboardEvent['key'],
|
||||
cb: () => void,
|
||||
) {
|
||||
useEffect(() => {
|
||||
const fn = (e: KeyboardEvent) => {
|
||||
if (e.key === key) cb();
|
||||
};
|
||||
document.addEventListener(event, fn);
|
||||
return () => document.removeEventListener(event, fn);
|
||||
|
||||
// Don't have `cb` as a dep for caller convenience
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [event]);
|
||||
}
|
||||
Reference in New Issue
Block a user