@tanstack/octane-hotkeys 0.12.0 → 0.14.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 +11 -2
- 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/types.d.ts +10 -5
- package/dist/useAppHotkeys.js +2 -2
- package/dist/useFormatForDisplay.d.ts +8 -2
- package/dist/useFormatForDisplay.js +5 -2
- package/dist/useKeyboardLayout.d.ts +25 -0
- package/dist/useKeyboardLayout.js +18 -0
- package/package.json +3 -3
- package/skills/octane-hotkeys/SKILL.md +22 -21
- package/skills/octane-hotkeys/references/app-hotkeys.md +5 -4
|
@@ -9,6 +9,14 @@ export interface DefaultHotkeysOptions {
|
|
|
9
9
|
* `detectPlatform(userAgent)`.
|
|
10
10
|
*/
|
|
11
11
|
platform?: HotkeyPlatform;
|
|
12
|
+
/**
|
|
13
|
+
* Whether labels from `useFormatForDisplay` 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 `useHotkey` and `useHotkeys`.
|
|
14
22
|
* `requireReset` applies to chords and `timeout` applies to sequences.
|
|
@@ -30,8 +38,9 @@ export interface HotkeysProviderProps extends HotkeysProviderScopeProps {
|
|
|
30
38
|
/** Merged with the parent provider's defaults. */
|
|
31
39
|
defaultOptions?: HotkeysProviderOptions;
|
|
32
40
|
/**
|
|
33
|
-
* Every app hotkey, from `defineAppHotkeys`.
|
|
34
|
-
*
|
|
41
|
+
* Every app hotkey, from `defineAppHotkeys`. Listed by `useAppHotkeyList`
|
|
42
|
+
* whether or not they are registered. It can start as `undefined` and arrive
|
|
43
|
+
* later, such as from a lazy import.
|
|
35
44
|
*/
|
|
36
45
|
appHotkeys?: AppHotkeys;
|
|
37
46
|
/**
|
|
@@ -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,10 +6,12 @@ import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
|
|
|
6
6
|
import { useKeyHold } from "./useKeyHold.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 { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
|
|
10
12
|
import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
|
|
11
13
|
import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
|
|
12
14
|
import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
|
|
13
15
|
import { HotkeyRegistrationsResult, OctaneHotkeyRecorder, UseCustomHotkeysResult } from "./types.js";
|
|
14
16
|
export * from "@tanstack/hotkeys";
|
|
15
|
-
export { type DefaultHotkeysOptions, HotkeyRegistrationsResult, HotkeysProvider, type HotkeysProviderOptions, type HotkeysProviderProps, OctaneHotkeyRecorder, UseAppHotkeyOptions, UseCustomHotkeysResult, UseHotkeyChordOptions, UseHotkeyCommonOptions, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeyOptionsFor, UseHotkeySequenceOptions, UseHotkeyTarget, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold };
|
|
17
|
+
export { type DefaultHotkeysOptions, HotkeyRegistrationsResult, HotkeysProvider, type HotkeysProviderOptions, type HotkeysProviderProps, OctaneHotkeyRecorder, 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 { useHeldKeyCodes } from "./useHeldKeyCodes.js";
|
|
|
6
6
|
import { useKeyHold } from "./useKeyHold.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 { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
|
|
10
12
|
import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
|
|
11
13
|
import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
|
|
@@ -13,4 +15,4 @@ import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotke
|
|
|
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/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AppHotkey, CustomHotkeys, Hotkey, HotkeyRegistrationView, HotkeySequence, RegisteredAppHotkeys } from "@tanstack/hotkeys";
|
|
1
|
+
import { AppHotkey, CustomHotkeys, Hotkey, HotkeyRegistrationView, HotkeySequence, RecordedKeyPresses, RegisteredAppHotkeys } from "@tanstack/hotkeys";
|
|
2
2
|
/** Recording state and controls owned by the containing component. */
|
|
3
3
|
export interface OctaneHotkeyRecorder<THotkey extends Hotkey = Hotkey> {
|
|
4
4
|
/** Whether the recorder is listening for a shortcut. */
|
|
@@ -27,13 +27,18 @@ export interface UseCustomHotkeysResult {
|
|
|
27
27
|
customHotkeys: CustomHotkeys;
|
|
28
28
|
/**
|
|
29
29
|
* Sets the user's hotkey for an app hotkey. `null` unbinds it.
|
|
30
|
-
* Returns conflicting app hotkeys; when any exist, nothing changes.
|
|
30
|
+
* Returns conflicting app hotkeys; when any exist, nothing changes. Pass
|
|
31
|
+
* the recorder's `context` for a recorded hotkey, as for `findConflicts`.
|
|
31
32
|
*/
|
|
32
|
-
setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null) => Array<AppHotkey>;
|
|
33
|
+
setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
|
|
33
34
|
/** Restores the default hotkey for an app hotkey. */
|
|
34
35
|
resetCustomHotkey: (id: keyof RegisteredAppHotkeys & string) => void;
|
|
35
36
|
/** Restores every default hotkey. */
|
|
36
37
|
resetCustomHotkeys: () => void;
|
|
37
|
-
/**
|
|
38
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Other app hotkeys that would collide if `id` used `hotkey`. Pass the
|
|
40
|
+
* recorder's `context` for a recorded hotkey: it has the key press, which
|
|
41
|
+
* also finds a hotkey written differently that the same press fires.
|
|
42
|
+
*/
|
|
43
|
+
findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
|
|
39
44
|
}
|
package/dist/useAppHotkeys.js
CHANGED
|
@@ -20,10 +20,10 @@ function useCustomHotkeys(slot) {
|
|
|
20
20
|
const store = useRequiredStore("useCustomHotkeys", slot);
|
|
21
21
|
return {
|
|
22
22
|
customHotkeys: store.customHotkeys,
|
|
23
|
-
setCustomHotkey: (id, hotkey) => store.setCustomHotkey(id, hotkey),
|
|
23
|
+
setCustomHotkey: (id, hotkey, recordedWith) => store.setCustomHotkey(id, hotkey, recordedWith),
|
|
24
24
|
resetCustomHotkey: (id) => store.resetCustomHotkey(id),
|
|
25
25
|
resetCustomHotkeys: () => store.resetCustomHotkeys(),
|
|
26
|
-
findConflicts: (id, hotkey) => store.findConflicts(id, hotkey)
|
|
26
|
+
findConflicts: (id, hotkey, recordedWith) => store.findConflicts(id, hotkey, recordedWith)
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -5,6 +5,12 @@ import { formatForDisplay } from "@tanstack/hotkeys";
|
|
|
5
5
|
* wins. Without a provider platform, server rendering and hydration use
|
|
6
6
|
* 'linux', then the client re-renders with the detected platform.
|
|
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. See `useKeyboardLayout`. A `layoutMap` passed to the returned
|
|
11
|
+
* function wins. Firefox and Safari report no layout, so labels there keep the
|
|
12
|
+
* key's US character.
|
|
13
|
+
*
|
|
8
14
|
* Call the hook once at the top of the component; the returned function can
|
|
9
15
|
* be called anywhere, such as in a loop or a condition. Server-rendered apps
|
|
10
16
|
* should set the provider's `defaultOptions.platform` from the request with
|
|
@@ -13,10 +19,10 @@ import { formatForDisplay } from "@tanstack/hotkeys";
|
|
|
13
19
|
* @example
|
|
14
20
|
* ```tsx
|
|
15
21
|
* function SaveButton() @{
|
|
16
|
-
* const
|
|
22
|
+
* const formatForDisplay = useFormatForDisplay()
|
|
17
23
|
*
|
|
18
24
|
* <button>
|
|
19
|
-
* Save <kbd>{
|
|
25
|
+
* Save <kbd>{formatForDisplay('Mod+S')}</kbd>
|
|
20
26
|
* </button>
|
|
21
27
|
* }
|
|
22
28
|
* ```
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
|
|
2
2
|
import { subSlot } from "./internal.js";
|
|
3
|
+
import { useKeyboardLayout } from "./useKeyboardLayout.js";
|
|
3
4
|
import { detectPlatform, formatForDisplay } from "@tanstack/hotkeys";
|
|
4
5
|
import { getServerPlatformSnapshot } from "@tanstack/hotkeys/adapter";
|
|
5
6
|
import { useCallback, useSyncExternalStore } from "octane";
|
|
@@ -9,10 +10,12 @@ const subscribe = () => () => {};
|
|
|
9
10
|
function useFormatForDisplay(slot) {
|
|
10
11
|
const provided = useDefaultHotkeysOptions().platform;
|
|
11
12
|
const platform = useSyncExternalStore(subscribe, () => provided ?? detectPlatform(), () => provided ?? getServerPlatformSnapshot(), subSlot(slot, "platform"));
|
|
13
|
+
const layoutMap = useKeyboardLayout(subSlot(slot, "layout"));
|
|
12
14
|
return useCallback((hotkey, options) => formatForDisplay(hotkey, {
|
|
13
15
|
...options,
|
|
14
|
-
platform: options?.platform ?? platform
|
|
15
|
-
|
|
16
|
+
platform: options?.platform ?? platform,
|
|
17
|
+
layoutMap: options?.layoutMap ?? layoutMap ?? void 0
|
|
18
|
+
}), [platform, layoutMap], subSlot(slot, "format"));
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
export { useFormatForDisplay };
|
|
@@ -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
|
+
* <kbd>{label('KeyW')}</kbd>
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function useKeyboardLayout(): KeyboardLayoutMap | null;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
|
|
2
|
+
import { subSlot } from "./internal.js";
|
|
3
|
+
import { getKeyboardLayoutStore, loadKeyboardLayout } from "@tanstack/hotkeys";
|
|
4
|
+
import { useEffect, useSyncExternalStore } from "octane";
|
|
5
|
+
|
|
6
|
+
// src/useKeyboardLayout.ts
|
|
7
|
+
const subscribe = (onChange) => getKeyboardLayoutStore().subscribe(onChange).unsubscribe;
|
|
8
|
+
const getSnapshot = () => getKeyboardLayoutStore().get();
|
|
9
|
+
const subscribeToNothing = () => () => {};
|
|
10
|
+
const getNothing = () => null;
|
|
11
|
+
const getServerSnapshot = () => null;
|
|
12
|
+
function useKeyboardLayout(slot) {
|
|
13
|
+
const enabled = useDefaultHotkeysOptions().keyboardLayout !== false;
|
|
14
|
+
useEffect(() => enabled ? loadKeyboardLayout() : void 0, [enabled], subSlot(slot, "keyboard-layout:load"));
|
|
15
|
+
return useSyncExternalStore(enabled ? subscribe : subscribeToNothing, enabled ? getSnapshot : getNothing, getServerSnapshot, subSlot(slot, "keyboard-layout"));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { useKeyboardLayout };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/octane-hotkeys",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "Octane adapter for TanStack Hotkeys",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -35,14 +35,14 @@
|
|
|
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
|
"@tanstack/hotkeys-test-utils": "0.0.0",
|
|
42
42
|
"@tsrx/oxc": "0.23.0",
|
|
43
43
|
"@tsrx/typescript-plugin": "^0.6.3",
|
|
44
44
|
"octane": "0.16.7",
|
|
45
|
-
"vite": "^8.3.
|
|
45
|
+
"vite": "^8.3.7"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"octane": ">=0.12.0"
|
|
@@ -32,13 +32,14 @@ Before starting, load the `@tanstack/hotkeys#hotkeys-core` skill (`npx @tanstack
|
|
|
32
32
|
|
|
33
33
|
```tsrx
|
|
34
34
|
import { useState } from 'octane'
|
|
35
|
-
import {
|
|
35
|
+
import { useFormatForDisplay, useHotkey } from '@tanstack/octane-hotkeys'
|
|
36
36
|
|
|
37
37
|
export function Panel({ onSave }: { onSave: () => void }) @{
|
|
38
38
|
const [panel, setPanel] = useState<HTMLDivElement | null>(null)
|
|
39
39
|
|
|
40
40
|
useHotkey('Mod+S', () => onSave(), { meta: { name: 'Save' } })
|
|
41
41
|
useHotkey('Escape', () => closePanel(), { target: panel })
|
|
42
|
+
const formatForDisplay = useFormatForDisplay()
|
|
42
43
|
|
|
43
44
|
<div ref={setPanel} tabIndex={0}>
|
|
44
45
|
Save <kbd>{formatForDisplay('Mod+S')}</kbd>
|
|
@@ -48,26 +49,26 @@ export function Panel({ onSave }: { onSave: () => void }) @{
|
|
|
48
49
|
|
|
49
50
|
## API
|
|
50
51
|
|
|
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
|
-
| `useFormatForDisplay()` | Get
|
|
59
|
-
| `useAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied, to pass to the register API.
|
|
60
|
-
| `useAppHotkeyList()` | List every app hotkey with its status, including ones whose components aren't mounted, for a cheat sheet or settings screen.
|
|
61
|
-
| `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts.
|
|
62
|
-
| `useHotkeyRecorder(options)` | Record a chord or sequence the user presses, for a settings screen.
|
|
63
|
-
| `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.
|
|
64
|
-
| `useHotkeyScopesState()` | Get the active scopes and the exclusive scope that owns the keyboard.
|
|
65
|
-
| `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.
|
|
66
|
-
| `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.
|
|
67
|
-
| `useHeldKeys()` | Get every key that's held right now.
|
|
68
|
-
| `useHeldKeyCodes()` | Map each held key to its physical `event.code`.
|
|
52
|
+
| API | What it's for |
|
|
53
|
+
| ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
54
|
+
| `useHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey while the component is mounted. |
|
|
55
|
+
| `useHotkeys(definitions, commonOptions?)` | Register several hotkeys, or a list built from data, in one call. |
|
|
56
|
+
| `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for the components inside it. |
|
|
57
|
+
| `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider. |
|
|
58
|
+
| `useDefaultHotkeysOptions()` | Read the default options merged from every provider above. |
|
|
59
|
+
| `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)`. |
|
|
60
|
+
| `useAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied, to pass to the register API. |
|
|
61
|
+
| `useAppHotkeyList()` | List every app hotkey with its status, including ones whose components aren't mounted, for a cheat sheet or settings screen. |
|
|
62
|
+
| `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
|
|
63
|
+
| `useHotkeyRecorder(options)` | Record a chord or sequence the user presses, for a settings screen. |
|
|
64
|
+
| `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. |
|
|
65
|
+
| `useHotkeyScopesState()` | Get the active scopes and the exclusive scope that owns the keyboard. |
|
|
66
|
+
| `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. |
|
|
67
|
+
| `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. |
|
|
68
|
+
| `useHeldKeys()` | Get every key that's held right now. |
|
|
69
|
+
| `useHeldKeyCodes()` | Map each held key to its physical `event.code`. |
|
|
69
70
|
|
|
70
|
-
The package also re-exports `@tanstack/hotkeys`, such as `formatForDisplay
|
|
71
|
+
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`.
|
|
71
72
|
|
|
72
73
|
No provider is required. Use Octane 0.12 or newer, with hooks in compiler-enabled `.tsrx` components.
|
|
73
74
|
|
|
@@ -78,7 +79,7 @@ No provider is required. Use Octane 0.12 or newer, with hooks in compiler-enable
|
|
|
78
79
|
- **Hooks are tracked by call site.** Use `useHotkeys(items.map(...))` for lists, not `useHotkey` in a loop.
|
|
79
80
|
- **Custom hooks in plain `.ts` and `.js` files need the compiler.** Calls from a `.ts` or `.js` file that the compiler skips share one slot per hook in each component, even across different custom hooks, so only the last `useHotkey` call works. The compiler handles a file that imports a value from `octane` or starts with `/** @jsxImportSource octane */`. A type-only import doesn't count. `.tsrx` and `.tsx` files are fine.
|
|
80
81
|
- **Call the hooks directly.** A `useShortcut(keys: string)` wrapper has to cast strings to hotkeys, which turns off type checking and drops sequences, options, and app hotkeys.
|
|
81
|
-
- **Server-rendered labels need the user's platform.**
|
|
82
|
+
- **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.
|
|
82
83
|
|
|
83
84
|
When declaring app hotkeys, building a cheat sheet, or letting users rebind shortcuts, load `@tanstack/hotkeys#app-hotkeys` and read [app hotkeys in Octane](references/app-hotkeys.md).
|
|
84
85
|
|
|
@@ -42,19 +42,20 @@ Register with `useHotkey(appHotkeys['doc.save'], save)`, using the import or `us
|
|
|
42
42
|
A settings row records with `useHotkeyRecorder` and saves through `useCustomHotkeys`:
|
|
43
43
|
|
|
44
44
|
```tsrx
|
|
45
|
-
import {
|
|
45
|
+
import { useAppHotkeys, useCustomHotkeys, useFormatForDisplay, useHotkeyRecorder } from '@tanstack/octane-hotkeys'
|
|
46
46
|
import type { AppHotkeyId } from '@tanstack/octane-hotkeys'
|
|
47
47
|
|
|
48
48
|
export function ShortcutRow({ id }: { id: AppHotkeyId }) @{
|
|
49
|
+
const formatForDisplay = useFormatForDisplay()
|
|
49
50
|
const appHotkey = useAppHotkeys()[id]
|
|
50
51
|
const { findConflicts, setCustomHotkey, resetCustomHotkey } = useCustomHotkeys()
|
|
51
52
|
const recorder = useHotkeyRecorder({
|
|
52
53
|
maxSteps: Infinity,
|
|
53
|
-
validate: (hotkey) => {
|
|
54
|
-
const [conflict] = findConflicts(id, hotkey)
|
|
54
|
+
validate: (hotkey, context) => {
|
|
55
|
+
const [conflict] = findConflicts(id, hotkey, context)
|
|
55
56
|
return conflict ? `Used by ${conflict.meta?.name ?? conflict.id}` : true
|
|
56
57
|
},
|
|
57
|
-
onRecord: (hotkey) => setCustomHotkey(id, hotkey),
|
|
58
|
+
onRecord: (hotkey, context) => setCustomHotkey(id, hotkey, context),
|
|
58
59
|
onClear: () => setCustomHotkey(id, null),
|
|
59
60
|
})
|
|
60
61
|
|