@tanstack/preact-hotkeys 0.24.5 → 0.25.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.
@@ -78,7 +78,7 @@ export interface HotkeysProviderProps<T extends AppHotkeys = AppHotkeys> extends
78
78
  * }
79
79
  * ```
80
80
  */
81
- export declare function HotkeysProvider<T extends AppHotkeys = AppHotkeys>({ children, appHotkeys, customHotkeys, onCustomHotkeysChange, defaultOptions, scope, exclusive, enabled }: HotkeysProviderProps<T>): import("preact").JSX.Element;
81
+ export declare function HotkeysProvider<T extends AppHotkeys = AppHotkeys>({ children, appHotkeys, customHotkeys, onCustomHotkeysChange, defaultOptions, scope, exclusive, enabled, suspendCharacterKeys }: HotkeysProviderProps<T>): import("preact").JSX.Element;
82
82
  export declare function useHotkeysContext(): HotkeysContextValue$1 | null;
83
83
  export declare function useDefaultHotkeysOptions(): HotkeysProviderOptions;
84
84
  export {};
@@ -1,4 +1,4 @@
1
- import { publishAppHotkeysStore } from "@tanstack/hotkeys";
1
+ import { getHotkeyManager, publishAppHotkeysStore } from "@tanstack/hotkeys";
2
2
  import { activateHotkeysProviderScope, createAppHotkeysStore, getHotkeysProviderScopeKey, mergeHotkeysProviderDefaults } from "@tanstack/hotkeys/adapter";
3
3
  import { createContext } from "preact";
4
4
  import { useContext, useLayoutEffect, useMemo, useRef } from "preact/hooks";
@@ -43,7 +43,7 @@ const NO_PARENT = {
43
43
  * }
44
44
  * ```
45
45
  */
