@tanstack/preact-hotkeys 0.20.2 → 0.20.4

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, and
8
- * recorders. Defaults to detection. When server rendering, set it from the
9
- * request with `detectPlatform(userAgent)`.
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
  /**
@@ -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);
@@ -1,8 +1,11 @@
1
1
  import { AppHotkey, HotkeyCallback, HotkeyCallbackContextFor, RegisterableHotkey } from "@tanstack/hotkeys";
2
2
  import { AdapterAppHotkeyOptions, AdapterChordOptions, AdapterCommonOptions, AdapterHotkeyOptions, AdapterOptionsFor, AdapterSequenceOptions } from "@tanstack/hotkeys/adapter";
3
3
  import { RefObject } from "preact";
4
- /** A Preact ref, element, document, or window to listen on. */
5
- export type UseHotkeyTarget = RefObject<HTMLElement | null> | HTMLElement | Document | Window | null;
4
+ /**
5
+ * A Preact ref, element, document, or window to listen on. A `null` or
6
+ * `undefined` target, or a ref that is still unset, waits for the element.
7
+ */
8
+ export type UseHotkeyTarget = RefObject<HTMLElement | null | undefined> | HTMLElement | Document | Window | null;
6
9
  /** Options shared by chords and sequences. */
7
10
  export type UseHotkeyOptions = AdapterHotkeyOptions<UseHotkeyTarget>;
8
11
  /** Options for a chord such as `'Mod+S'`. */
@@ -24,8 +24,9 @@ export interface PreactHotkeyRecorder<THotkey extends Hotkey = Hotkey> {
24
24
  * `commitKeys: 'none'`), after `idleTimeoutMs`, or when it reaches `maxSteps`,
25
25
  * and `onRecord` receives a chord (one step) or a sequence.
26
26
  *
27
- * Escape cancels. Backspace/Delete removes the last step, or calls `onClear`
28
- * when nothing is recorded.
27
+ * Escape cancels, and so do Tab and Shift+Tab unless `recordTab` is set.
28
+ * Backspace/Delete removes the last step, or calls `onClear` when nothing is
29
+ * recorded.
29
30
  *
30
31
  * @param options - Configuration options for the recorder
31
32
  * @returns The recorder state and controls
package/dist/utils.js CHANGED
@@ -6,7 +6,7 @@ function isRef(value) {
6
6
  return value !== null && typeof value === "object" && "current" in value;
7
7
  }
8
8
  /**
9
- * Resolves a hook target. A null target, or a ref that is still null, skips
9
+ * Resolves a hook target. A null target, or a ref that is still unset, skips
10
10
  * registration. Omit the target to listen on the document.
11
11
  */
12
12
  function resolveTarget(target) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/preact-hotkeys",
3
- "version": "0.20.2",
3
+ "version": "0.20.4",
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.0"
38
+ "@tanstack/hotkeys": "0.26.0"
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={{ hotkey: { platform: 'mac' } }}>`.
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