Skip to content

fix: focus the search input through the owning component instead of a DOM query - #3252

Open
Chirag6722 wants to merge 2 commits into
npmx-dev:mainfrom
Chirag6722:fix/3216-search-input-focus
Open

Chirag6722 wants to merge 2 commits into
npmx-dev:mainfrom
Chirag6722:fix/3216-search-input-focus

Conversation

@Chirag6722

@Chirag6722 Chirag6722 commented Sep 14, 2026

Copy link
Copy Markdown

resolves #3216

Context

The / shortcut in app.vue and the ArrowUp handler in pages/search.vue found the search input with a document.querySelector. That input lives in two other components, so a markup change there would silently break both shortcuts.

Description

Added a small useSearchInputFocus composable. app.vue provides it, Header/SearchBox.vue and pages/index.vue register their own input, and the two handlers call the injected focus() instead of querying the DOM. The header box returns false when its input is not rendered (homepage), so the homepage input is used there. If nothing is registered, / still falls back to router.push({ name: 'search' }).

Behaviour is unchanged. The existing mobile quirk (header input hidden by CSS, so focus does nothing) is left as is; happy to follow up on it.

Tests: 7 cases in test/nuxt/composables/use-search-input-focus.spec.ts plus one e2e case for / on the homepage. They fail if I remove the registrations or the cleanup. Lint, fmt, nuxt tests and the search e2e tests pass locally; test:types I could only verify on CI.

… DOM query

The '/' shortcut in app.vue and the ArrowUp handler on the search page
located the search input with a selector that depended on markup in
Header/SearchBox.vue and pages/index.vue. Add a useSearchInputFocus
composable: app.vue provides a registry, both search inputs register
their focus method, and the shortcut handlers call it.

resolves npmx-dev#3216
@agentscanapp

agentscanapp Bot commented Sep 14, 2026

Copy link
Copy Markdown

Thanks for opening this pull request! 🎉

We really appreciate you taking the time to contribute, @Chirag6722.

A maintainer will take a look as soon as they can. In the meantime, please make sure that:

  • the description explains what changed and why
  • any related issues are linked
  • existing tests still pass

If anything needs adjusting we'll leave comments here. Thanks again!

@vercel

vercel Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
npmx.dev Ready Ready Preview Sep 14, 2026 5:44am UTC
2 Skipped Deployments
Project Deployment Actions Updated
docs.npmx.dev Ignored Ignored Preview Sep 14, 2026 5:44am UTC
npmx-lunaria Ignored Ignored Sep 14, 2026 5:44am UTC

Request Review

@coderabbitai

coderabbitai Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: cf91d860-a96c-4860-97fe-f7b00008b8ab

📥 Commits

Reviewing files that changed from the base of the PR and between 0e3cdad and dfbc199.

📒 Files selected for processing (7)
  • app/app.vue
  • app/components/Header/SearchBox.vue
  • app/composables/useSearchInputFocus.ts
  • app/pages/index.vue
  • app/pages/search.vue
  • test/e2e/interactions.spec.ts
  • test/nuxt/composables/use-search-input-focus.spec.ts

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Summary

Summary by CodeRabbit

  • New Features

    • Pressing / now reliably focuses the available search input, including on the homepage and search page.
    • Search focus works across different search interfaces and safely handles unavailable inputs.
  • Bug Fixes

    • Improved keyboard shortcut behaviour when search inputs are not currently rendered.
  • Tests

    • Added coverage for keyboard-triggered search focus and focus handling across common scenarios.

Walkthrough

The PR adds a scoped search-input focus context. The homepage and header register focus targets. Keyboard handlers use the context instead of DOM selectors. Unit and end-to-end tests cover focus routing, cleanup, route changes, and the / shortcut.

Changes

Search input focus

Layer / File(s) Summary
Focus context and target lifecycle
app/composables/useSearchInputFocus.ts
The new composables provide a scoped focus function, register focus targets, skip unavailable targets, and remove targets when their scope ends.
Application focus integration
app/pages/index.vue, app/components/Header/SearchBox.vue, app/app.vue, app/pages/search.vue
The homepage and header register focus targets. The / shortcut and search-page handler use the focus context. The / shortcut retains its search-route fallback.
Focus behaviour validation
test/nuxt/composables/use-search-input-focus.spec.ts, test/e2e/interactions.spec.ts
Tests cover focus results, target ordering, cleanup, missing providers, route changes, and homepage refocusing with /.

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to dfbc1

Search focus routing preserves the navigation fallback when no target is available and includes lifecycle and shortcut coverage. The change is ready to merge.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Issue #3216 requires removal of fragile selector queries from the app-level / shortcut and the search-page ArrowUp handler. app/app.vue now calls provideSearchInputFocus().focus, and `app/pages/…
Out of Scope Changes check ✅ Passed The changes remain within Issue #3216. The composable, input registrations, focus-method update, handler updates, and related unit and end-to-end tests all support component-owned search-input focus a…
Description check ✅ Passed The description clearly explains the composable-based focus handling, affected components, preserved behaviour, and added tests. It directly relates to the changeset.
Title check ✅ Passed The title clearly and concisely identifies the main change: focusing the search input through its owning component instead of using a DOM query.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Sep 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 55.55556% with 12 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
app/pages/index.vue 0.00% 5 Missing and 1 partial ⚠️
app/app.vue 0.00% 1 Missing and 1 partial ⚠️
app/composables/useSearchInputFocus.ts 84.61% 0 Missing and 2 partials ⚠️
app/components/Header/SearchBox.vue 80.00% 0 Missing and 1 partial ⚠️
app/pages/search.vue 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@graphieros graphieros left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Chirag6722
As you know, if you read our contribution guide, the use of AI to assist with coding is perfectly acceptable, as long as it is human driven.
However, using your own voice is required when communicating with npmx contributors.
Please consider writing the PR description with your own words. It can be in your native language if it is more practical.

Thank you^^

This branch was successfully deployed

1 active deployment
Preview – npmx.dev dfbc1997 Deployed Sep 14, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fragile SearchBox selector queries

2 participants