Skip to content

fix(hotkeys): Validate adapter hotkeys only in register - #322

Merged
KevinVandy merged 4 commits into
mainfrom
refactor/adapter-validation-in-register
Nov 29, 2026
Merged

KevinVandy merged 4 commits into
mainfrom
refactor/adapter-validation-in-register

Conversation

@KevinVandy

@KevinVandy KevinVandy commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

🎯 Changes

createHotkeyBindings repeated work that HotkeyManager.register already does. It merged default options, skipped nullish, empty, and null-target hotkeys, parsed each hotkey to reject steps without a key (with its own dev warning), and dropped timeout from chords and requireReset from sequences. Now it only merges option layers and reconciles registrations, and register is the one place that validates.

  • One spelling rule for registration.hotkey. A string hotkey registers as written from every entry point. Adapters used to normalize first, so useHotkey('Control+S') on Windows listed Mod+S while register('Control+S') listed Control+S. A chord object registers in normalized form everywhere ({ key: 'K', mod: true } becomes Mod+K), as the framework guides promise for context.hotkey. Before, core stored Meta+K on macOS. createHotkeyChordHandler reports chord objects the same way. A parsed chord keeps its formatted form.
  • 'Control+[1]' no longer throws in adapters. The adapter re-parsed its own parse result, which throws for a bracketed key that isn't a code. register always accepted it.
  • Each invalid hotkey warns once per manager. register keeps a dev-only set of hotkeys it has warned about. Adapters retry an unregistered definition on each update, so a remount, a target change, or Lit's destroy() and reconnect doesn't repeat the warning. Retrying also means a definition skipped while there was no document registers on a later update.
  • Docs. sensible-defaults.md no longer says ctrl+s is stored as Mod+S. The generated reference pages pick up the new createHotkeyBindings doc comment and shifted line numbers.
  • CI. The TanStack/config action pins move to ade00ae6, the current main their version comments name, so zizmor passes. Nothing under .github changed in that repository between the two commits.

The adapter no longer parses every hotkey on every update.

Notes

The adapter keys each registration by the hotkey as written, so switching a call site between equivalent spellings re-registers instead of updating in place, and a sequence in progress resets. Examples are 'mod+s' and 'Mod+S', chord object keys in another order, or an omitted target and target: document. A source literal keeps one spelling, so this only matters for hotkeys built from data. HotkeyManager is now at 8,941 of its 9,000 B budget.

Bundle size

Brotli, from size-limit. Base is main at 54a14ddc. The other 13 entries are unchanged, including all devtools entries.

Entry Base PR Change
everything 15,202 B 15,198 B −4 B (−0.0%)
HotkeyManager 8,899 B 8,941 B +42 B (+0.5%)
React useHotkey 9,913 B 9,778 B −135 B (−1.4%)
React useHotkey + HotkeysProvider 10,478 B 10,320 B −158 B (−1.5%)
React useHotkey + AppHotkeysProvider 11,781 B 11,653 B −128 B (−1.1%)
Preact useHotkey 9,934 B 9,810 B −124 B (−1.2%)
Vue useHotkey 9,968 B 9,831 B −137 B (−1.4%)
Solid createHotkey 9,844 B 9,723 B −121 B (−1.2%)
Svelte createHotkey 9,817 B 9,644 B −173 B (−1.8%)
Angular injectHotkey 10,018 B 9,891 B −127 B (−1.3%)
Lit hotkey decorator 10,004 B 9,844 B −160 B (−1.6%)
Alpine createHotkeysScope + hotkeysPlugin 12,815 B 12,665 B −150 B (−1.2%)
Alpine hotkeyDirective 9,093 B 9,129 B +36 B (+0.4%)
Ember useHotkey 9,936 B 9,809 B −127 B (−1.3%)
Octane useHotkey 10,048 B 9,885 B −163 B (−1.6%)

HotkeyManager and Alpine's directive grew because register now imports normalizeHotkey instead of formatHotkey. The directive calls the manager directly, so it doesn't get the adapter savings.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • New Features
    • Bracketed non-code keys are now accepted in shortcuts.
  • Bug Fixes
    • Hotkey registration preserves the written form of string shortcuts and uses normalized forms for chord-object shortcuts.
    • Duplicate detection and matching compare parsed shortcuts, including platform-specific modifier equivalents.
    • Invalid shortcut warnings appear once per distinct shortcut, and bindings that cannot register can be retried on a later update.
  • Documentation
    • Clarified how shortcut spelling and modifier equivalence vary by platform.

createHotkeyBindings repeated checks that HotkeyManager.register already
does: option defaults, rejecting empty, nullish, null-target, and invalid
hotkeys with a dev warning, and dropping options for the other kind.
Adapters now pass hotkeys through and register is the one place that
validates.

