@tanstack/solid-hotkeys 0.22.1 → 0.24.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/HotkeysProvider.d.ts +8 -0
- package/dist/appHotkeys.d.ts +10 -5
- package/dist/appHotkeys.js +2 -2
- package/dist/createFormatForDisplay.d.ts +8 -2
- package/dist/createFormatForDisplay.js +13 -3
- package/dist/createHotkeyRegistrations.d.ts +1 -0
- package/dist/createHotkeyRegistrations.js +1 -0
- package/dist/createKeyboardLayout.d.ts +28 -0
- package/dist/createKeyboardLayout.js +52 -0
- package/dist/formatForDisplay.d.ts +12 -0
- package/dist/formatForDisplay.js +15 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/package.json +2 -2
- package/skills/solid-hotkeys/SKILL.md +20 -20
- package/skills/solid-hotkeys/references/app-hotkeys.md +5 -4
|
@@ -9,6 +9,14 @@ export interface HotkeysProviderOptions {
|
|
|
9
9
|
* from the request with `detectPlatform(userAgent)`.
|
|
10
10
|
*/
|
|
11
11
|
platform?: HotkeyPlatform;
|
|
12
|
+
/**
|
|
13
|
+
* Whether labels from `createFormatForDisplay` follow the user's keyboard
|
|
14
|
+
* layout, so `[KeyQ]` reads `A` on a French AZERTY keyboard. The layout
|
|
15
|
+
* loads from the browser when the first label mounts. Only Chromium
|
|
16
|
+
* browsers report one. Set `false` to never ask the browser for it.
|
|
17
|
+
* Default: `true`.
|
|
18
|
+
*/
|
|
19
|
+
keyboardLayout?: boolean;
|
|
12
20
|
/**
|
|
13
21
|
* Defaults for every hotkey registered by `createHotkey` and `createHotkeys`.
|
|
14
22
|
* `requireReset` applies to chords and `timeout` applies to sequences.
|
package/dist/appHotkeys.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, RegisteredAppHotkeys } from "@tanstack/hotkeys";
|
|
1
|
+
import { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, RecordedKeyPresses, RegisteredAppHotkeys } from "@tanstack/hotkeys";
|
|
2
2
|
/**
|
|
3
3
|
* Returns an accessor for the effective app hotkeys from `HotkeysProvider`,
|
|
4
4
|
* typed by `Register`: each declaration with the user's custom hotkey applied.
|
|
@@ -22,15 +22,20 @@ export interface UseCustomHotkeysResult {
|
|
|
22
22
|
customHotkeys: () => CustomHotkeys;
|
|
23
23
|
/**
|
|
24
24
|
* Sets the user's hotkey for an app hotkey. `null` unbinds it.
|
|
25
|
-
* Returns conflicting app hotkeys; when any exist, nothing changes.
|
|
25
|
+
* Returns conflicting app hotkeys; when any exist, nothing changes. Pass
|
|
26
|
+
* the recorder's `context` for a recorded hotkey, as for `findConflicts`.
|
|
26
27
|
*/
|
|
27
|
-
setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null) => Array<AppHotkey>;
|
|
28
|
+
setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
|
|
28
29
|
/** Restores the default hotkey for an app hotkey. */
|
|
29
30
|
resetCustomHotkey: (id: keyof RegisteredAppHotkeys & string) => void;
|
|
30
31
|
/** Restores every default hotkey. */
|
|
31
32
|
resetCustomHotkeys: () => void;
|
|
32
|
-
/**
|
|
33
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Other app hotkeys that would collide if `id` used `hotkey`. Pass the
|
|
35
|
+
* recorder's `context` for a recorded hotkey: it has the key press, which
|
|
36
|
+
* also finds a hotkey written differently that the same press fires.
|
|
37
|
+
*/
|
|
38
|
+
findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
|
|
34
39
|
}
|
|
35
40
|
/**
|
|
36
41
|
* Reads and changes the user's custom hotkeys. Changes apply to every
|
package/dist/appHotkeys.js
CHANGED
|
@@ -54,10 +54,10 @@ function useCustomHotkeys() {
|
|
|
54
54
|
state();
|
|
55
55
|
return store().customHotkeys;
|
|
56
56
|
}),
|
|
57
|
-
setCustomHotkey: (id, hotkey) => store().setCustomHotkey(id, hotkey),
|
|
57
|
+
setCustomHotkey: (id, hotkey, recordedWith) => store().setCustomHotkey(id, hotkey, recordedWith),
|
|
58
58
|
resetCustomHotkey: (id) => store().resetCustomHotkey(id),
|
|
59
59
|
resetCustomHotkeys: () => store().resetCustomHotkeys(),
|
|
60
|
-
findConflicts: (id, hotkey) => store().findConflicts(id, hotkey)
|
|
60
|
+
findConflicts: (id, hotkey, recordedWith) => store().findConflicts(id, hotkey, recordedWith)
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
63
|
|
|
@@ -5,14 +5,20 @@ import { formatForDisplay } from "@tanstack/hotkeys";
|
|
|
5
5
|
* function works anywhere, and JSX updates when the provider's platform
|
|
6
6
|
* changes. An explicit `platform` option wins.
|
|
7
7
|
*
|
|
8
|
+
* Physical bindings such as `[KeyQ]` are labeled with the character the key
|
|
9
|
+
* types for this user in Chromium browsers, once the keyboard layout has
|
|
10
|
+
* loaded, and JSX updates when the layout arrives. See `createKeyboardLayout`.
|
|
11
|
+
* An explicit `layoutMap` option wins. Firefox and Safari report no layout, so
|
|
12
|
+
* labels there keep the key's US character.
|
|
13
|
+
*
|
|
8
14
|
* Server-rendered apps should set the provider's `defaultOptions.platform`
|
|
9
15
|
* from the request user agent with `detectPlatform(userAgent)`.
|
|
10
16
|
*
|
|
11
17
|
* @example
|
|
12
18
|
* ```tsx
|
|
13
19
|
* function SaveButton() {
|
|
14
|
-
* const
|
|
15
|
-
* return <kbd>{
|
|
20
|
+
* const formatForDisplay = createFormatForDisplay()
|
|
21
|
+
* return <kbd>{formatForDisplay('Mod+S')}</kbd>
|
|
16
22
|
* }
|
|
17
23
|
* ```
|
|
18
24
|
*/
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { createKeyboardLayout } from "./createKeyboardLayout.js";
|
|
1
2
|
import { useDefaultHotkeysOptionsSource } from "./defaultOptions.js";
|
|
2
3
|
import { formatForDisplay } from "@tanstack/hotkeys";
|
|
4
|
+
import { namesPhysicalKey } from "@tanstack/hotkeys/adapter";
|
|
3
5
|
|
|
4
6
|
// src/createFormatForDisplay.ts
|
|
5
7
|
/**
|
|
@@ -8,22 +10,30 @@ import { formatForDisplay } from "@tanstack/hotkeys";
|
|
|
8
10
|
* function works anywhere, and JSX updates when the provider's platform
|
|
9
11
|
* changes. An explicit `platform` option wins.
|
|
10
12
|
*
|
|
13
|
+
* Physical bindings such as `[KeyQ]` are labeled with the character the key
|
|
14
|
+
* types for this user in Chromium browsers, once the keyboard layout has
|
|
15
|
+
* loaded, and JSX updates when the layout arrives. See `createKeyboardLayout`.
|
|
16
|
+
* An explicit `layoutMap` option wins. Firefox and Safari report no layout, so
|
|
17
|
+
* labels there keep the key's US character.
|
|
18
|
+
*
|
|
11
19
|
* Server-rendered apps should set the provider's `defaultOptions.platform`
|
|
12
20
|
* from the request user agent with `detectPlatform(userAgent)`.
|
|
13
21
|
*
|
|
14
22
|
* @example
|
|
15
23
|
* ```tsx
|
|
16
24
|
* function SaveButton() {
|
|
17
|
-
* const
|
|
18
|
-
* return <kbd>{
|
|
25
|
+
* const formatForDisplay = createFormatForDisplay()
|
|
26
|
+
* return <kbd>{formatForDisplay('Mod+S')}</kbd>
|
|
19
27
|
* }
|
|
20
28
|
* ```
|
|
21
29
|
*/
|
|
22
30
|
function createFormatForDisplay() {
|
|
23
31
|
const defaultOptions = useDefaultHotkeysOptionsSource();
|
|
32
|
+
const layoutMap = createKeyboardLayout();
|
|
24
33
|
return ((hotkey, options) => formatForDisplay(hotkey, {
|
|
25
34
|
...options,
|
|
26
|
-
platform: options?.platform ?? defaultOptions().platform
|
|
35
|
+
platform: options?.platform ?? defaultOptions().platform,
|
|
36
|
+
layoutMap: options?.layoutMap ?? (namesPhysicalKey(hotkey) ? layoutMap() ?? void 0 : void 0)
|
|
27
37
|
}));
|
|
28
38
|
}
|
|
29
39
|
|
|
@@ -10,6 +10,7 @@ import { HotkeyRegistrationView } from "@tanstack/hotkeys";
|
|
|
10
10
|
* @example
|
|
11
11
|
* ```tsx
|
|
12
12
|
* const registrations = createHotkeyRegistrations()
|
|
13
|
+
* const formatForDisplay = createFormatForDisplay()
|
|
13
14
|
* <For each={registrations()}>
|
|
14
15
|
* {(reg) => <li>{reg.options.meta?.name}: {formatForDisplay(reg.hotkey)}</li>}
|
|
15
16
|
* </For>
|
|
@@ -13,6 +13,7 @@ import { getHotkeyManager, toHotkeyRegistrationView } from "@tanstack/hotkeys";
|
|
|
13
13
|
* @example
|
|
14
14
|
* ```tsx
|
|
15
15
|
* const registrations = createHotkeyRegistrations()
|
|
16
|
+
* const formatForDisplay = createFormatForDisplay()
|
|
16
17
|
* <For each={registrations()}>
|
|
17
18
|
* {(reg) => <li>{reg.options.meta?.name}: {formatForDisplay(reg.hotkey)}</li>}
|
|
18
19
|
* </For>
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { KeyboardLayoutMap } from "@tanstack/hotkeys";
|
|
2
|
+
/**
|
|
3
|
+
* SolidJS primitive that returns a signal of the browser's keyboard layout,
|
|
4
|
+
* or `null` until it loads and in browsers that don't report one. The layout
|
|
5
|
+
* maps each physical code to the character it types, such as `KeyQ` to `a` on
|
|
6
|
+
* a French AZERTY keyboard. Only Chromium browsers report one.
|
|
7
|
+
*
|
|
8
|
+
* The layout loads after the first component that uses this primitive mounts,
|
|
9
|
+
* so server-rendered output hydrates unchanged. `createFormatForDisplay` uses
|
|
10
|
+
* this primitive, so you only need it for labels you draw yourself.
|
|
11
|
+
*
|
|
12
|
+
* To never ask the browser for the layout, set `keyboardLayout: false` in
|
|
13
|
+
* `HotkeysProvider`'s `defaultOptions`. The signal then stays `null`.
|
|
14
|
+
*
|
|
15
|
+
* @returns Signal accessor for the keyboard layout, or `null`
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* function MoveKeys() {
|
|
20
|
+
* const layoutMap = createKeyboardLayout()
|
|
21
|
+
* const label = (code: string) =>
|
|
22
|
+
* (layoutMap()?.get(code) ?? code).toUpperCase()
|
|
23
|
+
*
|
|
24
|
+
* return <kbd>{label('KeyW')}</kbd>
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function createKeyboardLayout(): () => KeyboardLayoutMap | null;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useDefaultHotkeysOptionsSource } from "./defaultOptions.js";
|
|
2
|
+
import { getKeyboardLayoutStore, loadKeyboardLayout } from "@tanstack/hotkeys";
|
|
3
|
+
import { createEffect, createMemo, createSignal, onCleanup } from "solid-js";
|
|
4
|
+
|
|
5
|
+
// src/createKeyboardLayout.ts
|
|
6
|
+
/**
|
|
7
|
+
* SolidJS primitive that returns a signal of the browser's keyboard layout,
|
|
8
|
+
* or `null` until it loads and in browsers that don't report one. The layout
|
|
9
|
+
* maps each physical code to the character it types, such as `KeyQ` to `a` on
|
|
10
|
+
* a French AZERTY keyboard. Only Chromium browsers report one.
|
|
11
|
+
*
|
|
12
|
+
* The layout loads after the first component that uses this primitive mounts,
|
|
13
|
+
* so server-rendered output hydrates unchanged. `createFormatForDisplay` uses
|
|
14
|
+
* this primitive, so you only need it for labels you draw yourself.
|
|
15
|
+
*
|
|
16
|
+
* To never ask the browser for the layout, set `keyboardLayout: false` in
|
|
17
|
+
* `HotkeysProvider`'s `defaultOptions`. The signal then stays `null`.
|
|
18
|
+
*
|
|
19
|
+
* @returns Signal accessor for the keyboard layout, or `null`
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* function MoveKeys() {
|
|
24
|
+
* const layoutMap = createKeyboardLayout()
|
|
25
|
+
* const label = (code: string) =>
|
|
26
|
+
* (layoutMap()?.get(code) ?? code).toUpperCase()
|
|
27
|
+
*
|
|
28
|
+
* return <kbd>{label('KeyW')}</kbd>
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
function createKeyboardLayout() {
|
|
33
|
+
const defaultOptions = useDefaultHotkeysOptionsSource();
|
|
34
|
+
const enabled = createMemo(() => defaultOptions().keyboardLayout !== false);
|
|
35
|
+
const [layoutMap, setLayoutMap] = createSignal(null);
|
|
36
|
+
createEffect(() => {
|
|
37
|
+
if (!enabled()) return;
|
|
38
|
+
const store = getKeyboardLayoutStore();
|
|
39
|
+
setLayoutMap(() => store.get());
|
|
40
|
+
const { unsubscribe } = store.subscribe((state) => {
|
|
41
|
+
setLayoutMap(() => state);
|
|
42
|
+
});
|
|
43
|
+
const stop = loadKeyboardLayout();
|
|
44
|
+
onCleanup(() => {
|
|
45
|
+
unsubscribe();
|
|
46
|
+
stop();
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
return () => enabled() ? layoutMap() : null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { createKeyboardLayout };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { formatForDisplay } from "@tanstack/hotkeys";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a hotkey as a label, without the provider's platform or the user's
|
|
4
|
+
* keyboard layout.
|
|
5
|
+
*
|
|
6
|
+
* @deprecated Use `createFormatForDisplay()` 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 formatForDisplay$1: typeof formatForDisplay;
|
|
12
|
+
export { formatForDisplay$1 as formatForDisplay };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { formatForDisplay } from "@tanstack/hotkeys";
|
|
2
|
+
|
|
3
|
+
// src/formatForDisplay.ts
|
|
4
|
+
/**
|
|
5
|
+
* Formats a hotkey as a label, without the provider's platform or the user's
|
|
6
|
+
* keyboard layout.
|
|
7
|
+
*
|
|
8
|
+
* @deprecated Use `createFormatForDisplay()` 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 formatForDisplay$1 = formatForDisplay;
|
|
14
|
+
|
|
15
|
+
export { formatForDisplay$1 as formatForDisplay };
|
package/dist/index.d.ts
CHANGED
|
@@ -6,9 +6,11 @@ import { createHotkeyRegistrations } from "./createHotkeyRegistrations.js";
|
|
|
6
6
|
import { createHotkeyScopesState } from "./createHotkeyScopesState.js";
|
|
7
7
|
import { createHotkeyHint } from "./createHotkeyHint.js";
|
|
8
8
|
import { createFormatForDisplay } from "./createFormatForDisplay.js";
|
|
9
|
+
import { formatForDisplay } from "./formatForDisplay.js";
|
|
10
|
+
import { createKeyboardLayout } from "./createKeyboardLayout.js";
|
|
9
11
|
import { UseCustomHotkeysResult, createAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./appHotkeys.js";
|
|
10
12
|
import { createHeldKeys } from "./createHeldKeys.js";
|
|
11
13
|
import { createHeldKeyCodes } from "./createHeldKeyCodes.js";
|
|
12
14
|
import { createKeyHold } from "./createKeyHold.js";
|
|
13
15
|
export * from "@tanstack/hotkeys";
|
|
14
|
-
export { CreateAppHotkeyOptions, CreateHotkeyChordOptions, CreateHotkeyCommonOptions, CreateHotkeyDefinition, CreateHotkeyOptions, CreateHotkeyOptionsFor, CreateHotkeySequenceOptions, CreateHotkeyTarget, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, UseCustomHotkeysResult, createAppHotkeyList, createFormatForDisplay, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyHint, createHotkeyRecorder, createHotkeyRegistrations, createHotkeyScopesState, createHotkeys, createKeyHold, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useHotkeysContext };
|
|
16
|
+
export { CreateAppHotkeyOptions, CreateHotkeyChordOptions, CreateHotkeyCommonOptions, CreateHotkeyDefinition, CreateHotkeyOptions, CreateHotkeyOptionsFor, CreateHotkeySequenceOptions, CreateHotkeyTarget, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, UseCustomHotkeysResult, createAppHotkeyList, createFormatForDisplay, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyHint, createHotkeyRecorder, createHotkeyRegistrations, createHotkeyScopesState, createHotkeys, createKeyHold, createKeyboardLayout, formatForDisplay, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useHotkeysContext };
|
package/dist/index.js
CHANGED
|
@@ -6,6 +6,8 @@ import { createHotkeyRegistrations } from "./createHotkeyRegistrations.js";
|
|
|
6
6
|
import { createHotkeyScopesState } from "./createHotkeyScopesState.js";
|
|
7
7
|
import { createHotkeyHint } from "./createHotkeyHint.js";
|
|
8
8
|
import { createFormatForDisplay } from "./createFormatForDisplay.js";
|
|
9
|
+
import { formatForDisplay } from "./formatForDisplay.js";
|
|
10
|
+
import { createKeyboardLayout } from "./createKeyboardLayout.js";
|
|
9
11
|
import { createAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./appHotkeys.js";
|
|
10
12
|
import { createHeldKeys } from "./createHeldKeys.js";
|
|
11
13
|
import { createHeldKeyCodes } from "./createHeldKeyCodes.js";
|
|
@@ -13,4 +15,4 @@ import { createKeyHold } from "./createKeyHold.js";
|
|
|
13
15
|
|
|
14
16
|
export * from "@tanstack/hotkeys"
|
|
15
17
|
|
|
16
|
-
export { HotkeysProvider, createAppHotkeyList, createFormatForDisplay, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyHint, createHotkeyRecorder, createHotkeyRegistrations, createHotkeyScopesState, createHotkeys, createKeyHold, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useHotkeysContext };
|
|
18
|
+
export { HotkeysProvider, createAppHotkeyList, createFormatForDisplay, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyHint, createHotkeyRecorder, createHotkeyRegistrations, createHotkeyScopesState, createHotkeys, createKeyHold, createKeyboardLayout, formatForDisplay, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useHotkeysContext };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/solid-hotkeys",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.24.0",
|
|
4
4
|
"description": "SolidJS adapter for TanStack Hotkeys",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"skills"
|
|
39
39
|
],
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@tanstack/hotkeys": "0.
|
|
41
|
+
"@tanstack/hotkeys": "0.29.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@solidjs/testing-library": "^0.8.10",
|
|
@@ -54,26 +54,26 @@ export function Editor() {
|
|
|
54
54
|
|
|
55
55
|
## API
|
|
56
56
|
|
|
57
|
-
| API | What it's for
|
|
58
|
-
| -------------------------------------------- |
|
|
59
|
-
| `createHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey for the owning component's lifetime; the hotkey and options can be accessors.
|
|
60
|
-
| `createHotkeys(definitions, commonOptions?)` | Register several hotkeys, or an accessor of a list built from data, in one call.
|
|
61
|
-
| `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for the components inside it.
|
|
62
|
-
| `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider.
|
|
63
|
-
| `useDefaultHotkeysOptions()` | Read the default options merged from every provider above.
|
|
64
|
-
| `createFormatForDisplay()` | Get
|
|
65
|
-
| `useAppHotkeys()` | Get an accessor of the declared app hotkeys with the user's custom hotkeys applied.
|
|
66
|
-
| `createAppHotkeyList()` | Get an accessor listing every app hotkey with its status, for a cheat sheet or settings screen.
|
|
67
|
-
| `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts.
|
|
68
|
-
| `createHotkeyRecorder(options)` | Record a chord or sequence the user presses; its state fields are accessors.
|
|
69
|
-
| `createHotkeyRegistrations()` | Get an accessor listing every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope).
|
|
70
|
-
| `createHotkeyScopesState()` | Get an accessor of the active scopes and the exclusive scope that owns the keyboard.
|
|
71
|
-
| `createHotkeyHint(hotkey, options?)` | Get an accessor of whether held modifiers should reveal a shortcut's hint. False unless a registration of the hotkey can fire: enabled, in an active scope, and with focus inside its element target.
|
|
72
|
-
| `createKeyHold(key)` | Get an accessor of whether one key is held. Letters match in any case; other names, such as `Shift`, are case-sensitive.
|
|
73
|
-
| `createHeldKeys()` | Get an accessor of every key that's held right now.
|
|
74
|
-
| `createHeldKeyCodes()` | Get an accessor mapping each held key to its physical `event.code`.
|
|
57
|
+
| API | What it's for |
|
|
58
|
+
| -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
59
|
+
| `createHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey for the owning component's lifetime; the hotkey and options can be accessors. |
|
|
60
|
+
| `createHotkeys(definitions, commonOptions?)` | Register several hotkeys, or an accessor of a list built from data, in one call. |
|
|
61
|
+
| `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for the components inside it. |
|
|
62
|
+
| `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider. |
|
|
63
|
+
| `useDefaultHotkeysOptions()` | Read the default options merged from every provider above. |
|
|
64
|
+
| `createFormatForDisplay()` | 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 = createFormatForDisplay()` in the component, then `formatForDisplay(hotkey)`. |
|
|
65
|
+
| `useAppHotkeys()` | Get an accessor of the declared app hotkeys with the user's custom hotkeys applied. |
|
|
66
|
+
| `createAppHotkeyList()` | Get an accessor listing every app hotkey with its status, for a cheat sheet or settings screen. |
|
|
67
|
+
| `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
|
|
68
|
+
| `createHotkeyRecorder(options)` | Record a chord or sequence the user presses; its state fields are accessors. |
|
|
69
|
+
| `createHotkeyRegistrations()` | Get an accessor listing every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
|
|
70
|
+
| `createHotkeyScopesState()` | Get an accessor of the active scopes and the exclusive scope that owns the keyboard. |
|
|
71
|
+
| `createHotkeyHint(hotkey, options?)` | Get an accessor of whether held modifiers should reveal a shortcut's hint. False unless a registration of the hotkey can fire: enabled, in an active scope, and with focus inside its element target. |
|
|
72
|
+
| `createKeyHold(key)` | Get an accessor of whether one key is held. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
|
|
73
|
+
| `createHeldKeys()` | Get an accessor of every key that's held right now. |
|
|
74
|
+
| `createHeldKeyCodes()` | Get an accessor mapping each held key to its physical `event.code`. |
|
|
75
75
|
|
|
76
|
-
The package also re-exports `@tanstack/hotkeys`, such as `formatForDisplay
|
|
76
|
+
The package also re-exports `@tanstack/hotkeys`, such as `matchesKeyboardEvent` and `defineAppHotkeys`. Its plain `formatForDisplay` export is deprecated: use `createFormatForDisplay()` in components. Outside a component, call the plain function and pass `platform`.
|
|
77
77
|
|
|
78
78
|
The state these return is accessors, including the recorder's fields. No provider is required.
|
|
79
79
|
|
|
@@ -85,7 +85,7 @@ The state these return is accessors, including the recorder's fields. No provide
|
|
|
85
85
|
- **The callback isn't tracked.** Read props inside it, as `() => props.onClose()`, rather than passing `props.onClose` itself.
|
|
86
86
|
- **Register a dialog's hotkeys in a child of its provider.** A primitive uses the nearest `HotkeysProvider` above its component, so a dialog that renders `<HotkeysProvider scope={...} exclusive>` and creates `Escape` itself puts it in the suspended outer scope. Give each open dialog its own scope name, such as `` `dialog-${createUniqueId()}` ``.
|
|
87
87
|
- **Call the primitives directly.** A `createShortcut(keys: string)` wrapper has to cast strings to hotkeys, which turns off type checking and drops sequences, accessor options, and app hotkeys.
|
|
88
|
-
- **Server-rendered labels need the user's platform.**
|
|
88
|
+
- **Server-rendered labels need the user's platform.** The server can't see the user's OS, so without a platform it renders Linux labels, and Solid keeps them after hydration for Mac and Windows users. Set the provider's `defaultOptions.platform` to `detectPlatform(userAgent)` from the request's `User-Agent` header. `createFormatForDisplay()` reads it.
|
|
89
89
|
|
|
90
90
|
When declaring app hotkeys, building a cheat sheet, or letting users rebind shortcuts, load `@tanstack/hotkeys#app-hotkeys` and read [app hotkeys in Solid](references/app-hotkeys.md).
|
|
91
91
|
|
|
@@ -47,25 +47,26 @@ A settings row records with `createHotkeyRecorder` and saves through `useCustomH
|
|
|
47
47
|
|
|
48
48
|
```tsx
|
|
49
49
|
import {
|
|
50
|
+
createFormatForDisplay,
|
|
50
51
|
createHotkeyRecorder,
|
|
51
|
-
formatForDisplay,
|
|
52
52
|
useAppHotkeys,
|
|
53
53
|
useCustomHotkeys,
|
|
54
54
|
} from '@tanstack/solid-hotkeys'
|
|
55
55
|
import type { AppHotkeyId } from '@tanstack/solid-hotkeys'
|
|
56
56
|
|
|
57
57
|
export function ShortcutRow(props: { id: AppHotkeyId }) {
|
|
58
|
+
const formatForDisplay = createFormatForDisplay()
|
|
58
59
|
const appHotkeys = useAppHotkeys()
|
|
59
60
|
const appHotkey = () => appHotkeys()[props.id]
|
|
60
61
|
const { findConflicts, setCustomHotkey, resetCustomHotkey } =
|
|
61
62
|
useCustomHotkeys()
|
|
62
63
|
const recorder = createHotkeyRecorder({
|
|
63
64
|
maxSteps: Infinity,
|
|
64
|
-
validate: (hotkey) => {
|
|
65
|
-
const [conflict] = findConflicts(props.id, hotkey)
|
|
65
|
+
validate: (hotkey, context) => {
|
|
66
|
+
const [conflict] = findConflicts(props.id, hotkey, context)
|
|
66
67
|
return conflict ? `Used by ${conflict.meta?.name ?? conflict.id}` : true
|
|
67
68
|
},
|
|
68
|
-
onRecord: (hotkey) => setCustomHotkey(props.id, hotkey),
|
|
69
|
+
onRecord: (hotkey, context) => setCustomHotkey(props.id, hotkey, context),
|
|
69
70
|
onClear: () => setCustomHotkey(props.id, null),
|
|
70
71
|
})
|
|
71
72
|
|