Skip to content
Source
Sign inSign up
TanStack/hotkeys

fix(hotkeys): Detect text fields more accurately and check input focus once per key press #209

MergedKevinVandymerged 1 commit intomain fromrefs/pull/209/headmerged by KevinVandy

KevinVandycommented

Fixes four input-detection bugs and reorganizes _event-target.ts, so every remaining code path is reached by a test.

Behavior changes

  • Non-text inputs no longer block single-key hotkeys. Checkbox, radio, range, color, file and image inputs, and read-only text inputs and textareas, aren't text fields. Each keeps only the keys it uses: Space on a checkbox, arrows on a radio, arrows, Home/End and Page Up/Down on a range input, and caret keys in a read-only field. K and J now fire while a list's checkbox has focus.
  • role="textbox" and role="searchbox" count as text fields.
  • Custom elements that hide a text field count as one. If the focused custom element has no open shadow root and no tabindex, focus has to be inside a closed shadow root. A form-associated custom element without a role attribute also counts. Only the focused element itself is checked, so a focusable <app-row tabindex="0">, or a form-associated wrapper around a checkbox, doesn't block keys.
  • hasActiveRegistration and hints see focus on slotted content and focus in nested shadow roots.

The changeset is a minor bump, because single-key hotkeys now fire where they were blocked before.

Refactor

  • One walk per key press. Input detection walks out from the focused element once and caches the result for every registration the event reaches. The manager used to repeat the walk for each registration.
  • Removed code no test could reach:
    • isEventForTarget, which always returned true because listeners are attached to each registration's own target
    • getActiveElementForListenerTarget
    • the listenerTarget parameter, which every caller set to the registration target
    • several unneeded conditions
  • One map of input types, recording which aren't text fields and the keys each keeps. It replaces two lists that had to stay in sync.
  • Plain array literals for the key lists, so bundles that don't use them can drop them. Built with spreads, they were kept in every bundle.

Sizes after the refactor, gzipped:

ImportmainThis PR
Everything13,268 B13,474 B
getHotkeyManager7,775 B7,900 B
HotkeyRecorder5,764 B5,953 B

Measured with esbuild (minified, @tanstack/store external). The increase over main comes from the new detection rules. The refactor took about 75 B off the full bundle compared with the same fixes before the refactor.

Docs

sensible-defaults.md now lists what counts as an input, and its table of controls covers the native ones.

Testing

  • Coverage: _event-target.ts has 100% statement, branch, function and line coverage with istanbul.
  • Mutation run: 116 changes, each removing one condition or one key from one list. The tests caught 115. The one they missed removes the per-event cache, which only affects speed.
  • New tests in tests/input-detection.test.ts:
    • a table of 42 controls, each checking all 11 keys, both kept and free
    • negative cases: a text field outside the target, a target never blocking itself, and an unfocused custom element getting a dispatched key
    • iframe targets (document, window and element)
    • closed shadow roots, form-associated elements, and slotted and nested shadow focus
  • New browser cases in e2e/browser/focus.spec.ts: native controls, text roles, a closed shadow root and slotted focus, in Chromium, Firefox and WebKit.
  • Removed tests: the direct tests for the two deleted helpers. The iframe behavior tests cover what they checked.
  • Other checks: lint, types, tests for all packages, the build with size limits, knip, sherif and the clean-clone Intent maintainer check pass.

happy-dom doesn't assign slots, so the unit test for slotted focus sets assignedSlot by hand. The browser test covers the real behavior.

