-
-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathSearchBox.vue
More file actions
126 lines (111 loc) · 3.2 KB
/
SearchBox.vue
File metadata and controls
126 lines (111 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<script setup lang="ts">
import { debounce } from 'perfect-debounce'
import { normalizeSearchParam } from '#shared/utils/url'
withDefaults(
defineProps<{
inputClass?: string
}>(),
{
inputClass: 'inline sm:block',
},
)
const emit = defineEmits(['blur', 'focus'])
const router = useRouter()
const route = useRoute()
const isSearchFocused = shallowRef(false)
const showSearchBar = computed(() => {
return route.name !== 'index'
})
// Local input value (updates immediately as user types)
const searchQuery = shallowRef(normalizeSearchParam(route.query.q))
// Pages that have their own local filter using ?q
const pagesWithLocalFilter = new Set(['~username', 'org'])
// Debounced URL update for search query
const updateUrlQuery = debounce((value: string) => {
// Don't navigate away from pages that use ?q for local filtering
if (pagesWithLocalFilter.has(route.name as string)) {
return
}
if (route.name === 'search') {
router.replace({ query: { q: value || undefined } })
return
}
if (!value) {
return
}
router.push({
name: 'search',
query: {
q: value,
},
})
}, 250)
// Watch input and debounce URL updates
watch(searchQuery, value => {
updateUrlQuery(value)
})
// Sync input with URL when navigating (e.g., back button)
watch(
() => route.query.q,
urlQuery => {
// Don't sync from pages that use ?q for local filtering
if (pagesWithLocalFilter.has(route.name as string)) {
return
}
const value = normalizeSearchParam(urlQuery)
if (searchQuery.value !== value) {
searchQuery.value = value
}
},
)
function handleSubmit() {
if (pagesWithLocalFilter.has(route.name as string)) {
router.push({
name: 'search',
query: {
q: searchQuery.value,
},
})
} else {
updateUrlQuery.flush()
}
}
// Expose focus method for parent components
const inputRef = useTemplateRef('inputRef')
function focus() {
inputRef.value?.focus()
}
defineExpose({ focus })
</script>
<template>
<search v-if="showSearchBar" :class="'flex-1 sm:max-w-md ' + inputClass">
<form method="GET" action="/search" class="relative" @submit.prevent="handleSubmit">
<label for="header-search" class="sr-only">
{{ $t('search.label') }}
</label>
<div class="relative group" :class="{ 'is-focused': isSearchFocused }">
<div class="search-box relative flex items-center">
<span
class="absolute inset-is-3 text-fg-subtle font-mono text-sm pointer-events-none transition-colors duration-200 motion-reduce:transition-none [.group:hover:not(:focus-within)_&]:text-fg/80 group-focus-within:text-accent z-1"
>
/
</span>
<InputBase
id="header-search"
ref="inputRef"
v-model="searchQuery"
type="search"
name="q"
:placeholder="$t('search.placeholder')"
no-correct
class="w-full min-w-25 ps-7"
@focus="isSearchFocused = true"
@blur="isSearchFocused = false"
size="small"
/>
<button type="submit" class="sr-only">{{ $t('search.button') }}</button>
</div>
</div>
</form>
</search>
</template>