-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathVHeaderMobile.vue
323 lines (293 loc) · 10.2 KB
/
VHeaderMobile.vue
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<template>
<header
ref="headerRef"
class="main-header z-30 flex w-full items-center bg-white px-6 py-4"
>
<VInputModal
class="flex w-full"
variant="recent-searches"
:is-active="isRecentSearchesModalOpen"
aria-label="inputmodal"
@close="deactivate"
>
<div class="flex w-full" :class="{ 'px-2': isRecentSearchesModalOpen }">
<!-- Form action is a fallback for when JavaScript is disabled. -->
<form
action="/search"
class="search-bar group flex h-12 w-full flex-row items-center overflow-hidden rounded-sm"
:class="
searchBarIsActive
? 'bg-white ring ring-pink'
: 'bg-dark-charcoal-06'
"
@submit.prevent="handleSearch"
>
<slot name="start">
<VLogoButton
v-show="!searchBarIsActive"
:is-fetching="isFetching"
/>
<VSearchBarButton
v-show="searchBarIsActive"
icon="chevron-left"
:label="$t('header.backButton')"
:rtl-flip="true"
variant="filled-gray"
@click="handleBack"
/>
</slot>
<input
id="search-bar"
ref="searchInputRef"
name="q"
:placeholder="$t('hero.search.placeholder').toString()"
type="search"
class="search-field ms-1 h-full w-full flex-grow appearance-none rounded-none border-tx bg-tx text-2xl text-dark-charcoal-70 placeholder-dark-charcoal-70 hover:text-dark-charcoal hover:placeholder-dark-charcoal focus-visible:outline-none"
:value="searchTerm"
:aria-label="
$t('search.searchBarLabel', {
openverse: 'Openverse',
}).toString()
"
autocomplete="off"
role="combobox"
aria-autocomplete="none"
:aria-expanded="showRecentSearches"
aria-controls="recent-searches-list"
:aria-activedescendant="
selectedIdx !== undefined ? `option-${selectedIdx}` : undefined
"
@input="updateSearchText"
@focus="activate"
@keydown="handleKeydown"
/>
<slot>
<VSearchBarButton
v-show="searchBarIsActive && searchTerm"
icon="close-small"
:label="$t('browsePage.searchForm.clear')"
inner-area-classes="bg-white hover:bg-dark-charcoal-10"
@click="clearSearchText"
/>
<span
v-show="!searchBarIsActive && searchStatus"
class="info mx-4 hidden whitespace-nowrap text-xs group-hover:text-dark-charcoal group-focus:text-dark-charcoal md:flex"
>
{{ searchStatus }}
</span>
<VContentSettingsButton
v-show="!searchBarIsActive"
:is-pressed="contentSettingsOpen"
:applied-filter-count="appliedFilterCount"
v-bind="triggerA11yProps"
@click="toggleContentSettings"
/>
<VContentSettingsModalContent
v-show="!searchBarIsActive"
:visible="contentSettingsOpen"
:is-fetching="isFetching"
:close="closeContentSettings"
labelledby="content-settings-button"
/>
</slot>
</form>
</div>
<ClientOnly>
<VRecentSearches
v-show="showRecentSearches"
:selected-idx="selectedIdx"
:entries="entries"
:bordered="false"
class="mt-4"
@select="handleSelect"
@clear="handleClear"
/>
</ClientOnly>
</VInputModal>
</header>
</template>
<script lang="ts">
import { computed, defineComponent, nextTick, ref, watch } from "vue"
import { ensureFocus } from "~/utils/reakit-utils/focus"
import { cyclicShift } from "~/utils/math"
import { keycodes } from "~/constants/key-codes"
import { useAnalytics } from "~/composables/use-analytics"
import { useDialogControl } from "~/composables/use-dialog-control"
import { useSearch } from "~/composables/use-search"
import { useMediaStore } from "~/stores/media"
import { useSearchStore } from "~/stores/search"
import VLogoButton from "~/components/VHeader/VLogoButton.vue"
import VInputModal from "~/components/VModal/VInputModal.vue"
import VContentSettingsModalContent from "~/components/VHeader/VHeaderMobile/VContentSettingsModalContent.vue"
import VContentSettingsButton from "~/components/VHeader/VHeaderMobile/VContentSettingsButton.vue"
import VRecentSearches from "~/components/VRecentSearches/VRecentSearches.vue"
import VSearchBarButton from "~/components/VHeader/VHeaderMobile/VSearchBarButton.vue"
/**
* Displays a text field for a search query and is attached to an action button
* that fires a search request. The loading state and number of hits are also
* displayed in the bar itself.
*/
export default defineComponent({
name: "VHeaderMobile",
components: {
VContentSettingsModalContent,
VContentSettingsButton,
VInputModal,
VLogoButton,
VRecentSearches,
VSearchBarButton,
},
setup(_, { emit }) {
const searchInputRef = ref<HTMLInputElement | null>(null)
const headerRef = ref<HTMLElement | null>(null)
const mediaStore = useMediaStore()
const searchStore = useSearchStore()
const searchBarIsActive = ref(false)
const contentSettingsOpen = ref(false)
const isFetching = computed(() => mediaStore.fetchState.isFetching)
const { sendCustomEvent } = useAnalytics()
const { updateSearchState, searchTerm, searchStatus } =
useSearch(sendCustomEvent)
const handleSearch = async () => {
window.scrollTo({ top: 0, left: 0, behavior: "auto" })
updateSearchState()
deactivate()
}
const isRecentSearchesModalOpen = ref(false)
const activate = () => (searchBarIsActive.value = true)
const deactivate = () => {
searchBarIsActive.value = false
}
watch(searchBarIsActive, (active) => {
if (active) {
isRecentSearchesModalOpen.value = true
/**
* Without `nextTick`, the search bar is not focused on click in Firefox
*/
nextTick(() => {
if (searchInputRef.value) ensureFocus(searchInputRef.value)
})
} else {
isRecentSearchesModalOpen.value = false
if (searchTerm.value === "" && searchStore.searchTerm !== "") {
searchTerm.value = searchStore.searchTerm
}
}
})
const appliedFilterCount = computed(() => searchStore.appliedFilterCount)
const updateSearchText = (event: Event) => {
searchTerm.value = (event.target as HTMLInputElement).value
}
const clearSearchText = () => {
searchTerm.value = ""
if (searchInputRef.value) {
ensureFocus(searchInputRef.value)
}
}
const handleBack = () => {
deactivate()
}
/**
* Refers to the current suggestion that has visual focus (not DOM focus)
* and is the active descendant. This should be set to `undefined` when the
* visual focus is on the input field.
*/
const selectedIdx = ref<number | undefined>(undefined)
const entries = computed(() => searchStore.recentSearches)
const handleVerticalArrows = (event: KeyboardEvent) => {
event.preventDefault() // Prevent the cursor from moving horizontally.
const { key, altKey } = event
// Show the recent searches.
isRecentSearchesModalOpen.value = true
if (altKey) return
// Shift selection (if Alt was not pressed with arrow keys)
let defaultValue: number
let offset: number
if (key == keycodes.ArrowUp) {
defaultValue = 0
offset = -1
} else {
defaultValue = -1
offset = 1
}
selectedIdx.value = cyclicShift(
selectedIdx.value ?? defaultValue,
offset,
0,
entries.value.length
)
}
const handleOtherKeys = (event: KeyboardEvent) => {
const { key } = event
if (key === keycodes.Enter && selectedIdx.value)
// If a recent search is selected, populate its value into the input.
searchTerm.value = entries.value[selectedIdx.value]
if (([keycodes.Escape] as string[]).includes(key))
// Hide the recent searches.
isRecentSearchesModalOpen.value = false
selectedIdx.value = undefined // Lose visual focus from entries.
}
const handleKeydown = (event: KeyboardEvent) => {
const { key } = event
return ([keycodes.ArrowUp, keycodes.ArrowDown] as string[]).includes(key)
? handleVerticalArrows(event)
: handleOtherKeys(event)
}
/* Populate the input with the clicked entry and execute the search. */
const handleSelect = (idx: number) => {
searchTerm.value = entries.value[idx]
isRecentSearchesModalOpen.value = false
selectedIdx.value = undefined // Lose visual focus from entries.
handleSearch() // Immediately execute the search manually.
}
/* Clear all recent searches from the store. */
const handleClear = () => {
searchStore.clearRecentSearches()
if (searchInputRef.value) {
ensureFocus(searchInputRef.value)
}
}
const showRecentSearches = computed(
() => isRecentSearchesModalOpen.value && entries.value.length > 0
)
const {
close: closeContentSettings,
open: openContentSettings,
onTriggerClick: toggleContentSettings,
triggerA11yProps,
} = useDialogControl({
visibleRef: contentSettingsOpen,
nodeRef: headerRef,
lockBodyScroll: true,
emit,
})
return {
searchInputRef,
headerRef,
isFetching,
appliedFilterCount,
contentSettingsOpen,
openContentSettings,
closeContentSettings,
toggleContentSettings,
triggerA11yProps,
isRecentSearchesModalOpen,
showRecentSearches,
searchBarIsActive,
activate,
deactivate,
searchStatus,
searchTerm,
clearSearchText,
updateSearchText,
handleSearch,
handleBack,
selectedIdx,
entries,
handleKeydown,
handleSelect,
handleClear,
}
},
})
</script>