@tanstack/octane-hotkeys 0.11.1 → 0.11.3
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.
|
@@ -3,9 +3,10 @@ import { AppHotkeys, AppHotkeysStore, CustomHotkeys, HotkeyChordRecorderOptions,
|
|
|
3
3
|
import { HotkeysProviderScopeProps } from "@tanstack/hotkeys/adapter";
|
|
4
4
|
export interface DefaultHotkeysOptions {
|
|
5
5
|
/**
|
|
6
|
-
* The user's platform, for labels from `useFormatForDisplay`, hotkeys,
|
|
7
|
-
* recorders
|
|
8
|
-
* request with
|
|
6
|
+
* The user's platform, for labels from `useFormatForDisplay`, hotkeys,
|
|
7
|
+
* recorders, and app hotkey checks such as conflicts. Defaults to detection.
|
|
8
|
+
* When server rendering, set it from the request with
|
|
9
|
+
* `detectPlatform(userAgent)`.
|
|
9
10
|
*/
|
|
10
11
|
platform?: HotkeyPlatform;
|
|
11
12
|
/**
|
package/dist/HotkeysProvider.js
CHANGED
|
@@ -24,18 +24,27 @@ function HotkeysProvider({ children, defaultOptions, appHotkeys, customHotkeys,
|
|
|
24
24
|
const parent = useContext(HotkeysContext);
|
|
25
25
|
const callbackRef = useRef(onCustomHotkeysChange, subSlot(void 0, "provider-callback"));
|
|
26
26
|
callbackRef.current = onCustomHotkeysChange;
|
|
27
|
+
const platform = defaultOptions?.platform ?? parent?.defaultOptions.platform;
|
|
27
28
|
const storeRef = useRef(void 0, subSlot(void 0, "provider-store"));
|
|
28
29
|
storeRef.current ??= appHotkeys ? createAppHotkeysStore(appHotkeys, {
|
|
29
30
|
customHotkeys,
|
|
30
|
-
onCustomHotkeysChange: (custom) => callbackRef.current?.(custom)
|
|
31
|
+
onCustomHotkeysChange: (custom) => callbackRef.current?.(custom),
|
|
32
|
+
platform
|
|
31
33
|
}) : null;
|
|
32
34
|
const store = storeRef.current;
|
|
33
35
|
useLayoutEffect(() => {
|
|
34
36
|
if (appHotkeys) store?.setAppHotkeys(appHotkeys);
|
|
35
37
|
}, [store, appHotkeys], subSlot(void 0, "provider-declarations"));
|
|
36
38
|
useLayoutEffect(() => {
|
|
37
|
-
store?.setOptions({
|
|
38
|
-
|
|
39
|
+
store?.setOptions({
|
|
40
|
+
customHotkeys,
|
|
41
|
+
platform
|
|
42
|
+
});
|
|
43
|
+
}, [
|
|
44
|
+
store,
|
|
45
|
+
customHotkeys,
|
|
46
|
+
platform
|
|
47
|
+
], subSlot(void 0, "provider-custom-hotkeys"));
|
|
39
48
|
useLayoutEffect(() => store ? publishAppHotkeysStore(store) : void 0, [store], subSlot(void 0, "provider-publish"));
|
|
40
49
|
const active = (parent?.enabled ?? true) && enabled;
|
|
41
50
|
const scopeKey = getHotkeysProviderScopeKey({
|
package/dist/useHotkey.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { AppHotkey, HotkeyCallback, HotkeyCallbackContextFor, RegisterableHotkey, Target } from "@tanstack/hotkeys";
|
|
2
2
|
import { AdapterAppHotkeyOptions, AdapterChordOptions, AdapterCommonOptions, AdapterHotkeyOptions, AdapterOptionsFor, AdapterSequenceOptions } from "@tanstack/hotkeys/adapter";
|
|
3
|
-
/**
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* A ref object, element, document, or window to listen on. A `null` or
|
|
5
|
+
* `undefined` target, or a ref that is still unset, waits for the element.
|
|
6
|
+
*/
|
|
7
|
+
export type UseHotkeyTarget = {
|
|
8
|
+
current: HTMLElement | null | undefined;
|
|
9
|
+
} | Target | null;
|
|
5
10
|
/** Options shared by chords and sequences. */
|
|
6
11
|
export type UseHotkeyOptions = AdapterHotkeyOptions<UseHotkeyTarget>;
|
|
7
12
|
/** Options for a chord such as `'Mod+S'`. */
|
package/dist/useHotkeys.js
CHANGED
|
@@ -17,7 +17,7 @@ function useHotkeys(definitions, ...rest) {
|
|
|
17
17
|
appHotkeysStore,
|
|
18
18
|
defaults: context?.defaultOptions.hotkey,
|
|
19
19
|
commonOptions,
|
|
20
|
-
resolveTarget: (target) => target
|
|
20
|
+
resolveTarget: (target) => target && "current" in target ? target.current : target
|
|
21
21
|
}));
|
|
22
22
|
}, null, subSlot(slot, "update"));
|
|
23
23
|
useLayoutEffect(() => () => bindings.destroy(), [], subSlot(slot, "cleanup"));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/octane-hotkeys",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.3",
|
|
4
4
|
"description": "Octane 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.26.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@tsrx/oxc": "0.23.0",
|
|
@@ -73,12 +73,12 @@ No provider is required. Use Octane 0.12 or newer, with hooks in compiler-enable
|
|
|
73
73
|
|
|
74
74
|
## Pitfalls
|
|
75
75
|
|
|
76
|
-
- **
|
|
76
|
+
- **Keep elements that mount later in state.** A ref object such as `useRef<HTMLDivElement | null>(null)` works as `target`, but the hook reads `ref.current` only after its component renders, so it misses an element that mounts later, such as one a child shows from its own state. Keep such an element in state with a callback ref, as above. A `null` or `undefined` target, or an unset ref, waits for the element.
|
|
77
77
|
- **Register a dialog's hotkeys in a child of its provider.** A hook uses the nearest `HotkeysProvider` above its component, so a dialog that renders `<HotkeysProvider scope={...} exclusive>` and registers `Escape` itself puts it in the suspended outer scope. Give each open dialog its own scope name, such as `` `dialog-${useId()}` ``.
|
|
78
78
|
- **Hooks are tracked by call site.** Use `useHotkeys(items.map(...))` for lists, not `useHotkey` in a loop.
|
|
79
79
|
- **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
80
|
- **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.** On the server, `formatForDisplay` can't see the user's OS, so 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, and format labels with `const format = useFormatForDisplay()` at the top of the component, then `format(hotkey)`. Keep plain `formatForDisplay` for labels that render only in the browser
|
|
81
|
+
- **Server-rendered labels need the user's platform.** On the server, `formatForDisplay` can't see the user's OS, so 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, and format labels with `const format = useFormatForDisplay()` at the top of the component, then `format(hotkey)`. Keep plain `formatForDisplay` for labels that render only in the browser, when no provider sets `platform`.
|
|
82
82
|
|
|
83
83
|
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
84
|
|