Skip to content

Commit 7bc1ec6

Browse files
fix: autofocus
1 parent ec394e5 commit 7bc1ec6

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

app/components/AppHeader.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ onKeyStroke(
107107
<div
108108
class="flex-1 flex items-center justify-center md:gap-6"
109109
:class="{ 'hidden sm:flex': !isSearchExpanded }"
110+
v-if="!isOnHomePage"
110111
>
111112
<!-- Search bar (hidden on mobile unless expanded) -->
112113
<HeaderSearchBox

app/components/Header/SearchBox.vue

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ const showSearchBar = computed(() => {
2121
2222
// Local input value (updates immediately as user types)
2323
const searchQuery = shallowRef('')
24-
onMounted(() => {
25-
searchQuery.value = normalizeSearchParam(route.query.q)
26-
})
24+
watch(
25+
() => route.query.q,
26+
query => {
27+
searchQuery.value = normalizeSearchParam(query)
28+
},
29+
{ immediate: true },
30+
)
2731
2832
// Pages that have their own local filter using ?q
2933
const pagesWithLocalFilter = new Set(['~username', 'org'])
@@ -57,6 +61,13 @@ function handleSearchFocus() {
5761
}
5862
5963
const inputRef = useTemplateRef('inputRef')
64+
function focus() {
65+
const input = inputRef.value
66+
if (input) {
67+
input.focus()
68+
input.setSelectionRange(input.value.length, input.value.length)
69+
}
70+
}
6071
6172
function handleSubmit() {
6273
if (pagesWithLocalFilter.has(route.name as string)) {
@@ -70,13 +81,12 @@ function handleSubmit() {
7081
updateUrlQuery(searchQuery.value)
7182
}
7283
73-
inputRef.value?.focus()
84+
focus()
7485
}
7586
87+
onMounted(focus)
88+
7689
// Expose focus method for parent components
77-
function focus() {
78-
inputRef.value?.focus()
79-
}
8090
defineExpose({ focus })
8191
</script>
8292
<template>

0 commit comments

Comments
 (0)