fix(hotkeys): Detect text fields more accurately and check input focus once per key press #209
main fromrefs/pull/209/head·merged by KevinVandy 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.
KandJnow fire while a list's checkbox has focus. role="textbox"androle="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 aroleattribute 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. hasActiveRegistrationand 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 targetgetActiveElementForListenerTarget- the
listenerTargetparameter, 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:
| Import | main | This PR |
|---|---|---|
| Everything | 13,268 B | 13,474 B |
getHotkeyManager | 7,775 B | 7,900 B |
HotkeyRecorder | 5,764 B | 5,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.tshas 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 Version Preview#
16 package(s) bumped directly, 2 bumped as dependents.
🟨 Minor bumps#
Package Version Reason @tanstack/alpine-hotkeys0.6.0 → 0.7.0 Changeset @tanstack/hotkeys0.18.0 → 0.19.0 Changeset @tanstack/react-hotkeys0.17.0 → 0.18.0 Changeset @tanstack/react-hotkeys-devtools0.11.1 → 0.12.0 Changeset 🟩 Patch bumps#
Package Version Reason @tanstack/angular-hotkeys-devtools0.3.1 → 0.3.2 Changeset @tanstack/ember-hotkeys0.6.0 → 0.6.1 Changeset @tanstack/hotkeys-devtools1.6.0 → 1.6.1 Changeset @tanstack/lit-hotkeys0.18.0 → 0.18.1 Changeset @tanstack/preact-hotkeys0.17.0 → 0.17.1 Changeset @tanstack/preact-hotkeys-devtools0.11.1 → 0.11.2 Changeset @tanstack/solid-hotkeys0.17.0 → 0.17.1 Changeset @tanstack/solid-hotkeys-devtools0.11.1 → 0.11.2 Changeset @tanstack/svelte-hotkeys0.17.0 → 0.17.1 Changeset @tanstack/svelte-hotkeys-devtools0.3.1 → 0.3.2 Changeset @tanstack/vue-hotkeys0.17.0 → 0.17.1 Changeset @tanstack/vue-hotkeys-devtools0.11.1 → 0.11.2 Changeset @tanstack/angular-hotkeys0.17.0 → 0.17.1 Dependent @tanstack/octane-hotkeys0.7.0 → 0.7.1 Dependent 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
Priority: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.tsFocus 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.tsInput 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.jsonHotkeyManageruses the revised input checks and resolves document-level targets. Tests cover target-relative blocking, iframe focus, and the updated internal test surface. NormalEstimated 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 decisionPossibly 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
- TanStack/hotkeys#199 — Both PRs change hotkey eligibility and focus checks in
_event-target.ts.
Suggested reviewers:
kadeangell,wojtowicz-artur,fredericbahr
Merge Risk:
Minimal · up to936e0-
No actionable merge-blocking issue was established for the hotkey behavior changes.
Pre-merge checks:
4 · 1-
Failed checks: 1 warningCheck name Status Explanation Resolution Docstring Coverage 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. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold. Passed checks: 4 passed-
Check name Status Explanation Title check The title is concise, specific, and accurately describes the main input-detection and per-key focus-check changes. Description check 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 heading… Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check 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.
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.-
@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@209commit:
936e0baKevinVandy merged this132e0c9
KevinVandy closed this
Sign in to join this conversation.
🦋 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
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