@tanstack/preact-hotkeys 0.15.1 → 0.16.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/preact-hotkeys",
3
- "version": "0.15.1",
3
+ "version": "0.16.0",
4
4
  "description": "Preact adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -17,7 +17,8 @@
17
17
  "keywords": [
18
18
  "preact",
19
19
  "tanstack",
20
- "keys"
20
+ "keys",
21
+ "tanstack-intent"
21
22
  ],
22
23
  "type": "module",
23
24
  "types": "./dist/index.d.ts",
@@ -30,10 +31,11 @@
30
31
  "node": ">=20"
31
32
  },
32
33
  "files": [
33
- "dist"
34
+ "dist",
35
+ "skills"
34
36
  ],
35
37
  "dependencies": {
36
- "@tanstack/hotkeys": "0.16.1",
38
+ "@tanstack/hotkeys": "0.17.0",
37
39
  "@tanstack/preact-store": "^0.13.5"
38
40
  },
39
41
  "devDependencies": {
@@ -0,0 +1,112 @@
1
+ ---
2
+ name: preact-hotkeys
3
+ description: >-
4
+ Use when adding or fixing keyboard shortcuts in a Preact app with
5
+ @tanstack/preact-hotkeys: useHotkey and useHotkeys, refs as targets,
6
+ HotkeysProvider defaults and dialog scopes, app hotkey hooks, recorders,
7
+ held-key and hint hooks, or replacing useEffect keydown listeners.
8
+ metadata:
9
+ purpose: >-
10
+ Preact bindings for TanStack Hotkeys: hooks, providers, refs, and the
11
+ Preact-specific mistakes to avoid.
12
+ type: framework
13
+ framework: preact
14
+ library: '@tanstack/preact-hotkeys'
15
+ library_version: '0.15.0'
16
+ sources:
17
+ - src/useHotkey.ts
18
+ - src/useHotkeys.ts
19
+ - src/HotkeysProvider.tsx
20
+ - src/useAppHotkeys.ts
21
+ - src/utils.ts
22
+ - TanStack/hotkeys:docs/framework/preact/quick-start.md
23
+ - TanStack/hotkeys:docs/framework/preact/guides/hotkeys-provider-and-scopes.md
24
+ requires:
25
+ - '@tanstack/hotkeys#hotkeys-core'
26
+ ---
27
+
28
+ # TanStack Hotkeys in Preact
29
+
30
+ Before starting, load the `@tanstack/hotkeys#hotkeys-core` skill (`npx @tanstack/intent@latest load @tanstack/hotkeys#hotkeys-core`).
31
+
32
+ ```tsx
33
+ import { formatForDisplay, useHotkey } from '@tanstack/preact-hotkeys'
34
+
35
+ export function SaveButton({ onSave }: { onSave: () => void }) {
36
+ useHotkey('Mod+S', () => onSave(), { meta: { name: 'Save' } })
37
+
38
+ return (
39
+ <button onClick={onSave}>
40
+ Save <kbd>{formatForDisplay('Mod+S')}</kbd>
41
+ </button>
42
+ )
43
+ }
44
+ ```
45
+
46
+ ## API
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`. |
64
+
65
+ The package also re-exports `@tanstack/hotkeys`, such as `formatForDisplay`, `matchesKeyboardEvent`, and `defineAppHotkeys`.
66
+
67
+ No provider is required.
68
+
69
+ ## A dialog that owns the keyboard
70
+
71
+ ```tsx no-check
72
+ import { useId } from 'preact/hooks'
73
+ import type { ComponentChildren } from 'preact'
74
+ import { HotkeysProvider, useHotkey } from '@tanstack/preact-hotkeys'
75
+
76
+ export function Dialog(props: {
77
+ onClose: () => void
78
+ children: ComponentChildren
79
+ }) {
80
+ return (
81
+ <HotkeysProvider scope={`dialog-${useId()}`} exclusive>
82
+ <DialogBody {...props} />
83
+ </HotkeysProvider>
84
+ )
85
+ }
86
+
87
+ function DialogBody({
88
+ onClose,
89
+ children,
90
+ }: {
91
+ onClose: () => void
92
+ children: ComponentChildren
93
+ }) {
94
+ useHotkey('Escape', onClose)
95
+ return <div role="dialog">{children}</div>
96
+ }
97
+ ```
98
+
99
+ A hook uses the nearest `HotkeysProvider` above its component. Registering `Escape` in `Dialog` itself would put it in the outer scope, which the exclusive scope suspends, so the dialog couldn't close. `useId` (Preact 10.11 or newer) gives each open dialog its own scope.
100
+
101
+ ## Pitfalls
102
+
103
+ - **Pass the ref, not `ref.current`.** `target: listRef.current` is `null` on the first render, and a `null` target means `document`, so the hotkey works page-wide. Pass `target: listRef`; a ref that is still empty waits for its element.
104
+ - **No dependency arrays or `useCallback`.** The hooks read the hotkey, callback, and options on every render, so inline callbacks always see current state.
105
+ - **Call the hooks directly.** A `useShortcut(keys: string)` wrapper has to cast strings to hotkeys, which turns off type checking and drops sequences, options, app hotkeys, and the typed callback context.
106
+ - **In tests,** set `<HotkeysProvider defaultOptions={{ hotkey: { platform: 'mac' } }}>`.
107
+
108
+ When declaring app hotkeys, building a cheat sheet, or letting users rebind shortcuts, load `@tanstack/hotkeys#app-hotkeys` and read [app hotkeys in Preact](references/app-hotkeys.md).
109
+
110
+ ## API discovery and docs
111
+
112
+ Signatures: `node_modules/@tanstack/preact-hotkeys/dist/*.d.ts`. Guides: `https://tanstack.com/hotkeys/latest/docs/framework/preact/guides/<page>.md`, such as `hotkeys`, `hotkeys-provider-and-scopes`, or `app-hotkeys`. Devtools: `hotkeysDevtoolsPlugin()` from `@tanstack/preact-hotkeys-devtools` in `@tanstack/preact-devtools`.
@@ -0,0 +1,80 @@
1
+ # App hotkeys in Preact
2
+
3
+ The `@tanstack/hotkeys#app-hotkeys` skill owns the rules. This is the Preact wiring.
4
+
5
+ ```ts
6
+ // app-hotkeys.ts
7
+ import { defineAppHotkeys } from '@tanstack/preact-hotkeys'
8
+
9
+ export const appHotkeys = defineAppHotkeys({
10
+ 'doc.save': { hotkey: 'Mod+S', meta: { name: 'Save' } },
11
+ 'help.shortcuts': { hotkey: 'Mod+/', meta: { name: 'Keyboard shortcuts' } },
12
+ })
13
+
14
+ declare module '@tanstack/preact-hotkeys' {
15
+ interface Register {
16
+ appHotkeys: typeof appHotkeys
17
+ }
18
+ }
19
+ ```
20
+
21
+ ```tsx no-check
22
+ import type { ComponentChildren } from 'preact'
23
+ import { HotkeysProvider } from '@tanstack/preact-hotkeys'
24
+ import type { CustomHotkeys } from '@tanstack/preact-hotkeys'
25
+ import { appHotkeys } from './app-hotkeys'
26
+
27
+ const saved: CustomHotkeys = JSON.parse(localStorage.getItem('hotkeys') ?? '{}')
28
+
29
+ export function Root({ children }: { children: ComponentChildren }) {
30
+ return (
31
+ <HotkeysProvider
32
+ appHotkeys={appHotkeys}
33
+ customHotkeys={saved}
34
+ onCustomHotkeysChange={(custom) =>
35
+ localStorage.setItem('hotkeys', JSON.stringify(custom))
36
+ }
37
+ >
38
+ {children}
39
+ </HotkeysProvider>
40
+ )
41
+ }
42
+ ```
43
+
44
+ Register with `useHotkey(appHotkeys['doc.save'], save)`, using the import or `useAppHotkeys()`. Build a cheat sheet from `useAppHotkeyList()`.
45
+
46
+ A settings row records with `useHotkeyRecorder` and saves through `useCustomHotkeys`:
47
+
48
+ ```tsx
49
+ import {
50
+ formatForDisplay,
51
+ useAppHotkeys,
52
+ useCustomHotkeys,
53
+ useHotkeyRecorder,
54
+ } from '@tanstack/preact-hotkeys'
55
+ import type { AppHotkeyId } from '@tanstack/preact-hotkeys'
56
+
57
+ export function ShortcutRow({ id }: { id: AppHotkeyId }) {
58
+ const appHotkey = useAppHotkeys()[id]
59
+ const { findConflicts, setCustomHotkey, resetCustomHotkey } =
60
+ useCustomHotkeys()
61
+ const recorder = useHotkeyRecorder({
62
+ maxSteps: Infinity,
63
+ validate: (hotkey) => {
64
+ const [conflict] = findConflicts(id, hotkey)
65
+ return conflict ? `Used by ${conflict.meta?.name ?? conflict.id}` : true
66
+ },
67
+ onRecord: (hotkey) => setCustomHotkey(id, hotkey),
68
+ onClear: () => setCustomHotkey(id, null),
69
+ })
70
+
71
+ return (
72
+ <div>
73
+ {appHotkey.meta?.name}:{' '}
74
+ {appHotkey.hotkey ? formatForDisplay(appHotkey.hotkey) : 'Unassigned'}
75
+ <button onClick={recorder.startRecording}>Change</button>
76
+ <button onClick={() => resetCustomHotkey(id)}>Reset</button>
77
+ </div>
78
+ )
79
+ }
80
+ ```