46
- function HotkeysProvider({ children, appHotkeys, customHotkeys, onCustomHotkeysChange, defaultOptions = DEFAULT_OPTIONS, scope, exclusive = false, enabled = true }) {
46
+ function HotkeysProvider({ children, appHotkeys, customHotkeys, onCustomHotkeysChange, defaultOptions = DEFAULT_OPTIONS, scope, exclusive = false, enabled = true, suspendCharacterKeys = false }) {
47
47
  const parent = useContext(HotkeysContext) ?? NO_PARENT;
48
48
  const platform = defaultOptions.platform ?? parent.defaultOptions.platform;
49
49
  const storeRef = useRef(null);
@@ -63,6 +63,9 @@ function HotkeysProvider({ children, appHotkeys, customHotkeys, onCustomHotkeysC
63
63
  useLayoutEffect(() => {
64
64
  return scopeKey ? activateHotkeysProviderScope(scopeKey) : void 0;
65
65
  }, [scopeKey]);
66
+ useLayoutEffect(() => {
67
+ return suspendCharacterKeys ? getHotkeyManager().suspendCharacterKeys() : void 0;
68
+ }, [suspendCharacterKeys]);
66
69
  useLayoutEffect(() => {
67
70
  store?.setOptions({
68
71
  customHotkeys,
@@ -0,0 +1,12 @@
1
+ import { formatForAria } from "@tanstack/hotkeys";
2
+ /**
3
+ * Formats a hotkey for `aria-keyshortcuts`, without the provider's platform or
4
+ * the user's keyboard layout.
5
+ *
6
+ * @deprecated Use `useFormatForAria()` in components. It returns this
7
+ * function with the provider's platform and the user's keyboard layout filled
8
+ * in. Outside a component, keep calling this function and pass `platform`
9
+ * yourself.
10
+ */
11
+ declare const formatForAria$1: typeof formatForAria;
12
+ export { formatForAria$1 as formatForAria };
@@ -0,0 +1,15 @@
1
+ import { formatForAria } from "@tanstack/hotkeys";
2
+
3
+ // src/formatForAria.ts
4
+ /**
5
+ * Formats a hotkey for `aria-keyshortcuts`, without the provider's platform or
6
+ * the user's keyboard layout.
7
+ *
8
+ * @deprecated Use `useFormatForAria()` in components. It returns this
9
+ * function with the provider's platform and the user's keyboard layout filled
10
+ * in. Outside a component, keep calling this function and pass `platform`
11
+ * yourself.
12
+ */
13
+ const formatForAria$1 = formatForAria;
14
+
15
+ export { formatForAria$1 as formatForAria };
package/dist/index.d.ts CHANGED
@@ -6,11 +6,13 @@ import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
6
6
  import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
7
7
  import { useHotkeyHint } from "./useHotkeyHint.js";
8
8
  import { useFormatForDisplay } from "./useFormatForDisplay.js";
9
+ import { useFormatForAria } from "./useFormatForAria.js";
9
10
  import { formatForDisplay } from "./formatForDisplay.js";
11
+ import { formatForAria } from "./formatForAria.js";
10
12
  import { useKeyboardLayout } from "./useKeyboardLayout.js";
11
13
  import { UseCustomHotkeysResult, useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
12
14
  import { useHeldKeys } from "./useHeldKeys.js";
13
15
  import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
14
16
  import { useKeyHold } from "./useKeyHold.js";
15
17
  export * from "@tanstack/hotkeys";
16
- export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, PreactHotkeyRecorder, UseAppHotkeyOptions, UseCustomHotkeysResult, UseHotkeyChordOptions, UseHotkeyCommonOptions, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeyOptionsFor, UseHotkeySequenceOptions, UseHotkeyTarget, formatForDisplay, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold, useKeyboardLayout };
18
+ export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, PreactHotkeyRecorder, UseAppHotkeyOptions, UseCustomHotkeysResult, UseHotkeyChordOptions, UseHotkeyCommonOptions, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeyOptionsFor, UseHotkeySequenceOptions, UseHotkeyTarget, formatForAria, formatForDisplay, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForAria, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold, useKeyboardLayout };
package/dist/index.js CHANGED
@@ -6,7 +6,9 @@ import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
6
6
  import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
7
7
  import { useHotkeyHint } from "./useHotkeyHint.js";
8
8
  import { useFormatForDisplay } from "./useFormatForDisplay.js";
9
+ import { useFormatForAria } from "./useFormatForAria.js";
9
10
  import { formatForDisplay } from "./formatForDisplay.js";
11
+ import { formatForAria } from "./formatForAria.js";
10
12
  import { useKeyboardLayout } from "./useKeyboardLayout.js";
11
13
  import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
12
14
  import { useHeldKeys } from "./useHeldKeys.js";
@@ -15,4 +17,4 @@ import { useKeyHold } from "./useKeyHold.js";
15
17
 
16
18
  export * from "@tanstack/hotkeys"
17
19
 
18
- export { HotkeysProvider, formatForDisplay, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold, useKeyboardLayout };
20
+ export { HotkeysProvider, formatForAria, formatForDisplay, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForAria, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold, useKeyboardLayout };
@@ -0,0 +1,36 @@
1
+ import { formatForAria } from "@tanstack/hotkeys";
2
+ /**
3
+ * Returns `formatForAria` with the platform from `HotkeysProvider`, or the
4
+ * detected platform without one. A `platform` passed to the returned function
5
+ * wins. Use its value for `aria-keyshortcuts`, so screen readers announce the
6
+ * shortcut. It returns `undefined` for a multi-step sequence, no hotkey, or a
7
+ * hotkey that can't fire right now (one that is disabled, in a suspended
8
+ * scope, a suspended character key, or not registered), so Preact omits the
9
+ * attribute.
10
+ *
11
+ * Physical bindings such as `[KeyQ]` name the key the user presses in
12
+ * Chromium browsers, once the keyboard layout has loaded. See
13
+ * `useKeyboardLayout`. A `layoutMap` passed to the returned function wins.
14
+ *
15
+ * Call the hook once at the top of the component; the returned function can
16
+ * be called anywhere. Server-rendered apps should set the provider's
17
+ * `defaultOptions.platform` from the request with `detectPlatform(userAgent)`,
18
+ * so the server HTML matches the user's platform.
19
+ *
20
+ * @example
21
+ * ```tsx
22
+ * function SearchButton() {
23
+ * const appHotkeys = useAppHotkeys()
24
+ * const formatForAria = useFormatForAria()
25
+ * const formatForDisplay = useFormatForDisplay()
26
+ * const { hotkey } = appHotkeys['search.open']
27
+ *
28
+ * return (
29
+ * <button aria-keyshortcuts={formatForAria(hotkey)}>
30
+ * Search {hotkey && <kbd>{formatForDisplay(hotkey)}</kbd>}
31
+ * </button>
32
+ * )
33
+ * }
34
+ * ```
35
+ */
36
+ export declare function useFormatForAria(): typeof formatForAria;
@@ -0,0 +1,54 @@
1
+ import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
+ import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
3
+ import { useKeyboardLayout } from "./useKeyboardLayout.js";
4
+ import { formatForAria } from "@tanstack/hotkeys";
5
+ import { announceLiveHotkeys, createFormatter } from "@tanstack/hotkeys/adapter";
6
+ import { useMemo } from "preact/hooks";
7
+
8
+ // src/useFormatForAria.ts
9
+ /**
10
+ * Returns `formatForAria` with the platform from `HotkeysProvider`, or the
11
+ * detected platform without one. A `platform` passed to the returned function
12
+ * wins. Use its value for `aria-keyshortcuts`, so screen readers announce the
13
+ * shortcut. It returns `undefined` for a multi-step sequence, no hotkey, or a
14
+ * hotkey that can't fire right now (one that is disabled, in a suspended
15
+ * scope, a suspended character key, or not registered), so Preact omits the
16
+ * attribute.
17
+ *
18
+ * Physical bindings such as `[KeyQ]` name the key the user presses in
19
+ * Chromium browsers, once the keyboard layout has loaded. See
20
+ * `useKeyboardLayout`. A `layoutMap` passed to the returned function wins.
21
+ *
22
+ * Call the hook once at the top of the component; the returned function can
23
+ * be called anywhere. Server-rendered apps should set the provider's
24
+ * `defaultOptions.platform` from the request with `detectPlatform(userAgent)`,
25
+ * so the server HTML matches the user's platform.
26
+ *
27
+ * @example
28
+ * ```tsx
29
+ * function SearchButton() {
30
+ * const appHotkeys = useAppHotkeys()
31
+ * const formatForAria = useFormatForAria()
32
+ * const formatForDisplay = useFormatForDisplay()
33
+ * const { hotkey } = appHotkeys['search.open']
34
+ *
35
+ * return (
36
+ * <button aria-keyshortcuts={formatForAria(hotkey)}>
37
+ * Search {hotkey && <kbd>{formatForDisplay(hotkey)}</kbd>}
38
+ * </button>
39
+ * )
40
+ * }
41
+ * ```
42
+ */
43
+ function useFormatForAria() {
44
+ const platform = useDefaultHotkeysOptions().platform;
45
+ const layoutMap = useKeyboardLayout();
46
+ const registrations = useHotkeyRegistrations();
47
+ return useMemo(() => announceLiveHotkeys(createFormatter(formatForAria, () => platform, () => layoutMap), () => registrations), [
48
+ platform,
49
+ layoutMap,
50
+ registrations
51
+ ]);
52
+ }
53
+
54
+ export { useFormatForAria };
@@ -1,7 +1,7 @@
1
1
  import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
2
  import { useSelector } from "./_useSelector.js";
3
3
  import { HotkeyRecorder } from "@tanstack/hotkeys";
4
- import { withDefaults } from "@tanstack/hotkeys/adapter";
4
+ import { syncRecorderOptions, withDefaults } from "@tanstack/hotkeys/adapter";
5
5
  import { useLayoutEffect, useRef, useState } from "preact/hooks";
6
6
 
7
7
  // src/useHotkeyRecorder.ts
@@ -9,7 +9,7 @@ function useHotkeyRecorder(options) {
9
9
  const mergedOptions = withDefaults(useDefaultHotkeysOptions().hotkeyRecorder, options);
10
10
  const recorderRef = useRef(null);
11
11
  if (!recorderRef.current) recorderRef.current = new HotkeyRecorder(mergedOptions);
12
- recorderRef.current.setOptions(mergedOptions);
12
+ syncRecorderOptions(recorderRef.current, mergedOptions);
13
13
  const recorder = recorderRef.current;
14
14
  const isRecording = useSelector(recorder.store, (state) => state.isRecording);
15
15
  const steps = useSelector(recorder.store, (state) => state.steps);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/preact-hotkeys",
3
- "version": "0.24.5",
3
+ "version": "0.25.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.35.0"
38
+ "@tanstack/hotkeys": "0.37.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@preact/preset-vite": "^2.10.6",
@@ -18,7 +18,6 @@ sources:
18
18
  - src/useHotkeys.ts
19
19
  - src/HotkeysProvider.tsx
20
20
  - src/useAppHotkeys.ts
21
- - src/utils.ts
22
21
  - TanStack/hotkeys:docs/framework/preact/quick-start.md
23
22
  - TanStack/hotkeys:docs/framework/preact/guides/hotkeys-provider-and-scopes.md
24
23
  requires:
@@ -46,26 +45,27 @@ export function SaveButton({ onSave }: { onSave: () => void }) {
46
45
 
47
46
  ## API
48
47
 
49
- | API | What it's for |
50
- | ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
51
- | `useHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey while the component is mounted. |
52
- | `useHotkeys(definitions, commonOptions?)` | Register several hotkeys, or a list built from data, in one call. |
53
- | `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for the components inside it. |
54
- | `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider. |
55
- | `useDefaultHotkeysOptions()` | Read the default options merged from every provider above. |
56
- | `useFormatForDisplay()` | Get the function that formats a hotkey as a label, with the provider's `platform` and the user's keyboard layout filled in. Only Chromium browsers report a layout, so physical keys keep their US labels in Firefox and Safari. Use it for every label in a component: `const formatForDisplay = useFormatForDisplay()` at the top, then `formatForDisplay(hotkey)`. |
57
- | `useAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied, to pass to the register API. |
58
- | `useAppHotkeyList()` | List every app hotkey with its status, including ones whose components aren't mounted, for a cheat sheet or settings screen. |
59
- | `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
60
- | `useHotkeyRecorder(options)` | Record a chord or sequence the user presses, for a settings screen. |
61
- | `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. |
62
- | `useHotkeyScopesState()` | Get the active scopes and the exclusive scope that owns the keyboard. |
63
- | `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. |
64
- | `useKeyHold(key)` | Get whether one key is held, such as Shift for a range-select mode. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
65
- | `useHeldKeys()` | Get every key that's held right now. |
66
- | `useHeldKeyCodes()` | Map each held key to its physical `event.code`. |
67
-
68
- The package also re-exports `@tanstack/hotkeys`, such as `matchesKeyboardEvent` and `defineAppHotkeys`. Its plain `formatForDisplay` export is deprecated: use `useFormatForDisplay()` in components. Outside a component, call the plain function and pass `platform`.
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`, `suspendCharacterKeys` (set from a user setting that turns off single-key shortcuts; it applies app-wide while the provider is mounted), 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
+ | `useFormatForDisplay()` | Get the function that formats a hotkey as a label, with the provider's `platform` and the user's keyboard layout filled in. Only Chromium browsers report a layout, so physical keys keep their US labels in Firefox and Safari. Use it for every label in a component: `const formatForDisplay = useFormatForDisplay()` at the top, then `formatForDisplay(hotkey)`. |
56
+ | `useFormatForAria()` | Get the function that formats a hotkey for `aria-keyshortcuts`, with the provider's `platform` and the user's keyboard layout filled in. Returns `undefined` for a multi-step sequence, no hotkey, or a hotkey that can't fire right now, such as a disabled or suspended one, so the attribute is omitted. Set `aria-keyshortcuts` on every control that has a shortcut, next to the visible label. |
57
+ | `useAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied, to pass to the register API. |
58
+ | `useAppHotkeyList()` | List every app hotkey with its status, including ones whose components aren't mounted, for a cheat sheet or settings screen. |
59
+ | `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
60
+ | `useHotkeyRecorder(options)` | Record a chord or sequence the user presses, for a settings screen. |
61
+ | `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. |
62
+ | `useHotkeyScopesState()` | Get the active scopes and the exclusive scope that owns the keyboard. |
63
+ | `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. |
64
+ | `useKeyHold(key)` | Get whether one key is held, such as Shift for a range-select mode. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
65
+ | `useHeldKeys()` | Get every key that's held right now. |
66
+ | `useHeldKeyCodes()` | Map each held key to its physical `event.code`. |
67
+
68
+ The package also re-exports `@tanstack/hotkeys`, such as `matchesKeyboardEvent` and `defineAppHotkeys`. Its plain `formatForDisplay` export is deprecated: use `useFormatForDisplay()` in components. Outside a component, call the plain function and pass `platform`. The plain `formatForAria` export is deprecated the same way, in favor of `useFormatForAria()`.
69
69
 
70
70
  No provider is required.
71
71