Skip to content

Commit ade821d

Browse files
committed
fix: watch if search input is visible and set focus
1 parent 6361d3c commit ade821d

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

app/components/SearchBox.vue

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@ const emit = defineEmits(['blur', 'focus'])
1515
const router = useRouter()
1616
const route = useRoute()
1717
18+
const searchInputRef = useTemplateRef('searchInputRef')
1819
const isSearchFocused = ref(false)
1920
2021
const showSearchBar = computed(() => {
2122
return route.name !== 'index'
2223
})
2324
25+
// Focus input when search bar becomes visible (e.g., when navigating from homepage)
26+
watch(showSearchBar, visible => {
27+
if (visible) {
28+
nextTick(() => {
29+
searchInputRef.value?.focus()
30+
})
31+
}
32+
})
33+
2434
// Local input value (updates immediately as user types)
2535
const searchQuery = ref((route.query.q as string) ?? '')
2636
@@ -84,7 +94,7 @@ function handleSearchFocus() {
8494

8595
<input
8696
id="header-search"
87-
autofocus
97+
ref="searchInputRef"
8898
v-model="searchQuery"
8999
type="search"
90100
name="q"

0 commit comments

Comments
 (0)