Skip to content
Merged
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/remove-unreachable-checks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/hotkeys': patch
---

The hotkey manager and recorder bundles are slightly smaller. Checks that could never fail and an unused internal match field were removed, with no change in behavior.
130 changes: 69 additions & 61 deletions .intent/review-state.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions docs/reference/classes/HotkeyManager.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ manager.activateScope('dialog', { exclusive: { allow: ['global'] } })
destroy(): void;
```

Defined in: [hotkey-manager.ts:1831](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-manager.ts#L1831)
Defined in: [hotkey-manager.ts:1820](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-manager.ts#L1820)

Destroys the manager and removes all listeners.

Expand All @@ -155,7 +155,7 @@ Destroys the manager and removes all listeners.
getRegistrationCount(): number;
```

Defined in: [hotkey-manager.ts:1763](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-manager.ts#L1763)
Defined in: [hotkey-manager.ts:1752](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-manager.ts#L1752)

Gets the number of registered hotkeys, chords and sequences.

Expand All @@ -171,7 +171,7 @@ Gets the number of registered hotkeys, chords and sequences.
hasActiveRegistration(hotkey): boolean;
```

Defined in: [hotkey-manager.ts:1801](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-manager.ts#L1801)
Defined in: [hotkey-manager.ts:1790](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-manager.ts#L1790)

Checks whether a hotkey can fire now: at least one registration is
enabled, in an active scope, and listening where key presses go. A
Expand Down Expand Up @@ -205,7 +205,7 @@ True if a registration that can fire exists
isRegistered(hotkey, target?): boolean;
```

Defined in: [hotkey-manager.ts:1774](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-manager.ts#L1774)
Defined in: [hotkey-manager.ts:1763](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-manager.ts#L1763)

Checks if a specific hotkey is registered.

Expand Down Expand Up @@ -265,7 +265,7 @@ register<T>(
): HotkeyRegistrationHandle<HotkeyOptionsFor<T>>;
```

Defined in: [hotkey-manager.ts:886](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-manager.ts#L886)
Defined in: [hotkey-manager.ts:877](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-manager.ts#L877)

Registers a hotkey handler and returns a handle for updating the registration.

Expand Down Expand Up @@ -331,7 +331,7 @@ handle.unregister()
resetSequenceProgress(): void;
```

Defined in: [hotkey-manager.ts:1748](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-manager.ts#L1748)
Defined in: [hotkey-manager.ts:1737](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-manager.ts#L1737)

Resets the progress of every in-progress sequence.

Expand Down
10 changes: 5 additions & 5 deletions docs/reference/classes/HotkeyRecorder.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Defined in: [hotkey-recorder.ts:265](https://github.com/TanStack/hotkeys/blob/ma
cancel(): void;
```

Defined in: [hotkey-recorder.ts:546](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-recorder.ts#L546)
Defined in: [hotkey-recorder.ts:545](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-recorder.ts#L545)

Cancel recording without saving.

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

Defined in: [hotkey-recorder.ts:456](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-recorder.ts#L456)
Defined in: [hotkey-recorder.ts:455](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-recorder.ts#L455)

Commits the recorded steps after validation and conflict checks.
Called automatically at `maxSteps`, on Enter, or after `idleTimeoutMs`.
Expand All @@ -135,7 +135,7 @@ A rejected commit leaves recording active with its steps intact.
destroy(): void;
```

Defined in: [hotkey-recorder.ts:573](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-recorder.ts#L573)
Defined in: [hotkey-recorder.ts:556](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-recorder.ts#L556)

Clean up event listeners and reset state.

Expand Down Expand Up @@ -177,7 +177,7 @@ This allows framework adapters to sync callback changes without recreating the r
start(): void;
```

Defined in: [hotkey-recorder.ts:331](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-recorder.ts#L331)
Defined in: [hotkey-recorder.ts:329](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-recorder.ts#L329)

Start recording a new hotkey.

Expand All @@ -196,7 +196,7 @@ and records chords according to `maxSteps`.
stop(): void;
```

Defined in: [hotkey-recorder.ts:536](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-recorder.ts#L536)
Defined in: [hotkey-recorder.ts:535](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-recorder.ts#L535)

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

Expand Down
10 changes: 5 additions & 5 deletions docs/reference/classes/KeyStateTracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Use this to subscribe to state changes or access current state.
areAllKeysHeld(keys): boolean;
```

Defined in: [key-state-tracker.ts:299](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/key-state-tracker.ts#L299)
Defined in: [key-state-tracker.ts:294](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/key-state-tracker.ts#L294)

Checks if all of the given keys are currently held.

Expand All @@ -88,7 +88,7 @@ True if all of the keys are currently held
destroy(): void;
```

Defined in: [key-state-tracker.ts:306](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/key-state-tracker.ts#L306)
Defined in: [key-state-tracker.ts:301](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/key-state-tracker.ts#L301)

Destroys the tracker and removes all listeners.

Expand All @@ -104,7 +104,7 @@ Destroys the tracker and removes all listeners.
getHeldKeys(): string[];
```

Defined in: [key-state-tracker.ts:262](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/key-state-tracker.ts#L262)
Defined in: [key-state-tracker.ts:257](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/key-state-tracker.ts#L257)

Gets an array of currently held key names.

Expand All @@ -122,7 +122,7 @@ Array of key names currently being pressed
isAnyKeyHeld(keys): boolean;
```

Defined in: [key-state-tracker.ts:289](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/key-state-tracker.ts#L289)
Defined in: [key-state-tracker.ts:284](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/key-state-tracker.ts#L284)

Checks if any of the given keys are currently held.

Expand All @@ -148,7 +148,7 @@ True if any of the keys are currently held
isKeyHeld(key): boolean;
```

Defined in: [key-state-tracker.ts:276](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/key-state-tracker.ts#L276)
Defined in: [key-state-tracker.ts:271](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/key-state-tracker.ts#L271)

Checks if a specific key is currently being held.

Expand Down
2 changes: 1 addition & 1 deletion docs/reference/functions/getHotkeyManager.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ title: getHotkeyManager
function getHotkeyManager(): HotkeyManager;
```

Defined in: [hotkey-manager.ts:1846](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-manager.ts#L1846)
Defined in: [hotkey-manager.ts:1835](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/hotkey-manager.ts#L1835)

Gets the singleton HotkeyManager instance.
Convenience function for accessing the manager.
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/functions/getKeyStateTracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ title: getKeyStateTracker
function getKeyStateTracker(): KeyStateTracker;
```

Defined in: [key-state-tracker.ts:317](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/key-state-tracker.ts#L317)
Defined in: [key-state-tracker.ts:312](https://github.com/TanStack/hotkeys/blob/main/packages/hotkeys/src/key-state-tracker.ts#L312)

Gets the singleton KeyStateTracker instance.
Convenience function for accessing the tracker.
Expand Down
22 changes: 7 additions & 15 deletions packages/hotkeys/src/_match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import {
keysEqual,
} from './_keys'
import { isSingleLetterKey } from './constants'
import { parseHotkey } from './parse'
import { detectPlatform } from './platform'
import type { HotkeyChord, ParsedHotkeyChord } from './hotkey.types'
import type { ParsedHotkeyChord } from './hotkey.types'
import type { KeyIdentity, NormalizedKeyboardEvent } from './key.types'
import type { HotkeyPlatform } from './platform'

Expand All @@ -30,18 +29,15 @@ import type { HotkeyPlatform } from './platform'
export interface KeyboardEventMatch {
matched: boolean
score: 0 | 1 | 2 | 3
source?: 'key' | 'code' | 'fallback'
identity?: KeyIdentity
}

/** Matches an event and reports the winning candidate and release identity. */
export function matchKeyboardEvent(
event: KeyboardEvent,
hotkey: HotkeyChord | ParsedHotkeyChord,
parsed: ParsedHotkeyChord,
platform: HotkeyPlatform = detectPlatform(),
): KeyboardEventMatch {
const parsed =
typeof hotkey === 'string' ? parseHotkey(hotkey, platform) : hotkey
const normalized = normalizeKeyboardEvent(event, platform)
const noMatch: KeyboardEventMatch = { matched: false, score: 0 }

Expand All @@ -66,7 +62,7 @@ export function matchKeyboardEvent(

if (parsed.code !== undefined) {
return !altGraph && shiftMatches && normalized.code === parsed.code
? matched('code', 3)
? matched(3)
: noMatch
}

Expand All @@ -92,7 +88,7 @@ export function matchKeyboardEvent(
!parsed.shift &&
!shiftTypedNothing
if (shiftMatches || (implicitShift && normalized.shift)) {
return matched('key', implicitShift ? 2 : 3)
return matched(implicitShift ? 2 : 3)
}
}

Expand All @@ -104,7 +100,7 @@ export function matchKeyboardEvent(
// native Windows apps. An exact `Δ±` binding outranks it. With Alt, the
// position decides instead: macOS Option+Shift+B types `Δ±` too.
if (normalized.key === 'Δ±' && parsed.key === 'I' && !normalized.alt) {
return matched('key', 2)
return matched(2)
}

// macOS Option changes what a key types: French Option+A types `Γ¦` on the
Expand All @@ -126,18 +122,14 @@ export function matchKeyboardEvent(
}
const fallbackKey = optionLetter ?? codeToLogicalKey(normalized.code)
return fallbackKey !== undefined && keysEqual(fallbackKey, parsed.key)
? matched('fallback', 1)
? matched(1)
: noMatch

// Preserve the triggering code so keyup can reset a shortcut even if its character changes.
function matched(
source: NonNullable<KeyboardEventMatch['source']>,
score: 1 | 2 | 3,
): KeyboardEventMatch {
function matched(score: 1 | 2 | 3): KeyboardEventMatch {
return {
matched: true,
score,
source,
identity: { key: normalized.key, code: normalized.code },
}
}
Expand Down
37 changes: 13 additions & 24 deletions packages/hotkeys/src/hotkey-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,34 +819,25 @@ export class HotkeyManager {
// The composed path includes targets inside open shadow roots, which
// `event.target` hides from listeners outside them.
const path = event.composedPath()
for (const registration of this.registrations.state.values()) {
if (
!this.#isLive(registration) ||
registration.options.eventType !== eventType ||
this.#skips(registration, event) ||
!path.includes(registration.target) ||
ignoredInInput(registration, event, registration.target)
) {
continue
}
const steps =
registration.kind === 'chord'
return [...this.registrations.state.values()].some(
(registration) =>
this.#isLive(registration) &&
registration.options.eventType === eventType &&
!this.#skips(registration, event) &&
path.includes(registration.target) &&
!ignoredInInput(registration, event, registration.target) &&
(registration.kind === 'chord'
? [registration.parsedChord]
: [
registration.parsedSequence[registration.currentIndex]!,
registration.parsedSequence[0]!,
]
if (
steps.some(
).some(
(step) =>
matchKeyboardEvent(event, step, registration.options.platform)
.matched,
)
) {
return true
}
}
return false
),
)
}

/** Notifies subscribers after in-place updates such as counts and sequence progress. */
Expand Down Expand Up @@ -1725,10 +1716,8 @@ export class HotkeyManager {
const parsed = registration.parsedChord
const releasedKey = normalizeKeyName(event.key)

if (
registration.activeMatch?.code &&
event.code === registration.activeMatch.code
) {
const code = registration.activeMatch?.code
if (code && event.code === code) {
return true
}

Expand Down
Loading
Loading