kaneo (minimalist kanban) fork to experiment adding a tangled integration
github.com/usekaneo/kaneo
1import { useEffect } from "react";
2
3type NumberedShortcutOption = {
4 onSelect: () => void;
5};
6
7export function useNumberedShortcuts(
8 isOpen: boolean,
9 options: NumberedShortcutOption[],
10 maxNumbers = 9,
11) {
12 useEffect(() => {
13 if (!isOpen) return;
14
15 const handleKeyDown = (e: KeyboardEvent) => {
16 const target = e.target as HTMLElement;
17 const isTypingInInput =
18 target.tagName === "INPUT" ||
19 target.tagName === "TEXTAREA" ||
20 target.contentEditable === "true";
21
22 if (isTypingInInput) return;
23
24 const num = Number.parseInt(e.key, 10);
25 const isValidNumber =
26 !Number.isNaN(num) &&
27 num >= 1 &&
28 num <= Math.min(options.length, maxNumbers);
29
30 if (isValidNumber) {
31 e.preventDefault();
32 e.stopPropagation();
33 options[num - 1].onSelect();
34 }
35 };
36
37 document.addEventListener("keydown", handleKeyDown, { capture: true });
38
39 return () => {
40 document.removeEventListener("keydown", handleKeyDown, { capture: true });
41 };
42 }, [isOpen, options, maxNumbers]);
43}