@tanstack/preact-hotkeys 0.21.1 → 0.23.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/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/dist/useAppHotkeys.d.ts +16 -8
- package/dist/useAppHotkeys.js +8 -5
- package/dist/useFormatForDisplay.d.ts +8 -2
- package/dist/useFormatForDisplay.js +13 -4
- package/dist/useHotkeyRecorder.d.ts +1 -0
- package/dist/useHotkeyRegistrations.d.ts +1 -0
- package/dist/useHotkeyRegistrations.js +1 -0
- package/dist/useKeyboardLayout.d.ts +25 -0
- package/dist/useKeyboardLayout.js +46 -0
- package/package.json +2 -2
- package/skills/preact-hotkeys/SKILL.md +23 -22
- package/skills/preact-hotkeys/references/app-hotkeys.md +5 -4
|
@@ -10,6 +10,14 @@ export interface HotkeysProviderOptions {
|
|
|
10
10
|
* `detectPlatform(userAgent)`.
|
|
11
11
|
*/
|
|
12
12
|
platform?: HotkeyPlatform;
|
|
13
|
+
/**
|
|
14
|
+
* Whether labels from `useFormatForDisplay` follow the user's keyboard
|
|
15
|
+
* layout, so `[KeyQ]` reads `A` on a French AZERTY keyboard. The layout
|
|
16
|
+
* loads from the browser when the first label mounts. Only Chromium
|
|
17
|
+
* browsers report one. Set `false` to never ask the browser for it.
|
|
18
|
+
* Default: `true`.
|
|
19
|
+
*/
|
|
20
|
+
keyboardLayout?: boolean;
|
|
13
21
|
/**
|
|
14
22
|
* Defaults for every hotkey registered by `useHotkey` and `useHotkeys`.
|
|
15
23
|
* `requireReset` applies to chords and `timeout` applies to sequences.
|
|
@@ -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 `useFormatForDisplay()` 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 `useFormatForDisplay()` 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 { 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 { formatForDisplay } from "./formatForDisplay.js";
|
|
10
|
+
import { useKeyboardLayout } from "./useKeyboardLayout.js";
|
|
9
11
|
import { UseCustomHotkeysResult, useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
|
|
10
12
|
import { useHeldKeys } from "./useHeldKeys.js";
|
|
11
13
|
import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
|
|
12
14
|
import { useKeyHold } from "./useKeyHold.js";
|
|
13
15
|
export * from "@tanstack/hotkeys";
|
|
14
|
-
export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, PreactHotkeyRecorder, UseAppHotkeyOptions, UseCustomHotkeysResult, UseHotkeyChordOptions, UseHotkeyCommonOptions, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeyOptionsFor, UseHotkeySequenceOptions, UseHotkeyTarget, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold };
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -6,6 +6,8 @@ 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 { formatForDisplay } from "./formatForDisplay.js";
|
|
10
|
+
import { useKeyboardLayout } from "./useKeyboardLayout.js";
|
|
9
11
|
import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
|
|
10
12
|
import { useHeldKeys } from "./useHeldKeys.js";
|
|
11
13
|
import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
|
|
@@ -13,4 +15,4 @@ import { useKeyHold } from "./useKeyHold.js";
|
|
|
13
15
|
|
|
14
16
|
export * from "@tanstack/hotkeys"
|
|
15
17
|
|
|
16
|
-
export { HotkeysProvider, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold };
|
|
18
|
+
export { HotkeysProvider, formatForDisplay, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold, useKeyboardLayout };
|
package/dist/useAppHotkeys.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 the effective app hotkeys from `HotkeysProvider`, typed by
|
|
4
4
|
* `Register`: each declaration with the user's custom hotkey applied.
|
|
@@ -19,7 +19,9 @@ export declare function useAppHotkeys(): RegisteredAppHotkeys;
|
|
|
19
19
|
* @example
|
|
20
20
|
* ```tsx
|
|
21
21
|
* function CheatSheet() {
|
|
22
|
+
* const formatForDisplay = useFormatForDisplay()
|
|
22
23
|
* const items = useAppHotkeyList()
|
|
24
|
+
*
|
|
23
25
|
* return (
|
|
24
26
|
* <ul>
|
|
25
27
|
* {items.map(({ appHotkey, status }) => (
|
|
@@ -40,15 +42,20 @@ export interface UseCustomHotkeysResult {
|
|
|
40
42
|
customHotkeys: CustomHotkeys;
|
|
41
43
|
/**
|
|
42
44
|
* Sets the user's hotkey for an app hotkey. `null` unbinds it.
|
|
43
|
-
* Returns conflicting app hotkeys; when any exist, nothing changes.
|
|
45
|
+
* Returns conflicting app hotkeys; when any exist, nothing changes. Pass
|
|
46
|
+
* the recorder's `context` for a recorded hotkey, as for `findConflicts`.
|
|
44
47
|
*/
|
|
45
|
-
setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null) => Array<AppHotkey>;
|
|
48
|
+
setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
|
|
46
49
|
/** Restores the default hotkey for an app hotkey. */
|
|
47
50
|
resetCustomHotkey: (id: keyof RegisteredAppHotkeys & string) => void;
|
|
48
51
|
/** Restores every default hotkey. */
|
|
49
52
|
resetCustomHotkeys: () => void;
|
|
50
|
-
/**
|
|
51
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Other app hotkeys that would collide if `id` used `hotkey`. Pass the
|
|
55
|
+
* recorder's `context` for a recorded hotkey: it has the key press, which
|
|
56
|
+
* also finds a hotkey written differently that the same press fires.
|
|
57
|
+
*/
|
|
58
|
+
findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
|
|
52
59
|
}
|
|
53
60
|
/**
|
|
54
61
|
* Reads and changes the user's custom hotkeys. Changes apply to every
|
|
@@ -60,9 +67,10 @@ export interface UseCustomHotkeysResult {
|
|
|
60
67
|
* const { setCustomHotkey, findConflicts } = useCustomHotkeys()
|
|
61
68
|
* const recorder = useHotkeyRecorder({
|
|
62
69
|
* maxSteps: Infinity,
|
|
63
|
-
* validate: (hotkey) =>
|
|
64
|
-
* findConflicts('doc.save', hotkey).length === 0 ||
|
|
65
|
-
*
|
|
70
|
+
* validate: (hotkey, context) =>
|
|
71
|
+
* findConflicts('doc.save', hotkey, context).length === 0 ||
|
|
72
|
+
* 'Already in use',
|
|
73
|
+
* onRecord: (hotkey, context) => setCustomHotkey('doc.save', hotkey, context),
|
|
66
74
|
* })
|
|
67
75
|
* ```
|
|
68
76
|
*/
|
package/dist/useAppHotkeys.js
CHANGED
|
@@ -29,7 +29,9 @@ function useAppHotkeys() {
|
|
|
29
29
|
* @example
|
|
30
30
|
* ```tsx
|
|
31
31
|
* function CheatSheet() {
|
|
32
|
+
* const formatForDisplay = useFormatForDisplay()
|
|
32
33
|
* const items = useAppHotkeyList()
|
|
34
|
+
*
|
|
33
35
|
* return (
|
|
34
36
|
* <ul>
|
|
35
37
|
* {items.map(({ appHotkey, status }) => (
|
|
@@ -58,9 +60,10 @@ function useAppHotkeyList() {
|
|
|
58
60
|
* const { setCustomHotkey, findConflicts } = useCustomHotkeys()
|
|
59
61
|
* const recorder = useHotkeyRecorder({
|
|
60
62
|
* maxSteps: Infinity,
|
|
61
|
-
* validate: (hotkey) =>
|
|
62
|
-
* findConflicts('doc.save', hotkey).length === 0 ||
|
|
63
|
-
*
|
|
63
|
+
* validate: (hotkey, context) =>
|
|
64
|
+
* findConflicts('doc.save', hotkey, context).length === 0 ||
|
|
65
|
+
* 'Already in use',
|
|
66
|
+
* onRecord: (hotkey, context) => setCustomHotkey('doc.save', hotkey, context),
|
|
64
67
|
* })
|
|
65
68
|
* ```
|
|
66
69
|
*/
|
|
@@ -68,10 +71,10 @@ function useCustomHotkeys() {
|
|
|
68
71
|
const store = useRequiredStore("useCustomHotkeys");
|
|
69
72
|
return {
|
|
70
73
|
customHotkeys: store.customHotkeys,
|
|
71
|
-
setCustomHotkey: (id, hotkey) => store.setCustomHotkey(id, hotkey),
|
|
74
|
+
setCustomHotkey: (id, hotkey, recordedWith) => store.setCustomHotkey(id, hotkey, recordedWith),
|
|
72
75
|
resetCustomHotkey: (id) => store.resetCustomHotkey(id),
|
|
73
76
|
resetCustomHotkeys: () => store.resetCustomHotkeys(),
|
|
74
|
-
findConflicts: (id, hotkey) => store.findConflicts(id, hotkey)
|
|
77
|
+
findConflicts: (id, hotkey, recordedWith) => store.findConflicts(id, hotkey, recordedWith)
|
|
75
78
|
};
|
|
76
79
|
}
|
|
77
80
|
|
|
@@ -4,6 +4,12 @@ import { formatForDisplay } from "@tanstack/hotkeys";
|
|
|
4
4
|
* detected platform without one. A `platform` passed to the returned function
|
|
5
5
|
* wins.
|
|
6
6
|
*
|
|
7
|
+
* Physical bindings such as `[KeyQ]` are labeled with the character the key
|
|
8
|
+
* types for this user in Chromium browsers, once the keyboard layout has
|
|
9
|
+
* loaded. See `useKeyboardLayout`. A `layoutMap` passed to the returned
|
|
10
|
+
* function wins. Firefox and Safari report no layout, so labels there keep the
|
|
11
|
+
* key's US character.
|
|
12
|
+
*
|
|
7
13
|
* Call the hook once at the top of the component; the returned function can
|
|
8
14
|
* be called anywhere, such as in a loop or a condition. Server-rendered apps
|
|
9
15
|
* should set the provider's `defaultOptions.platform` from the request with
|
|
@@ -12,11 +18,11 @@ import { formatForDisplay } from "@tanstack/hotkeys";
|
|
|
12
18
|
* @example
|
|
13
19
|
* ```tsx
|
|
14
20
|
* function SaveButton() {
|
|
15
|
-
* const
|
|
21
|
+
* const formatForDisplay = useFormatForDisplay()
|
|
16
22
|
*
|
|
17
23
|
* return (
|
|
18
24
|
* <button>
|
|
19
|
-
* Save <kbd>{
|
|
25
|
+
* Save <kbd>{formatForDisplay('Mod+S')}</kbd>
|
|
20
26
|
* </button>
|
|
21
27
|
* )
|
|
22
28
|
* }
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
|
|
2
|
+
import { useKeyboardLayout } from "./useKeyboardLayout.js";
|
|
2
3
|
import { formatForDisplay } from "@tanstack/hotkeys";
|
|
3
4
|
import { useCallback } from "preact/hooks";
|
|
4
5
|
|
|
@@ -8,6 +9,12 @@ import { useCallback } from "preact/hooks";
|
|
|
8
9
|
* detected platform without one. A `platform` passed to the returned function
|
|
9
10
|
* wins.
|
|
10
11
|
*
|
|
12
|
+
* Physical bindings such as `[KeyQ]` are labeled with the character the key
|
|
13
|
+
* types for this user in Chromium browsers, once the keyboard layout has
|
|
14
|
+
* loaded. See `useKeyboardLayout`. A `layoutMap` passed to the returned
|
|
15
|
+
* function wins. Firefox and Safari report no layout, so labels there keep the
|
|
16
|
+
* key's US character.
|
|
17
|
+
*
|
|
11
18
|
* Call the hook once at the top of the component; the returned function can
|
|
12
19
|
* be called anywhere, such as in a loop or a condition. Server-rendered apps
|
|
13
20
|
* should set the provider's `defaultOptions.platform` from the request with
|
|
@@ -16,11 +23,11 @@ import { useCallback } from "preact/hooks";
|
|
|
16
23
|
* @example
|
|
17
24
|
* ```tsx
|
|
18
25
|
* function SaveButton() {
|
|
19
|
-
* const
|
|
26
|
+
* const formatForDisplay = useFormatForDisplay()
|
|
20
27
|
*
|
|
21
28
|
* return (
|
|
22
29
|
* <button>
|
|
23
|
-
* Save <kbd>{
|
|
30
|
+
* Save <kbd>{formatForDisplay('Mod+S')}</kbd>
|
|
24
31
|
* </button>
|
|
25
32
|
* )
|
|
26
33
|
* }
|
|
@@ -28,10 +35,12 @@ import { useCallback } from "preact/hooks";
|
|
|
28
35
|
*/
|
|
29
36
|
function useFormatForDisplay() {
|
|
30
37
|
const platform = useDefaultHotkeysOptions().platform;
|
|
38
|
+
const layoutMap = useKeyboardLayout();
|
|
31
39
|
return useCallback((hotkey, options) => formatForDisplay(hotkey, {
|
|
32
40
|
...options,
|
|
33
|
-
platform: options?.platform ?? platform
|
|
34
|
-
|
|
41
|
+
platform: options?.platform ?? platform,
|
|
42
|
+
layoutMap: options?.layoutMap ?? layoutMap ?? void 0
|
|
43
|
+
}), [platform, layoutMap]);
|
|
35
44
|
}
|
|
36
45
|
|
|
37
46
|
export { useFormatForDisplay };
|
|
@@ -34,6 +34,7 @@ export interface PreactHotkeyRecorder<THotkey extends Hotkey = Hotkey> {
|
|
|
34
34
|
* @example
|
|
35
35
|
* ```tsx
|
|
36
36
|
* function ShortcutInput() {
|
|
37
|
+
* const formatForDisplay = useFormatForDisplay()
|
|
37
38
|
* const [shortcut, setShortcut] = useState<Hotkey>('Mod+S')
|
|
38
39
|
* const recorder = useHotkeyRecorder({ onRecord: setShortcut })
|
|
39
40
|
*
|
|
@@ -14,6 +14,7 @@ import { getHotkeyManager, toHotkeyRegistrationView } from "@tanstack/hotkeys";
|
|
|
14
14
|
* @example
|
|
15
15
|
* ```tsx
|
|
16
16
|
* function ShortcutPalette() {
|
|
17
|
+
* const formatForDisplay = useFormatForDisplay()
|
|
17
18
|
* const registrations = useHotkeyRegistrations()
|
|
18
19
|
*
|
|
19
20
|
* return (
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { KeyboardLayoutMap } from "@tanstack/hotkeys";
|
|
2
|
+
/**
|
|
3
|
+
* The browser's keyboard layout, or `null` until it loads and in browsers
|
|
4
|
+
* that don't report one. The layout maps each physical code to the character
|
|
5
|
+
* it types, such as `KeyQ` to `a` on a French AZERTY keyboard. Only Chromium
|
|
6
|
+
* browsers report one.
|
|
7
|
+
*
|
|
8
|
+
* The layout loads after the first component that uses this hook mounts, so
|
|
9
|
+
* server-rendered output hydrates unchanged. `useFormatForDisplay` uses this
|
|
10
|
+
* hook, 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`. This hook then returns `null`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* function MoveKeys() {
|
|
18
|
+
* const layoutMap = useKeyboardLayout()
|
|
19
|
+
* const label = (code: string) => (layoutMap?.get(code) ?? code).toUpperCase()
|
|
20
|
+
*
|
|
21
|
+
* return <kbd>{label('KeyW')}</kbd>
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function useKeyboardLayout(): KeyboardLayoutMap | null;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
|
|
2
|
+
import { getKeyboardLayoutStore, loadKeyboardLayout } from "@tanstack/hotkeys";
|
|
3
|
+
import { useLayoutEffect, useState } from "preact/hooks";
|
|
4
|
+
|
|
5
|
+
// src/useKeyboardLayout.ts
|
|
6
|
+
/**
|
|
7
|
+
* The browser's keyboard layout, or `null` until it loads and in browsers
|
|
8
|
+
* that don't report one. The layout maps each physical code to the character
|
|
9
|
+
* it types, such as `KeyQ` to `a` on a French AZERTY keyboard. Only Chromium
|
|
10
|
+
* browsers report one.
|
|
11
|
+
*
|
|
12
|
+
* The layout loads after the first component that uses this hook mounts, so
|
|
13
|
+
* server-rendered output hydrates unchanged. `useFormatForDisplay` uses this
|
|
14
|
+
* hook, 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`. This hook then returns `null`.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* function MoveKeys() {
|
|
22
|
+
* const layoutMap = useKeyboardLayout()
|
|
23
|
+
* const label = (code: string) => (layoutMap?.get(code) ?? code).toUpperCase()
|
|
24
|
+
*
|
|
25
|
+
* return <kbd>{label('KeyW')}</kbd>
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
function useKeyboardLayout() {
|
|
30
|
+
const enabled = useDefaultHotkeysOptions().keyboardLayout !== false;
|
|
31
|
+
const [layoutMap, setLayoutMap] = useState(null);
|
|
32
|
+
useLayoutEffect(() => {
|
|
33
|
+
if (!enabled) return;
|
|
34
|
+
const store = getKeyboardLayoutStore();
|
|
35
|
+
setLayoutMap(store.get());
|
|
36
|
+
const { unsubscribe } = store.subscribe(setLayoutMap);
|
|
37
|
+
const stop = loadKeyboardLayout();
|
|
38
|
+
return () => {
|
|
39
|
+
unsubscribe();
|
|
40
|
+
stop();
|
|
41
|
+
};
|
|
42
|
+
}, [enabled]);
|
|
43
|
+
return enabled ? layoutMap : null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { useKeyboardLayout };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/preact-hotkeys",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.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.
|
|
38
|
+
"@tanstack/hotkeys": "0.29.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@preact/preset-vite": "^2.10.6",
|
|
@@ -30,10 +30,11 @@ requires:
|
|
|
30
30
|
Before starting, load the `@tanstack/hotkeys#hotkeys-core` skill (`npx @tanstack/intent@latest load @tanstack/hotkeys#hotkeys-core`).
|
|
31
31
|
|
|
32
32
|
```tsx
|
|
33
|
-
import {
|
|
33
|
+
import { useFormatForDisplay, useHotkey } from '@tanstack/preact-hotkeys'
|
|
34
34
|
|
|
35
35
|
export function SaveButton({ onSave }: { onSave: () => void }) {
|
|
36
36
|
useHotkey('Mod+S', () => onSave(), { meta: { name: 'Save' } })
|
|
37
|
+
const formatForDisplay = useFormatForDisplay()
|
|
37
38
|
|
|
38
39
|
return (
|
|
39
40
|
<button onClick={onSave}>
|
|
@@ -45,26 +46,26 @@ export function SaveButton({ onSave }: { onSave: () => void }) {
|
|
|
45
46
|
|
|
46
47
|
## API
|
|
47
48
|
|
|
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
|
-
| `useFormatForDisplay()` | Get
|
|
56
|
-
| `useAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied, to pass to the register API.
|
|
57
|
-
| `useAppHotkeyList()` | List every app hotkey with its status, including ones whose components aren't mounted, for a cheat sheet or settings screen.
|
|
58
|
-
| `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts.
|
|
59
|
-
| `useHotkeyRecorder(options)` | Record a chord or sequence the user presses, for a settings screen.
|
|
60
|
-
| `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.
|
|
61
|
-
| `useHotkeyScopesState()` | Get the active scopes and the exclusive scope that owns the keyboard.
|
|
62
|
-
| `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.
|
|
63
|
-
| `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.
|
|
64
|
-
| `useHeldKeys()` | Get every key that's held right now.
|
|
65
|
-
| `useHeldKeyCodes()` | Map each held key to its physical `event.code`.
|
|
66
|
-
|
|
67
|
-
The package also re-exports `@tanstack/hotkeys`, such as `formatForDisplay
|
|
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`.
|
|
68
69
|
|
|
69
70
|
No provider is required.
|
|
70
71
|
|
|
@@ -107,7 +108,7 @@ A hook uses the nearest `HotkeysProvider` above its component. Registering `Esca
|
|
|
107
108
|
- **No dependency arrays or `useCallback`.** The hooks read the hotkey, callback, and options on every render, so inline callbacks always see current state.
|
|
108
109
|
- **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.
|
|
109
110
|
- **In tests,** set `<HotkeysProvider defaultOptions={{ platform: 'mac' }}>`. It applies to hotkeys, recorders, labels, and app hotkey conflict checks.
|
|
110
|
-
- **Server-rendered labels need the user's platform.**
|
|
111
|
+
- **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 that differ after hydration for Mac and Windows users. Set the provider's `defaultOptions.platform` to `detectPlatform(userAgent)` from the request's `User-Agent` header. `useFormatForDisplay()` reads it.
|
|
111
112
|
|
|
112
113
|
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).
|
|
113
114
|
|
|
@@ -47,24 +47,25 @@ A settings row records with `useHotkeyRecorder` and saves through `useCustomHotk
|
|
|
47
47
|
|
|
48
48
|
```tsx
|
|
49
49
|
import {
|
|
50
|
-
formatForDisplay,
|
|
51
50
|
useAppHotkeys,
|
|
52
51
|
useCustomHotkeys,
|
|
52
|
+
useFormatForDisplay,
|
|
53
53
|
useHotkeyRecorder,
|
|
54
54
|
} from '@tanstack/preact-hotkeys'
|
|
55
55
|
import type { AppHotkeyId } from '@tanstack/preact-hotkeys'
|
|
56
56
|
|
|
57
57
|
export function ShortcutRow({ id }: { id: AppHotkeyId }) {
|
|
58
|
+
const formatForDisplay = useFormatForDisplay()
|
|
58
59
|
const appHotkey = useAppHotkeys()[id]
|
|
59
60
|
const { findConflicts, setCustomHotkey, resetCustomHotkey } =
|
|
60
61
|
useCustomHotkeys()
|
|
61
62
|
const recorder = useHotkeyRecorder({
|
|
62
63
|
maxSteps: Infinity,
|
|
63
|
-
validate: (hotkey) => {
|
|
64
|
-
const [conflict] = findConflicts(id, hotkey)
|
|
64
|
+
validate: (hotkey, context) => {
|
|
65
|
+
const [conflict] = findConflicts(id, hotkey, context)
|
|
65
66
|
return conflict ? `Used by ${conflict.meta?.name ?? conflict.id}` : true
|
|
66
67
|
},
|
|
67
|
-
onRecord: (hotkey) => setCustomHotkey(id, hotkey),
|
|
68
|
+
onRecord: (hotkey, context) => setCustomHotkey(id, hotkey, context),
|
|
68
69
|
onClear: () => setCustomHotkey(id, null),
|
|
69
70
|
})
|
|
70
71
|
|