@tanstack/preact-hotkeys 0.3.0
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.
- package/README.md +122 -0
- package/dist/HotkeysProvider.cjs +26 -0
- package/dist/HotkeysProvider.cjs.map +1 -0
- package/dist/HotkeysProvider.d.cts +28 -0
- package/dist/HotkeysProvider.d.ts +28 -0
- package/dist/HotkeysProvider.js +24 -0
- package/dist/HotkeysProvider.js.map +1 -0
- package/dist/index.cjs +25 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +11 -0
- package/dist/useHeldKeyCodes.cjs +37 -0
- package/dist/useHeldKeyCodes.cjs.map +1 -0
- package/dist/useHeldKeyCodes.d.cts +31 -0
- package/dist/useHeldKeyCodes.d.ts +31 -0
- package/dist/useHeldKeyCodes.js +37 -0
- package/dist/useHeldKeyCodes.js.map +1 -0
- package/dist/useHeldKeys.cjs +33 -0
- package/dist/useHeldKeys.cjs.map +1 -0
- package/dist/useHeldKeys.d.cts +27 -0
- package/dist/useHeldKeys.d.ts +27 -0
- package/dist/useHeldKeys.js +33 -0
- package/dist/useHeldKeys.js.map +1 -0
- package/dist/useHotkey.cjs +118 -0
- package/dist/useHotkey.cjs.map +1 -0
- package/dist/useHotkey.d.cts +74 -0
- package/dist/useHotkey.d.ts +74 -0
- package/dist/useHotkey.js +118 -0
- package/dist/useHotkey.js.map +1 -0
- package/dist/useHotkeyRecorder.cjs +71 -0
- package/dist/useHotkeyRecorder.cjs.map +1 -0
- package/dist/useHotkeyRecorder.d.cts +57 -0
- package/dist/useHotkeyRecorder.d.ts +57 -0
- package/dist/useHotkeyRecorder.js +71 -0
- package/dist/useHotkeyRecorder.js.map +1 -0
- package/dist/useHotkeySequence.cjs +96 -0
- package/dist/useHotkeySequence.cjs.map +1 -0
- package/dist/useHotkeySequence.d.cts +48 -0
- package/dist/useHotkeySequence.d.ts +48 -0
- package/dist/useHotkeySequence.js +96 -0
- package/dist/useHotkeySequence.js.map +1 -0
- package/dist/useKeyHold.cjs +53 -0
- package/dist/useKeyHold.cjs.map +1 -0
- package/dist/useKeyHold.d.cts +47 -0
- package/dist/useKeyHold.d.ts +47 -0
- package/dist/useKeyHold.js +53 -0
- package/dist/useKeyHold.js.map +1 -0
- package/package.json +63 -0
- package/src/HotkeysProvider.tsx +52 -0
- package/src/index.ts +13 -0
- package/src/useHeldKeyCodes.ts +33 -0
- package/src/useHeldKeys.ts +29 -0
- package/src/useHotkey.ts +192 -0
- package/src/useHotkeyRecorder.ts +101 -0
- package/src/useHotkeySequence.ts +169 -0
- 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 "preact/hooks";
|
|
4
|
+
import { useStore } from "@tanstack/preact-store";
|
|
5
|
+
|
|
6
|
+
//#region src/useHotkeyRecorder.ts
|
|
7
|
+
/**
|
|
8
|
+
* Preact 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 'preact/hooks'\nimport { useStore } from '@tanstack/preact-store'\nimport { HotkeyRecorder } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type { Hotkey, HotkeyRecorderOptions } from '@tanstack/hotkeys'\n\nexport interface PreactHotkeyRecorder {\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 * Preact 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): PreactHotkeyRecorder {\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,SACsB;CACtB,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,96 @@
|
|
|
1
|
+
const require_HotkeysProvider = require('./HotkeysProvider.cjs');
|
|
2
|
+
let _tanstack_hotkeys = require("@tanstack/hotkeys");
|
|
3
|
+
let preact_hooks = require("preact/hooks");
|
|
4
|
+
|
|
5
|
+
//#region src/useHotkeySequence.ts
|
|
6
|
+
/**
|
|
7
|
+
* Preact 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 mergedOptions = {
|
|
40
|
+
...require_HotkeysProvider.useDefaultHotkeysOptions().hotkeySequence,
|
|
41
|
+
...options
|
|
42
|
+
};
|
|
43
|
+
const manager = (0, _tanstack_hotkeys.getSequenceManager)();
|
|
44
|
+
const registrationRef = (0, preact_hooks.useRef)(null);
|
|
45
|
+
const callbackRef = (0, preact_hooks.useRef)(callback);
|
|
46
|
+
const optionsRef = (0, preact_hooks.useRef)(mergedOptions);
|
|
47
|
+
const managerRef = (0, preact_hooks.useRef)(manager);
|
|
48
|
+
callbackRef.current = callback;
|
|
49
|
+
optionsRef.current = mergedOptions;
|
|
50
|
+
managerRef.current = manager;
|
|
51
|
+
const prevTargetRef = (0, preact_hooks.useRef)(null);
|
|
52
|
+
const prevSequenceRef = (0, preact_hooks.useRef)(null);
|
|
53
|
+
const hotkeySequenceString = (0, _tanstack_hotkeys.formatHotkeySequence)(sequence);
|
|
54
|
+
const { target: _target, ...optionsWithoutTarget } = mergedOptions;
|
|
55
|
+
(0, preact_hooks.useEffect)(() => {
|
|
56
|
+
if (sequence.length === 0) return;
|
|
57
|
+
const resolvedTarget = isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
|
|
58
|
+
if (!resolvedTarget) return;
|
|
59
|
+
const targetChanged = prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget;
|
|
60
|
+
const sequenceChanged = prevSequenceRef.current !== null && prevSequenceRef.current !== hotkeySequenceString;
|
|
61
|
+
if (registrationRef.current?.isActive && (targetChanged || sequenceChanged)) {
|
|
62
|
+
registrationRef.current.unregister();
|
|
63
|
+
registrationRef.current = null;
|
|
64
|
+
}
|
|
65
|
+
if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(sequence, (event, context) => callbackRef.current(event, context), {
|
|
66
|
+
...optionsRef.current,
|
|
67
|
+
target: resolvedTarget
|
|
68
|
+
});
|
|
69
|
+
prevTargetRef.current = resolvedTarget;
|
|
70
|
+
prevSequenceRef.current = hotkeySequenceString;
|
|
71
|
+
return () => {
|
|
72
|
+
if (registrationRef.current?.isActive) {
|
|
73
|
+
registrationRef.current.unregister();
|
|
74
|
+
registrationRef.current = null;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}, [
|
|
78
|
+
hotkeySequenceString,
|
|
79
|
+
mergedOptions.enabled,
|
|
80
|
+
sequence
|
|
81
|
+
]);
|
|
82
|
+
if (registrationRef.current?.isActive) {
|
|
83
|
+
registrationRef.current.callback = (event, context) => callbackRef.current(event, context);
|
|
84
|
+
registrationRef.current.setOptions(optionsWithoutTarget);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Type guard to check if a value is a Preact ref-like object.
|
|
89
|
+
*/
|
|
90
|
+
function isRef(value) {
|
|
91
|
+
return value !== null && typeof value === "object" && "current" in value;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
exports.useHotkeySequence = useHotkeySequence;
|
|
96
|
+
//# sourceMappingURL=useHotkeySequence.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useHotkeySequence.cjs","names":["useDefaultHotkeysOptions"],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type { RefObject } from 'preact'\nimport type {\n HotkeyCallback,\n HotkeyCallbackContext,\n HotkeySequence,\n SequenceOptions,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'target'\n> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a Preact ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * Preact 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 manager = getSequenceManager()\n\n // Stable ref for registration handle\n const registrationRef = useRef<SequenceRegistrationHandle | null>(null)\n\n // Refs to capture current values for use in effect without adding dependencies\n const callbackRef = useRef(callback)\n const optionsRef = useRef(mergedOptions)\n const managerRef = useRef(manager)\n\n // Update refs on every render\n callbackRef.current = callback\n optionsRef.current = mergedOptions\n managerRef.current = manager\n\n // Track previous target and sequence to detect changes requiring re-registration\n const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)\n const prevSequenceRef = useRef<string | null>(null)\n\n // Normalize to hotkey sequence string (join with spaces)\n const hotkeySequenceString = formatHotkeySequence(sequence)\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n useEffect(() => {\n if (sequence.length === 0) {\n return\n }\n\n // Resolve target inside the effect so refs are already attached after mount\n const resolvedTarget = isRef(optionsRef.current.target)\n ? optionsRef.current.target.current\n : (optionsRef.current.target ??\n (typeof document !== 'undefined' ? document : null))\n\n // Skip if no valid target (SSR or ref still null)\n if (!resolvedTarget) {\n return\n }\n\n // Check if we need to re-register (target or sequence changed)\n const targetChanged =\n prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget\n const sequenceChanged =\n prevSequenceRef.current !== null &&\n prevSequenceRef.current !== hotkeySequenceString\n\n // If we have an active registration and target/sequence changed, unregister first\n if (\n registrationRef.current?.isActive &&\n (targetChanged || sequenceChanged)\n ) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n\n // Register if needed (no active registration)\n if (!registrationRef.current || !registrationRef.current.isActive) {\n registrationRef.current = managerRef.current.register(\n sequence,\n (event, context) => callbackRef.current(event, context),\n {\n ...optionsRef.current,\n target: resolvedTarget,\n },\n )\n }\n\n // Update tracking refs\n prevTargetRef.current = resolvedTarget\n prevSequenceRef.current = hotkeySequenceString\n\n // Cleanup on unmount\n return () => {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n }\n }, [hotkeySequenceString, mergedOptions.enabled, sequence])\n\n // Sync callback and options on EVERY render (outside useEffect)\n if (registrationRef.current?.isActive) {\n registrationRef.current.callback = (\n event: KeyboardEvent,\n context: HotkeyCallbackContext,\n ) => callbackRef.current(event, context)\n registrationRef.current.setOptions(optionsWithoutTarget)\n }\n}\n\n/**\n * Type guard to check if a value is a Preact ref-like object.\n */\nfunction isRef(value: unknown): value is RefObject<HTMLElement | null> {\n return value !== null && typeof value === 'object' && 'current' in value\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6DA,SAAgB,kBACd,UACA,UACA,UAAoC,EAAE,EAChC;CACN,MAAM,gBAAgB;EACpB,GAAGA,kDAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,qDAA8B;CAGpC,MAAM,2CAA4D,KAAK;CAGvE,MAAM,uCAAqB,SAAS;CACpC,MAAM,sCAAoB,cAAc;CACxC,MAAM,sCAAoB,QAAQ;AAGlC,aAAY,UAAU;AACtB,YAAW,UAAU;AACrB,YAAW,UAAU;CAGrB,MAAM,yCAA+D,KAAK;CAC1E,MAAM,2CAAwC,KAAK;CAGnD,MAAM,mEAA4C,SAAS;CAG3D,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,mCAAgB;AACd,MAAI,SAAS,WAAW,EACtB;EAIF,MAAM,iBAAiB,MAAM,WAAW,QAAQ,OAAO,GACnD,WAAW,QAAQ,OAAO,UACzB,WAAW,QAAQ,WACnB,OAAO,aAAa,cAAc,WAAW;AAGlD,MAAI,CAAC,eACH;EAIF,MAAM,gBACJ,cAAc,YAAY,QAAQ,cAAc,YAAY;EAC9D,MAAM,kBACJ,gBAAgB,YAAY,QAC5B,gBAAgB,YAAY;AAG9B,MACE,gBAAgB,SAAS,aACxB,iBAAiB,kBAClB;AACA,mBAAgB,QAAQ,YAAY;AACpC,mBAAgB,UAAU;;AAI5B,MAAI,CAAC,gBAAgB,WAAW,CAAC,gBAAgB,QAAQ,SACvD,iBAAgB,UAAU,WAAW,QAAQ,SAC3C,WACC,OAAO,YAAY,YAAY,QAAQ,OAAO,QAAQ,EACvD;GACE,GAAG,WAAW;GACd,QAAQ;GACT,CACF;AAIH,gBAAc,UAAU;AACxB,kBAAgB,UAAU;AAG1B,eAAa;AACX,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;;IAG7B;EAAC;EAAsB,cAAc;EAAS;EAAS,CAAC;AAG3D,KAAI,gBAAgB,SAAS,UAAU;AACrC,kBAAgB,QAAQ,YACtB,OACA,YACG,YAAY,QAAQ,OAAO,QAAQ;AACxC,kBAAgB,QAAQ,WAAW,qBAAqB;;;;;;AAO5D,SAAS,MAAM,OAAwD;AACrE,QAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,aAAa"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { HotkeyCallback, HotkeySequence, SequenceOptions } from "@tanstack/hotkeys";
|
|
2
|
+
import { RefObject } from "preact";
|
|
3
|
+
|
|
4
|
+
//#region src/useHotkeySequence.d.ts
|
|
5
|
+
interface UseHotkeySequenceOptions extends Omit<SequenceOptions, 'target'> {
|
|
6
|
+
/**
|
|
7
|
+
* The DOM element to attach the event listener to.
|
|
8
|
+
* Can be a Preact ref, direct DOM element, or null.
|
|
9
|
+
* Defaults to document.
|
|
10
|
+
*/
|
|
11
|
+
target?: RefObject<HTMLElement | null> | HTMLElement | Document | Window | null;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Preact hook for registering a keyboard shortcut sequence (Vim-style).
|
|
15
|
+
*
|
|
16
|
+
* This hook allows you to register multi-key sequences like 'g g' or 'd d'
|
|
17
|
+
* that trigger when the full sequence is pressed within a timeout.
|
|
18
|
+
*
|
|
19
|
+
* @param sequence - Array of hotkey strings that form the sequence
|
|
20
|
+
* @param callback - Function to call when the sequence is completed
|
|
21
|
+
* @param options - Options for the sequence behavior
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```tsx
|
|
25
|
+
* function VimEditor() {
|
|
26
|
+
* // 'g g' to go to top
|
|
27
|
+
* useHotkeySequence(['G', 'G'], () => {
|
|
28
|
+
* scrollToTop()
|
|
29
|
+
* })
|
|
30
|
+
*
|
|
31
|
+
* // 'd d' to delete line
|
|
32
|
+
* useHotkeySequence(['D', 'D'], () => {
|
|
33
|
+
* deleteLine()
|
|
34
|
+
* })
|
|
35
|
+
*
|
|
36
|
+
* // 'd i w' to delete inner word
|
|
37
|
+
* useHotkeySequence(['D', 'I', 'W'], () => {
|
|
38
|
+
* deleteInnerWord()
|
|
39
|
+
* }, { timeout: 500 })
|
|
40
|
+
*
|
|
41
|
+
* return <div>...</div>
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
declare function useHotkeySequence(sequence: HotkeySequence, callback: HotkeyCallback, options?: UseHotkeySequenceOptions): void;
|
|
46
|
+
//#endregion
|
|
47
|
+
export { UseHotkeySequenceOptions, useHotkeySequence };
|
|
48
|
+
//# sourceMappingURL=useHotkeySequence.d.cts.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { HotkeyCallback, HotkeySequence, SequenceOptions } from "@tanstack/hotkeys";
|
|
2
|
+
import { RefObject } from "preact";
|
|
3
|
+
|
|
4
|
+
//#region src/useHotkeySequence.d.ts
|
|
5
|
+
interface UseHotkeySequenceOptions extends Omit<SequenceOptions, 'target'> {
|
|
6
|
+
/**
|
|
7
|
+
* The DOM element to attach the event listener to.
|
|
8
|
+
* Can be a Preact ref, direct DOM element, or null.
|
|
9
|
+
* Defaults to document.
|
|
10
|
+
*/
|
|
11
|
+
target?: RefObject<HTMLElement | null> | HTMLElement | Document | Window | null;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Preact hook for registering a keyboard shortcut sequence (Vim-style).
|
|
15
|
+
*
|
|
16
|
+
* This hook allows you to register multi-key sequences like 'g g' or 'd d'
|
|
17
|
+
* that trigger when the full sequence is pressed within a timeout.
|
|
18
|
+
*
|
|
19
|
+
* @param sequence - Array of hotkey strings that form the sequence
|
|
20
|
+
* @param callback - Function to call when the sequence is completed
|
|
21
|
+
* @param options - Options for the sequence behavior
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```tsx
|
|
25
|
+
* function VimEditor() {
|
|
26
|
+
* // 'g g' to go to top
|
|
27
|
+
* useHotkeySequence(['G', 'G'], () => {
|
|
28
|
+
* scrollToTop()
|
|
29
|
+
* })
|
|
30
|
+
*
|
|
31
|
+
* // 'd d' to delete line
|
|
32
|
+
* useHotkeySequence(['D', 'D'], () => {
|
|
33
|
+
* deleteLine()
|
|
34
|
+
* })
|
|
35
|
+
*
|
|
36
|
+
* // 'd i w' to delete inner word
|
|
37
|
+
* useHotkeySequence(['D', 'I', 'W'], () => {
|
|
38
|
+
* deleteInnerWord()
|
|
39
|
+
* }, { timeout: 500 })
|
|
40
|
+
*
|
|
41
|
+
* return <div>...</div>
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
declare function useHotkeySequence(sequence: HotkeySequence, callback: HotkeyCallback, options?: UseHotkeySequenceOptions): void;
|
|
46
|
+
//#endregion
|
|
47
|
+
export { UseHotkeySequenceOptions, useHotkeySequence };
|
|
48
|
+
//# sourceMappingURL=useHotkeySequence.d.ts.map
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
|
|
2
|
+
import { formatHotkeySequence, getSequenceManager } from "@tanstack/hotkeys";
|
|
3
|
+
import { useEffect, useRef } from "preact/hooks";
|
|
4
|
+
|
|
5
|
+
//#region src/useHotkeySequence.ts
|
|
6
|
+
/**
|
|
7
|
+
* Preact 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 mergedOptions = {
|
|
40
|
+
...useDefaultHotkeysOptions().hotkeySequence,
|
|
41
|
+
...options
|
|
42
|
+
};
|
|
43
|
+
const manager = getSequenceManager();
|
|
44
|
+
const registrationRef = useRef(null);
|
|
45
|
+
const callbackRef = useRef(callback);
|
|
46
|
+
const optionsRef = useRef(mergedOptions);
|
|
47
|
+
const managerRef = useRef(manager);
|
|
48
|
+
callbackRef.current = callback;
|
|
49
|
+
optionsRef.current = mergedOptions;
|
|
50
|
+
managerRef.current = manager;
|
|
51
|
+
const prevTargetRef = useRef(null);
|
|
52
|
+
const prevSequenceRef = useRef(null);
|
|
53
|
+
const hotkeySequenceString = formatHotkeySequence(sequence);
|
|
54
|
+
const { target: _target, ...optionsWithoutTarget } = mergedOptions;
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
if (sequence.length === 0) return;
|
|
57
|
+
const resolvedTarget = isRef(optionsRef.current.target) ? optionsRef.current.target.current : optionsRef.current.target ?? (typeof document !== "undefined" ? document : null);
|
|
58
|
+
if (!resolvedTarget) return;
|
|
59
|
+
const targetChanged = prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget;
|
|
60
|
+
const sequenceChanged = prevSequenceRef.current !== null && prevSequenceRef.current !== hotkeySequenceString;
|
|
61
|
+
if (registrationRef.current?.isActive && (targetChanged || sequenceChanged)) {
|
|
62
|
+
registrationRef.current.unregister();
|
|
63
|
+
registrationRef.current = null;
|
|
64
|
+
}
|
|
65
|
+
if (!registrationRef.current || !registrationRef.current.isActive) registrationRef.current = managerRef.current.register(sequence, (event, context) => callbackRef.current(event, context), {
|
|
66
|
+
...optionsRef.current,
|
|
67
|
+
target: resolvedTarget
|
|
68
|
+
});
|
|
69
|
+
prevTargetRef.current = resolvedTarget;
|
|
70
|
+
prevSequenceRef.current = hotkeySequenceString;
|
|
71
|
+
return () => {
|
|
72
|
+
if (registrationRef.current?.isActive) {
|
|
73
|
+
registrationRef.current.unregister();
|
|
74
|
+
registrationRef.current = null;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}, [
|
|
78
|
+
hotkeySequenceString,
|
|
79
|
+
mergedOptions.enabled,
|
|
80
|
+
sequence
|
|
81
|
+
]);
|
|
82
|
+
if (registrationRef.current?.isActive) {
|
|
83
|
+
registrationRef.current.callback = (event, context) => callbackRef.current(event, context);
|
|
84
|
+
registrationRef.current.setOptions(optionsWithoutTarget);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Type guard to check if a value is a Preact ref-like object.
|
|
89
|
+
*/
|
|
90
|
+
function isRef(value) {
|
|
91
|
+
return value !== null && typeof value === "object" && "current" in value;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
export { useHotkeySequence };
|
|
96
|
+
//# sourceMappingURL=useHotkeySequence.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useHotkeySequence.js","names":[],"sources":["../src/useHotkeySequence.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport { formatHotkeySequence, getSequenceManager } from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport type { RefObject } from 'preact'\nimport type {\n HotkeyCallback,\n HotkeyCallbackContext,\n HotkeySequence,\n SequenceOptions,\n SequenceRegistrationHandle,\n} from '@tanstack/hotkeys'\n\nexport interface UseHotkeySequenceOptions extends Omit<\n SequenceOptions,\n 'target'\n> {\n /**\n * The DOM element to attach the event listener to.\n * Can be a Preact ref, direct DOM element, or null.\n * Defaults to document.\n */\n target?:\n | RefObject<HTMLElement | null>\n | HTMLElement\n | Document\n | Window\n | null\n}\n\n/**\n * Preact 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 manager = getSequenceManager()\n\n // Stable ref for registration handle\n const registrationRef = useRef<SequenceRegistrationHandle | null>(null)\n\n // Refs to capture current values for use in effect without adding dependencies\n const callbackRef = useRef(callback)\n const optionsRef = useRef(mergedOptions)\n const managerRef = useRef(manager)\n\n // Update refs on every render\n callbackRef.current = callback\n optionsRef.current = mergedOptions\n managerRef.current = manager\n\n // Track previous target and sequence to detect changes requiring re-registration\n const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)\n const prevSequenceRef = useRef<string | null>(null)\n\n // Normalize to hotkey sequence string (join with spaces)\n const hotkeySequenceString = formatHotkeySequence(sequence)\n\n // Extract options without target (target is handled separately)\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n\n useEffect(() => {\n if (sequence.length === 0) {\n return\n }\n\n // Resolve target inside the effect so refs are already attached after mount\n const resolvedTarget = isRef(optionsRef.current.target)\n ? optionsRef.current.target.current\n : (optionsRef.current.target ??\n (typeof document !== 'undefined' ? document : null))\n\n // Skip if no valid target (SSR or ref still null)\n if (!resolvedTarget) {\n return\n }\n\n // Check if we need to re-register (target or sequence changed)\n const targetChanged =\n prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget\n const sequenceChanged =\n prevSequenceRef.current !== null &&\n prevSequenceRef.current !== hotkeySequenceString\n\n // If we have an active registration and target/sequence changed, unregister first\n if (\n registrationRef.current?.isActive &&\n (targetChanged || sequenceChanged)\n ) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n\n // Register if needed (no active registration)\n if (!registrationRef.current || !registrationRef.current.isActive) {\n registrationRef.current = managerRef.current.register(\n sequence,\n (event, context) => callbackRef.current(event, context),\n {\n ...optionsRef.current,\n target: resolvedTarget,\n },\n )\n }\n\n // Update tracking refs\n prevTargetRef.current = resolvedTarget\n prevSequenceRef.current = hotkeySequenceString\n\n // Cleanup on unmount\n return () => {\n if (registrationRef.current?.isActive) {\n registrationRef.current.unregister()\n registrationRef.current = null\n }\n }\n }, [hotkeySequenceString, mergedOptions.enabled, sequence])\n\n // Sync callback and options on EVERY render (outside useEffect)\n if (registrationRef.current?.isActive) {\n registrationRef.current.callback = (\n event: KeyboardEvent,\n context: HotkeyCallbackContext,\n ) => callbackRef.current(event, context)\n registrationRef.current.setOptions(optionsWithoutTarget)\n }\n}\n\n/**\n * Type guard to check if a value is a Preact ref-like object.\n */\nfunction isRef(value: unknown): value is RefObject<HTMLElement | null> {\n return value !== null && typeof value === 'object' && 'current' in value\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6DA,SAAgB,kBACd,UACA,UACA,UAAoC,EAAE,EAChC;CACN,MAAM,gBAAgB;EACpB,GAAG,0BAA0B,CAAC;EAC9B,GAAG;EACJ;CAED,MAAM,UAAU,oBAAoB;CAGpC,MAAM,kBAAkB,OAA0C,KAAK;CAGvE,MAAM,cAAc,OAAO,SAAS;CACpC,MAAM,aAAa,OAAO,cAAc;CACxC,MAAM,aAAa,OAAO,QAAQ;AAGlC,aAAY,UAAU;AACtB,YAAW,UAAU;AACrB,YAAW,UAAU;CAGrB,MAAM,gBAAgB,OAA+C,KAAK;CAC1E,MAAM,kBAAkB,OAAsB,KAAK;CAGnD,MAAM,uBAAuB,qBAAqB,SAAS;CAG3D,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AAErD,iBAAgB;AACd,MAAI,SAAS,WAAW,EACtB;EAIF,MAAM,iBAAiB,MAAM,WAAW,QAAQ,OAAO,GACnD,WAAW,QAAQ,OAAO,UACzB,WAAW,QAAQ,WACnB,OAAO,aAAa,cAAc,WAAW;AAGlD,MAAI,CAAC,eACH;EAIF,MAAM,gBACJ,cAAc,YAAY,QAAQ,cAAc,YAAY;EAC9D,MAAM,kBACJ,gBAAgB,YAAY,QAC5B,gBAAgB,YAAY;AAG9B,MACE,gBAAgB,SAAS,aACxB,iBAAiB,kBAClB;AACA,mBAAgB,QAAQ,YAAY;AACpC,mBAAgB,UAAU;;AAI5B,MAAI,CAAC,gBAAgB,WAAW,CAAC,gBAAgB,QAAQ,SACvD,iBAAgB,UAAU,WAAW,QAAQ,SAC3C,WACC,OAAO,YAAY,YAAY,QAAQ,OAAO,QAAQ,EACvD;GACE,GAAG,WAAW;GACd,QAAQ;GACT,CACF;AAIH,gBAAc,UAAU;AACxB,kBAAgB,UAAU;AAG1B,eAAa;AACX,OAAI,gBAAgB,SAAS,UAAU;AACrC,oBAAgB,QAAQ,YAAY;AACpC,oBAAgB,UAAU;;;IAG7B;EAAC;EAAsB,cAAc;EAAS;EAAS,CAAC;AAG3D,KAAI,gBAAgB,SAAS,UAAU;AACrC,kBAAgB,QAAQ,YACtB,OACA,YACG,YAAY,QAAQ,OAAO,QAAQ;AACxC,kBAAgB,QAAQ,WAAW,qBAAqB;;;;;;AAO5D,SAAS,MAAM,OAAwD;AACrE,QAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,aAAa"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
let _tanstack_hotkeys = require("@tanstack/hotkeys");
|
|
2
|
+
let _tanstack_preact_store = require("@tanstack/preact-store");
|
|
3
|
+
|
|
4
|
+
//#region src/useKeyHold.ts
|
|
5
|
+
/**
|
|
6
|
+
* Preact hook that returns whether a specific key is currently being held.
|
|
7
|
+
*
|
|
8
|
+
* This hook uses `useStore` from `@tanstack/preact-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 = (0, _tanstack_hotkeys.getKeyStateTracker)();
|
|
47
|
+
const normalizedKey = key.toLowerCase();
|
|
48
|
+
return (0, _tanstack_preact_store.useStore)(tracker.store, (state) => state.heldKeys.some((heldKey) => heldKey.toLowerCase() === normalizedKey));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
exports.useKeyHold = useKeyHold;
|
|
53
|
+
//# sourceMappingURL=useKeyHold.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useKeyHold.cjs","names":[],"sources":["../src/useKeyHold.ts"],"sourcesContent":["import { useStore } from '@tanstack/preact-store'\nimport { getKeyStateTracker } from '@tanstack/hotkeys'\nimport type { HeldKey } from '@tanstack/hotkeys'\n\n/**\n * Preact hook that returns whether a specific key is currently being held.\n *\n * This hook uses `useStore` from `@tanstack/preact-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,6CAAgB,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
|
+
* Preact hook that returns whether a specific key is currently being held.
|
|
6
|
+
*
|
|
7
|
+
* This hook uses `useStore` from `@tanstack/preact-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
|
+
* Preact hook that returns whether a specific key is currently being held.
|
|
6
|
+
*
|
|
7
|
+
* This hook uses `useStore` from `@tanstack/preact-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/preact-store";
|
|
3
|
+
|
|
4
|
+
//#region src/useKeyHold.ts
|
|
5
|
+
/**
|
|
6
|
+
* Preact hook that returns whether a specific key is currently being held.
|
|
7
|
+
*
|
|
8
|
+
* This hook uses `useStore` from `@tanstack/preact-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/preact-store'\nimport { getKeyStateTracker } from '@tanstack/hotkeys'\nimport type { HeldKey } from '@tanstack/hotkeys'\n\n/**\n * Preact hook that returns whether a specific key is currently being held.\n *\n * This hook uses `useStore` from `@tanstack/preact-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"}
|