Summary by CodeRabbit

  • Bug Fixes
    • Single-key shortcuts now work in eligible non-text controls, while controls retain only the keys needed for their native behavior.
    • Hotkeys correctly recognize text-entry fields, including ARIA textbox and searchbox roles, custom elements with hidden text fields, and read-only text fields.
    • Focus detection now accounts for slotted content and nested shadow roots.
  • Documentation
    • Updated the input-handling guide with supported text fields and keys reserved by common controls.
    1. 🦋 Changeset detected

      Latest commit: 936e0bafea67ddc49a1cb9d081424cc0be348272

      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/alpine-hotkeysPatch
      @tanstack/angular-hotkeysPatch
      @tanstack/ember-hotkeysPatch
      @tanstack/hotkeys-devtoolsPatch
      @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. 🚀 Changeset Version Preview

      16 package(s) bumped directly, 2 bumped as dependents.

      🟨 Minor bumps

      PackageVersionReason
      @tanstack/alpine-hotkeys0.6.0 → 0.7.0Changeset
      @tanstack/hotkeys0.18.0 → 0.19.0Changeset
      @tanstack/react-hotkeys0.17.0 → 0.18.0Changeset
      @tanstack/react-hotkeys-devtools0.11.1 → 0.12.0Changeset

      🟩 Patch bumps

      PackageVersionReason
      @tanstack/angular-hotkeys-devtools0.3.1 → 0.3.2Changeset
      @tanstack/ember-hotkeys0.6.0 → 0.6.1Changeset
      @tanstack/hotkeys-devtools1.6.0 → 1.6.1Changeset
      @tanstack/lit-hotkeys0.18.0 → 0.18.1Changeset
      @tanstack/preact-hotkeys0.17.0 → 0.17.1Changeset
      @tanstack/preact-hotkeys-devtools0.11.1 → 0.11.2Changeset
      @tanstack/solid-hotkeys0.17.0 → 0.17.1Changeset
      @tanstack/solid-hotkeys-devtools0.11.1 → 0.11.2Changeset
      @tanstack/svelte-hotkeys0.17.0 → 0.17.1Changeset
      @tanstack/svelte-hotkeys-devtools0.3.1 → 0.3.2Changeset
      @tanstack/vue-hotkeys0.17.0 → 0.17.1Changeset
      @tanstack/vue-hotkeys-devtools0.11.1 → 0.11.2Changeset
      @tanstack/angular-hotkeys0.17.0 → 0.17.1Dependent
      @tanstack/octane-hotkeys0.7.0 → 0.7.1Dependent
    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →
      Walkthrough

      Hotkey input detection now distinguishes text-entry fields from other controls, accounts for keys reserved by focused controls, and traverses focus through shadow DOM and frames. Hotkey routing and tests use the updated target-aware checks.

      Changes: Hotkey input and focus detection

      Layer / File(s)Summary
      Deep focus and target detection
      packages/hotkeys/src/_event-target.ts, packages/hotkeys/tests/input-detection.test.ts, e2e/browser/focus.spec.ts
      Focus checks now traverse open shadow roots and composed parents. Tests cover slotted content, shadow descendants, and nested shadow roots.
      Control key ownership and input blocking
      packages/hotkeys/src/_event-target.ts, packages/hotkeys/tests/input-detection.test.ts, docs/guides/sensible-defaults.md, .changeset/input-detection.md, e2e/browser/focus.spec.ts
      Input detection and key ownership cover native controls, ARIA widgets, editable fields, and custom elements. Documentation and tests describe which keys remain reserved.
      Hotkey routing and target-relative checks
      packages/hotkeys/src/hotkey-manager.ts, packages/hotkeys/tests/input-detection.test.ts, packages/hotkeys/tests/registration-and-target.test.ts, .intent/review-state.json
      HotkeyManager uses the revised input checks and resolves document-level targets. Tests cover target-relative blocking, iframe focus, and the updated internal test surface.
      Priority: Normal

      Estimated code review effort: 4 (Complex) | ~40 minutes

      Change: Bug fix · Severity of issue fixed: Medium

      Sequence Diagram(s)

      sequenceDiagram
        participant HotkeyManager
        participant shouldIgnoreInputEvent
        participant FocusedDOM
        HotkeyManager->>shouldIgnoreInputEvent: Pass keyboard event and target
        shouldIgnoreInputEvent->>FocusedDOM: Inspect deep focus and composed event path
        FocusedDOM-->>shouldIgnoreInputEvent: Return focused element and event path
        shouldIgnoreInputEvent-->>HotkeyManager: Return input-blocking decision

      Possibly related issues

      • TanStack/hotkeys#206 — The issue reports that range, checkbox, and radio inputs block single-key hotkeys; this change lets those controls receive single-key hotkeys while retaining their reserved keys.

      Possibly related PRs

      Suggested reviewers: kadeangell, wojtowicz-artur, fredericbahr

      Merge Risk: Minimal · up to 936e0

      No actionable merge-blocking issue was established for the hotkey behavior changes.

      Pre-merge checks: Passed 4 · Failed 1
      Failed Failed checks: 1 warning
      Check nameStatusExplanationResolution
      Docstring CoverageWarningDocstring coverage is 72.22% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta…Write docstrings for the functions missing them to satisfy the coverage threshold.
      Passed Passed checks: 4 passed
      Check nameStatusExplanation
      Title checkPassedThe title is concise, specific, and accurately describes the main input-detection and per-key focus-check changes.
      Description checkPassedThe description is detailed and on-topic. It explains the behavior changes, refactor, documentation updates, testing, coverage, and release impact. It does not reproduce the template checklist heading…
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      Full details: Description check

      Explanation

      The description is detailed and on-topic. It explains the behavior changes, refactor, documentation updates, testing, coverage, and release impact. It does not reproduce the template checklist headings, but it provides most required information.

      Full details: Docstring Coverage

      Explanation

      Docstring coverage is 72.22% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 18 functions across 5 files. (3 skipped: 3 unsupported.)

      Resolution

      Write docstrings for the functions missing them to satisfy the coverage threshold.

      Finishing Touches: 💡 1
      📝 Generate docstrings: 💡
      • Commit to this branch
      • Create a new PR
      🧪 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 key with care
      The text fields keep their quiet air
      Range and checkbox claim their keys
      Slotted buttons now answer with ease
      Deep shadows yield the focus trail
      And hotkeys hop along the trail
      Comment `@coderabbitai help` to get the list of available commands.
    4. Open in StackBlitz

      @tanstack/alpine-hotkeys

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

      @tanstack/angular-hotkeys

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

      @tanstack/angular-hotkeys-devtools

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

      @tanstack/ember-hotkeys

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

      @tanstack/hotkeys

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

      @tanstack/hotkeys-devtools

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

      @tanstack/lit-hotkeys

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

      @tanstack/octane-hotkeys

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

      @tanstack/preact-hotkeys

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

      @tanstack/preact-hotkeys-devtools

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

      @tanstack/react-hotkeys

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

      @tanstack/react-hotkeys-devtools

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

      @tanstack/solid-hotkeys

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

      @tanstack/solid-hotkeys-devtools

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

      @tanstack/svelte-hotkeys

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

      @tanstack/svelte-hotkeys-devtools

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

      @tanstack/vue-hotkeys

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

      @tanstack/vue-hotkeys-devtools

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

      commit: 936e0ba

    5. KevinVandy merged this132e0c9

    6. KevinVandy closed this

    7. 🎉 This PR has been released!

      Thank you for your contribution!

Sign in to join this conversation.