Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Prevent clearing of the search bar value when homepage is hydrated (#…
Browse files Browse the repository at this point in the history
…1934)

* Add uncontrolled VStandaloneSearchBarOld component

* Add snapshots
  • Loading branch information
obulat authored Oct 25, 2022
1 parent 45354df commit 976ee8e
Show file tree
Hide file tree
Showing 57 changed files with 154 additions and 104 deletions.
2 changes: 2 additions & 0 deletions src/assets/oops.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 22 additions & 42 deletions src/components/VFourOhFour.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
<div
class="error relative flex min-h-screen flex-col overflow-x-hidden bg-yellow"
>
<Oops
<svg
class="pointer-events-none absolute top-20 z-0 -mt-[10%] -ml-[20%] w-[140%] fill-dark-charcoal px-6 opacity-5 lg:mx-auto lg:ml-0 lg:w-full lg:px-16"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 1320 569"
aria-hidden="true"
class="pointer-events-none absolute top-20 z-0 -mt-[10%] -ml-[20%] w-[140%] fill-dark-charcoal px-6 opacity-5 lg:mx-auto lg:w-full lg:px-16"
/>

focusable="false"
>
<use :href="`${Oops}#oops`" />
</svg>
<div>
<VLink href="/" class="relative z-10 text-dark-charcoal">
<VBrand class="m-6 text-[18px] lg:mx-10 lg:my-8" />
Expand All @@ -19,10 +23,10 @@
<!-- Push content by 1/4th height without absolute positioning. -->
<div class="spacer grow" />
<div class="z-10 grow-[3] space-y-4 lg:space-y-6">
<h1 class="mb-6 text-3xl lg:mb-10 lg:text-6xl lg:leading-tight">
<h1 class="heading-5 lg:heading-2 mb-6 lg:mb-10 lg:leading-tight">
{{ $t('404.title') }}
</h1>
<p class="font-semibold">
<p class="label-bold lg:heading-6">
<i18n path="404.main">
<template #link>
<VLink
Expand All @@ -33,16 +37,7 @@
</template>
</i18n>
</p>
<VSearchBarOld
:value="searchTerm"
:label-text="$t('404.search-placeholder')"
field-id="404-search"
:placeholder="$t('404.search-placeholder').toString()"
:is404="true"
size="standalone"
@input="setSearchTerm"
@submit="handleSearch"
/>
<VStandaloneSearchBarOld route="404" @submit="handleSearch" />
</div>
</main>

Expand All @@ -55,55 +50,44 @@
</template>

