Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wild-snails-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/hotkeys': patch
---

Remove redundant `setState` in `destroy()` and share a single `IDLE_STATE` constant across `stop()` and `cancel()`
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changeset message says the shared IDLE_STATE is used across stop() and cancel(), but the code also uses it in start() and on successful record, and destroy() no longer directly sets state either. Please update the changeset text to accurately describe the shipped behavior so release notes are correct.

Copilot uses AI. Check for mistakes.
16 changes: 8 additions & 8 deletions docs/reference/classes/HotkeyRecorder.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: HotkeyRecorder

# Class: HotkeyRecorder

Defined in: [recorder.ts:72](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L72)
Defined in: [recorder.ts:80](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L80)

Framework-agnostic class for recording keyboard shortcuts.

Expand Down Expand Up @@ -54,7 +54,7 @@ unsubscribe()
new HotkeyRecorder(options): HotkeyRecorder;
```

Defined in: [recorder.ts:86](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L86)
Defined in: [recorder.ts:93](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L93)

#### Parameters

Expand All @@ -74,7 +74,7 @@ Defined in: [recorder.ts:86](https://github.com/TanStack/hotkeys/blob/main/packa
readonly store: Store<HotkeyRecorderState>;
```

Defined in: [recorder.ts:77](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L77)
Defined in: [recorder.ts:85](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L85)

The TanStack Store instance containing the recorder state.
Use this to subscribe to state changes or access current state.
Expand All @@ -87,7 +87,7 @@ Use this to subscribe to state changes or access current state.
cancel(): void;
```

Defined in: [recorder.ts:214](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L214)
Defined in: [recorder.ts:218](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L218)

Cancel recording without saving.

Expand All @@ -106,7 +106,7 @@ the onCancel callback if provided.
destroy(): void;
```

Defined in: [recorder.ts:259](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L259)
Defined in: [recorder.ts:260](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L260)

Clean up event listeners and reset state.

Expand All @@ -125,7 +125,7 @@ all event listeners are properly removed.
setOptions(options): void;
```

Defined in: [recorder.ts:95](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L95)
Defined in: [recorder.ts:102](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L102)

Updates the recorder options, including callbacks.
This allows framework adapters to sync callback changes without recreating the recorder.
Expand All @@ -148,7 +148,7 @@ This allows framework adapters to sync callback changes without recreating the r
start(): void;
```

Defined in: [recorder.ts:109](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L109)
Defined in: [recorder.ts:116](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L116)

Start recording a new hotkey.

Expand All @@ -168,7 +168,7 @@ a valid hotkey is recorded, Escape is pressed, or stop/cancel is called.
stop(): void;
```

Defined in: [recorder.ts:194](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L194)
Defined in: [recorder.ts:201](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L201)

Stop recording (same as cancel, but doesn't call onCancel).

Expand Down
8 changes: 4 additions & 4 deletions docs/reference/interfaces/HotkeyRecorderOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: HotkeyRecorderOptions

# Interface: HotkeyRecorderOptions

Defined in: [recorder.ts:24](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L24)
Defined in: [recorder.ts:32](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L32)

Options for configuring a HotkeyRecorder instance.

Expand All @@ -17,7 +17,7 @@ Options for configuring a HotkeyRecorder instance.
optional onCancel: () => void;
```

Defined in: [recorder.ts:28](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L28)
Defined in: [recorder.ts:36](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L36)

Optional callback when recording is cancelled (Escape pressed)

Expand All @@ -33,7 +33,7 @@ Optional callback when recording is cancelled (Escape pressed)
optional onClear: () => void;
```

Defined in: [recorder.ts:30](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L30)
Defined in: [recorder.ts:38](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L38)

Optional callback when shortcut is cleared (Backspace/Delete pressed)

Expand All @@ -49,7 +49,7 @@ Optional callback when shortcut is cleared (Backspace/Delete pressed)
onRecord: (hotkey) => void;
```

Defined in: [recorder.ts:26](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L26)
Defined in: [recorder.ts:34](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/recorder.ts#L34)

Callback when a hotkey is successfully recorded

Expand Down
29 changes: 13 additions & 16 deletions packages/hotkeys/src/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ export interface HotkeyRecorderState {
recordedHotkey: Hotkey | null
}

/**
* Initial idle state for the recorder, used when not recording.
*/
const IDLE_STATE: HotkeyRecorderState = {
isRecording: false,
recordedHotkey: null,
}

/**
* Options for configuring a HotkeyRecorder instance.
*/
Expand Down Expand Up @@ -74,10 +82,9 @@ export class HotkeyRecorder {
* The TanStack Store instance containing the recorder state.
* Use this to subscribe to state changes or access current state.
*/
readonly store: Store<HotkeyRecorderState> = new Store<HotkeyRecorderState>({
isRecording: false,
recordedHotkey: null,
})
readonly store: Store<HotkeyRecorderState> = new Store<HotkeyRecorderState>(
IDLE_STATE,
)

#keydownHandler: ((event: KeyboardEvent) => void) | null = null
#options: HotkeyRecorderOptions
Expand Down Expand Up @@ -199,10 +206,7 @@ export class HotkeyRecorder {
}

// Update store state
this.store.setState(() => ({
isRecording: false,
recordedHotkey: null,
}))
this.store.setState(() => IDLE_STATE)
}

/**
Expand All @@ -219,10 +223,7 @@ export class HotkeyRecorder {
}

// Update store state
this.store.setState(() => ({
isRecording: false,
recordedHotkey: null,
}))
this.store.setState(() => IDLE_STATE)

// Call cancel callback
this.#options.onCancel?.()
Expand Down Expand Up @@ -258,9 +259,5 @@ export class HotkeyRecorder {
*/
destroy(): void {
this.stop()
this.store.setState(() => ({
isRecording: false,
recordedHotkey: null,
}))
}
}