@tanstack/react-hotkeys 0.15.0 → 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/react-hotkeys",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "React adapter for TanStack Hotkeys",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"keywords": [
|
|
18
18
|
"react",
|
|
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.
|
|
38
|
+
"@tanstack/hotkeys": "0.17.0",
|
|
37
39
|
"@tanstack/react-store": "^0.11.2"
|
|
38
40
|
},
|
|
39
41
|
"devDependencies": {
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: react-hotkeys
|
|
3
|
+
description: >-
|
|
4
|
+
Use when adding or fixing keyboard shortcuts in a React app with
|
|
5
|
+
@tanstack/react-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 and other
|
|
8
|
+
React shortcut libraries.
|
|
9
|
+
metadata:
|
|
10
|
+
purpose: >-
|
|
11
|
+
React bindings for TanStack Hotkeys: hooks, providers, refs, and the
|
|
12
|
+
React-specific mistakes to avoid.
|
|
13
|
+
type: framework
|
|
14
|
+
framework: react
|
|
15
|
+
library: '@tanstack/react-hotkeys'
|
|
16
|
+
library_version: '0.15.0'
|
|
17
|
+
sources:
|
|
18
|
+
- src/useHotkey.ts
|
|
19
|
+
- src/useHotkeys.ts
|
|
20
|
+
- src/HotkeysProvider.tsx
|
|
21
|
+
- src/useAppHotkeys.ts
|
|
22
|
+
- src/utils.ts
|
|
23
|
+
- TanStack/hotkeys:docs/framework/react/quick-start.md
|
|
24
|
+
- TanStack/hotkeys:docs/framework/react/guides/hotkeys-provider-and-scopes.md
|
|
25
|
+
- 'tests/skills/editor-shortcuts/**'
|
|
26
|
+
- 'tests/skills/shortcut-settings/**'
|
|
27
|
+
requires:
|
|
28
|
+
- '@tanstack/hotkeys#hotkeys-core'
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
# TanStack Hotkeys in React
|
|
32
|
+
|
|
33
|
+
Before starting, load the `@tanstack/hotkeys#hotkeys-core` skill (`npx @tanstack/intent@latest load @tanstack/hotkeys#hotkeys-core`).
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
import { formatForDisplay, useHotkey } from '@tanstack/react-hotkeys'
|
|
37
|
+
|
|
38
|
+
export function SaveButton({ onSave }: { onSave: () => void }) {
|
|
39
|
+
useHotkey('Mod+S', () => onSave(), { meta: { name: 'Save' } })
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<button onClick={onSave}>
|
|
43
|
+
Save <kbd>{formatForDisplay('Mod+S')}</kbd>
|
|
44
|
+
</button>
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## API
|
|
50
|
+
|
|
51
|
+
| API | What it's for |
|
|
52
|
+
| ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
|
|
53
|
+
| `useHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey while the component is mounted. |
|
|
54
|
+
| `useHotkeys(definitions, commonOptions?)` | Register several hotkeys, or a list built from data, in one call. |
|
|
55
|
+
| `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for the components inside it. |
|
|
56
|
+
| `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider. |
|
|
57
|
+
| `useDefaultHotkeysOptions()` | Read the default options merged from every provider above. |
|
|
58
|
+
| `useAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied, to pass to the register API. |
|
|
59
|
+
| `useAppHotkeyList()` | List every app hotkey with its status, including ones whose components aren't mounted, for a cheat sheet or settings screen. |
|
|
60
|
+
| `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
|
|
61
|
+
| `useHotkeyRecorder(options)` | Record a chord or sequence the user presses, for a settings screen. |
|
|
62
|
+
| `useHotkeyRegistrations()` | List every registered hotkey with its options and `meta`, for a cheat sheet of what's mounted. |
|
|
63
|
+
| `useHotkeyHint(hotkey, options?)` | Get whether held modifiers should reveal a shortcut's hint, such as labels that appear while Mod is held. |
|
|
64
|
+
| `useKeyHold(key)` | Get whether one key is held, such as Shift for a range-select mode. |
|
|
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 `formatForDisplay`, `matchesKeyboardEvent`, and `defineAppHotkeys`.
|
|
69
|
+
|
|
70
|
+
No provider is required. In React Server Components frameworks, use the hooks and `HotkeysProvider` in client components.
|
|
71
|
+
|
|
72
|
+
## A dialog that owns the keyboard
|
|
73
|
+
|
|
74
|
+
```tsx
|
|
75
|
+
import { useId } from 'react'
|
|
76
|
+
import type { ReactNode } from 'react'
|
|
77
|
+
import { HotkeysProvider, useHotkey } from '@tanstack/react-hotkeys'
|
|
78
|
+
|
|
79
|
+
export function Dialog(props: { onClose: () => void; children: ReactNode }) {
|
|
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: ReactNode
|
|
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` 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
|
+
- **`matchesKeyboardEvent` takes the DOM event.** In a widget's `onKeyDown`, pass `event.nativeEvent`.
|
|
107
|
+
- **In tests,** set `<HotkeysProvider defaultOptions={{ hotkey: { platform: 'mac' } }}>`.
|
|
108
|
+
|
|
109
|
+
When declaring app hotkeys, building a cheat sheet, or letting users rebind shortcuts, load `@tanstack/hotkeys#app-hotkeys` and read [app hotkeys in React](references/app-hotkeys.md).
|
|
110
|
+
|
|
111
|
+
## API discovery and docs
|
|
112
|
+
|
|
113
|
+
Signatures: `node_modules/@tanstack/react-hotkeys/dist/*.d.ts`. Guides: `https://tanstack.com/hotkeys/latest/docs/framework/react/guides/<page>.md`, such as `hotkeys`, `hotkeys-provider-and-scopes`, or `app-hotkeys`. Devtools: `hotkeysDevtoolsPlugin()` from `@tanstack/react-hotkeys-devtools` in `@tanstack/react-devtools`.
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# App hotkeys in React
|
|
2
|
+
|
|
3
|
+
The `@tanstack/hotkeys#app-hotkeys` skill owns the rules. This is the React wiring.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
// app-hotkeys.ts
|
|
7
|
+
import { defineAppHotkeys } from '@tanstack/react-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/react-hotkeys' {
|
|
15
|
+
interface Register {
|
|
16
|
+
appHotkeys: typeof appHotkeys
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import type { ReactNode } from 'react'
|
|
23
|
+
import { HotkeysProvider } from '@tanstack/react-hotkeys'
|
|
24
|
+
import type { CustomHotkeys } from '@tanstack/react-hotkeys'
|
|
25
|
+
import { appHotkeys } from './app-hotkeys'
|
|
26
|
+
|
|
27
|
+
const saved: CustomHotkeys = JSON.parse(localStorage.getItem('hotkeys') ?? '{}')
|
|
28
|
+
|
|
29
|
+
export function Root({ children }: { children: ReactNode }) {
|
|
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/react-hotkeys'
|
|
55
|
+
import type { AppHotkeyId } from '@tanstack/react-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
|
+
```
|