-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathSearchBox.vue
More file actions
81 lines (73 loc) · 2.46 KB
/
SearchBox.vue
File metadata and controls
81 lines (73 loc) · 2.46 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
<script setup lang="ts">
withDefaults(
defineProps<{
inputClass?: string
}>(),
{
inputClass: 'inline sm:block',
},
)
const emit = defineEmits(['blur', 'focus'])
const route = useRoute()
const isSearchFocused = shallowRef(false)
const showSearchBar = computed(() => {
return route.name !== 'index'
})
const { model: searchQuery, startSearch } = useGlobalSearch('header')
const hasSearchQuery = computed(() => searchQuery.value.trim().length > 0)
function handleSubmit() {
startSearch()
}
function clearSearch() {
searchQuery.value = ''
inputRef.value?.focus()
}
// 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 pe-8"
@focus="isSearchFocused = true"
@blur="isSearchFocused = false"
size="small"
/>
<button
v-if="hasSearchQuery"
type="button"
class="absolute inset-ie-2 h-6 w-6 items-center justify-center rounded text-fg-muted hover:text-fg focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent group-focus-within:flex group-hover:inline-flex hidden"
@click="clearSearch"
aria-hidden="true"
tabindex="-1"
>
<span class="i-lucide:circle-x h-4 w-4" />
</button>
<button type="submit" class="sr-only">{{ $t('search.button') }}</button>
</div>
</div>
</form>
</search>
</template>