Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Hide the recent searches when navigating with the keyboard (#1889)

Dhruv Bhanushali755915569fa1
diff --git a/src/components/VHeader/VSearchBar/VSearchBar.vue b/src/components/VHeader/VSearchBar/VSearchBar.vue
index a60d91b59..8514f8604 100644
--- a/src/components/VHeader/VSearchBar/VSearchBar.vue
+++ b/src/components/VHeader/VSearchBar/VSearchBar.vue
@@ -6,6 +6,7 @@
@submit.prevent="handleSearch"
>
<VInputField
+ ref="inputFieldRef"
v-bind="$attrs"
v-model="modelMedium"
:placeholder="placeholder || $t('hero.search.placeholder')"
@@ -37,7 +38,12 @@
<!-- @slot Extra information such as loading message or result count goes here. -->
<slot />
</VInputField>
- <VSearchButton type="submit" :size="size" :route="route" />
+ <VSearchButton
+ type="submit"
+ :size="size"
+ :route="route"
+ @keydown.tab="handleSearchBlur"
+ />
</form>
<ClientOnly>
<VRecentSearches
@@ -48,6 +54,7 @@
:class="recentClasses"
@select="handleSelect"
@clear="handleClear"
+ @keydown.tab.native="handleBlur"
/>
</ClientOnly>
</div>
@@ -115,6 +122,7 @@ export default defineComponent({
},
setup(props, { emit }) {
const searchBarEl = ref<HTMLElement | null>(null)
+ const inputFieldRef = ref<InstanceType<typeof VInputField> | null>(null)
const { matches: isHomeRoute } = useMatchHomeRoute()
@@ -140,6 +148,9 @@ export default defineComponent({
const handleBlur = () => {
isRecentVisible.value = false
}
+ const handleSearchBlur = () => {
+ if (!entries.value.length) handleBlur()
+ }
onClickOutside(searchBarEl, handleBlur)
/* Recent searches */
@@ -224,11 +235,13 @@ export default defineComponent({
}
/* Clear all recent searches from the store. */
const handleClear = () => {
+ inputFieldRef.value?.focusInput()
searchStore.clearRecentSearches()
}
return {
searchBarEl,
+ inputFieldRef,
handleSearch,
route,
@@ -236,6 +249,7 @@ export default defineComponent({
handleFocus,
handleBlur,
+ handleSearchBlur,
isNewHeaderEnabled,
isRecentVisible,
diff --git a/src/components/VInputField/VInputField.vue b/src/components/VInputField/VInputField.vue
index 9ce8ad91a..c5de3ac44 100644
--- a/src/components/VInputField/VInputField.vue
+++ b/src/components/VInputField/VInputField.vue
@@ -15,6 +15,7 @@
<input
:id="fieldId"
v-bind="$attrs"
+ ref="inputEl"
:type="type"
class="h-full w-full appearance-none rounded-none bg-tx text-2xl font-semibold leading-none placeholder-dark-charcoal-70 ms-4 focus:outline-none md:text-base"
:value="modelValue"
@@ -29,7 +30,12 @@
</template>
<script lang="ts">
-import { computed, defineComponent, PropType } from '@nuxtjs/composition-api'
+import {
+ ref,
+ computed,
+ defineComponent,
+ PropType,
+} from '@nuxtjs/composition-api'
import { defineEvent } from '~/types/emits'
@@ -100,7 +106,14 @@ export default defineComponent({
emits: {
'update:modelValue': defineEvent<[string]>(),
},
+ expose: ['focusInput'],
setup(props, { emit, attrs }) {
+ const inputEl = ref<HTMLInputElement | null>(null)
+
+ const focusInput = () => {
+ inputEl.value?.focus()
+ }
+
const type = typeof attrs['type'] === 'string' ? attrs['type'] : 'text'
const updateModelValue = (event: Event) => {
@@ -109,6 +122,10 @@ export default defineComponent({
const sizeClass = computed(() => FIELD_SIZES[props.size])
return {
+ inputEl,
+
+ focusInput,
+
emit,
type,