@tanstack/preact-hotkeys 0.16.0 → 0.17.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/dist/index.d.ts CHANGED
@@ -3,10 +3,11 @@ import { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, useDefau
3
3
  import { UseHotkeyDefinition, useHotkeys } from "./useHotkeys.js";
4
4
  import { PreactHotkeyRecorder, useHotkeyRecorder } from "./useHotkeyRecorder.js";
5
5
  import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
6
+ import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
6
7
  import { useHotkeyHint } from "./useHotkeyHint.js";
7
8
  import { UseCustomHotkeysResult, useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
8
9
  import { useHeldKeys } from "./useHeldKeys.js";
9
10
  import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
10
11
  import { useKeyHold } from "./useKeyHold.js";
11
12
  export * from "@tanstack/hotkeys";
12
- export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, PreactHotkeyRecorder, UseAppHotkeyOptions, UseCustomHotkeysResult, UseHotkeyChordOptions, UseHotkeyCommonOptions, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeyOptionsFor, UseHotkeySequenceOptions, UseHotkeyTarget, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeys, useHotkeysContext, useKeyHold };
13
+ export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, PreactHotkeyRecorder, UseAppHotkeyOptions, UseCustomHotkeysResult, UseHotkeyChordOptions, UseHotkeyCommonOptions, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeyOptionsFor, UseHotkeySequenceOptions, UseHotkeyTarget, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold };
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ import { useHotkeys } from "./useHotkeys.js";
3
3
  import { useHotkey } from "./useHotkey.js";
4
4
  import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
5
5
  import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
6
+ import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
6
7
  import { useHotkeyHint } from "./useHotkeyHint.js";
7
8
  import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
8
9
  import { useHeldKeys } from "./useHeldKeys.js";
@@ -11,4 +12,4 @@ import { useKeyHold } from "./useKeyHold.js";
11
12
 
12
13
  export * from "@tanstack/hotkeys"
13
14
 
14
- export { HotkeysProvider, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeys, useHotkeysContext, useKeyHold };
15
+ export { HotkeysProvider, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold };
@@ -2,12 +2,12 @@ import { AppHotkey, HeldModifierOptions, RegisterableHotkey } from "@tanstack/ho
2
2
  //#region src/useHotkeyHint.d.ts
3
3
  /**
4
4
  * Returns a boolean indicating whether held modifiers reveal this shortcut.
5
- * Only visibility changes from held-key updates cause a rerender.
6
- * Uses the nonempty-subset/AltGr rules of `matchesHeldModifiers`; `exact` requires
7
- * every modifier. A sequence reveals its hint from its first step, and an app
8
- * hotkey uses its effective hotkey (false while unbound). Combine with the
9
- * action's enabled state before showing a badge. This helper does not register
10
- * a shortcut or check whether its target is focused.
5
+ * The hint shows only while the hotkey can fire: some registration of it is
6
+ * enabled and in an active scope. An app hotkey matches its registrations by
7
+ * id and uses its effective hotkey (false while unbound).
8
+ * Uses the nonempty-subset/AltGr rules of `matchesHeldModifiers`; `exact`
9
+ * requires every modifier. A sequence reveals its hint from its first step.
10
+ * Only visibility changes cause a rerender. This helper does not register a shortcut. A hotkey on an element counts only while focus is inside it.
11
11
  */
12
12
  export declare function useHotkeyHint(hotkey: RegisterableHotkey | AppHotkey, options?: HeldModifierOptions): boolean;
13
13
  //#endregion
@@ -1,21 +1,24 @@
1
1
  import { useAppHotkeysStoreState } from "./_appHotkeysStore.js";
2
- import { getKeyStateTracker, isAppHotkey, matchesHeldModifiers, resolveAppHotkey } from "@tanstack/hotkeys";
2
+ import { getHotkeyManager, getKeyStateTracker, isAppHotkey, matchesHeldModifiers, resolveAppHotkey } from "@tanstack/hotkeys";
3
3
  import { useSelector } from "@tanstack/preact-store";
4
4
 
5
5
  //#region src/useHotkeyHint.ts
6
6
  /**
7
7
  * Returns a boolean indicating whether held modifiers reveal this shortcut.
8
- * Only visibility changes from held-key updates cause a rerender.
9
- * Uses the nonempty-subset/AltGr rules of `matchesHeldModifiers`; `exact` requires
10
- * every modifier. A sequence reveals its hint from its first step, and an app
11
- * hotkey uses its effective hotkey (false while unbound). Combine with the
12
- * action's enabled state before showing a badge. This helper does not register
13
- * a shortcut or check whether its target is focused.
8
+ * The hint shows only while the hotkey can fire: some registration of it is
9
+ * enabled and in an active scope. An app hotkey matches its registrations by
10
+ * id and uses its effective hotkey (false while unbound).
11
+ * Uses the nonempty-subset/AltGr rules of `matchesHeldModifiers`; `exact`
12
+ * requires every modifier. A sequence reveals its hint from its first step.
13
+ * Only visibility changes cause a rerender. This helper does not register a shortcut. A hotkey on an element counts only while focus is inside it.
14
14
  */
15
15
  function useHotkeyHint(hotkey, options = {}) {
16
16
  const store = useAppHotkeysStoreState();
17
17
  const resolved = isAppHotkey(hotkey) ? resolveAppHotkey(hotkey, store).hotkey : hotkey;
18
- return useSelector(getKeyStateTracker().store, (state) => resolved !== null && matchesHeldModifiers(resolved, state.heldKeys, options));
18
+ const manager = getHotkeyManager();
19
+ const active = useSelector(manager.registrations, () => resolved !== null && manager.hasActiveRegistration(hotkey));
20
+ const held = useSelector(getKeyStateTracker().store, (state) => resolved !== null && matchesHeldModifiers(resolved, state.heldKeys, options));
21
+ return active && held;
19
22
  }
