refactor: better shortcut hook
This commit is contained in:
14
packages/ui/lib/shortcuts/hooks.ts
Normal file
14
packages/ui/lib/shortcuts/hooks.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
import { keysStore } from "./store";
|
||||
|
||||
export const useShortcut = (key: string, handler: () => void) => {
|
||||
const ref = useRef(handler);
|
||||
ref.current = handler;
|
||||
|
||||
useEffect(() => {
|
||||
keysStore.register(key, ref);
|
||||
return () => {
|
||||
keysStore.deregister(key);
|
||||
};
|
||||
}, []);
|
||||
};
|
||||
Reference in New Issue
Block a user