Skip to content

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

Close the recent searches on Shift+Tab (#1906)

Olga Bulatbb6e47b840fb
diff --git a/src/components/VHeader/VSearchBar/VSearchBar.vue b/src/components/VHeader/VSearchBar/VSearchBar.vue
index 8514f8604..1b2ace5f1 100644
--- a/src/components/VHeader/VSearchBar/VSearchBar.vue
+++ b/src/components/VHeader/VSearchBar/VSearchBar.vue
@@ -32,7 +32,7 @@
:aria-activedescendant="
selectedIdx !== undefined ? `option-${selectedIdx}` : undefined
"
- @focus="handleFocus"
+ @focus="showRecentSearches"
@keydown="handleKeydown"
>
<!-- @slot Extra information such as loading message or result count goes here. -->
@@ -54,7 +54,7 @@
:class="recentClasses"
@select="handleSelect"
@clear="handleClear"
- @keydown.tab.native="handleBlur"
+ @keydown.tab.native="hideRecentSearches"
/>
</ClientOnly>
</div>
@@ -141,18 +141,6 @@ export default defineComponent({
emit('submit')
}
- /* Focus */
- const handleFocus = () => {
- isRecentVisible.value = true
- }
- const handleBlur = () => {
- isRecentVisible.value = false
- }
- const handleSearchBlur = () => {
- if (!entries.value.length) handleBlur()
- }
- onClickOutside(searchBarEl, handleBlur)
-
/* Recent searches */
const featureFlagStore = useFeatureFlagStore()
const isNewHeaderEnabled = featureFlagStore.isOn('new_header')
@@ -170,6 +158,24 @@ export default defineComponent({
} as const
return FIELD_OFFSETS[props.size]
})
+
+ /**
+ * Show and hide recent searches.
+ */
+ const showRecentSearches = () => {
+ isRecentVisible.value = true
+ }
+ const hideRecentSearches = () => {
+ isRecentVisible.value = false
+ }
+ /**
+ * Hide recent searches on blur and click outside.
+ */
+ const handleSearchBlur = () => {
+ if (!entries.value.length) hideRecentSearches()
+ }
+ onClickOutside(searchBarEl, hideRecentSearches)
+
/**
* 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
@@ -182,8 +188,7 @@ export default defineComponent({
event.preventDefault() // Prevent the cursor from moving horizontally.
const { key, altKey } = event
- // Show the recent searches.
- isRecentVisible.value = true
+ showRecentSearches()
if (altKey) return
// Shift selection (if Alt was not pressed with arrow keys)
@@ -211,9 +216,13 @@ export default defineComponent({
// If a recent search is selected, populate its value into the input.
modelMedium.value = entries.value[selectedIdx.value]
- if (([keycodes.Escape, keycodes.Enter] as string[]).includes(key))
- // Hide the recent searches.
- isRecentVisible.value = false
+ // Hide the recent searches popover when the user presses Enter, Escape or Shift+Tab on the input.
+ if (
+ (key === keycodes.Tab && event.shiftKey) ||
+ ([keycodes.Escape, keycodes.Enter] as string[]).includes(key)
+ ) {
+ hideRecentSearches()
+ }
selectedIdx.value = undefined // Lose visual focus from entries.
}
@@ -229,7 +238,7 @@ export default defineComponent({
const handleSelect = (idx: number) => {
modelMedium.value = entries.value[idx]
- isRecentVisible.value = false
+ hideRecentSearches()
selectedIdx.value = undefined // Lose visual focus from entries.
handleSearch() // Immediately execute the search manually.
}
@@ -247,8 +256,8 @@ export default defineComponent({
route,
modelMedium,
- handleFocus,
- handleBlur,
+ showRecentSearches,
+ hideRecentSearches,
handleSearchBlur,
isNewHeaderEnabled,