- String hotkeys register as written from every entry point. Adapters
  used to normalize them first, so useHotkey('Control+S') listed Mod+S
  on Windows while register listed Control+S.
- Chord objects register in normalized form everywhere, as the framework
  docs promise for context.hotkey, and createHotkeyChordHandler reports
  them the same way.
- A bracketed key that isn't a code, such as 'Control+[1]', no longer
  throws in adapters.
- register warns about each invalid hotkey once per manager, so adapters
  that register again after a remount or target change don't repeat it.
@changeset-bot

changeset-bot Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7c0ab0b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 18 packages
Name Type
@tanstack/hotkeys Patch
@tanstack/alpine-hotkeys Patch
@tanstack/angular-hotkeys Patch
@tanstack/ember-hotkeys Patch
@tanstack/hotkeys-devtools Patch
@tanstack/lit-hotkeys Patch
@tanstack/octane-hotkeys Patch
@tanstack/preact-hotkeys Patch
@tanstack/react-hotkeys Patch
@tanstack/solid-hotkeys Patch
@tanstack/svelte-hotkeys Patch
@tanstack/vue-hotkeys Patch
@tanstack/angular-hotkeys-devtools Patch
@tanstack/preact-hotkeys-devtools Patch
@tanstack/react-hotkeys-devtools Patch
@tanstack/solid-hotkeys-devtools Patch
@tanstack/svelte-hotkeys-devtools Patch
@tanstack/vue-hotkeys-devtools Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The adapter now delegates hotkey registration to HotkeyManager.register. String hotkeys retain their spelling, while chord objects use normalized forms. Invalid-hotkey warnings are deduplicated per manager. Tests and documentation describe the registration behavior, and CI workflows use updated action revisions.

Changes: Hotkey registration

Layer / File(s) Summary
Registration validation and representation
packages/hotkeys/src/hotkey-manager.ts, packages/hotkeys/tests/untrusted-input.test.ts, .changeset/register-validates-adapter-hotkeys.md
HotkeyManager normalizes object-form chords and preserves string forms. It warns once per distinct invalid hotkey.
Adapter registration delegation
packages/hotkeys/src/adapter.ts, packages/hotkeys/tests/adapter.test.ts, .intent/review-state.json
The adapter passes raw hotkeys and merged options to HotkeyManager.register. Tests cover retrying registration and warning deduplication across updates and target changes.
Callback context and platform expectations
packages/hotkeys/src/match.ts, packages/hotkeys/tests/match.test.ts, packages/hotkeys/tests/unified-hotkeys.test.ts, packages/ember-hotkeys/tests/integration/app-hotkeys.test.gts, docs/guides/sensible-defaults.md, .intent/review-state.json
Chord-object callback contexts use normalized forms. Documentation describes platform-specific parsed equivalence and registration forms. Integration tests expect string registrations to retain their spelling across platforms.

CI action pins

