Skip to content
Source
Sign inSign up
TanStack/hotkeys

feat(hotkeys): Reject unusable hotkeys, return reason codes, and let the top modal dialog take Escape #230

OpenKevinVandywants to merge 5 commits intomain fromfix/reject-unusable-hotkeysopened

KevinVandycommented

🎯 Changes

  • Unknown modifiers register nothing. parseHotkey used to drop a modifier it didn't know, so Ctl+K from 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 and matchesKeyboardEvent already treat that as "registers nothing", so it never fires. normalizeHotkey and formatForDisplay return '' for it.
  • Modifier-only hotkeys register nothing. chordHasKey now 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. hasNonModifierKey reuses the same check.
  • Validation and types. validateHotkey reports modifier-only hotkeys as errors. PhysicalKeyCode no longer includes ShiftLeft, ControlRight and the other modifier codes, so [ShiftLeft] is a type error like Shift. Chord objects still type-check, because their key and code accept any string.
  • Reason codes instead of text. validateHotkey returns HotkeyIssue objects such as { reason: 'unknown-modifier', part: 'Ctl', step: 1 } instead of English sentences with a Step 2: prefix. RecorderRejection drops its built-in message and has one only when a validate callback 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 escape that 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 a ReferenceError where process is undefined, such as plain ESM from a CDN. Each dev-only guard now checks typeof process first. 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. closesNativeLayer counted 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 process undefined
    • 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 main and this branch, except recorders. 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 HotkeyRecorder from 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. changeset-bot[bot](GitHub profile, opens in a new tab)commented

      🦋 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
      NameType
      @tanstack/hotkeysMinor
      @tanstack/hotkeys-devtoolsMinor
      @tanstack/alpine-hotkeysPatch
      @tanstack/angular-hotkeysPatch
      @tanstack/ember-hotkeysPatch
      @tanstack/lit-hotkeysPatch
      @tanstack/octane-hotkeysPatch
      @tanstack/preact-hotkeysPatch
      @tanstack/react-hotkeysPatch
      @tanstack/solid-hotkeysPatch
      @tanstack/svelte-hotkeysPatch
      @tanstack/vue-hotkeysPatch
      @tanstack/angular-hotkeys-devtoolsPatch
      @tanstack/preact-hotkeys-devtoolsPatch
      @tanstack/react-hotkeys-devtoolsPatch
      @tanstack/solid-hotkeys-devtoolsPatch
      @tanstack/svelte-hotkeys-devtoolsPatch
      @tanstack/vue-hotkeys-devtoolsPatch

      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

    2. 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.

      Share

    3. github-actions[bot](GitHub profile, opens in a new tab)commented

      🚀 Changeset Version Preview

      12 package(s) bumped directly, 6 bumped as dependents.

      🟨 Minor bumps

      PackageVersionReason
      @tanstack/hotkeys0.24.0 → 0.25.0Changeset
      @tanstack/hotkeys-devtools1.8.1 → 1.9.0Changeset

      🟩 Patch bumps

      PackageVersionReason
      @tanstack/alpine-hotkeys0.11.1 → 0.11.2Changeset
      @tanstack/angular-hotkeys0.21.1 → 0.21.2Changeset
      @tanstack/ember-hotkeys0.10.1 → 0.10.2Changeset
      @tanstack/lit-hotkeys0.22.1 → 0.22.2Changeset
      @tanstack/octane-hotkeys0.11.0 → 0.11.1Changeset
      @tanstack/preact-hotkeys0.20.1 → 0.20.2Changeset
      @tanstack/react-hotkeys0.22.0 → 0.22.1Changeset
      @tanstack/solid-hotkeys0.21.0 → 0.21.1Changeset
      @tanstack/svelte-hotkeys0.21.0 → 0.21.1Changeset
      @tanstack/vue-hotkeys0.21.1 → 0.21.2Changeset
      @tanstack/angular-hotkeys-devtools0.5.1 → 0.5.2Dependent
      @tanstack/preact-hotkeys-devtools0.13.1 → 0.13.2Dependent
      @tanstack/react-hotkeys-devtools0.14.1 → 0.14.2Dependent
      @tanstack/solid-hotkeys-devtools0.13.1 → 0.13.2Dependent
      @tanstack/svelte-hotkeys-devtools0.5.1 → 0.5.2Dependent
      @tanstack/vue-hotkeys-devtools0.13.1 → 0.13.2Dependent
    4. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented

      Open in StackBlitz

      @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@230

      commit: 33cb287

    5. KevinVandy changed the title

Sign in to join this conversation.