From 12d53e52326ca6b28bff7e362a834a3516e63834 Mon Sep 17 00:00:00 2001 From: Chirag6722 Date: Mon, 14 Sep 2026 11:00:06 +0530 Subject: [PATCH 1/2] fix: focus the search input through the owning component instead of a 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 #3216 --- app/app.vue | 10 +- app/components/Header/SearchBox.vue | 6 +- app/composables/useSearchInputFocus.ts | 54 ++++++ app/pages/index.vue | 9 + app/pages/search.vue | 10 +- .../use-search-input-focus.spec.ts | 163 ++++++++++++++++++ 6 files changed, 234 insertions(+), 18 deletions(-) create mode 100644 app/composables/useSearchInputFocus.ts create mode 100644 test/nuxt/composables/use-search-input-focus.spec.ts diff --git a/app/app.vue b/app/app.vue index 738d278529..61109846ec 100644 --- a/app/app.vue +++ b/app/app.vue @@ -48,6 +48,7 @@ if (import.meta.server) { } const keyboardShortcuts = useKeyboardShortcuts() +const focusSearchInput = provideSearchInputFocus().focus const { settings } = useSettings() initKeyShortcuts() @@ -64,14 +65,7 @@ onKeyDown( if (!keyboardShortcuts.value || isEditableElement(e.target)) return e.preventDefault() - const searchInput = document.querySelector( - 'input[type="search"], input[name="q"]', - ) - - if (searchInput) { - searchInput.focus() - return - } + if (focusSearchInput()) return router.push({ name: 'search' }) }, diff --git a/app/components/Header/SearchBox.vue b/app/components/Header/SearchBox.vue index e416735a58..6ae3a23b9d 100644 --- a/app/components/Header/SearchBox.vue +++ b/app/components/Header/SearchBox.vue @@ -39,11 +39,15 @@ function handleBlur() { emit('blur') } function focus() { - inputRef.value?.focus() + const input = inputRef.value + if (!input) return false + input.focus() + return true } function blur() { inputRef.value?.blur() } +useSearchInputFocusTarget(focus) defineExpose({ focus, blur })