@tanstack/react-hotkeys 0.20.1 → 0.20.2

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,7 +3,7 @@
3
3
  import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
4
4
  import { useSelector } from "./_useSelector.js";
5
5
  import { HotkeyRecorder } from "@tanstack/hotkeys";
6
- import { useEffect, useRef } from "react";
6
+ import { useEffect, useRef, useState } from "react";
7
7
  import { withDefaults } from "@tanstack/hotkeys/adapter";
8
8
 
9
9
  //#region src/useHotkeyRecorder.ts
@@ -21,14 +21,17 @@ function useHotkeyRecorder(options) {
21
21
  recorderRef.current?.destroy();
22
22
  };
23
23
  }, []);
24
- return {
25
- isRecording,
26
- steps,
27
- recordedHotkey,
24
+ const [actions] = useState(() => ({
28
25
  startRecording: () => recorderRef.current?.start(),
29
26
  stopRecording: () => recorderRef.current?.stop(),
30
27
  cancelRecording: () => recorderRef.current?.cancel(),
31
28
  commitRecording: () => recorderRef.current?.commit()
29
+ }));
30
+ return {
31
+ isRecording,
32
+ steps,
33
+ recordedHotkey,
34
+ ...actions
32
35
  };
33
36
  }
34
37
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-hotkeys",
3
- "version": "0.20.1",
3
+ "version": "0.20.2",
4
4
  "description": "React 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.22.0"
38
+ "@tanstack/hotkeys": "0.22.1"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@eslint-react/eslint-plugin": "^4.2.3",
@@ -101,7 +101,8 @@ A hook uses the nearest `HotkeysProvider` above its component. Registering `Esca
101
101
 
102
102
  ## Pitfalls
103
103
 
104
- - **Pass the ref, not `ref.current`.** `target: listRef.current` is `null` on the first render, and a `null` target registers nothing, so the hotkey doesn't work until the component renders again. Pass `target: listRef`; a ref that is still empty waits for its element.
104
+ - **Pass the ref, not `ref.current`.** `target: listRef.current` is `null` on the first render, and a `null` target registers nothing, so the hotkey doesn't work until the component renders again. Pass `target: listRef` for an element the component always renders.
105
+ - **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.
105
106
  - **No dependency arrays or `useCallback`.** The hooks read the hotkey, callback, and options on every render, so inline callbacks always see current state.
106
107
  - **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.
107
108
  - **`matchesKeyboardEvent` takes the DOM event.** In a widget's `onKeyDown`, pass `event.nativeEvent`.