@tanstack/preact-hotkeys 0.11.0 → 0.12.1
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/dist/HotkeysProvider.d.ts +6 -13
- package/dist/HotkeysProvider.js +1 -2
- package/dist/useHeldKeyCodes.d.ts +2 -4
- package/dist/useHeldKeyCodes.js +3 -3
- package/dist/useHeldKeys.d.ts +2 -4
- package/dist/useHeldKeys.js +3 -3
- package/dist/useHotkey.d.ts +3 -6
- package/dist/useHotkey.js +3 -3
- package/dist/useHotkeyHint.d.ts +2 -5
- package/dist/useHotkeyHint.js +1 -2
- package/dist/useHotkeyRecorder.d.ts +3 -6
- package/dist/useHotkeyRecorder.js +1 -2
- package/dist/useHotkeyRegistrations.d.ts +3 -6
- package/dist/useHotkeyRegistrations.js +1 -2
- package/dist/useHotkeySequence.d.ts +3 -6
- package/dist/useHotkeySequence.js +1 -2
- package/dist/useHotkeySequenceRecorder.d.ts +3 -6
- package/dist/useHotkeySequenceRecorder.js +1 -2
- package/dist/useHotkeySequences.d.ts +3 -6
- package/dist/useHotkeySequences.js +1 -2
- package/dist/useHotkeys.d.ts +3 -6
- package/dist/useHotkeys.js +1 -2
- package/dist/useKeyHold.d.ts +2 -5
- package/dist/useKeyHold.js +1 -2
- package/dist/utils.js +1 -2
- package/package.json +11 -16
- package/dist/HotkeysProvider.cjs +0 -26
- package/dist/HotkeysProvider.cjs.map +0 -1
- package/dist/HotkeysProvider.d.cts +0 -29
- package/dist/HotkeysProvider.js.map +0 -1
- package/dist/index.cjs +0 -35
- package/dist/index.d.cts +0 -14
- package/dist/useHeldKeyCodes.cjs +0 -37
- package/dist/useHeldKeyCodes.cjs.map +0 -1
- package/dist/useHeldKeyCodes.d.cts +0 -31
- package/dist/useHeldKeyCodes.js.map +0 -1
- package/dist/useHeldKeys.cjs +0 -33
- package/dist/useHeldKeys.cjs.map +0 -1
- package/dist/useHeldKeys.d.cts +0 -27
- package/dist/useHeldKeys.js.map +0 -1
- package/dist/useHotkey.cjs +0 -125
- package/dist/useHotkey.cjs.map +0 -1
- package/dist/useHotkey.d.cts +0 -75
- package/dist/useHotkey.js.map +0 -1
- package/dist/useHotkeyHint.cjs +0 -18
- package/dist/useHotkeyHint.cjs.map +0 -1
- package/dist/useHotkeyHint.d.cts +0 -14
- package/dist/useHotkeyHint.js.map +0 -1
- package/dist/useHotkeyRecorder.cjs +0 -71
- package/dist/useHotkeyRecorder.cjs.map +0 -1
- package/dist/useHotkeyRecorder.d.cts +0 -57
- package/dist/useHotkeyRecorder.js.map +0 -1
- package/dist/useHotkeyRegistrations.cjs +0 -29
- package/dist/useHotkeyRegistrations.cjs.map +0 -1
- package/dist/useHotkeyRegistrations.d.cts +0 -26
- package/dist/useHotkeyRegistrations.js.map +0 -1
- package/dist/useHotkeySequence.cjs +0 -120
- package/dist/useHotkeySequence.cjs.map +0 -1
- package/dist/useHotkeySequence.d.cts +0 -59
- package/dist/useHotkeySequence.js.map +0 -1
- package/dist/useHotkeySequenceRecorder.cjs +0 -39
- package/dist/useHotkeySequenceRecorder.cjs.map +0 -1
- package/dist/useHotkeySequenceRecorder.d.cts +0 -19
- package/dist/useHotkeySequenceRecorder.js.map +0 -1
- package/dist/useHotkeySequences.cjs +0 -126
- package/dist/useHotkeySequences.cjs.map +0 -1
- package/dist/useHotkeySequences.d.cts +0 -63
- package/dist/useHotkeySequences.js.map +0 -1
- package/dist/useHotkeys.cjs +0 -124
- package/dist/useHotkeys.cjs.map +0 -1
- package/dist/useHotkeys.d.cts +0 -63
- package/dist/useHotkeys.js.map +0 -1
- package/dist/useKeyHold.cjs +0 -53
- package/dist/useKeyHold.cjs.map +0 -1
- package/dist/useKeyHold.d.cts +0 -47
- package/dist/useKeyHold.js.map +0 -1
- package/dist/utils.cjs +0 -12
- package/dist/utils.cjs.map +0 -1
- package/dist/utils.js.map +0 -1
- package/src/HotkeysProvider.tsx +0 -56
- package/src/index.ts +0 -18
- package/src/useHeldKeyCodes.ts +0 -33
- package/src/useHeldKeys.ts +0 -29
- package/src/useHotkey.ts +0 -191
- package/src/useHotkeyHint.ts +0 -19
- package/src/useHotkeyRecorder.ts +0 -101
- package/src/useHotkeyRegistrations.ts +0 -51
- package/src/useHotkeySequence.ts +0 -192
- package/src/useHotkeySequenceRecorder.ts +0 -64
- package/src/useHotkeySequences.ts +0 -192
- package/src/useHotkeys.ts +0 -193
- package/src/useKeyHold.ts +0 -52
- package/src/utils.ts +0 -8
package/dist/useHotkeys.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useHotkeys.cjs","names":["useDefaultHotkeysOptions","isRef"],"sources":["../src/useHotkeys.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport {\n detectPlatform,\n getHotkeyManager,\n normalizeRegisterableHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { UseHotkeyOptions } from './useHotkey'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\n/**\n * A single hotkey definition for use with `useHotkeys`.\n */\nexport interface UseHotkeyDefinition {\n /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */\n hotkey: RegisterableHotkey\n /** The function to call when the hotkey is pressed */\n callback: HotkeyCallback\n /** Per-hotkey options (merged on top of commonOptions) */\n options?: UseHotkeyOptions\n}\n\n/**\n * Preact hook for registering multiple keyboard hotkeys at once.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * Accepts a dynamic array of hotkey definitions, making it safe to use\n * with variable-length lists without violating the rules of hooks.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * Callbacks and options are synced on every render to avoid stale closures.\n *\n * @param hotkeys - Array of hotkey definitions to register\n * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options).\n * Per-row `enabled: false` still registers that hotkey: `HotkeyManager` suppresses execution only (the row\n * stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle\n * via `setOptions` (no unregister/re-register churn).\n *\n * @example\n * ```tsx\n * function Editor() {\n * useHotkeys([\n * { hotkey: 'Mod+S', callback: () => save() },\n * { hotkey: 'Mod+Z', callback: () => undo() },\n * { hotkey: 'Escape', callback: () => close() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function MenuShortcuts({ items }) {\n * // Dynamic hotkeys from data -- safe because it's a single hook call\n * useHotkeys(\n * items.map((item) => ({\n * hotkey: item.shortcut,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function useHotkeys(\n hotkeys: Array<UseHotkeyDefinition>,\n commonOptions: UseHotkeyOptions = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions().hotkey\n const manager = getHotkeyManager()\n const platform =\n commonOptions.platform ?? defaultOptions?.platform ?? detectPlatform()\n\n const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())\n const hotkeysRef = useRef(hotkeys)\n const hotkeyStringsRef = useRef<Array<Hotkey>>([])\n const commonOptionsRef = useRef(commonOptions)\n const defaultOptionsRef = useRef(defaultOptions)\n const managerRef = useRef(manager)\n\n const hotkeyStrings = hotkeys.map((def) =>\n normalizeRegisterableHotkey(def.hotkey, platform),\n )\n\n hotkeysRef.current = hotkeys\n hotkeyStringsRef.current = hotkeyStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n useEffect(() => {\n const prevRegistrations = registrationsRef.current\n const nextRegistrations = new Map<string, RegistrationRecord>()\n\n const rows: Array<{\n registrationKey: string\n def: (typeof hotkeysRef.current)[number]\n hotkeyStr: Hotkey\n mergedOptions: UseHotkeyOptions\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < hotkeysRef.current.length; i++) {\n const def = hotkeysRef.current[i]!\n const hotkeyStr = hotkeyStringsRef.current[i]!\n const mergedOptions = {\n ...defaultOptionsRef.current,\n ...commonOptionsRef.current,\n ...def.options,\n } as UseHotkeyOptions\n\n const resolvedTarget = isRef(mergedOptions.target)\n ? mergedOptions.target.current\n : (mergedOptions.target ??\n (typeof document !== 'undefined' ? document : null))\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${hotkeyStr}`\n rows.push({\n registrationKey,\n def,\n hotkeyStr,\n mergedOptions,\n resolvedTarget,\n })\n }\n\n const nextKeys = new Set(rows.map((r) => r.registrationKey))\n\n for (const [key, record] of prevRegistrations) {\n if (!nextKeys.has(key) && record.handle.isActive) {\n record.handle.unregister()\n }\n }\n\n for (const row of rows) {\n const { registrationKey, def, hotkeyStr, mergedOptions, resolvedTarget } =\n row\n\n const existing = prevRegistrations.get(registrationKey)\n if (existing?.handle.isActive && existing.target === resolvedTarget) {\n // Publish callback/options only after commit, never during render.\n existing.handle.callback = def.callback\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n existing.handle.setOptions(optionsWithoutTarget)\n nextRegistrations.set(registrationKey, existing)\n continue\n }\n\n if (existing?.handle.isActive) {\n existing.handle.unregister()\n }\n\n const handle = managerRef.current.register(hotkeyStr, def.callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n nextRegistrations.set(registrationKey, {\n handle,\n target: resolvedTarget,\n })\n }\n\n registrationsRef.current = nextRegistrations\n })\n\n useEffect(() => {\n return () => {\n for (const { handle } of registrationsRef.current.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrationsRef.current = new Map()\n }\n }, [])\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA,SAAgB,WACd,SACA,gBAAkC,EAAE,EAC9B;CAMN,MAAM,iBAAiBA,kDAA0B,CAAC;CAClD,MAAM,mDAA4B;CAClC,MAAM,WACJ,cAAc,YAAY,gBAAgB,mDAA4B;CAExE,MAAM,4DAA2D,IAAI,KAAK,CAAC;CAC3E,MAAM,sCAAoB,QAAQ;CAClC,MAAM,4CAAyC,EAAE,CAAC;CAClD,MAAM,4CAA0B,cAAc;CAC9C,MAAM,6CAA2B,eAAe;CAChD,MAAM,sCAAoB,QAAQ;CAElC,MAAM,gBAAgB,QAAQ,KAAK,2DACL,IAAI,QAAQ,SAAS,CAClD;AAED,YAAW,UAAU;AACrB,kBAAiB,UAAU;AAC3B,kBAAiB,UAAU;AAC3B,mBAAkB,UAAU;AAC5B,YAAW,UAAU;AAErB,mCAAgB;EACd,MAAM,oBAAoB,iBAAiB;EAC3C,MAAM,oCAAoB,IAAI,KAAiC;EAE/D,MAAM,OAMD,EAAE;AAEP,OAAK,IAAI,IAAI,GAAG,IAAI,WAAW,QAAQ,QAAQ,KAAK;GAClD,MAAM,MAAM,WAAW,QAAQ;GAC/B,MAAM,YAAY,iBAAiB,QAAQ;GAC3C,MAAM,gBAAgB;IACpB,GAAG,kBAAkB;IACrB,GAAG,iBAAiB;IACpB,GAAG,IAAI;IACR;GAED,MAAM,iBAAiBC,oBAAM,cAAc,OAAO,GAC9C,cAAc,OAAO,UACpB,cAAc,WACd,OAAO,aAAa,cAAc,WAAW;AAElD,OAAI,CAAC,eACH;GAGF,MAAM,kBAAkB,GAAG,EAAE,GAAG;AAChC,QAAK,KAAK;IACR;IACA;IACA;IACA;IACA;IACD,CAAC;;EAGJ,MAAM,WAAW,IAAI,IAAI,KAAK,KAAK,MAAM,EAAE,gBAAgB,CAAC;AAE5D,OAAK,MAAM,CAAC,KAAK,WAAW,kBAC1B,KAAI,CAAC,SAAS,IAAI,IAAI,IAAI,OAAO,OAAO,SACtC,QAAO,OAAO,YAAY;AAI9B,OAAK,MAAM,OAAO,MAAM;GACtB,MAAM,EAAE,iBAAiB,KAAK,WAAW,eAAe,mBACtD;GAEF,MAAM,WAAW,kBAAkB,IAAI,gBAAgB;AACvD,OAAI,UAAU,OAAO,YAAY,SAAS,WAAW,gBAAgB;AAEnE,aAAS,OAAO,WAAW,IAAI;IAC/B,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AACrD,aAAS,OAAO,WAAW,qBAAqB;AAChD,sBAAkB,IAAI,iBAAiB,SAAS;AAChD;;AAGF,OAAI,UAAU,OAAO,SACnB,UAAS,OAAO,YAAY;GAG9B,MAAM,SAAS,WAAW,QAAQ,SAAS,WAAW,IAAI,UAAU;IAClE,GAAG;IACH,QAAQ;IACT,CAAC;AACF,qBAAkB,IAAI,iBAAiB;IACrC;IACA,QAAQ;IACT,CAAC;;AAGJ,mBAAiB,UAAU;GAC3B;AAEF,mCAAgB;AACd,eAAa;AACX,QAAK,MAAM,EAAE,YAAY,iBAAiB,QAAQ,QAAQ,CACxD,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,oBAAiB,0BAAU,IAAI,KAAK;;IAErC,EAAE,CAAC"}
|
package/dist/useHotkeys.d.cts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { UseHotkeyOptions } from "./useHotkey.cjs";
|
|
2
|
-
import { HotkeyCallback, RegisterableHotkey } from "@tanstack/hotkeys";
|
|
3
|
-
|
|
4
|
-
//#region src/useHotkeys.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* A single hotkey definition for use with `useHotkeys`.
|
|
7
|
-
*/
|
|
8
|
-
interface UseHotkeyDefinition {
|
|
9
|
-
/** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */
|
|
10
|
-
hotkey: RegisterableHotkey;
|
|
11
|
-
/** The function to call when the hotkey is pressed */
|
|
12
|
-
callback: HotkeyCallback;
|
|
13
|
-
/** Per-hotkey options (merged on top of commonOptions) */
|
|
14
|
-
options?: UseHotkeyOptions;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Preact hook for registering multiple keyboard hotkeys at once.
|
|
18
|
-
*
|
|
19
|
-
* Uses the singleton HotkeyManager for efficient event handling.
|
|
20
|
-
* Accepts a dynamic array of hotkey definitions, making it safe to use
|
|
21
|
-
* with variable-length lists without violating the rules of hooks.
|
|
22
|
-
*
|
|
23
|
-
* Options are merged in this order:
|
|
24
|
-
* HotkeysProvider defaults < commonOptions < per-definition options
|
|
25
|
-
*
|
|
26
|
-
* Callbacks and options are synced on every render to avoid stale closures.
|
|
27
|
-
*
|
|
28
|
-
* @param hotkeys - Array of hotkey definitions to register
|
|
29
|
-
* @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options).
|
|
30
|
-
* Per-row `enabled: false` still registers that hotkey: `HotkeyManager` suppresses execution only (the row
|
|
31
|
-
* stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle
|
|
32
|
-
* via `setOptions` (no unregister/re-register churn).
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```tsx
|
|
36
|
-
* function Editor() {
|
|
37
|
-
* useHotkeys([
|
|
38
|
-
* { hotkey: 'Mod+S', callback: () => save() },
|
|
39
|
-
* { hotkey: 'Mod+Z', callback: () => undo() },
|
|
40
|
-
* { hotkey: 'Escape', callback: () => close() },
|
|
41
|
-
* ])
|
|
42
|
-
* }
|
|
43
|
-
* ```
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* ```tsx
|
|
47
|
-
* function MenuShortcuts({ items }) {
|
|
48
|
-
* // Dynamic hotkeys from data -- safe because it's a single hook call
|
|
49
|
-
* useHotkeys(
|
|
50
|
-
* items.map((item) => ({
|
|
51
|
-
* hotkey: item.shortcut,
|
|
52
|
-
* callback: item.action,
|
|
53
|
-
* options: { enabled: item.enabled },
|
|
54
|
-
* })),
|
|
55
|
-
* { preventDefault: true },
|
|
56
|
-
* )
|
|
57
|
-
* }
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
declare function useHotkeys(hotkeys: Array<UseHotkeyDefinition>, commonOptions?: UseHotkeyOptions): void;
|
|
61
|
-
//#endregion
|
|
62
|
-
export { UseHotkeyDefinition, useHotkeys };
|
|
63
|
-
//# sourceMappingURL=useHotkeys.d.cts.map
|
package/dist/useHotkeys.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useHotkeys.js","names":[],"sources":["../src/useHotkeys.ts"],"sourcesContent":["import { useEffect, useRef } from 'preact/hooks'\nimport {\n detectPlatform,\n getHotkeyManager,\n normalizeRegisterableHotkey,\n} from '@tanstack/hotkeys'\nimport { useDefaultHotkeysOptions } from './HotkeysProvider'\nimport { isRef } from './utils'\nimport type { UseHotkeyOptions } from './useHotkey'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyRegistrationHandle,\n RegisterableHotkey,\n} from '@tanstack/hotkeys'\n\n/**\n * A single hotkey definition for use with `useHotkeys`.\n */\nexport interface UseHotkeyDefinition {\n /** The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object */\n hotkey: RegisterableHotkey\n /** The function to call when the hotkey is pressed */\n callback: HotkeyCallback\n /** Per-hotkey options (merged on top of commonOptions) */\n options?: UseHotkeyOptions\n}\n\n/**\n * Preact hook for registering multiple keyboard hotkeys at once.\n *\n * Uses the singleton HotkeyManager for efficient event handling.\n * Accepts a dynamic array of hotkey definitions, making it safe to use\n * with variable-length lists without violating the rules of hooks.\n *\n * Options are merged in this order:\n * HotkeysProvider defaults < commonOptions < per-definition options\n *\n * Callbacks and options are synced on every render to avoid stale closures.\n *\n * @param hotkeys - Array of hotkey definitions to register\n * @param commonOptions - Shared options applied to all hotkeys (overridden by per-definition options).\n * Per-row `enabled: false` still registers that hotkey: `HotkeyManager` suppresses execution only (the row\n * stays in the store and appears in TanStack Hotkeys devtools). Toggling `enabled` updates the existing handle\n * via `setOptions` (no unregister/re-register churn).\n *\n * @example\n * ```tsx\n * function Editor() {\n * useHotkeys([\n * { hotkey: 'Mod+S', callback: () => save() },\n * { hotkey: 'Mod+Z', callback: () => undo() },\n * { hotkey: 'Escape', callback: () => close() },\n * ])\n * }\n * ```\n *\n * @example\n * ```tsx\n * function MenuShortcuts({ items }) {\n * // Dynamic hotkeys from data -- safe because it's a single hook call\n * useHotkeys(\n * items.map((item) => ({\n * hotkey: item.shortcut,\n * callback: item.action,\n * options: { enabled: item.enabled },\n * })),\n * { preventDefault: true },\n * )\n * }\n * ```\n */\nexport function useHotkeys(\n hotkeys: Array<UseHotkeyDefinition>,\n commonOptions: UseHotkeyOptions = {},\n): void {\n type RegistrationRecord = {\n handle: HotkeyRegistrationHandle\n target: Document | HTMLElement | Window\n }\n\n const defaultOptions = useDefaultHotkeysOptions().hotkey\n const manager = getHotkeyManager()\n const platform =\n commonOptions.platform ?? defaultOptions?.platform ?? detectPlatform()\n\n const registrationsRef = useRef<Map<string, RegistrationRecord>>(new Map())\n const hotkeysRef = useRef(hotkeys)\n const hotkeyStringsRef = useRef<Array<Hotkey>>([])\n const commonOptionsRef = useRef(commonOptions)\n const defaultOptionsRef = useRef(defaultOptions)\n const managerRef = useRef(manager)\n\n const hotkeyStrings = hotkeys.map((def) =>\n normalizeRegisterableHotkey(def.hotkey, platform),\n )\n\n hotkeysRef.current = hotkeys\n hotkeyStringsRef.current = hotkeyStrings\n commonOptionsRef.current = commonOptions\n defaultOptionsRef.current = defaultOptions\n managerRef.current = manager\n\n useEffect(() => {\n const prevRegistrations = registrationsRef.current\n const nextRegistrations = new Map<string, RegistrationRecord>()\n\n const rows: Array<{\n registrationKey: string\n def: (typeof hotkeysRef.current)[number]\n hotkeyStr: Hotkey\n mergedOptions: UseHotkeyOptions\n resolvedTarget: Document | HTMLElement | Window\n }> = []\n\n for (let i = 0; i < hotkeysRef.current.length; i++) {\n const def = hotkeysRef.current[i]!\n const hotkeyStr = hotkeyStringsRef.current[i]!\n const mergedOptions = {\n ...defaultOptionsRef.current,\n ...commonOptionsRef.current,\n ...def.options,\n } as UseHotkeyOptions\n\n const resolvedTarget = isRef(mergedOptions.target)\n ? mergedOptions.target.current\n : (mergedOptions.target ??\n (typeof document !== 'undefined' ? document : null))\n\n if (!resolvedTarget) {\n continue\n }\n\n const registrationKey = `${i}:${hotkeyStr}`\n rows.push({\n registrationKey,\n def,\n hotkeyStr,\n mergedOptions,\n resolvedTarget,\n })\n }\n\n const nextKeys = new Set(rows.map((r) => r.registrationKey))\n\n for (const [key, record] of prevRegistrations) {\n if (!nextKeys.has(key) && record.handle.isActive) {\n record.handle.unregister()\n }\n }\n\n for (const row of rows) {\n const { registrationKey, def, hotkeyStr, mergedOptions, resolvedTarget } =\n row\n\n const existing = prevRegistrations.get(registrationKey)\n if (existing?.handle.isActive && existing.target === resolvedTarget) {\n // Publish callback/options only after commit, never during render.\n existing.handle.callback = def.callback\n const { target: _target, ...optionsWithoutTarget } = mergedOptions\n existing.handle.setOptions(optionsWithoutTarget)\n nextRegistrations.set(registrationKey, existing)\n continue\n }\n\n if (existing?.handle.isActive) {\n existing.handle.unregister()\n }\n\n const handle = managerRef.current.register(hotkeyStr, def.callback, {\n ...mergedOptions,\n target: resolvedTarget,\n })\n nextRegistrations.set(registrationKey, {\n handle,\n target: resolvedTarget,\n })\n }\n\n registrationsRef.current = nextRegistrations\n })\n\n useEffect(() => {\n return () => {\n for (const { handle } of registrationsRef.current.values()) {\n if (handle.isActive) {\n handle.unregister()\n }\n }\n registrationsRef.current = new Map()\n }\n }, [])\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA,SAAgB,WACd,SACA,gBAAkC,EAAE,EAC9B;CAMN,MAAM,iBAAiB,0BAA0B,CAAC;CAClD,MAAM,UAAU,kBAAkB;CAClC,MAAM,WACJ,cAAc,YAAY,gBAAgB,YAAY,gBAAgB;CAExE,MAAM,mBAAmB,uBAAwC,IAAI,KAAK,CAAC;CAC3E,MAAM,aAAa,OAAO,QAAQ;CAClC,MAAM,mBAAmB,OAAsB,EAAE,CAAC;CAClD,MAAM,mBAAmB,OAAO,cAAc;CAC9C,MAAM,oBAAoB,OAAO,eAAe;CAChD,MAAM,aAAa,OAAO,QAAQ;CAElC,MAAM,gBAAgB,QAAQ,KAAK,QACjC,4BAA4B,IAAI,QAAQ,SAAS,CAClD;AAED,YAAW,UAAU;AACrB,kBAAiB,UAAU;AAC3B,kBAAiB,UAAU;AAC3B,mBAAkB,UAAU;AAC5B,YAAW,UAAU;AAErB,iBAAgB;EACd,MAAM,oBAAoB,iBAAiB;EAC3C,MAAM,oCAAoB,IAAI,KAAiC;EAE/D,MAAM,OAMD,EAAE;AAEP,OAAK,IAAI,IAAI,GAAG,IAAI,WAAW,QAAQ,QAAQ,KAAK;GAClD,MAAM,MAAM,WAAW,QAAQ;GAC/B,MAAM,YAAY,iBAAiB,QAAQ;GAC3C,MAAM,gBAAgB;IACpB,GAAG,kBAAkB;IACrB,GAAG,iBAAiB;IACpB,GAAG,IAAI;IACR;GAED,MAAM,iBAAiB,MAAM,cAAc,OAAO,GAC9C,cAAc,OAAO,UACpB,cAAc,WACd,OAAO,aAAa,cAAc,WAAW;AAElD,OAAI,CAAC,eACH;GAGF,MAAM,kBAAkB,GAAG,EAAE,GAAG;AAChC,QAAK,KAAK;IACR;IACA;IACA;IACA;IACA;IACD,CAAC;;EAGJ,MAAM,WAAW,IAAI,IAAI,KAAK,KAAK,MAAM,EAAE,gBAAgB,CAAC;AAE5D,OAAK,MAAM,CAAC,KAAK,WAAW,kBAC1B,KAAI,CAAC,SAAS,IAAI,IAAI,IAAI,OAAO,OAAO,SACtC,QAAO,OAAO,YAAY;AAI9B,OAAK,MAAM,OAAO,MAAM;GACtB,MAAM,EAAE,iBAAiB,KAAK,WAAW,eAAe,mBACtD;GAEF,MAAM,WAAW,kBAAkB,IAAI,gBAAgB;AACvD,OAAI,UAAU,OAAO,YAAY,SAAS,WAAW,gBAAgB;AAEnE,aAAS,OAAO,WAAW,IAAI;IAC/B,MAAM,EAAE,QAAQ,SAAS,GAAG,yBAAyB;AACrD,aAAS,OAAO,WAAW,qBAAqB;AAChD,sBAAkB,IAAI,iBAAiB,SAAS;AAChD;;AAGF,OAAI,UAAU,OAAO,SACnB,UAAS,OAAO,YAAY;GAG9B,MAAM,SAAS,WAAW,QAAQ,SAAS,WAAW,IAAI,UAAU;IAClE,GAAG;IACH,QAAQ;IACT,CAAC;AACF,qBAAkB,IAAI,iBAAiB;IACrC;IACA,QAAQ;IACT,CAAC;;AAGJ,mBAAiB,UAAU;GAC3B;AAEF,iBAAgB;AACd,eAAa;AACX,QAAK,MAAM,EAAE,YAAY,iBAAiB,QAAQ,QAAQ,CACxD,KAAI,OAAO,SACT,QAAO,YAAY;AAGvB,oBAAiB,0BAAU,IAAI,KAAK;;IAErC,EAAE,CAAC"}
|
package/dist/useKeyHold.cjs
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
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 `useSelector` 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.useSelector)(tracker.store, (state) => state.heldKeys.some((heldKey) => heldKey.toLowerCase() === normalizedKey));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
//#endregion
|
|
52
|
-
exports.useKeyHold = useKeyHold;
|
|
53
|
-
//# sourceMappingURL=useKeyHold.cjs.map
|
package/dist/useKeyHold.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useKeyHold.cjs","names":[],"sources":["../src/useKeyHold.ts"],"sourcesContent":["import { useSelector } from '@tanstack/preact-store'\nimport { getKeyStateTracker } from '@tanstack/hotkeys'\nimport type { IndividualKey } from '@tanstack/hotkeys'\n\n/**\n * Preact hook that returns whether a specific key is currently being held.\n *\n * This hook uses `useSelector` 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: IndividualKey): boolean {\n const tracker = getKeyStateTracker()\n const normalizedKey = key.toLowerCase()\n\n return useSelector(tracker.store, (state) =>\n state.heldKeys.some((heldKey) => heldKey.toLowerCase() === normalizedKey),\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CA,SAAgB,WAAW,KAA6B;CACtD,MAAM,qDAA8B;CACpC,MAAM,gBAAgB,IAAI,aAAa;AAEvC,gDAAmB,QAAQ,QAAQ,UACjC,MAAM,SAAS,MAAM,YAAY,QAAQ,aAAa,KAAK,cAAc,CAC1E"}
|
package/dist/useKeyHold.d.cts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { IndividualKey } 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 `useSelector` 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: IndividualKey): boolean;
|
|
45
|
-
//#endregion
|
|
46
|
-
export { useKeyHold };
|
|
47
|
-
//# sourceMappingURL=useKeyHold.d.cts.map
|
package/dist/useKeyHold.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useKeyHold.js","names":[],"sources":["../src/useKeyHold.ts"],"sourcesContent":["import { useSelector } from '@tanstack/preact-store'\nimport { getKeyStateTracker } from '@tanstack/hotkeys'\nimport type { IndividualKey } from '@tanstack/hotkeys'\n\n/**\n * Preact hook that returns whether a specific key is currently being held.\n *\n * This hook uses `useSelector` 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: IndividualKey): boolean {\n const tracker = getKeyStateTracker()\n const normalizedKey = key.toLowerCase()\n\n return useSelector(tracker.store, (state) =>\n state.heldKeys.some((heldKey) => heldKey.toLowerCase() === normalizedKey),\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CA,SAAgB,WAAW,KAA6B;CACtD,MAAM,UAAU,oBAAoB;CACpC,MAAM,gBAAgB,IAAI,aAAa;AAEvC,QAAO,YAAY,QAAQ,QAAQ,UACjC,MAAM,SAAS,MAAM,YAAY,QAAQ,aAAa,KAAK,cAAc,CAC1E"}
|
package/dist/utils.cjs
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
//#region src/utils.ts
|
|
3
|
-
/**
|
|
4
|
-
* Type guard to check if a value is a Preact ref-like object.
|
|
5
|
-
*/
|
|
6
|
-
function isRef(value) {
|
|
7
|
-
return value !== null && typeof value === "object" && "current" in value;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
//#endregion
|
|
11
|
-
exports.isRef = isRef;
|
|
12
|
-
//# sourceMappingURL=utils.cjs.map
|
package/dist/utils.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","names":[],"sources":["../src/utils.ts"],"sourcesContent":["import type { RefObject } from 'preact'\n\n/**\n * Type guard to check if a value is a Preact ref-like object.\n */\nexport function isRef(value: unknown): value is RefObject<HTMLElement | null> {\n return value !== null && typeof value === 'object' && 'current' in value\n}\n"],"mappings":";;;;;AAKA,SAAgB,MAAM,OAAwD;AAC5E,QAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,aAAa"}
|
package/dist/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","names":[],"sources":["../src/utils.ts"],"sourcesContent":["import type { RefObject } from 'preact'\n\n/**\n * Type guard to check if a value is a Preact ref-like object.\n */\nexport function isRef(value: unknown): value is RefObject<HTMLElement | null> {\n return value !== null && typeof value === 'object' && 'current' in value\n}\n"],"mappings":";;;;AAKA,SAAgB,MAAM,OAAwD;AAC5E,QAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,aAAa"}
|
package/src/HotkeysProvider.tsx
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { createContext } from 'preact'
|
|
2
|
-
import { useContext, useMemo } from 'preact/hooks'
|
|
3
|
-
import type { ComponentChildren } from 'preact'
|
|
4
|
-
import type {
|
|
5
|
-
HotkeyRecorderOptions,
|
|
6
|
-
HotkeySequenceRecorderOptions,
|
|
7
|
-
} from '@tanstack/hotkeys'
|
|
8
|
-
import type { UseHotkeyOptions } from './useHotkey'
|
|
9
|
-
import type { UseHotkeySequenceOptions } from './useHotkeySequence'
|
|
10
|
-
|
|
11
|
-
export interface HotkeysProviderOptions {
|
|
12
|
-
hotkey?: Partial<UseHotkeyOptions>
|
|
13
|
-
hotkeyRecorder?: Partial<HotkeyRecorderOptions>
|
|
14
|
-
hotkeySequenceRecorder?: Partial<HotkeySequenceRecorderOptions>
|
|
15
|
-
hotkeySequence?: Partial<UseHotkeySequenceOptions>
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
interface HotkeysContextValue {
|
|
19
|
-
defaultOptions: HotkeysProviderOptions
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const HotkeysContext = createContext<HotkeysContextValue | null>(null)
|
|
23
|
-
|
|
24
|
-
export interface HotkeysProviderProps {
|
|
25
|
-
children: ComponentChildren
|
|
26
|
-
defaultOptions?: HotkeysProviderOptions
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const DEFAULT_OPTIONS: HotkeysProviderOptions = {}
|
|
30
|
-
|
|
31
|
-
export function HotkeysProvider({
|
|
32
|
-
children,
|
|
33
|
-
defaultOptions = DEFAULT_OPTIONS,
|
|
34
|
-
}: HotkeysProviderProps) {
|
|
35
|
-
const contextValue: HotkeysContextValue = useMemo(
|
|
36
|
-
() => ({
|
|
37
|
-
defaultOptions,
|
|
38
|
-
}),
|
|
39
|
-
[defaultOptions],
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
return (
|
|
43
|
-
<HotkeysContext.Provider value={contextValue}>
|
|
44
|
-
{children}
|
|
45
|
-
</HotkeysContext.Provider>
|
|
46
|
-
)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function useHotkeysContext() {
|
|
50
|
-
return useContext(HotkeysContext)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function useDefaultHotkeysOptions() {
|
|
54
|
-
const context = useContext(HotkeysContext)
|
|
55
|
-
return context?.defaultOptions ?? {}
|
|
56
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
// Re-export everything from the core package
|
|
2
|
-
export * from '@tanstack/hotkeys'
|
|
3
|
-
|
|
4
|
-
// provider
|
|
5
|
-
export * from './HotkeysProvider'
|
|
6
|
-
|
|
7
|
-
// Preact-specific exports
|
|
8
|
-
export * from './useHotkey'
|
|
9
|
-
export * from './useHotkeys'
|
|
10
|
-
export * from './useHeldKeys'
|
|
11
|
-
export * from './useHeldKeyCodes'
|
|
12
|
-
export * from './useKeyHold'
|
|
13
|
-
export * from './useHotkeySequence'
|
|
14
|
-
export * from './useHotkeySequences'
|
|
15
|
-
export * from './useHotkeyRecorder'
|
|
16
|
-
export * from './useHotkeyRegistrations'
|
|
17
|
-
export * from './useHotkeySequenceRecorder'
|
|
18
|
-
export * from './useHotkeyHint'
|
package/src/useHeldKeyCodes.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { useSelector } from '@tanstack/preact-store'
|
|
2
|
-
import { getKeyStateTracker } from '@tanstack/hotkeys'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Preact hook that returns a map of currently held key names to their physical `event.code` values.
|
|
6
|
-
*
|
|
7
|
-
* This is useful for debugging which physical key was pressed (e.g. distinguishing
|
|
8
|
-
* left vs right Shift via "ShiftLeft" / "ShiftRight").
|
|
9
|
-
*
|
|
10
|
-
* @returns Record mapping normalized key names to their `event.code` values
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```tsx
|
|
14
|
-
* function KeyDebugDisplay() {
|
|
15
|
-
* const heldKeys = useHeldKeys()
|
|
16
|
-
* const heldCodes = useHeldKeyCodes()
|
|
17
|
-
*
|
|
18
|
-
* return (
|
|
19
|
-
* <div>
|
|
20
|
-
* {heldKeys.map((key) => (
|
|
21
|
-
* <kbd key={key}>
|
|
22
|
-
* {key} <small>{heldCodes[key]}</small>
|
|
23
|
-
* </kbd>
|
|
24
|
-
* ))}
|
|
25
|
-
* </div>
|
|
26
|
-
* )
|
|
27
|
-
* }
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
export function useHeldKeyCodes(): Record<string, string> {
|
|
31
|
-
const tracker = getKeyStateTracker()
|
|
32
|
-
return useSelector(tracker.store, (state) => state.heldCodes)
|
|
33
|
-
}
|
package/src/useHeldKeys.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { useSelector } from '@tanstack/preact-store'
|
|
2
|
-
import { getKeyStateTracker } from '@tanstack/hotkeys'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Preact hook that returns an array of currently held keyboard keys.
|
|
6
|
-
*
|
|
7
|
-
* This hook uses `useSelector` from `@tanstack/preact-store` to subscribe
|
|
8
|
-
* to the global KeyStateTracker and updates whenever keys are pressed
|
|
9
|
-
* or released.
|
|
10
|
-
*
|
|
11
|
-
* @returns Array of currently held key names
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```tsx
|
|
15
|
-
* function KeyDisplay() {
|
|
16
|
-
* const heldKeys = useHeldKeys()
|
|
17
|
-
*
|
|
18
|
-
* return (
|
|
19
|
-
* <div>
|
|
20
|
-
* Currently pressed: {heldKeys.join(' + ') || 'None'}
|
|
21
|
-
* </div>
|
|
22
|
-
* )
|
|
23
|
-
* }
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
export function useHeldKeys(): Array<string> {
|
|
27
|
-
const tracker = getKeyStateTracker()
|
|
28
|
-
return useSelector(tracker.store, (state) => state.heldKeys)
|
|
29
|
-
}
|
package/src/useHotkey.ts
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'preact/hooks'
|
|
2
|
-
import {
|
|
3
|
-
detectPlatform,
|
|
4
|
-
getHotkeyManager,
|
|
5
|
-
normalizeRegisterableHotkey,
|
|
6
|
-
} from '@tanstack/hotkeys'
|
|
7
|
-
import { useDefaultHotkeysOptions } from './HotkeysProvider'
|
|
8
|
-
import { isRef } from './utils'
|
|
9
|
-
import type { RefObject } from 'preact'
|
|
10
|
-
import type {
|
|
11
|
-
HotkeyCallback,
|
|
12
|
-
HotkeyOptions,
|
|
13
|
-
HotkeyRegistrationHandle,
|
|
14
|
-
RegisterableHotkey,
|
|
15
|
-
} from '@tanstack/hotkeys'
|
|
16
|
-
|
|
17
|
-
export interface UseHotkeyOptions extends Omit<HotkeyOptions, 'target'> {
|
|
18
|
-
/**
|
|
19
|
-
* The DOM element to attach the event listener to.
|
|
20
|
-
* Can be a Preact ref, direct DOM element, or null.
|
|
21
|
-
* Defaults to document.
|
|
22
|
-
*/
|
|
23
|
-
target?:
|
|
24
|
-
| RefObject<HTMLElement | null>
|
|
25
|
-
| HTMLElement
|
|
26
|
-
| Document
|
|
27
|
-
| Window
|
|
28
|
-
| null
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Preact hook for registering a keyboard hotkey.
|
|
33
|
-
*
|
|
34
|
-
* Uses the singleton HotkeyManager for efficient event handling.
|
|
35
|
-
* The callback receives both the keyboard event and a context object
|
|
36
|
-
* containing the hotkey string and parsed hotkey.
|
|
37
|
-
*
|
|
38
|
-
* This hook syncs the callback and options on every render to avoid
|
|
39
|
-
* stale closures. This means
|
|
40
|
-
* callbacks that reference Preact state will always have access to
|
|
41
|
-
* the latest values.
|
|
42
|
-
*
|
|
43
|
-
* @param hotkey - The hotkey string (e.g., 'Mod+S', 'Escape') or RawHotkey object (supports `mod` for cross-platform)
|
|
44
|
-
* @param callback - The function to call when the hotkey is pressed
|
|
45
|
-
* @param options - Options for the hotkey behavior. `enabled: false` keeps the registration (visible in devtools)
|
|
46
|
-
* and only suppresses firing; the hook updates the existing handle instead of unregistering.
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```tsx
|
|
50
|
-
* function SaveButton() {
|
|
51
|
-
* const [count, setCount] = useState(0)
|
|
52
|
-
*
|
|
53
|
-
* // Callback always has access to latest count value
|
|
54
|
-
* useHotkey('Mod+S', (event, { hotkey }) => {
|
|
55
|
-
* console.log(`Save triggered, count is ${count}`)
|
|
56
|
-
* handleSave()
|
|
57
|
-
* })
|
|
58
|
-
*
|
|
59
|
-
* return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>
|
|
60
|
-
* }
|
|
61
|
-
* ```
|
|
62
|
-
*
|
|
63
|
-
* @example
|
|
64
|
-
* ```tsx
|
|
65
|
-
* function Modal({ isOpen, onClose }) {
|
|
66
|
-
* // enabled option is synced on every render
|
|
67
|
-
* useHotkey('Escape', () => {
|
|
68
|
-
* onClose()
|
|
69
|
-
* }, { enabled: isOpen })
|
|
70
|
-
*
|
|
71
|
-
* if (!isOpen) return null
|
|
72
|
-
* return <div className="modal">...</div>
|
|
73
|
-
* }
|
|
74
|
-
* ```
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* ```tsx
|
|
78
|
-
* function Editor() {
|
|
79
|
-
* const editorRef = useRef<HTMLDivElement>(null)
|
|
80
|
-
*
|
|
81
|
-
* // Scoped to a specific element
|
|
82
|
-
* useHotkey('Mod+S', () => {
|
|
83
|
-
* save()
|
|
84
|
-
* }, { target: editorRef })
|
|
85
|
-
*
|
|
86
|
-
* return <div ref={editorRef}>...</div>
|
|
87
|
-
* }
|
|
88
|
-
* ```
|
|
89
|
-
*/
|
|
90
|
-
export function useHotkey(
|
|
91
|
-
hotkey: RegisterableHotkey,
|
|
92
|
-
callback: HotkeyCallback,
|
|
93
|
-
options: UseHotkeyOptions = {},
|
|
94
|
-
): void {
|
|
95
|
-
const mergedOptions = {
|
|
96
|
-
...useDefaultHotkeysOptions().hotkey,
|
|
97
|
-
...options,
|
|
98
|
-
} as UseHotkeyOptions
|
|
99
|
-
|
|
100
|
-
const manager = getHotkeyManager()
|
|
101
|
-
|
|
102
|
-
// Stable ref for registration handle
|
|
103
|
-
const registrationRef = useRef<HotkeyRegistrationHandle | null>(null)
|
|
104
|
-
|
|
105
|
-
// Refs to capture current values for use in effect without adding dependencies
|
|
106
|
-
const callbackRef = useRef(callback)
|
|
107
|
-
const optionsRef = useRef(mergedOptions)
|
|
108
|
-
const managerRef = useRef(manager)
|
|
109
|
-
|
|
110
|
-
// Update refs on every render
|
|
111
|
-
callbackRef.current = callback
|
|
112
|
-
optionsRef.current = mergedOptions
|
|
113
|
-
managerRef.current = manager
|
|
114
|
-
|
|
115
|
-
// Track previous target and hotkey to detect changes requiring re-registration
|
|
116
|
-
const prevTargetRef = useRef<HTMLElement | Document | Window | null>(null)
|
|
117
|
-
const prevHotkeyRef = useRef<string | null>(null)
|
|
118
|
-
|
|
119
|
-
// Normalize to hotkey string
|
|
120
|
-
const platform = mergedOptions.platform ?? detectPlatform()
|
|
121
|
-
const hotkeyString = normalizeRegisterableHotkey(hotkey, platform)
|
|
122
|
-
|
|
123
|
-
// Extract options without target (target is handled separately)
|
|
124
|
-
const { target: _target, ...optionsWithoutTarget } = mergedOptions
|
|
125
|
-
|
|
126
|
-
useEffect(() => {
|
|
127
|
-
// Resolve target inside the effect so refs are already attached after mount
|
|
128
|
-
const resolvedTarget = isRef(optionsRef.current.target)
|
|
129
|
-
? optionsRef.current.target.current
|
|
130
|
-
: (optionsRef.current.target ??
|
|
131
|
-
(typeof document !== 'undefined' ? document : null))
|
|
132
|
-
|
|
133
|
-
// Skip if no valid target (SSR or ref still null)
|
|
134
|
-
if (!resolvedTarget) {
|
|
135
|
-
if (registrationRef.current?.isActive) {
|
|
136
|
-
registrationRef.current.unregister()
|
|
137
|
-
registrationRef.current = null
|
|
138
|
-
}
|
|
139
|
-
prevTargetRef.current = null
|
|
140
|
-
prevHotkeyRef.current = null
|
|
141
|
-
return
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// Check if we need to re-register (target or hotkey changed)
|
|
145
|
-
const targetChanged =
|
|
146
|
-
prevTargetRef.current !== null && prevTargetRef.current !== resolvedTarget
|
|
147
|
-
const hotkeyChanged =
|
|
148
|
-
prevHotkeyRef.current !== null && prevHotkeyRef.current !== hotkeyString
|
|
149
|
-
|
|
150
|
-
// If we have an active registration and target/hotkey changed, unregister first
|
|
151
|
-
if (registrationRef.current?.isActive && (targetChanged || hotkeyChanged)) {
|
|
152
|
-
registrationRef.current.unregister()
|
|
153
|
-
registrationRef.current = null
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Register if needed (no active registration)
|
|
157
|
-
// Use refs to access current values without adding them to dependencies
|
|
158
|
-
if (!registrationRef.current || !registrationRef.current.isActive) {
|
|
159
|
-
registrationRef.current = managerRef.current.register(
|
|
160
|
-
hotkeyString,
|
|
161
|
-
callbackRef.current,
|
|
162
|
-
{
|
|
163
|
-
...optionsRef.current,
|
|
164
|
-
target: resolvedTarget,
|
|
165
|
-
},
|
|
166
|
-
)
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// Update tracking refs
|
|
170
|
-
prevTargetRef.current = resolvedTarget
|
|
171
|
-
prevHotkeyRef.current = hotkeyString
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
useEffect(() => {
|
|
175
|
-
// Cleanup on unmount
|
|
176
|
-
return () => {
|
|
177
|
-
if (registrationRef.current?.isActive) {
|
|
178
|
-
registrationRef.current.unregister()
|
|
179
|
-
registrationRef.current = null
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}, [])
|
|
183
|
-
|
|
184
|
-
// Publish option changes after commit, never during render.
|
|
185
|
-
useEffect(() => {
|
|
186
|
-
if (registrationRef.current?.isActive) {
|
|
187
|
-
registrationRef.current.callback = callback
|
|
188
|
-
registrationRef.current.setOptions(optionsWithoutTarget)
|
|
189
|
-
}
|
|
190
|
-
})
|
|
191
|
-
}
|
package/src/useHotkeyHint.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { useSelector } from '@tanstack/preact-store'
|
|
2
|
-
import { getKeyStateTracker, matchesHeldModifiers } from '@tanstack/hotkeys'
|
|
3
|
-
import type { HeldModifierOptions, RegisterableHotkey } from '@tanstack/hotkeys'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Returns a boolean indicating whether held modifiers reveal this shortcut.
|
|
7
|
-
* Only visibility changes from held-key updates cause a rerender.
|
|
8
|
-
* Uses the nonempty-subset/AltGr rules of `matchesHeldModifiers`; `exact` requires
|
|
9
|
-
* every modifier. Combine with the action's enabled state before showing a badge.
|
|
10
|
-
* This helper does not register a shortcut or check whether its target is focused.
|
|
11
|
-
*/
|
|
12
|
-
export function useHotkeyHint(
|
|
13
|
-
hotkey: RegisterableHotkey,
|
|
14
|
-
options: HeldModifierOptions = {},
|
|
15
|
-
): boolean {
|
|
16
|
-
return useSelector(getKeyStateTracker().store, (state) =>
|
|
17
|
-
matchesHeldModifiers(hotkey, state.heldKeys, options),
|
|
18
|
-
)
|
|
19
|
-
}
|