Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Refactor VSearchButton (#1961)
Olga Bulat6c397c35c67d
diff --git a/src/components/VFourOhFour.vue b/src/components/VFourOhFour.vueindex 79403ed02..ed70cf75d 100644--- a/src/components/VFourOhFour.vue+++ b/src/components/VFourOhFour.vue@@ -37,7 +37,7 @@</template></i18n></p>- <VStandaloneSearchBarOld route="404" @submit="handleSearch" />+ <VStandaloneSearchBar route="404" @submit="handleSearch" /></div></main>@@ -58,7 +58,7 @@ import { useFeatureFlagStore } from '~/stores/feature-flag'import { ALL_MEDIA, searchPath } from '~/constants/media'-import VStandaloneSearchBarOld from '~/components/VHeaderOld/VSearchBar/VStandaloneSearchBarOld.vue'+import VStandaloneSearchBar from '~/components/VHeader/VSearchBar/VStandaloneSearchBar.vue'import VLink from '~/components/VLink.vue'import VBrand from '~/components/VBrand/VBrand.vue'import VFooter from '~/components/VFooter/VFooter.vue'@@ -69,7 +69,7 @@ export default defineComponent({name: 'VFourOhFour',components: {VLink,- VStandaloneSearchBarOld,+ VStandaloneSearchBar,VBrand,VFooter,},@@ -82,13 +82,13 @@ export default defineComponent({const handleSearch = async (searchTerm) => {if (!searchTerm) return- searchStore.setSearchTerm(searchTerm.value)+ searchStore.setSearchTerm(searchTerm)searchStore.setSearchType(ALL_MEDIA)router.push(app.localePath({path: searchPath(ALL_MEDIA),- query: { q: searchTerm.value },+ query: { q: searchTerm },}))}@@ -113,20 +113,3 @@ export default defineComponent({},})</script>--<style>-/* Override the default search bar styles.- Maybe in the future this would warrant a- variant of the searchbar, but that seems- excessive for this one-off usage.-*/-.page-404 .search-bar > div:not(:focus):not(:focus-within) {- border-color: black;-}-.page-404- .search-bar:not(:hover)- button:not(:hover):not(:focus):not(:focus-within) {- border-color: black;- border-inline-start-color: transparent;-}-</style>diff --git a/src/components/VHeader/VSearchBar/VSearchBar.vue b/src/components/VHeader/VSearchBar/VSearchBar.vueindex 1b2ace5f1..196fd604a 100644--- a/src/components/VHeader/VSearchBar/VSearchBar.vue+++ b/src/components/VHeader/VSearchBar/VSearchBar.vue@@ -1,8 +1,7 @@<template><div ref="searchBarEl" class="relative"><form- class="search-bar group flex flex-row items-center rounded-sm border-tx bg-white"- :class="{ 'h-[57px] md:h-[69px]': size === 'standalone' }"+ class="search-bar group flex h-12 flex-row items-center rounded-sm border-tx bg-white"@submit.prevent="handleSearch"><VInputField@@ -10,12 +9,7 @@v-bind="$attrs"v-model="modelMedium":placeholder="placeholder || $t('hero.search.placeholder')"- class="search-field flex-grow focus:border-pink"- :class="[- route === 'home'- ? 'border-tx'- : 'border-tx bg-dark-charcoal-10 text-dark-charcoal-70 focus-within:bg-white group-hover:bg-dark-charcoal-10 group-hover:text-dark-charcoal group-hover:focus-within:bg-white',- ]"+ class="search-field flex-grow border-tx bg-dark-charcoal-10 text-dark-charcoal-70 focus-within:bg-white focus:border-pink group-hover:bg-dark-charcoal-10 group-hover:text-dark-charcoal group-hover:focus-within:bg-white":label-text="$t('search.search-bar-label', { openverse: 'Openverse' }).toString()"@@ -41,7 +35,7 @@<VSearchButtontype="submit":size="size"- :route="route"+ route="search"@keydown.tab="handleSearchBlur"/></form>@@ -70,7 +64,6 @@ import {import { onClickOutside } from '@vueuse/core'-import { useMatchHomeRoute } from '~/composables/use-match-routes'import { defineEvent } from '~/types/emits'import { useSearchStore } from '~/stores/search'@@ -87,6 +80,10 @@ import VSearchButton from '~/components/VHeader/VSearchBar/VSearchButton.vue'import VRecentSearches from '~/components/VRecentSearches/VRecentSearches.vue'/**+ * The search bar displayed on the search page.+ * TODO: remove the next line after new header is set as default+ * Only on the search page with `new_header` flag on.+ ** 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.@@ -111,10 +108,6 @@ export default defineComponent({type: String,required: false,},- is404: {- type: Boolean,- default: false,- },},emits: {input: defineEvent<[string]>(),@@ -124,12 +117,6 @@ export default defineComponent({const searchBarEl = ref<HTMLElement | null>(null)const inputFieldRef = ref<InstanceType<typeof VInputField> | null>(null)- const { matches: isHomeRoute } = useMatchHomeRoute()-- const route = computed(() => {- return isHomeRoute?.value ? 'home' : props.is404 ? '404' : undefined- })-const modelMedium = computed<string>({get: () => props.value ?? '',set: (value: string) => {@@ -253,7 +240,6 @@ export default defineComponent({inputFieldRef,handleSearch,- route,modelMedium,showRecentSearches,@@ -273,13 +259,3 @@ export default defineComponent({},})</script>--<style>-/* Removes the cross icon to clear the field */-.search-field input[type='search']::-webkit-search-decoration,-.search-field input[type='search']::-webkit-search-cancel-button,-.search-field input[type='search']::-webkit-search-results-button,-.search-field input[type='search']::-webkit-search-results-decoration {- -webkit-appearance: none;-}-</style>diff --git a/src/components/VHeader/VSearchBar/VSearchButton.vue b/src/components/VHeader/VSearchBar/VSearchButton.vueindex 2fb3264d2..a9673f4ca 100644--- a/src/components/VHeader/VSearchBar/VSearchButton.vue+++ b/src/components/VHeader/VSearchBar/VSearchButton.vue@@ -3,16 +3,18 @@v-bind="$attrs":aria-label="$t('search.search')"size="disabled"- :variant="isIcon ? 'plain' : 'primary'"- class="flex-shrink-0 text-2xl font-semibold transition-none rounded-s-none hover:bg-pink hover:text-white focus-visible:ring focus-visible:ring-pink"+ :variant="route === 'home' ? 'primary' : 'plain'"+ class="heading-6 h-full flex-shrink-0 transition-none rounded-s-none":class="[- isIcon- ? 'search-button ps-[1.5px] p-[0.5px] focus-visible:bg-pink focus-visible:text-white'- : 'h-full whitespace-nowrap py-6 px-10',- sizeClasses,route === 'home'- ? 'border-b border-tx border-b-pink bg-pink text-white group-hover:border-pink group-hover:bg-pink group-hover:text-white'- : 'border-tx bg-dark-charcoal-10 group-focus-within:bg-pink group-focus-within:text-white group-focus-within:hover:bg-dark-pink',+ ? 'w-[57px] whitespace-nowrap md:w-auto md:py-6 md:px-10'+ : 'search-button p-0.5px ps-1.5px hover:bg-pink hover:text-white focus-visible:bg-pink focus-visible:text-white group-focus-within:border-pink group-focus-within:bg-pink group-focus-within:text-white group-focus-within:hover:bg-dark-pink group-hover:border-pink group-hover:bg-pink group-hover:text-white',+ {+ 'w-10 bg-dark-charcoal-10 md:w-12': route === 'search',+ 'border-black focus:border-tx group-focus-within:border-tx group-hover:border-tx group-focus:border-tx':+ route === '404',+ },+ sizeClasses,]"v-on="$listeners">@@ -36,7 +38,11 @@ import VButton from '~/components/VButton.vue'import type { FieldSize } from '~/components/VInputField/VInputField.vue'import searchIcon from '~/assets/icons/search.svg'-+/**+ * The search button used in the search bar on the homepage and on the 404 page,+ * and on the search page.+ * TODO: remove when `new_header` is default: only on the search page with the `new_header`.+ */export default defineComponent({name: 'VSearchButton',components: { VIcon, VButton },@@ -47,8 +53,8 @@ export default defineComponent({required: true,},route: {- type: String as PropType<'home' | '404'>,- validator: (v: string) => ['home', '404'].includes(v),+ type: String as PropType<'home' | '404' | 'search'>,+ validator: (v: string) => ['home', '404', 'search'].includes(v),},},setup(props) {@@ -56,48 +62,32 @@ export default defineComponent({const isMinScreenMd = isMinScreen('md', { shouldPassInSSR: !isMobile })- const isIcon = computed(() => {- // split the evaluation of the isMinScreenMd ref- // out to prevent short-circuiting from creating- // problems with `computed`'s dependency detection- const currentIsMinScreenMd = isMinScreenMd.value+ /**+ * The search button has a text label on the homepage on screen larger than `md`,+ * everywhere else it has an icon.+ */+ const isIcon = computed(+ () => !(props.route === 'home' && isMinScreenMd.value)+ )- return (- props.route === '404' ||- props.size !== 'standalone' ||- (props.size === 'standalone' && !currentIsMinScreenMd)- )- })-- const sizeClasses = computed(() => {- return isIcon.value+ const sizeClasses = computed(() =>+ isIcon.value? {- small: 'w-10 md:w-12 h-10 md:h-12',- medium: 'w-12 h-12',- large: 'w-14 h-14',- standalone: 'w-[57px] md:w-[69px] h-[57px] md:h-[69px]',+ small: 'w-10 md:w-12',+ medium: 'w-12',+ large: 'w-14',+ standalone: 'w-[57px] md:w-[69px]',}[props.size]: undefined- })+ )- return { isMinScreenMd, searchIcon, sizeClasses, isIcon }+ return { searchIcon, sizeClasses, isIcon }},})</script><style scoped>.search-button {- /* Negative margin removes a tiny gap between the button and the input borders */- margin-inline-start: -1px;- border-inline-start-color: transparent;- border-width: 1px;-}-.search-button.search-button:not(:hover):not(:focus):not(:focus-within) {- border-inline-start-color: transparent;- border-width: 1px;-}-.search-button.search-button:hover {- border-inline-start-color: rgba(214, 212, 213, 1);- border-width: 1px;+ border-inline-start-width: 0;}</style>diff --git a/src/components/VHeaderOld/VSearchBar/VStandaloneSearchBarOld.vue b/src/components/VHeader/VSearchBar/VStandaloneSearchBar.vuesimilarity index 77%rename from src/components/VHeaderOld/VSearchBar/VStandaloneSearchBarOld.vuerename to src/components/VHeader/VSearchBar/VStandaloneSearchBar.vueindex ba564393b..774944f0d 100644--- a/src/components/VHeaderOld/VSearchBar/VStandaloneSearchBarOld.vue+++ b/src/components/VHeader/VSearchBar/VStandaloneSearchBar.vue@@ -4,8 +4,8 @@@submit.prevent="handleSearch"><div- class="input-field search-field group flex h-full flex-grow items-center overflow-hidden rounded-sm border p-0.5px pe-1.5px rounded-e-none border-e-0 focus-within:border-1.5 focus-within:border-pink focus-within:bg-dark-charcoal-06 focus-within:p-0 group-hover:bg-dark-charcoal-06"- :class="[isHomeRoute ? 'border-tx' : 'border-dark-charcoal-20']"+ class="input-field search-field group flex h-full flex-grow items-center overflow-hidden rounded-sm border p-0.5px pe-1.5px rounded-e-none border-e-0 focus-within:border-1.5 focus-within:border-pink focus-within:bg-dark-charcoal-06 focus-within:p-0 focus-within:pe-1.5px group-hover:bg-dark-charcoal-06"+ :class="[isHomeRoute ? 'border-tx' : 'border-black']"><inputid="search-bar"@@ -29,7 +29,7 @@<!-- @slot Extra information goes here --><slot /></div>- <VSearchButtonOld type="submit" size="standalone" :route="route" />+ <VSearchButton type="submit" size="standalone" :route="route" /></form></template>@@ -43,7 +43,7 @@ import {import { defineEvent } from '~/types/emits'-import VSearchButtonOld from '~/components/VHeaderOld/VSearchBar/VSearchButtonOld.vue'+import VSearchButton from '~/components/VHeader/VSearchBar/VSearchButton.vue'/*** Displays a search input for a search query and is attached to an action button@@ -53,8 +53,8 @@ import VSearchButtonOld from '~/components/VHeaderOld/VSearchBar/VSearchButtonOl* is not removed.*/export default defineComponent({- name: 'VStandaloneSearchBarOld',- components: { VSearchButtonOld },+ name: 'VStandaloneSearchBar',+ components: { VSearchButton },props: {route: {type: String as PropType<'home' | '404'>,@@ -62,14 +62,17 @@ export default defineComponent({},},emits: {- submit: defineEvent(),+ submit: defineEvent<[string]>(),},setup(props, { emit }) {const inputRef = ref<HTMLInputElement | null>(null)+ // Only emit `submit` if the input value is not blankconst handleSearch = () => {const searchTerm = inputRef.value?.value.trim()- emit('submit', searchTerm)+ if (searchTerm) {+ emit('submit', searchTerm)+ }}const isHomeRoute = computed(() => props.route === 'home')@@ -82,3 +85,8 @@ export default defineComponent({},})</script>+<style scoped>+.input-field {+ border-inline-end-width: 0;+}+</style>diff --git a/src/components/VHeaderOld/VSearchBar/VSearchBarOld.vue b/src/components/VHeaderOld/VSearchBar/VSearchBarOld.vueindex 771ea21c0..a674aae99 100644--- a/src/components/VHeaderOld/VSearchBar/VSearchBarOld.vue+++ b/src/components/VHeaderOld/VSearchBar/VSearchBarOld.vue@@ -21,11 +21,7 @@<!-- @slot Extra information such as loading message or result count goes here. --><slot /></VInputFieldOld>- <VSearchButtonOld- type="submit"- :size="size"- :route="is404 ? '404' : undefined"- />+ <VSearchButtonOld type="submit" :size="size" /></form></template>diff --git a/src/components/VHeaderOld/VSearchBar/VSearchButtonOld.vue b/src/components/VHeaderOld/VSearchBar/VSearchButtonOld.vueindex 6c9ce6a39..f0a7d59f0 100644--- a/src/components/VHeaderOld/VSearchBar/VSearchButtonOld.vue+++ b/src/components/VHeaderOld/VSearchBar/VSearchButtonOld.vue@@ -3,40 +3,27 @@v-bind="$attrs":aria-label="$t('search.search')"size="disabled"- :variant="isIcon ? 'plain' : 'primary'"- class="heading-6 flex-shrink-0 transition-none rounded-s-none hover:bg-pink hover:text-white focus-visible:ring focus-visible:ring-pink group-hover:border-pink group-hover:bg-pink group-hover:text-white"- :class="[- isIcon- ? 'search-button ps-[1.5px] p-[0.5px] focus-visible:bg-pink focus-visible:text-white'- : 'h-full whitespace-nowrap py-6 px-10',- sizeClasses,- route === 'home'- ? 'border-b border-tx border-b-pink bg-pink text-white'- : 'border-dark-charcoal-20',- ]"+ variant="plain"+ class="heading-6 search-button flex-shrink-0 border-dark-charcoal-20 p-0.5px transition-none ps-1.5px rounded-s-none hover:bg-pink hover:text-white focus-visible:bg-pink focus-visible:text-white group-focus-within:border-pink group-focus-within:bg-pink group-focus-within:text-white group-focus-within:hover:bg-dark-pink group-hover:border-pink group-hover:bg-pink group-hover:text-white"+ :class="sizeClasses"v-on="$listeners">- <template v-if="isIcon">- <VIcon :icon-path="searchIcon" />- </template>- <template v-else>- <span>{{ $t('search.search') }}</span>- </template>+ <VIcon :icon-path="searchIcon" /></VButton></template><script lang="ts">import { defineComponent, computed, PropType } from '@nuxtjs/composition-api'-import { isMinScreen } from '~/composables/use-media-query'-import { useBrowserIsMobile } from '~/composables/use-browser-detection'-import VIcon from '~/components/VIcon/VIcon.vue'import VButton from '~/components/VButton.vue'import type { FieldSize } from '~/components/VInputFieldOld/VInputFieldOld.vue'import searchIcon from '~/assets/icons/search.svg'+/**+ * The search button used in the search bar on the search page with the `old_header`.+ */export default defineComponent({name: 'VSearchButtonOld',components: { VIcon, VButton },@@ -46,49 +33,24 @@ export default defineComponent({type: String as PropType<FieldSize>,required: true,},- route: {- type: String as PropType<'home' | '404'>,- validator: (v: string) => ['home', '404'].includes(v),- },},setup(props) {- const isMobile = useBrowserIsMobile()-- const isMinScreenMd = isMinScreen('md', { shouldPassInSSR: !isMobile })-- const isIcon = computed(() => {- // split the evaluation of the isMinScreenMd ref- // out to prevent short-circuiting from creating- // problems with `computed`'s dependency detection- const currentIsMinScreenMd = isMinScreenMd.value-- return (- props.route === '404' ||- props.size !== 'standalone' ||- (props.size === 'standalone' && !currentIsMinScreenMd)- )- })-const sizeClasses = computed(() => {- return isIcon.value- ? {- small: 'w-10 md:w-12 h-10 md:h-12',- medium: 'w-12 h-12',- large: 'w-14 h-14',- standalone: 'w-[57px] md:w-[69px] h-full',- }[props.size]- : undefined+ return {+ small: 'w-10 md:w-12 h-10 md:h-12',+ medium: 'w-12 h-12',+ large: 'w-14 h-14',+ }[props.size]})- return { isMinScreenMd, searchIcon, sizeClasses, isIcon }+ return { searchIcon, sizeClasses }},})</script><style scoped>.search-button {- /* Negative margin removes a tiny gap between the button and the input borders */- margin-inline-start: -1px;- border-inline-start-color: transparent;+ /* Removes a tiny gap between the button and the input borders */+ border-inline-start: 0;}</style>diff --git a/src/pages/index.vue b/src/pages/index.vueindex 93721958d..fa8d08ea5 100644--- a/src/pages/index.vue+++ b/src/pages/index.vue@@ -33,7 +33,7 @@@select="setSearchType"/></div>- <VStandaloneSearchBarOld+ <VStandaloneSearchBarclass="mt-4 max-w-[40rem] md:mt-6"@submit="handleSearch">@@ -45,7 +45,7 @@placement="searchbar"@select="setSearchType"/>- </VStandaloneSearchBarOld>+ </VStandaloneSearchBar><!-- Disclaimer for large screens --><i18n@@ -143,7 +143,7 @@ import { useSearchStore } from '~/stores/search'import VLink from '~/components/VLink.vue'import VLogoButtonOld from '~/components/VHeaderOld/VLogoButtonOld.vue'-import VStandaloneSearchBarOld from '~/components/VHeaderOld/VSearchBar/VStandaloneSearchBarOld.vue'+import VStandaloneSearchBar from '~/components/VHeader/VSearchBar/VStandaloneSearchBar.vue'import VSearchTypeRadio from '~/components/VContentSwitcher/VSearchTypeRadio.vue'import VSearchTypePopoverOld from '~/components/VContentSwitcherOld/VSearchTypePopoverOld.vue'import VBrand from '~/components/VBrand/VBrand.vue'@@ -156,7 +156,7 @@ export default defineComponent({VBrand,VSearchTypePopoverOld,VSearchTypeRadio,- VStandaloneSearchBarOld,+ VStandaloneSearchBar,VLink,VLogoButtonOld,},