@tanstack/preact-hotkeys 0.20.2 → 0.20.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.
|
@@ -4,9 +4,10 @@ import { HotkeysProviderScopeProps } from "@tanstack/hotkeys/adapter";
|
|
|
4
4
|
import { ComponentChildren } from "preact";
|
|
5
5
|
export interface HotkeysProviderOptions {
|
|
6
6
|
/**
|
|
7
|
-
* The user's platform, for labels from `useFormatForDisplay`, hotkeys,
|
|
8
|
-
* recorders
|
|
9
|
-
* request with
|
|
7
|
+
* The user's platform, for labels from `useFormatForDisplay`, hotkeys,
|
|
8
|
+
* recorders, and app hotkey checks such as conflicts. Defaults to detection.
|
|
9
|
+
* When server rendering, set it from the request with
|
|
10
|
+
* `detectPlatform(userAgent)`.
|
|
10
11
|
*/
|
|
11
12
|
platform?: HotkeyPlatform;
|
|
12
13
|
/**
|
package/dist/HotkeysProvider.js
CHANGED
|
@@ -45,9 +45,11 @@ const NO_PARENT = {
|
|
|
45
45
|
*/
|
|
46
46
|
function HotkeysProvider({ children, appHotkeys, customHotkeys, onCustomHotkeysChange, defaultOptions = DEFAULT_OPTIONS, scope, exclusive = false, enabled = true }) {
|
|
47
47
|
const parent = useContext(HotkeysContext) ?? NO_PARENT;
|
|
48
|
+
const platform = defaultOptions.platform ?? parent.defaultOptions.platform;
|
|
48
49
|
const [store] = useState(() => appHotkeys ? createAppHotkeysStore(appHotkeys, {
|
|
49
50
|
customHotkeys,
|
|
50
|
-
onCustomHotkeysChange
|
|
51
|
+
onCustomHotkeysChange,
|
|
52
|
+
platform
|
|
51
53
|
}) : null);
|
|
52
54
|
const active = parent.enabled && enabled;
|
|
53
55
|
const scopeKey = getHotkeysProviderScopeKey({
|
|
@@ -62,12 +64,14 @@ function HotkeysProvider({ children, appHotkeys, customHotkeys, onCustomHotkeysC
|
|
|
62
64
|
useLayoutEffect(() => {
|
|
63
65
|
store?.setOptions({
|
|
64
66
|
customHotkeys,
|
|
65
|
-
onCustomHotkeysChange
|
|
67
|
+
onCustomHotkeysChange,
|
|
68
|
+
platform
|
|
66
69
|
});
|
|
67
70
|
}, [
|
|
68
71
|
store,
|
|
69
72
|
customHotkeys,
|
|
70
|
-
onCustomHotkeysChange
|
|
73
|
+
onCustomHotkeysChange,
|
|
74
|
+
platform
|
|
71
75
|
]);
|
|
72
76
|
useLayoutEffect(() => {
|
|
73
77
|
if (appHotkeys) store?.setAppHotkeys(appHotkeys);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/preact-hotkeys",
|
|
3
|
-
"version": "0.20.
|
|
3
|
+
"version": "0.20.3",
|
|
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.25.
|
|
38
|
+
"@tanstack/hotkeys": "0.25.1"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@preact/preset-vite": "^2.10.6",
|
|
@@ -106,8 +106,8 @@ A hook uses the nearest `HotkeysProvider` above its component. Registering `Esca
|
|
|
106
106
|
- **Keep elements that mount later in state.** The hook reads a ref only after its component renders, so it misses an element that mounts later, such as one a child shows from its own state. Use `const [el, setEl] = useState<HTMLElement | null>(null)`, `target: el`, and `ref={setEl}`; the hotkey registers when the element mounts and unregisters when it unmounts.
|
|
107
107
|
- **No dependency arrays or `useCallback`.** The hooks read the hotkey, callback, and options on every render, so inline callbacks always see current state.
|
|
108
108
|
- **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
|
-
- **In tests,** set `<HotkeysProvider defaultOptions={{
|
|
110
|
-
- **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
|
|
109
|
+
- **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.** 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`.
|
|
111
111
|
|
|
112
112
|
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
113
|
|