<script>
import {
defineComponent,
ref,
useContext,
useRouter,
} from '@nuxtjs/composition-api'
import { defineComponent, useContext, useRouter } from '@nuxtjs/composition-api'
import { useMediaStore } from '~/stores/media'
import { useSearchStore } from '~/stores/search'
import { useFeatureFlagStore } from '~/stores/feature-flag'
import VSearchBarOld from '~/components/VHeaderOld/VSearchBar/VSearchBarOld.vue'
import { ALL_MEDIA, searchPath } from '~/constants/media'
import VStandaloneSearchBarOld from '~/components/VHeaderOld/VSearchBar/VStandaloneSearchBarOld.vue'
import VLink from '~/components/VLink.vue'
import VBrand from '~/components/VBrand/VBrand.vue'
import VFooter from '~/components/VFooter/VFooter.vue'
import Oops from '~/assets/oops.svg?inline'
import Oops from '~/assets/oops.svg'
export default defineComponent({
name: 'VFourOhFour',
components: {
Oops,
VLink,
VSearchBarOld,
VStandaloneSearchBarOld,
VBrand,
VFooter,
},
props: ['error'],
setup() {
const mediaStore = useMediaStore()
const searchStore = useSearchStore()
const { app } = useContext()
const router = useRouter()
const searchTerm = ref('')
const setSearchTerm = (value) => {
searchTerm.value = value
}
const handleSearch = async () => {
if (searchTerm.value === '') return
const handleSearch = async (searchTerm) => {
if (!searchTerm) return
searchStore.setSearchTerm(searchTerm.value)
await mediaStore.fetchMedia()
searchStore.setSearchType(ALL_MEDIA)
router.push(
app.localePath({
path: `/search`,
path: searchPath(ALL_MEDIA),
query: { q: searchTerm.value },
})
)
Expand All @@ -113,10 +97,9 @@ export default defineComponent({
const isNewHeaderEnabled = featureFlagStore.isOn('new_header')
return {
searchTerm,
setSearchTerm,
handleSearch,
isNewHeaderEnabled,
Oops,
}
},
head: {
Expand Down Expand Up @@ -146,7 +129,4 @@ export default defineComponent({
border-color: black;
border-inline-start-color: transparent;
}
.page-404 .search-bar button {
border-width: 1px;
}
</style>
28 changes: 6 additions & 22 deletions src/components/VHeaderOld/VSearchBar/VSearchBarOld.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<template>
<form
class="search-bar group flex flex-row items-center rounded-sm border-tx bg-white"
:class="{ 'h-[57px] md:h-[69px]': size === 'standalone' }"
@submit.prevent="handleSearch"
>
<VInputFieldOld
:placeholder="placeholder || $t('hero.search.placeholder')"
v-bind="$attrs"
class="search-field flex-grow focus:border-pink"
:class="[route === 'home' ? 'border-tx' : 'border-dark-charcoal-20']"
class="search-field flex-grow border-dark-charcoal-20 focus:border-pink"
:label-text="
$t('search.search-bar-label', { openverse: 'Openverse' }).toString()
"
Expand All @@ -23,14 +21,17 @@
<!-- @slot Extra information such as loading message or result count goes here. -->
<slot />
</VInputFieldOld>
<VSearchButtonOld type="submit" :size="size" :route="route" />
<VSearchButtonOld
type="submit"
:size="size"
:route="is404 ? '404' : undefined"
/>
</form>
</template>

<script lang="ts">
import { computed, defineComponent, PropType } from '@nuxtjs/composition-api'
import { useMatchHomeRoute } from '~/composables/use-match-routes'
import { defineEvent } from '~/types/emits'
import VInputFieldOld, {
Expand Down Expand Up @@ -73,12 +74,6 @@ export default defineComponent({
submit: defineEvent(),
},
setup(props, { emit }) {
const { matches: isHomeRoute } = useMatchHomeRoute()
const route = computed(() => {
return isHomeRoute?.value ? 'home' : props.is404 ? '404' : undefined
})
const searchText = computed(() => props.value)
const updateSearchText = (val: string) => {
Expand All @@ -91,20 +86,9 @@ export default defineComponent({
return {
handleSearch,
route,
searchText,
updateSearchText,
}
},
})
</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>
13 changes: 2 additions & 11 deletions src/components/VHeaderOld/VSearchBar/VSearchButtonOld.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
: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 group-hover:border-pink group-hover:bg-pink group-hover:text-white"
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'
Expand Down Expand Up @@ -75,7 +75,7 @@ export default defineComponent({
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]',
standalone: 'w-[57px] md:w-[69px] h-full',
}[props.size]
: undefined
})
Expand All @@ -90,14 +90,5 @@ export default defineComponent({
/* 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;
}
</style>
84 changes: 84 additions & 0 deletions src/components/VHeaderOld/VSearchBar/VStandaloneSearchBarOld.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<template>
<form
class="search-bar group flex h-[57px] flex-row items-center rounded-sm border-tx bg-white md:h-[69px]"
@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']"
>
<input
id="search-bar"
ref="inputRef"
type="search"
name="q"
:placeholder="
$t(
isHomeRoute ? 'hero.search.placeholder' : '404.search-placeholder'
).toString()
"
class="h-full w-full appearance-none rounded-none bg-tx text-base leading-none text-dark-charcoal placeholder-dark-charcoal-70 ms-4 focus:outline-none md:text-2xl"
:aria-label="
isHomeRoute
? $t('search.search-bar-label', {
openverse: 'Openverse',
}).toString()
: $t('404.search-placeholder').toString()
"
/>
<!-- @slot Extra information goes here -->
<slot />
</div>
<VSearchButtonOld type="submit" size="standalone" :route="route" />
</form>
</template>

<script lang="ts">
import {
computed,
defineComponent,
PropType,
ref,
} from '@nuxtjs/composition-api'
import { defineEvent } from '~/types/emits'
import VSearchButtonOld from '~/components/VHeaderOld/VSearchBar/VSearchButtonOld.vue'
/**
* Displays a search input for a search query and is attached to an action button
* that fires a search request. Can contain other elements like the search type
* popover. Is uncontrolled: Vue code does not try to set a default value when
* hydrating the server-rendered code, so the value entered before full hydration
* is not removed.
*/
export default defineComponent({
name: 'VStandaloneSearchBarOld',
components: { VSearchButtonOld },
props: {
route: {
type: String as PropType<'home' | '404'>,
default: 'home',
},
},
emits: {
submit: defineEvent(),
},
setup(props, { emit }) {
const inputRef = ref<HTMLInputElement | null>(null)
const handleSearch = () => {
const searchTerm = inputRef.value?.value.trim()
emit('submit', searchTerm)
}
const isHomeRoute = computed(() => props.route === 'home')
return {
inputRef,
handleSearch,
isHomeRoute,
}
},
})
</script>
Loading

0 comments on commit 976ee8e

Please sign in to comment.