Layer / File(s) Summary
Workflow action revisions
.github/workflows/*.yml, .intent/review-state.json
CI workflows update their pinned TanStack action revisions. The release workflow also updates the pinned release-comment action revision.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Adapter as createHotkeyBindings
  participant Manager as HotkeyManager
  participant Parser as parseHotkey
  Adapter->>Manager: register raw hotkey, callback, and options
  Manager->>Parser: parse and validate hotkey
  Parser-->>Manager: parsed hotkey or validation result
  Manager-->>Adapter: registration result or invalid-hotkey warning
Loading

Possibly related PRs

  • TanStack/hotkeys#268: Both changes update invalid-hotkey warning behavior in HotkeyManager.register and adapter registration.
  • TanStack/hotkeys#320: Both changes update chord-object normalization and its representation in callback context.

Suggested reviewers: schiller-manuel, alemtuzlak

Merge Risk: Minimal · up to 7c0ab · assessed changes only

Path filters excluded changed files. This rating applies only to the assessed changes.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: adapter hotkey validation moved into HotkeyManager.register. It uses the conventional fix(hotkeys) prefix and is specific enough for a teammate scanning history. The t…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description covers the change motivation and scope in detail. It explains the move of validation into HotkeyManager.register, the spelling rule for registration.hotkey, the once-per-manager warnin…
Full details: Title check

Explanation

The title names the main change: adapter hotkey validation moved into HotkeyManager.register. It uses the conventional fix(hotkeys) prefix and is specific enough for a teammate scanning history. The title is slightly terse but matches the changeset.

Full details: Description check

Explanation

The description covers the change motivation and scope in detail. It explains the move of validation into HotkeyManager.register, the spelling rule for registration.hotkey, the once-per-manager warning behavior, the docs fix, and the CI pin update. It includes a bundle size table and notes on re-registration behavior. The template's Changes section is filled in. The checklist is mostly ticked, but the 'I fully understand the code' box is left unchecked, and the Release Impact section is marked as published code with a changeset. The description does not mention the changeset file by name, but the raw summary confirms a changeset exists. The description is substantively complete and matches the template.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit taps a chord in time
Strings keep their spelling, neat in line
Object keys take normalized shape
One warning marks each invalid escape
New action pins hop through the night

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between aabc444 and 7c0ab0b.

Files selected for processing: 1

  • .intent/review-state.json

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 0 remain after this review.

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Changeset Version Preview

1 package(s) bumped directly, 17 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/hotkeys 0.39.0 → 0.39.1 Changeset
@tanstack/alpine-hotkeys 0.17.1 → 0.17.2 Dependent
@tanstack/angular-hotkeys 0.27.1 → 0.27.2 Dependent
@tanstack/angular-hotkeys-devtools 0.5.21 → 0.5.22 Dependent
@tanstack/ember-hotkeys 0.16.1 → 0.16.2 Dependent
@tanstack/hotkeys-devtools 1.13.1 → 1.13.2 Dependent
@tanstack/lit-hotkeys 0.29.1 → 0.29.2 Dependent
@tanstack/octane-hotkeys 0.17.1 → 0.17.2 Dependent
@tanstack/preact-hotkeys 0.26.1 → 0.26.2 Dependent
@tanstack/preact-hotkeys-devtools 0.13.21 → 0.13.22 Dependent
@tanstack/react-hotkeys 0.29.1 → 0.29.2 Dependent
@tanstack/react-hotkeys-devtools 0.14.21 → 0.14.22 Dependent
@tanstack/solid-hotkeys 0.28.1 → 0.28.2 Dependent
@tanstack/solid-hotkeys-devtools 0.14.6 → 0.14.7 Dependent
@tanstack/svelte-hotkeys 0.27.1 → 0.27.2 Dependent
@tanstack/svelte-hotkeys-devtools 0.5.21 → 0.5.22 Dependent
@tanstack/vue-hotkeys 0.27.1 → 0.27.2 Dependent
@tanstack/vue-hotkeys-devtools 0.13.21 → 0.13.22 Dependent

@pkg-pr-new

pkg-pr-new Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@tanstack/alpine-hotkeys

npm i https://pkg.pr.new/@tanstack/alpine-hotkeys@322

@tanstack/angular-hotkeys

npm i https://pkg.pr.new/@tanstack/angular-hotkeys@322

@tanstack/angular-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/angular-hotkeys-devtools@322

@tanstack/ember-hotkeys

npm i https://pkg.pr.new/@tanstack/ember-hotkeys@322

@tanstack/hotkeys

npm i https://pkg.pr.new/@tanstack/hotkeys@322

@tanstack/hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/hotkeys-devtools@322

@tanstack/lit-hotkeys

npm i https://pkg.pr.new/@tanstack/lit-hotkeys@322

@tanstack/octane-hotkeys

npm i https://pkg.pr.new/@tanstack/octane-hotkeys@322

@tanstack/preact-hotkeys

npm i https://pkg.pr.new/@tanstack/preact-hotkeys@322

@tanstack/preact-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/preact-hotkeys-devtools@322

@tanstack/react-hotkeys

npm i https://pkg.pr.new/@tanstack/react-hotkeys@322

@tanstack/react-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/react-hotkeys-devtools@322

@tanstack/solid-hotkeys

npm i https://pkg.pr.new/@tanstack/solid-hotkeys@322

@tanstack/solid-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/solid-hotkeys-devtools@322

@tanstack/svelte-hotkeys

npm i https://pkg.pr.new/@tanstack/svelte-hotkeys@322

@tanstack/svelte-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/svelte-hotkeys-devtools@322

@tanstack/vue-hotkeys

npm i https://pkg.pr.new/@tanstack/vue-hotkeys@322

@tanstack/vue-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/vue-hotkeys-devtools@322

commit: 7c0ab0b

zizmor failed because the pinned commit no longer matched the main
branch named in each pin's version comment. The actions under .github
are unchanged between the two commits.
@KevinVandy
KevinVandy requested a review from a team as a code owner November 29, 2026 15:13
@KevinVandy
KevinVandy merged commit 3471fbe into main Nov 29, 2026
18 checks passed
@KevinVandy
KevinVandy deleted the refactor/adapter-validation-in-register branch November 29, 2026 15:54
@github-actions github-actions Bot mentioned this pull request Nov 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR has been released!

Thank you for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants