feat: add scoped shortcuts

This commit is contained in:
Max Koon
2025-12-12 18:44:18 -05:00
parent 27f6e627d4
commit c6dd174376
13 changed files with 451 additions and 138 deletions

View File

@@ -3,16 +3,20 @@ import { keysStore } from "./store";
import type { Key } from "./types";
import { enforceKeyOptions } from "./util";
export const useShortcut = (key: Key, handler: () => void) => {
export const useShortcut = (
key: Key,
handler: () => void,
scope: string = "global",
) => {
const keyOptions = enforceKeyOptions(key);
const keyName = keyOptions.name;
const ref = useRef(handler);
ref.current = handler;
useEffect(() => {
keysStore.register(keyName, ref);
keysStore.register(keyName, ref, scope);
return () => {
keysStore.deregister(keyName);
keysStore.deregister(keyName, scope);
};
}, []);
};