feat(hotkeys): Reject unusable hotkeys, return reason codes, and let the top modal dialog take Escape #230
Open
KevinVandywants to merge 5 commits into
main fromfix/reject-unusable-hotkeys·opened 🎯 Changes#
- Unknown modifiers register nothing.
parseHotkeyused to drop a modifier it didn't know, soCtl+Kfrom plain JavaScript or saved data fired on a bare K. A chord with an unknown modifier now parses with an empty key.register, adapter bindings andmatchesKeyboardEventalready treat that as "registers nothing", so it never fires.normalizeHotkeyandformatForDisplayreturn''for it. - Modifier-only hotkeys register nothing.
chordHasKeynow also rejects a modifier key (Shift,Shift+Alt+Mod,{ key: 'Shift' }) and a modifier code ([ShiftLeft],Shift+[ShiftLeft],{ code: 'MetaLeft' }). Before, these either never fired or, as keyup hotkeys, fired on every Shift release.hasNonModifierKeyreuses the same check. - Validation and types.
validateHotkeyreports modifier-only hotkeys as errors.PhysicalKeyCodeno longer includesShiftLeft,ControlRightand the other modifier codes, so[ShiftLeft]is a type error likeShift. Chord objects still type-check, because theirkeyandcodeaccept any string. - Reason codes instead of text.
validateHotkeyreturnsHotkeyIssueobjects such as{ reason: 'unknown-modifier', part: 'Ctl', step: 1 }instead of English sentences with aStep 2:prefix.RecorderRejectiondrops its built-inmessageand has one only when avalidatecallback returns it. The'invalid'rejection reason is now'whitespace'. Apps rendered these strings to users, so they now write their own text. The guides and examples show a small reason-to-text map. - Devtools describe issues. A hotkey's details panel lists its validation issues in plain words, such as a lowercase
escapethat never fires. - Shorter messages. Console warnings and thrown errors in core and the adapters are one line that starts with
[hotkeys], such as[hotkeys] 'Mod+S' is already registered. - Dev warnings without
process. Registering a duplicate hotkey threw aReferenceErrorwhereprocessis undefined, such as plain ESM from a CDN. Each dev-only guard now checkstypeof processfirst. The check stays inline because rolldown and esbuild keep the warning in production builds when it comes from a shared variable or function. - Stacked modal dialogs.
closesNativeLayercounted every open modal dialog, so with one modal dialog on top of another, a hotkey targeted inside the top dialog skipped Escape because the lower dialog didn't contain it. Focus can't leave the top modal dialog, so while focus is inside a modal dialog, only the dialogs that contain focus count. Popovers are unchanged. - Tests:
- validation and recorder tests assert reason codes, and a devtools test checks the described issue
- every console call is checked with
processundefined - unit tests for untyped unknown-modifier and modifier-only input, in both string and object form, covering registration, matching, validation and types
- browser tests for stacked dialogs in Chromium, Firefox and WebKit
- Typed-behavior fingerprints: the old values covered the removed modifier codes. With those codes dropped from the test's list, the new values are identical on
mainand this branch, exceptrecorders. That one changes only because'invalid'became'whitespace'. Mapping the new name back in the test restores the old value. - Docs: the sensible-defaults guide and the hotkeys-core skill now say these hotkeys register nothing, and the guide describes stacked dialogs. All size budgets pass. Dropping the message text cuts the full bundle from 12.85 kB to 12.68 kB and
HotkeyRecorderfrom 4.57 kB to 4.45 kB (brotli).
Notes#
Tapping or double-tapping a modifier alone used to work by accident through Shift+[ShiftLeft] with eventType: 'keyup', which was undocumented and untested. That spelling now registers nothing. Taps belong in a separate gesture API.
✅ 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).
- 1
Important
Draft PR not reviewed#
Draft PRs are not automatically reviewed by default.
- Trigger a manual review
To automatically review draft PRs, update your CodeRabbit configuration:
reviews: auto_review: drafts: true- 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.
🚀 Changeset Version Preview#
12 package(s) bumped directly, 6 bumped as dependents.
🟨 Minor bumps#
Package Version Reason @tanstack/hotkeys0.24.0 → 0.25.0 Changeset @tanstack/hotkeys-devtools1.8.1 → 1.9.0 Changeset 🟩 Patch bumps#
Package Version Reason @tanstack/alpine-hotkeys0.11.1 → 0.11.2 Changeset @tanstack/angular-hotkeys0.21.1 → 0.21.2 Changeset @tanstack/ember-hotkeys0.10.1 → 0.10.2 Changeset @tanstack/lit-hotkeys0.22.1 → 0.22.2 Changeset @tanstack/octane-hotkeys0.11.0 → 0.11.1 Changeset @tanstack/preact-hotkeys0.20.1 → 0.20.2 Changeset @tanstack/react-hotkeys0.22.0 → 0.22.1 Changeset @tanstack/solid-hotkeys0.21.0 → 0.21.1 Changeset @tanstack/svelte-hotkeys0.21.0 → 0.21.1 Changeset @tanstack/vue-hotkeys0.21.1 → 0.21.2 Changeset @tanstack/angular-hotkeys-devtools0.5.1 → 0.5.2 Dependent @tanstack/preact-hotkeys-devtools0.13.1 → 0.13.2 Dependent @tanstack/react-hotkeys-devtools0.14.1 → 0.14.2 Dependent @tanstack/solid-hotkeys-devtools0.13.1 → 0.13.2 Dependent @tanstack/svelte-hotkeys-devtools0.5.1 → 0.5.2 Dependent @tanstack/vue-hotkeys-devtools0.13.1 → 0.13.2 Dependent @tanstack/alpine-hotkeys
npm i https://pkg.pr.new/@tanstack/alpine-hotkeys@230@tanstack/angular-hotkeys
npm i https://pkg.pr.new/@tanstack/angular-hotkeys@230@tanstack/angular-hotkeys-devtools
npm i https://pkg.pr.new/@tanstack/angular-hotkeys-devtools@230@tanstack/ember-hotkeys
npm i https://pkg.pr.new/@tanstack/ember-hotkeys@230@tanstack/hotkeys
npm i https://pkg.pr.new/@tanstack/hotkeys@230@tanstack/hotkeys-devtools
npm i https://pkg.pr.new/@tanstack/hotkeys-devtools@230@tanstack/lit-hotkeys
npm i https://pkg.pr.new/@tanstack/lit-hotkeys@230@tanstack/octane-hotkeys
npm i https://pkg.pr.new/@tanstack/octane-hotkeys@230@tanstack/preact-hotkeys
npm i https://pkg.pr.new/@tanstack/preact-hotkeys@230@tanstack/preact-hotkeys-devtools
npm i https://pkg.pr.new/@tanstack/preact-hotkeys-devtools@230@tanstack/react-hotkeys
npm i https://pkg.pr.new/@tanstack/react-hotkeys@230@tanstack/react-hotkeys-devtools
npm i https://pkg.pr.new/@tanstack/react-hotkeys-devtools@230@tanstack/solid-hotkeys
npm i https://pkg.pr.new/@tanstack/solid-hotkeys@230@tanstack/solid-hotkeys-devtools
npm i https://pkg.pr.new/@tanstack/solid-hotkeys-devtools@230@tanstack/svelte-hotkeys
npm i https://pkg.pr.new/@tanstack/svelte-hotkeys@230@tanstack/svelte-hotkeys-devtools
npm i https://pkg.pr.new/@tanstack/svelte-hotkeys-devtools@230@tanstack/vue-hotkeys
npm i https://pkg.pr.new/@tanstack/vue-hotkeys@230@tanstack/vue-hotkeys-devtools
npm i https://pkg.pr.new/@tanstack/vue-hotkeys-devtools@230commit:
33cb287KevinVandy changed the title
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 33cb28743ebe6330713fb89c920ce983ed4098d2
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 18 packages
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