20
23
 
21
24
  //#endregion
@@ -0,0 +1,20 @@
1
+ import { HotkeyScopesState } from "@tanstack/hotkeys";
2
+ //#region src/useHotkeyScopesState.d.ts
3
+ /**
4
+ * Preact hook that returns the hotkey manager's scope state: the active
5
+ * scopes, the exclusive scope that owns the keyboard, and the scopes it
6
+ * allows or suspends. Re-renders when scopes activate or release.
7
+ *
8
+ * @returns The current scope state
9
+ *
10
+ * @example
11
+ * ```tsx
12
+ * function KeyboardOwner() {
13
+ * const { exclusive } = useHotkeyScopesState()
14
+ *
15
+ * return exclusive ? <p>Shortcuts belong to {exclusive}</p> : null
16
+ * }
17
+ * ```
18
+ */
19
+ export declare function useHotkeyScopesState(): HotkeyScopesState;
20
+ //#endregion
@@ -0,0 +1,26 @@
1
+ import { getHotkeyManager } from "@tanstack/hotkeys";
2
+ import { useSelector } from "@tanstack/preact-store";
3
+
4
+ //#region src/useHotkeyScopesState.ts
5
+ /**
6
+ * Preact hook that returns the hotkey manager's scope state: the active
7
+ * scopes, the exclusive scope that owns the keyboard, and the scopes it
8
+ * allows or suspends. Re-renders when scopes activate or release.
9
+ *
10
+ * @returns The current scope state
11
+ *
12
+ * @example
13
+ * ```tsx
14
+ * function KeyboardOwner() {
15
+ * const { exclusive } = useHotkeyScopesState()
16
+ *
17
+ * return exclusive ? <p>Shortcuts belong to {exclusive}</p> : null
18
+ * }
19
+ * ```
20
+ */
21
+ function useHotkeyScopesState() {
22
+ return useSelector(getHotkeyManager().scopes, (state) => state);
23
+ }
24
+
25
+ //#endregion
26
+ export { useHotkeyScopesState };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/preact-hotkeys",
3
- "version": "0.16.0",
3
+ "version": "0.17.0",
4
4
  "description": "Preact adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -35,7 +35,7 @@
35
35
  "skills"
36
36
  ],
37
37
  "dependencies": {
38
- "@tanstack/hotkeys": "0.17.0",
38
+ "@tanstack/hotkeys": "0.18.0",
39
39
  "@tanstack/preact-store": "^0.13.5"
40
40
  },
41
41
  "devDependencies": {
@@ -45,22 +45,23 @@ export function SaveButton({ onSave }: { onSave: () => void }) {
45
45
 
46
46
  ## API
47
47
 
48
- | API | What it's for |
49
- | ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
50
- | `useHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey while the component is mounted. |
51
- | `useHotkeys(definitions, commonOptions?)` | Register several hotkeys, or a list built from data, in one call. |
52
- | `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for the components inside it. |
53
- | `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider. |
54
- | `useDefaultHotkeysOptions()` | Read the default options merged from every provider above. |
55
- | `useAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied, to pass to the register API. |
56
- | `useAppHotkeyList()` | List every app hotkey with its status, including ones whose components aren't mounted, for a cheat sheet or settings screen. |
57
- | `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
58
- | `useHotkeyRecorder(options)` | Record a chord or sequence the user presses, for a settings screen. |
59
- | `useHotkeyRegistrations()` | List every registered hotkey with its options and `meta`, for a cheat sheet of what's mounted. |
60
- | `useHotkeyHint(hotkey, options?)` | Get whether held modifiers should reveal a shortcut's hint, such as labels that appear while Mod is held. |
61
- | `useKeyHold(key)` | Get whether one key is held, such as Shift for a range-select mode. |
62
- | `useHeldKeys()` | Get every key that's held right now. |
63
- | `useHeldKeyCodes()` | Map each held key to its physical `event.code`. |
48
+ | API | What it's for |
49
+ | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
50
+ | `useHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey while the component is mounted. |
51
+ | `useHotkeys(definitions, commonOptions?)` | Register several hotkeys, or a list built from data, in one call. |
52
+ | `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for the components inside it. |
53
+ | `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider. |
54
+ | `useDefaultHotkeysOptions()` | Read the default options merged from every provider above. |
55
+ | `useAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied, to pass to the register API. |
56
+ | `useAppHotkeyList()` | List every app hotkey with its status, including ones whose components aren't mounted, for a cheat sheet or settings screen. |
57
+ | `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
58
+ | `useHotkeyRecorder(options)` | Record a chord or sequence the user presses, for a settings screen. |
59
+ | `useHotkeyRegistrations()` | List every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope), for a cheat sheet of what's mounted. |
60
+ | `useHotkeyScopesState()` | Get the active scopes and the exclusive scope that owns the keyboard. |
61
+ | `useHotkeyHint(hotkey, options?)` | Get whether held modifiers should reveal a shortcut's hint, such as labels that appear while Mod is held. False unless a registration of the hotkey can fire: enabled, in an active scope, and with focus inside its element target. |
62
+ | `useKeyHold(key)` | Get whether one key is held, such as Shift for a range-select mode. |
63
+ | `useHeldKeys()` | Get every key that's held right now. |
64
+ | `useHeldKeyCodes()` | Map each held key to its physical `event.code`. |
64
65
 
65
66
  The package also re-exports `@tanstack/hotkeys`, such as `formatForDisplay`, `matchesKeyboardEvent`, and `defineAppHotkeys`.
66
67