@tanstack/react-hotkeys 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +121 -45
  3. package/dist/HotkeysProvider.cjs +23 -0
  4. package/dist/HotkeysProvider.cjs.map +1 -0
  5. package/dist/HotkeysProvider.d.cts +27 -0
  6. package/dist/HotkeysProvider.d.ts +27 -0
  7. package/dist/HotkeysProvider.js +19 -0
  8. package/dist/HotkeysProvider.js.map +1 -0
  9. package/dist/_virtual/_rolldown/runtime.cjs +29 -0
  10. package/dist/index.cjs +25 -0
  11. package/dist/index.d.cts +9 -0
  12. package/dist/index.d.ts +9 -0
  13. package/dist/index.js +11 -0
  14. package/dist/useHeldKeyCodes.cjs +38 -0
  15. package/dist/useHeldKeyCodes.cjs.map +1 -0
  16. package/dist/useHeldKeyCodes.d.cts +31 -0
  17. package/dist/useHeldKeyCodes.d.ts +31 -0
  18. package/dist/useHeldKeyCodes.js +37 -0
  19. package/dist/useHeldKeyCodes.js.map +1 -0
  20. package/dist/useHeldKeys.cjs +34 -0
  21. package/dist/useHeldKeys.cjs.map +1 -0
  22. package/dist/useHeldKeys.d.cts +27 -0
  23. package/dist/useHeldKeys.d.ts +27 -0
  24. package/dist/useHeldKeys.js +33 -0
  25. package/dist/useHeldKeys.js.map +1 -0
  26. package/dist/useHotkey.cjs +119 -0
  27. package/dist/useHotkey.cjs.map +1 -0
  28. package/dist/useHotkey.d.cts +73 -0
  29. package/dist/useHotkey.d.ts +73 -0
  30. package/dist/useHotkey.js +118 -0
  31. package/dist/useHotkey.js.map +1 -0
  32. package/dist/useHotkeyRecorder.cjs +72 -0
  33. package/dist/useHotkeyRecorder.cjs.map +1 -0
  34. package/dist/useHotkeyRecorder.d.cts +57 -0
  35. package/dist/useHotkeyRecorder.d.ts +57 -0
  36. package/dist/useHotkeyRecorder.js +71 -0
  37. package/dist/useHotkeyRecorder.js.map +1 -0
  38. package/dist/useHotkeySequence.cjs +65 -0
  39. package/dist/useHotkeySequence.cjs.map +1 -0
  40. package/dist/useHotkeySequence.d.cts +43 -0
  41. package/dist/useHotkeySequence.d.ts +43 -0
  42. package/dist/useHotkeySequence.js +64 -0
  43. package/dist/useHotkeySequence.js.map +1 -0
  44. package/dist/useKeyHold.cjs +54 -0
  45. package/dist/useKeyHold.cjs.map +1 -0
  46. package/dist/useKeyHold.d.cts +47 -0
  47. package/dist/useKeyHold.d.ts +47 -0
  48. package/dist/useKeyHold.js +53 -0
  49. package/dist/useKeyHold.js.map +1 -0
  50. package/package.json +66 -7
  51. package/src/HotkeysProvider.tsx +51 -0
  52. package/src/index.ts +13 -0
  53. package/src/useHeldKeyCodes.ts +33 -0
  54. package/src/useHeldKeys.ts +29 -0
  55. package/src/useHotkey.ts +191 -0
  56. package/src/useHotkeyRecorder.ts +101 -0
  57. package/src/useHotkeySequence.ts +92 -0
  58. package/src/useKeyHold.ts +52 -0
@@ -0,0 +1,71 @@
1
+ import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
+ import { HotkeyRecorder } from "@tanstack/hotkeys";
3
+ import { useEffect, useRef } from "react";
4
+ import { useStore } from "@tanstack/react-store";
5
+
6
+ //#region src/useHotkeyRecorder.ts
7
+ /**
8
+ * React hook for recording keyboard shortcuts.
9
+ *
10
+ * This hook provides a thin wrapper around the framework-agnostic `HotkeyRecorder`
11
+ * class, managing all the complexity of capturing keyboard events, converting them
12
+ * to hotkey strings, and handling edge cases like Escape to cancel or Backspace/Delete
13
+ * to clear.
14
+ *
15
+ * @param options - Configuration options for the recorder
16
+ * @returns An object with recording state and control functions
17
+ *
18
+ * @example
19
+ * ```tsx
20
+ * function ShortcutSettings() {
21
+ * const [shortcut, setShortcut] = useState<Hotkey>('Mod+S')
22
+ *
23
+ * const recorder = useHotkeyRecorder({
24
+ * onRecord: (hotkey) => {
25
+ * setShortcut(hotkey)
26
+ * },
27
+ * onCancel: () => {
28
+ * console.log('Recording cancelled')
29
+ * },
30
+ * })
31
+ *
32
+ * return (
33
+ * <div>
34
+ * <button onClick={recorder.startRecording}>
35
+ * {recorder.isRecording ? 'Recording...' : 'Edit Shortcut'}
36
+ * </button>
37
+ * {recorder.recordedHotkey && (
38
+ * <div>Recording: {recorder.recordedHotkey}</div>
39
+ * )}
40
+ * </div>
41
+ * )
42
+ * }
43
+ * ```
44
+ */
45
+ function useHotkeyRecorder(options) {
46
+ const mergedOptions = {
47
+ ...useDefaultHotkeysOptions().hotkeyRecorder,
48
+ ...options
49
+ };
50
+ const recorderRef = useRef(null);
51
+ if (!recorderRef.current) recorderRef.current = new HotkeyRecorder(mergedOptions);
52
+ recorderRef.current.setOptions(mergedOptions);
53
+ const isRecording = useStore(recorderRef.current.store, (state) => state.isRecording);
54
+ const recordedHotkey = useStore(recorderRef.current.store, (state) => state.recordedHotkey);
55
+ useEffect(() => {
56
+ return () => {
57
+ recorderRef.current?.destroy();
58
+ };
59
+ }, []);
60
+ return {
61
+ isRecording,
62
+ recordedHotkey,
63
+ startRecording: () => recorderRef.current?.start(),
64
+ stopRecording: () => recorderRef.current?.stop(),
65
+ cancelRecording: () => recorderRef.current?.cancel()
66
+ };
67
+ }
68
+
69
+ //#endregion
70
+ export { useHotkeyRecorder };
71
+ //# sourceMappingURL=useHotkeyRecorder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useHotkeyRecorder.js","names":[],"sources":["../src/useHotkeyRecorder.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport { useStore } from '@tanstack/react-store'\nimport { HotkeyRecorder } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type { Hotkey, HotkeyRecorderOptions } from '@tanstack/hotkeys'\n\nexport interface ReactHotkeyRecorder {\n /** Whether recording is currently active */\n isRecording: boolean\n /** The currently recorded hotkey (for live preview) */\n recordedHotkey: Hotkey | null\n /** Start recording a new hotkey */\n startRecording: () => void\n /** Stop recording (same as cancel) */\n stopRecording: () => void\n /** Cancel recording without saving */\n cancelRecording: () => void\n}\n\n/**\n * React hook for recording keyboard shortcuts.\n *\n * This hook provides a thin wrapper around the framework-agnostic `HotkeyRecorder`\n * class, managing all the complexity of capturing keyboard events, converting them\n * to hotkey strings, and handling edge cases like Escape to cancel or Backspace/Delete\n * to clear.\n *\n * @param options - Configuration options for the recorder\n * @returns An object with recording state and control functions\n *\n * @example\n * ```tsx\n * function ShortcutSettings() {\n * const [shortcut, setShortcut] = useState<Hotkey>('Mod+S')\n *\n * const recorder = useHotkeyRecorder({\n * onRecord: (hotkey) => {\n * setShortcut(hotkey)\n * },\n * onCancel: () => {\n * console.log('Recording cancelled')\n * },\n * })\n *\n * return (\n * <div>\n * <button onClick={recorder.startRecording}>\n * {recorder.isRecording ? 'Recording...' : 'Edit Shortcut'}\n * </button>\n * {recorder.recordedHotkey && (\n * <div>Recording: {recorder.recordedHotkey}</div>\n * )}\n * </div>\n * )\n * }\n * ```\n */\nexport function useHotkeyRecorder(\n options: HotkeyRecorderOptions,\n): ReactHotkeyRecorder {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkeyRecorder,\n ...options,\n } as HotkeyRecorderOptions\n\n const recorderRef = useRef<HotkeyRecorder | null>(null)\n\n // Create recorder instance once\n if (!recorderRef.current) {\n recorderRef.current = new HotkeyRecorder(mergedOptions)\n }\n\n // Sync options on every render (same pattern as useHotkey)\n // This ensures callbacks always have access to latest values\n recorderRef.current.setOptions(mergedOptions)\n\n // Subscribe to recorder state using useStore (same pattern as useHeldKeys)\n const isRecording = useStore(\n recorderRef.current.store,\n (state) => state.isRecording,\n )\n const recordedHotkey = useStore(\n recorderRef.current.store,\n (state) => state.recordedHotkey,\n )\n\n // Cleanup on unmount\n useEffect(() => {\n return () => {\n recorderRef.current?.destroy()\n }\n }, [])\n\n return {\n isRecording,\n recordedHotkey,\n startRecording: () => recorderRef.current?.start(),\n stopRecording: () => recorderRef.current?.stop(),\n cancelRecording: () => recorderRef.current?.cancel(),\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,SAAgB,kBACd,SACqB;CACrB,MAAM,gBAAgB;EACpB,GAAG,0BAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,cAAc,OAA8B,KAAK;AAGvD,KAAI,CAAC,YAAY,QACf,aAAY,UAAU,IAAI,eAAe,cAAc;AAKzD,aAAY,QAAQ,WAAW,cAAc;CAG7C,MAAM,cAAc,SAClB,YAAY,QAAQ,QACnB,UAAU,MAAM,YAClB;CACD,MAAM,iBAAiB,SACrB,YAAY,QAAQ,QACnB,UAAU,MAAM,eAClB;AAGD,iBAAgB;AACd,eAAa;AACX,eAAY,SAAS,SAAS;;IAE/B,EAAE,CAAC;AAEN,QAAO;EACL;EACA;EACA,sBAAsB,YAAY,SAAS,OAAO;EAClD,qBAAqB,YAAY,SAAS,MAAM;EAChD,uBAAuB,YAAY,SAAS,QAAQ;EACrD"}
@@ -0,0 +1,65 @@
1
+ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
2
+ const require_HotkeysProvider = require('./HotkeysProvider.cjs');
3
+ let _tanstack_hotkeys = require("@tanstack/hotkeys");
4
+ let react = require("react");
5
+
6
+ //#region src/useHotkeySequence.ts
7
+ /**
8
+ * React hook for registering a keyboard shortcut sequence (Vim-style).
9
+ *
10
+ * This hook allows you to register multi-key sequences like 'g g' or 'd d'
11
+ * that trigger when the full sequence is pressed within a timeout.
12
+ *
13
+ * @param sequence - Array of hotkey strings that form the sequence
14
+ * @param callback - Function to call when the sequence is completed
15
+ * @param options - Options for the sequence behavior
16
+ *
17
+ * @example
18
+ * ```tsx
19
+ * function VimEditor() {
20
+ * // 'g g' to go to top
21
+ * useHotkeySequence(['G', 'G'], () => {
22
+ * scrollToTop()
23
+ * })
24
+ *
25
+ * // 'd d' to delete line
26
+ * useHotkeySequence(['D', 'D'], () => {
27
+ * deleteLine()
28
+ * })
29
+ *
30
+ * // 'd i w' to delete inner word
31
+ * useHotkeySequence(['D', 'I', 'W'], () => {
32
+ * deleteInnerWord()
33
+ * }, { timeout: 500 })
34
+ *
35
+ * return <div>...</div>
36
+ * }
37
+ * ```
38
+ */
39
+ function useHotkeySequence(sequence, callback, options = {}) {
40
+ const { enabled = true, ...sequenceOptions } = {
41
+ ...require_HotkeysProvider.useDefaultHotkeysOptions().hotkeySequence,
42
+ ...options
43
+ };
44
+ const { timeout, platform } = sequenceOptions;
45
+ const callbackRef = (0, react.useRef)(callback);
46
+ callbackRef.current = callback;
47
+ (0, react.useEffect)(() => {
48
+ if (!enabled || sequence.length === 0) return;
49
+ const manager = (0, _tanstack_hotkeys.getSequenceManager)();
50
+ const registerOptions = { enabled: true };
51
+ if (timeout !== void 0) registerOptions.timeout = timeout;
52
+ if (platform !== void 0) registerOptions.platform = platform;
53
+ return manager.register(sequence, (event, context) => callbackRef.current(event, context), registerOptions);
54
+ }, [
55
+ enabled,
56
+ sequence,
57
+ sequence.join("|"),
58
+ timeout,
59
+ platform
60
+ ]);
61
+ }
62
+
63
+ //#endregion
64
+ exports.useHotkeySequence = useHotkeySequence;
65
+ //# sourceMappingURL=useHotkeySequence.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useHotkeySequence.cjs","names":["useDefaultHotkeysOptions"],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport { getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type {\n HotkeyCallback,\n HotkeySequence,\n SequenceOptions,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'enabled'\n> {\n /** Whether the sequence is enabled. Defaults to true. */\n enabled?: boolean\n}\n\n/**\n * React hook for registering a keyboard shortcut sequence (Vim-style).\n *\n * This hook allows you to register multi-key sequences like 'g g' or 'd d'\n * that trigger when the full sequence is pressed within a timeout.\n *\n * @param sequence - Array of hotkey strings that form the sequence\n * @param callback - Function to call when the sequence is completed\n * @param options - Options for the sequence behavior\n *\n * @example\n * ```tsx\n * function VimEditor() {\n * // 'g g' to go to top\n * useHotkeySequence(['G', 'G'], () => {\n * scrollToTop()\n * })\n *\n * // 'd d' to delete line\n * useHotkeySequence(['D', 'D'], () => {\n * deleteLine()\n * })\n *\n * // 'd i w' to delete inner word\n * useHotkeySequence(['D', 'I', 'W'], () => {\n * deleteInnerWord()\n * }, { timeout: 500 })\n *\n * return <div>...</div>\n * }\n * ```\n */\nexport function useHotkeySequence(\n sequence: HotkeySequence,\n callback: HotkeyCallback,\n options: UseHotkeySequenceOptions = {},\n): void {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkeySequence,\n ...options,\n } as UseHotkeySequenceOptions\n\n const { enabled = true, ...sequenceOptions } = mergedOptions\n\n // Extract options for stable dependencies\n const { timeout, platform } = sequenceOptions\n\n // Use refs to keep callback stable\n const callbackRef = useRef(callback)\n callbackRef.current = callback\n\n // Serialize sequence for dependency comparison\n const sequenceKey = sequence.join('|')\n\n useEffect(() => {\n if (!enabled || sequence.length === 0) {\n return\n }\n\n const manager = getSequenceManager()\n\n // Build options object conditionally to avoid overwriting manager defaults with undefined\n const registerOptions: SequenceOptions = { enabled: true }\n if (timeout !== undefined) registerOptions.timeout = timeout\n if (platform !== undefined) registerOptions.platform = platform\n\n const unregister = manager.register(\n sequence,\n (event, context) => callbackRef.current(event, context),\n registerOptions,\n )\n\n return unregister\n }, [enabled, sequence, sequenceKey, timeout, platform])\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiDA,SAAgB,kBACd,UACA,UACA,UAAoC,EAAE,EAChC;CAMN,MAAM,EAAE,UAAU,MAAM,GAAG,oBALL;EACpB,GAAGA,kDAA0B,CAAC;EAC9B,GAAG;EACJ;CAKD,MAAM,EAAE,SAAS,aAAa;CAG9B,MAAM,gCAAqB,SAAS;AACpC,aAAY,UAAU;AAKtB,4BAAgB;AACd,MAAI,CAAC,WAAW,SAAS,WAAW,EAClC;EAGF,MAAM,qDAA8B;EAGpC,MAAM,kBAAmC,EAAE,SAAS,MAAM;AAC1D,MAAI,YAAY,OAAW,iBAAgB,UAAU;AACrD,MAAI,aAAa,OAAW,iBAAgB,WAAW;AAQvD,SANmB,QAAQ,SACzB,WACC,OAAO,YAAY,YAAY,QAAQ,OAAO,QAAQ,EACvD,gBACD;IAGA;EAAC;EAAS;EArBO,SAAS,KAAK,IAAI;EAqBF;EAAS;EAAS,CAAC"}
@@ -0,0 +1,43 @@
1
+ import { HotkeyCallback, HotkeySequence, SequenceOptions } from "@tanstack/hotkeys";
2
+
3
+ //#region src/useHotkeySequence.d.ts
4
+ interface UseHotkeySequenceOptions extends Omit<SequenceOptions, 'enabled'> {
5
+ /** Whether the sequence is enabled. Defaults to true. */
6
+ enabled?: boolean;
7
+ }
8
+ /**
9
+ * React hook for registering a keyboard shortcut sequence (Vim-style).
10
+ *
11
+ * This hook allows you to register multi-key sequences like 'g g' or 'd d'
12
+ * that trigger when the full sequence is pressed within a timeout.
13
+ *
14
+ * @param sequence - Array of hotkey strings that form the sequence
15
+ * @param callback - Function to call when the sequence is completed
16
+ * @param options - Options for the sequence behavior
17
+ *
18
+ * @example
19
+ * ```tsx
20
+ * function VimEditor() {
21
+ * // 'g g' to go to top
22
+ * useHotkeySequence(['G', 'G'], () => {
23
+ * scrollToTop()
24
+ * })
25
+ *
26
+ * // 'd d' to delete line
27
+ * useHotkeySequence(['D', 'D'], () => {
28
+ * deleteLine()
29
+ * })
30
+ *
31
+ * // 'd i w' to delete inner word
32
+ * useHotkeySequence(['D', 'I', 'W'], () => {
33
+ * deleteInnerWord()
34
+ * }, { timeout: 500 })
35
+ *
36
+ * return <div>...</div>
37
+ * }
38
+ * ```
39
+ */
40
+ declare function useHotkeySequence(sequence: HotkeySequence, callback: HotkeyCallback, options?: UseHotkeySequenceOptions): void;
41
+ //#endregion
42
+ export { UseHotkeySequenceOptions, useHotkeySequence };
43
+ //# sourceMappingURL=useHotkeySequence.d.cts.map
@@ -0,0 +1,43 @@
1
+ import { HotkeyCallback, HotkeySequence, SequenceOptions } from "@tanstack/hotkeys";
2
+
3
+ //#region src/useHotkeySequence.d.ts
4
+ interface UseHotkeySequenceOptions extends Omit<SequenceOptions, 'enabled'> {
5
+ /** Whether the sequence is enabled. Defaults to true. */
6
+ enabled?: boolean;
7
+ }
8
+ /**
9
+ * React hook for registering a keyboard shortcut sequence (Vim-style).
10
+ *
11
+ * This hook allows you to register multi-key sequences like 'g g' or 'd d'
12
+ * that trigger when the full sequence is pressed within a timeout.
13
+ *
14
+ * @param sequence - Array of hotkey strings that form the sequence
15
+ * @param callback - Function to call when the sequence is completed
16
+ * @param options - Options for the sequence behavior
17
+ *
18
+ * @example
19
+ * ```tsx
20
+ * function VimEditor() {
21
+ * // 'g g' to go to top
22
+ * useHotkeySequence(['G', 'G'], () => {
23
+ * scrollToTop()
24
+ * })
25
+ *
26
+ * // 'd d' to delete line
27
+ * useHotkeySequence(['D', 'D'], () => {
28
+ * deleteLine()
29
+ * })
30
+ *
31
+ * // 'd i w' to delete inner word
32
+ * useHotkeySequence(['D', 'I', 'W'], () => {
33
+ * deleteInnerWord()
34
+ * }, { timeout: 500 })
35
+ *
36
+ * return <div>...</div>
37
+ * }
38
+ * ```
39
+ */
40
+ declare function useHotkeySequence(sequence: HotkeySequence, callback: HotkeyCallback, options?: UseHotkeySequenceOptions): void;
41
+ //#endregion
42
+ export { UseHotkeySequenceOptions, useHotkeySequence };
43
+ //# sourceMappingURL=useHotkeySequence.d.ts.map
@@ -0,0 +1,64 @@
1
+ import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
+ import { getSequenceManager } from "@tanstack/hotkeys";
3
+ import { useEffect, useRef } from "react";
4
+
5
+ //#region src/useHotkeySequence.ts
6
+ /**
7
+ * React hook for registering a keyboard shortcut sequence (Vim-style).
8
+ *
9
+ * This hook allows you to register multi-key sequences like 'g g' or 'd d'
10
+ * that trigger when the full sequence is pressed within a timeout.
11
+ *
12
+ * @param sequence - Array of hotkey strings that form the sequence
13
+ * @param callback - Function to call when the sequence is completed
14
+ * @param options - Options for the sequence behavior
15
+ *
16
+ * @example
17
+ * ```tsx
18
+ * function VimEditor() {
19
+ * // 'g g' to go to top
20
+ * useHotkeySequence(['G', 'G'], () => {
21
+ * scrollToTop()
22
+ * })
23
+ *
24
+ * // 'd d' to delete line
25
+ * useHotkeySequence(['D', 'D'], () => {
26
+ * deleteLine()
27
+ * })
28
+ *
29
+ * // 'd i w' to delete inner word
30
+ * useHotkeySequence(['D', 'I', 'W'], () => {
31
+ * deleteInnerWord()
32
+ * }, { timeout: 500 })
33
+ *
34
+ * return <div>...</div>
35
+ * }
36
+ * ```
37
+ */
38
+ function useHotkeySequence(sequence, callback, options = {}) {
39
+ const { enabled = true, ...sequenceOptions } = {
40
+ ...useDefaultHotkeysOptions().hotkeySequence,
41
+ ...options
42
+ };
43
+ const { timeout, platform } = sequenceOptions;
44
+ const callbackRef = useRef(callback);
45
+ callbackRef.current = callback;
46
+ useEffect(() => {
47
+ if (!enabled || sequence.length === 0) return;
48
+ const manager = getSequenceManager();
49
+ const registerOptions = { enabled: true };
50
+ if (timeout !== void 0) registerOptions.timeout = timeout;
51
+ if (platform !== void 0) registerOptions.platform = platform;
52
+ return manager.register(sequence, (event, context) => callbackRef.current(event, context), registerOptions);
53
+ }, [
54
+ enabled,
55
+ sequence,
56
+ sequence.join("|"),
57
+ timeout,
58
+ platform
59
+ ]);
60
+ }
61
+
62
+ //#endregion
63
+ export { useHotkeySequence };
64
+ //# sourceMappingURL=useHotkeySequence.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useHotkeySequence.js","names":[],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport { getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type {\n HotkeyCallback,\n HotkeySequence,\n SequenceOptions,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'enabled'\n> {\n /** Whether the sequence is enabled. Defaults to true. */\n enabled?: boolean\n}\n\n/**\n * React hook for registering a keyboard shortcut sequence (Vim-style).\n *\n * This hook allows you to register multi-key sequences like 'g g' or 'd d'\n * that trigger when the full sequence is pressed within a timeout.\n *\n * @param sequence - Array of hotkey strings that form the sequence\n * @param callback - Function to call when the sequence is completed\n * @param options - Options for the sequence behavior\n *\n * @example\n * ```tsx\n * function VimEditor() {\n * // 'g g' to go to top\n * useHotkeySequence(['G', 'G'], () => {\n * scrollToTop()\n * })\n *\n * // 'd d' to delete line\n * useHotkeySequence(['D', 'D'], () => {\n * deleteLine()\n * })\n *\n * // 'd i w' to delete inner word\n * useHotkeySequence(['D', 'I', 'W'], () => {\n * deleteInnerWord()\n * }, { timeout: 500 })\n *\n * return <div>...</div>\n * }\n * ```\n */\nexport function useHotkeySequence(\n sequence: HotkeySequence,\n callback: HotkeyCallback,\n options: UseHotkeySequenceOptions = {},\n): void {\n const mergedOptions = {\n ...useDefaultHotkeysOptions().hotkeySequence,\n ...options,\n } as UseHotkeySequenceOptions\n\n const { enabled = true, ...sequenceOptions } = mergedOptions\n\n // Extract options for stable dependencies\n const { timeout, platform } = sequenceOptions\n\n // Use refs to keep callback stable\n const callbackRef = useRef(callback)\n callbackRef.current = callback\n\n // Serialize sequence for dependency comparison\n const sequenceKey = sequence.join('|')\n\n useEffect(() => {\n if (!enabled || sequence.length === 0) {\n return\n }\n\n const manager = getSequenceManager()\n\n // Build options object conditionally to avoid overwriting manager defaults with undefined\n const registerOptions: SequenceOptions = { enabled: true }\n if (timeout !== undefined) registerOptions.timeout = timeout\n if (platform !== undefined) registerOptions.platform = platform\n\n const unregister = manager.register(\n sequence,\n (event, context) => callbackRef.current(event, context),\n registerOptions,\n )\n\n return unregister\n }, [enabled, sequence, sequenceKey, timeout, platform])\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiDA,SAAgB,kBACd,UACA,UACA,UAAoC,EAAE,EAChC;CAMN,MAAM,EAAE,UAAU,MAAM,GAAG,oBALL;EACpB,GAAG,0BAA0B,CAAC;EAC9B,GAAG;EACJ;CAKD,MAAM,EAAE,SAAS,aAAa;CAG9B,MAAM,cAAc,OAAO,SAAS;AACpC,aAAY,UAAU;AAKtB,iBAAgB;AACd,MAAI,CAAC,WAAW,SAAS,WAAW,EAClC;EAGF,MAAM,UAAU,oBAAoB;EAGpC,MAAM,kBAAmC,EAAE,SAAS,MAAM;AAC1D,MAAI,YAAY,OAAW,iBAAgB,UAAU;AACrD,MAAI,aAAa,OAAW,iBAAgB,WAAW;AAQvD,SANmB,QAAQ,SACzB,WACC,OAAO,YAAY,YAAY,QAAQ,OAAO,QAAQ,EACvD,gBACD;IAGA;EAAC;EAAS;EArBO,SAAS,KAAK,IAAI;EAqBF;EAAS;EAAS,CAAC"}
@@ -0,0 +1,54 @@
1
+ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
2
+ let _tanstack_hotkeys = require("@tanstack/hotkeys");
3
+ let _tanstack_react_store = require("@tanstack/react-store");
4
+
5
+ //#region src/useKeyHold.ts
6
+ /**
7
+ * React hook that returns whether a specific key is currently being held.
8
+ *
9
+ * This hook uses `useStore` from `@tanstack/react-store` to subscribe
10
+ * to the global KeyStateTracker and uses a selector to determine if
11
+ * the specified key is held.
12
+ *
13
+ * @param key - The key to check (e.g., 'Shift', 'Control', 'A')
14
+ * @returns True if the key is currently held down
15
+ *
16
+ * @example
17
+ * ```tsx
18
+ * function ShiftIndicator() {
19
+ * const isShiftHeld = useKeyHold('Shift')
20
+ *
21
+ * return (
22
+ * <div style={{ opacity: isShiftHeld ? 1 : 0.5 }}>
23
+ * {isShiftHeld ? 'Shift is pressed!' : 'Press Shift'}
24
+ * </div>
25
+ * )
26
+ * }
27
+ * ```
28
+ *
29
+ * @example
30
+ * ```tsx
31
+ * function ModifierIndicators() {
32
+ * const ctrl = useKeyHold('Control')
33
+ * const shift = useKeyHold('Shift')
34
+ * const alt = useKeyHold('Alt')
35
+ *
36
+ * return (
37
+ * <div>
38
+ * <span style={{ opacity: ctrl ? 1 : 0.3 }}>Ctrl</span>
39
+ * <span style={{ opacity: shift ? 1 : 0.3 }}>Shift</span>
40
+ * <span style={{ opacity: alt ? 1 : 0.3 }}>Alt</span>
41
+ * </div>
42
+ * )
43
+ * }
44
+ * ```
45
+ */
46
+ function useKeyHold(key) {
47
+ const tracker = (0, _tanstack_hotkeys.getKeyStateTracker)();
48
+ const normalizedKey = key.toLowerCase();
49
+ return (0, _tanstack_react_store.useStore)(tracker.store, (state) => state.heldKeys.some((heldKey) => heldKey.toLowerCase() === normalizedKey));
50
+ }
51
+
52
+ //#endregion
53
+ exports.useKeyHold = useKeyHold;
54
+ //# sourceMappingURL=useKeyHold.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useKeyHold.cjs","names":[],"sources":["../src/useKeyHold.ts"],"sourcesContent":["import { useStore } from '@tanstack/react-store'\nimport { getKeyStateTracker } from '@tanstack/hotkeys'\nimport type { HeldKey } from '@tanstack/hotkeys'\n\n/**\n * React hook that returns whether a specific key is currently being held.\n *\n * This hook uses `useStore` from `@tanstack/react-store` to subscribe\n * to the global KeyStateTracker and uses a selector to determine if\n * the specified key is held.\n *\n * @param key - The key to check (e.g., 'Shift', 'Control', 'A')\n * @returns True if the key is currently held down\n *\n * @example\n * ```tsx\n * function ShiftIndicator() {\n * const isShiftHeld = useKeyHold('Shift')\n *\n * return (\n * <div style={{ opacity: isShiftHeld ? 1 : 0.5 }}>\n * {isShiftHeld ? 'Shift is pressed!' : 'Press Shift'}\n * </div>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * function ModifierIndicators() {\n * const ctrl = useKeyHold('Control')\n * const shift = useKeyHold('Shift')\n * const alt = useKeyHold('Alt')\n *\n * return (\n * <div>\n * <span style={{ opacity: ctrl ? 1 : 0.3 }}>Ctrl</span>\n * <span style={{ opacity: shift ? 1 : 0.3 }}>Shift</span>\n * <span style={{ opacity: alt ? 1 : 0.3 }}>Alt</span>\n * </div>\n * )\n * }\n * ```\n */\nexport function useKeyHold(key: HeldKey): boolean {\n const tracker = getKeyStateTracker()\n const normalizedKey = key.toLowerCase()\n\n return useStore(tracker.store, (state) =>\n state.heldKeys.some((heldKey) => heldKey.toLowerCase() === normalizedKey),\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CA,SAAgB,WAAW,KAAuB;CAChD,MAAM,qDAA8B;CACpC,MAAM,gBAAgB,IAAI,aAAa;AAEvC,4CAAgB,QAAQ,QAAQ,UAC9B,MAAM,SAAS,MAAM,YAAY,QAAQ,aAAa,KAAK,cAAc,CAC1E"}
@@ -0,0 +1,47 @@
1
+ import { HeldKey } from "@tanstack/hotkeys";
2
+
3
+ //#region src/useKeyHold.d.ts
4
+ /**
5
+ * React hook that returns whether a specific key is currently being held.
6
+ *
7
+ * This hook uses `useStore` from `@tanstack/react-store` to subscribe
8
+ * to the global KeyStateTracker and uses a selector to determine if
9
+ * the specified key is held.
10
+ *
11
+ * @param key - The key to check (e.g., 'Shift', 'Control', 'A')
12
+ * @returns True if the key is currently held down
13
+ *
14
+ * @example
15
+ * ```tsx
16
+ * function ShiftIndicator() {
17
+ * const isShiftHeld = useKeyHold('Shift')
18
+ *
19
+ * return (
20
+ * <div style={{ opacity: isShiftHeld ? 1 : 0.5 }}>
21
+ * {isShiftHeld ? 'Shift is pressed!' : 'Press Shift'}
22
+ * </div>
23
+ * )
24
+ * }
25
+ * ```
26
+ *
27
+ * @example
28
+ * ```tsx
29
+ * function ModifierIndicators() {
30
+ * const ctrl = useKeyHold('Control')
31
+ * const shift = useKeyHold('Shift')
32
+ * const alt = useKeyHold('Alt')
33
+ *
34
+ * return (
35
+ * <div>
36
+ * <span style={{ opacity: ctrl ? 1 : 0.3 }}>Ctrl</span>
37
+ * <span style={{ opacity: shift ? 1 : 0.3 }}>Shift</span>
38
+ * <span style={{ opacity: alt ? 1 : 0.3 }}>Alt</span>
39
+ * </div>
40
+ * )
41
+ * }
42
+ * ```
43
+ */
44
+ declare function useKeyHold(key: HeldKey): boolean;
45
+ //#endregion
46
+ export { useKeyHold };
47
+ //# sourceMappingURL=useKeyHold.d.cts.map
@@ -0,0 +1,47 @@
1
+ import { HeldKey } from "@tanstack/hotkeys";
2
+
3
+ //#region src/useKeyHold.d.ts
4
+ /**
5
+ * React hook that returns whether a specific key is currently being held.
6
+ *
7
+ * This hook uses `useStore` from `@tanstack/react-store` to subscribe
8
+ * to the global KeyStateTracker and uses a selector to determine if
9
+ * the specified key is held.
10
+ *
11
+ * @param key - The key to check (e.g., 'Shift', 'Control', 'A')
12
+ * @returns True if the key is currently held down
13
+ *
14
+ * @example
15
+ * ```tsx
16
+ * function ShiftIndicator() {
17
+ * const isShiftHeld = useKeyHold('Shift')
18
+ *
19
+ * return (
20
+ * <div style={{ opacity: isShiftHeld ? 1 : 0.5 }}>
21
+ * {isShiftHeld ? 'Shift is pressed!' : 'Press Shift'}
22
+ * </div>
23
+ * )
24
+ * }
25
+ * ```
26
+ *
27
+ * @example
28
+ * ```tsx
29
+ * function ModifierIndicators() {
30
+ * const ctrl = useKeyHold('Control')
31
+ * const shift = useKeyHold('Shift')
32
+ * const alt = useKeyHold('Alt')
33
+ *
34
+ * return (
35
+ * <div>
36
+ * <span style={{ opacity: ctrl ? 1 : 0.3 }}>Ctrl</span>
37
+ * <span style={{ opacity: shift ? 1 : 0.3 }}>Shift</span>
38
+ * <span style={{ opacity: alt ? 1 : 0.3 }}>Alt</span>
39
+ * </div>
40
+ * )
41
+ * }
42
+ * ```
43
+ */
44
+ declare function useKeyHold(key: HeldKey): boolean;
45
+ //#endregion
46
+ export { useKeyHold };
47
+ //# sourceMappingURL=useKeyHold.d.ts.map
@@ -0,0 +1,53 @@
1
+ import { getKeyStateTracker } from "@tanstack/hotkeys";
2
+ import { useStore } from "@tanstack/react-store";
3
+
4
+ //#region src/useKeyHold.ts
5
+ /**
6
+ * React hook that returns whether a specific key is currently being held.
7
+ *
8
+ * This hook uses `useStore` from `@tanstack/react-store` to subscribe
9
+ * to the global KeyStateTracker and uses a selector to determine if
10
+ * the specified key is held.
11
+ *
12
+ * @param key - The key to check (e.g., 'Shift', 'Control', 'A')
13
+ * @returns True if the key is currently held down
14
+ *
15
+ * @example
16
+ * ```tsx
17
+ * function ShiftIndicator() {
18
+ * const isShiftHeld = useKeyHold('Shift')
19
+ *
20
+ * return (
21
+ * <div style={{ opacity: isShiftHeld ? 1 : 0.5 }}>
22
+ * {isShiftHeld ? 'Shift is pressed!' : 'Press Shift'}
23
+ * </div>
24
+ * )
25
+ * }
26
+ * ```
27
+ *
28
+ * @example
29
+ * ```tsx
30
+ * function ModifierIndicators() {
31
+ * const ctrl = useKeyHold('Control')
32
+ * const shift = useKeyHold('Shift')
33
+ * const alt = useKeyHold('Alt')
34
+ *
35
+ * return (
36
+ * <div>
37
+ * <span style={{ opacity: ctrl ? 1 : 0.3 }}>Ctrl</span>
38
+ * <span style={{ opacity: shift ? 1 : 0.3 }}>Shift</span>
39
+ * <span style={{ opacity: alt ? 1 : 0.3 }}>Alt</span>
40
+ * </div>
41
+ * )
42
+ * }
43
+ * ```
44
+ */
45
+ function useKeyHold(key) {
46
+ const tracker = getKeyStateTracker();
47
+ const normalizedKey = key.toLowerCase();
48
+ return useStore(tracker.store, (state) => state.heldKeys.some((heldKey) => heldKey.toLowerCase() === normalizedKey));
49
+ }
50
+
51
+ //#endregion
52
+ export { useKeyHold };
53
+ //# sourceMappingURL=useKeyHold.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useKeyHold.js","names":[],"sources":["../src/useKeyHold.ts"],"sourcesContent":["import { useStore } from '@tanstack/react-store'\nimport { getKeyStateTracker } from '@tanstack/hotkeys'\nimport type { HeldKey } from '@tanstack/hotkeys'\n\n/**\n * React hook that returns whether a specific key is currently being held.\n *\n * This hook uses `useStore` from `@tanstack/react-store` to subscribe\n * to the global KeyStateTracker and uses a selector to determine if\n * the specified key is held.\n *\n * @param key - The key to check (e.g., 'Shift', 'Control', 'A')\n * @returns True if the key is currently held down\n *\n * @example\n * ```tsx\n * function ShiftIndicator() {\n * const isShiftHeld = useKeyHold('Shift')\n *\n * return (\n * <div style={{ opacity: isShiftHeld ? 1 : 0.5 }}>\n * {isShiftHeld ? 'Shift is pressed!' : 'Press Shift'}\n * </div>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * function ModifierIndicators() {\n * const ctrl = useKeyHold('Control')\n * const shift = useKeyHold('Shift')\n * const alt = useKeyHold('Alt')\n *\n * return (\n * <div>\n * <span style={{ opacity: ctrl ? 1 : 0.3 }}>Ctrl</span>\n * <span style={{ opacity: shift ? 1 : 0.3 }}>Shift</span>\n * <span style={{ opacity: alt ? 1 : 0.3 }}>Alt</span>\n * </div>\n * )\n * }\n * ```\n */\nexport function useKeyHold(key: HeldKey): boolean {\n const tracker = getKeyStateTracker()\n const normalizedKey = key.toLowerCase()\n\n return useStore(tracker.store, (state) =>\n state.heldKeys.some((heldKey) => heldKey.toLowerCase() === normalizedKey),\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CA,SAAgB,WAAW,KAAuB;CAChD,MAAM,UAAU,oBAAoB;CACpC,MAAM,gBAAgB,IAAI,aAAa;AAEvC,QAAO,SAAS,QAAQ,QAAQ,UAC9B,MAAM,SAAS,MAAM,YAAY,QAAQ,aAAa,KAAK,cAAc,CAC1E"}
package/package.json CHANGED
@@ -1,10 +1,69 @@
1
1
  {
2
2
  "name": "@tanstack/react-hotkeys",
3
- "version": "0.0.1",
4
- "description": "OIDC trusted publishing setup package for @tanstack/react-hotkeys",
3
+ "version": "0.0.3",
4
+ "description": "React adapter for TanStack Hotkeys",
5
+ "author": "Tanner Linsley",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/TanStack/hotkeys.git",
10
+ "directory": "packages/react-hotkeys"
11
+ },
12
+ "homepage": "https://tanstack.com/hotkeys",
13
+ "funding": {
14
+ "type": "github",
15
+ "url": "https://github.com/sponsors/tannerlinsley"
16
+ },
5
17
  "keywords": [
6
- "oidc",
7
- "trusted-publishing",
8
- "setup"
9
- ]
10
- }
18
+ "react",
19
+ "tanstack",
20
+ "keys"
21
+ ],
22
+ "type": "module",
23
+ "main": "./dist/index.cjs",
24
+ "module": "./dist/index.js",
25
+ "types": "./dist/index.d.cts",
26
+ "exports": {
27
+ ".": {
28
+ "import": "./dist/index.js",
29
+ "require": "./dist/index.cjs"
30
+ },
31
+ "./package.json": "./package.json"
32
+ },
33
+ "sideEffects": false,
34
+ "engines": {
35
+ "node": ">=18"
36
+ },
37
+ "files": [
38
+ "dist",
39
+ "src"
40
+ ],
41
+ "dependencies": {
42
+ "@tanstack/react-store": "^0.8.0",
43
+ "@tanstack/hotkeys": "0.0.2"
44
+ },
45
+ "devDependencies": {
46
+ "@eslint-react/eslint-plugin": "^2.12.2",
47
+ "@testing-library/react": "^16.3.2",
48
+ "@types/react": "^19.2.13",
49
+ "@vitejs/plugin-react": "^5.1.4",
50
+ "eslint-plugin-react-compiler": "19.1.0-rc.2",
51
+ "eslint-plugin-react-hooks": "^7.0.1",
52
+ "react": "^19.2.4",
53
+ "react-dom": "^19.2.4"
54
+ },
55
+ "peerDependencies": {
56
+ "react": ">=16.8",
57
+ "react-dom": ">=16.8"
58
+ },
59
+ "scripts": {
60
+ "clean": "premove ./build ./dist",
61
+ "lint": "eslint ./src",
62
+ "lint:fix": "eslint ./src --fix",
63
+ "test:eslint": "eslint ./src",
64
+ "test:lib": "vitest --passWithNoTests",
65
+ "test:lib:dev": "pnpm test:lib --watch",
66
+ "test:types": "tsc",
67
+ "build": "tsdown"
68
+ }
69
+ }
@@ -0,0 +1,51 @@
1
+ import React, { createContext, useContext, useMemo } from 'react'
2
+ import type { ReactNode } from 'react'
3
+ import type { HotkeyRecorderOptions } from '@tanstack/hotkeys'
4
+ import type { UseHotkeyOptions } from './useHotkey'
5
+ import type { UseHotkeySequenceOptions } from './useHotkeySequence'
6
+
7
+ export interface HotkeysProviderOptions {
8
+ hotkey?: Partial<UseHotkeyOptions>
9
+ hotkeyRecorder?: Partial<HotkeyRecorderOptions>
10
+ hotkeySequence?: Partial<UseHotkeySequenceOptions>
11
+ }
12
+
13
+ interface HotkeysContextValue {
14
+ defaultOptions: HotkeysProviderOptions
15
+ }
16
+
17
+ const HotkeysContext = createContext<HotkeysContextValue | null>(null)
18
+
19
+ export interface HotkeysProviderProps {
20
+ children: ReactNode
21
+ defaultOptions?: HotkeysProviderOptions
22
+ }
23
+
24
+ const DEFAULT_OPTIONS: HotkeysProviderOptions = {}
25
+
26
+ export function HotkeysProvider({
27
+ children,
28
+ defaultOptions = DEFAULT_OPTIONS,
29
+ }: HotkeysProviderProps) {
30
+ const contextValue: HotkeysContextValue = useMemo(
31
+ () => ({
32
+ defaultOptions,
33
+ }),
34
+ [defaultOptions],
35
+ )
36
+
37
+ return (
38
+ <HotkeysContext.Provider value={contextValue}>
39
+ {children}
40
+ </HotkeysContext.Provider>
41
+ )
42
+ }
43
+
44
+ export function useHotkeysContext() {
45
+ return useContext(HotkeysContext)
46
+ }
47
+
48
+ export function useDefaultHotkeysOptions() {
49
+ const context = useContext(HotkeysContext)
50
+ return context?.defaultOptions ?? {}
51
+ }