Skip to content

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

Fix search bar button crashing index page (#852)

sarayourfriend6c938c1c6bb7
diff --git a/package.json b/package.json
index 3d7b9a2eb..2a8c96160 100644
--- a/package.json
+++ b/package.json
@@ -91,6 +91,7 @@
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/user-event": "^13.2.1",
"@testing-library/vue": "^5.8.2",
+ "@types/express-useragent": "^1.0.2",
"@types/jest": "^26.0.22",
"@types/uuid": "^8.3.4",
"@vue/test-utils": "^1.1.3",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 2fbbb378e..1a56bda1e 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -22,6 +22,7 @@ specifiers:
'@testing-library/jest-dom': ^5.16.1
'@testing-library/user-event': ^13.2.1
'@testing-library/vue': ^5.8.2
+ '@types/express-useragent': ^1.0.2
'@types/jest': ^26.0.22
'@types/uuid': ^8.3.4
'@vue/test-utils': ^1.1.3
@@ -135,6 +136,7 @@ devDependencies:
'@testing-library/jest-dom': 5.16.1
'@testing-library/user-event': 13.5.0
'@testing-library/vue': 5.8.2
+ '@types/express-useragent': 1.0.2
'@types/jest': 26.0.24
'@types/uuid': 8.3.4
'@vue/test-utils': 1.3.0
@@ -5475,7 +5477,7 @@ packages:
resolution: {integrity: sha512-ALYone6pm6QmwZoAgeyNksccT9Q4AWZQ6PvfwR37GT6r6FWUPguq6sUmNGSMV2Wr761oQoBxwGGa6DR5o1DC9g==}
dependencies:
'@types/connect': 3.4.34
- '@types/node': 12.20.12
+ '@types/node': 16.11.13
dev: true
/@types/braces/3.0.1:
@@ -5538,11 +5540,17 @@ packages:
/@types/express-serve-static-core/4.17.26:
resolution: {integrity: sha512-zeu3tpouA043RHxW0gzRxwCHchMgftE8GArRsvYT0ByDMbn19olQHx5jLue0LxWY6iYtXb7rXmuVtSkhy9YZvQ==}
dependencies:
- '@types/node': 12.20.12
+ '@types/node': 16.11.13
'@types/qs': 6.9.7
'@types/range-parser': 1.2.4
dev: true
+ /@types/express-useragent/1.0.2:
+ resolution: {integrity: sha512-eUVCqMsmEO7adMJSxuAARPUxbEJLYQJATiB86bx3MGeyUOTgKNnLTfAMaF+z84DftcH6NBbFFwiRomIcsFVdUQ==}
+ dependencies:
+ '@types/express': 4.17.13
+ dev: true
+
/@types/express/4.17.13:
resolution: {integrity: sha512-6bSZTPaTIACxn48l50SR+axgrqm6qXFIxrdAKaG6PaJk3+zuUr35hBlgT7vOmJcum+OEaIBLtHV/qloEAFITeA==}
dependencies:
@@ -5566,7 +5574,7 @@ packages:
resolution: {integrity: sha512-ZUxbzKl0IfJILTS6t7ip5fQQM/J3TJYubDm3nMbgubNNYS62eXeUpoLUC8/7fJNiFYHTrGPQn7hspDUzIHX3UA==}
dependencies:
'@types/minimatch': 3.0.5
- '@types/node': 14.18.0
+ '@types/node': 16.11.13
dev: true
/@types/graceful-fs/4.1.5:
@@ -5655,7 +5663,7 @@ packages:
/@types/node-fetch/2.5.12:
resolution: {integrity: sha512-MKgC4dlq4kKNa/mYrwpKfzQMB5X3ee5U6fSprkKpToBqBmX4nFZL9cW5jl6sWn+xpRJ7ypWh2yyqqr8UUCstSw==}
dependencies:
- '@types/node': 14.18.0
+ '@types/node': 16.11.13
form-data: 3.0.1
dev: true
@@ -5773,7 +5781,7 @@ packages:
/@types/sax/1.2.3:
resolution: {integrity: sha512-+QSw6Tqvs/KQpZX8DvIl3hZSjNFLW/OqE5nlyHXtTwODaJvioN2rOWpBNEWZp2HZUFhOh+VohmJku/WxEXU2XA==}
dependencies:
- '@types/node': 12.20.37
+ '@types/node': 16.11.13
dev: false
/@types/scheduler/0.16.2:
diff --git a/src/components/VHeader/VSearchBar/VSearchButton.vue b/src/components/VHeader/VSearchBar/VSearchButton.vue
index 6deb53fc2..41623709f 100644
--- a/src/components/VHeader/VSearchBar/VSearchButton.vue
+++ b/src/components/VHeader/VSearchBar/VSearchButton.vue
@@ -1,45 +1,55 @@
<template>
- <div>
- <VButton
- v-if="isHomeRoute && isMinScreenMd"
- v-bind="$attrs"
- :aria-label="$t('search.search')"
- class="inline-block cta-search-button font-semibold h-full text-2xl"
- v-on="$listeners"
- >{{ $t('search.search') }}</VButton
- >
- <VIconButton
- v-else
- v-bind="$attrs"
- :size="`search-${size}`"
- class="search-button hover:text-white group-hover:text-white hover:bg-pink group-hover:bg-pink p-0.5px ps-1.5px focus:p-0 border hover:border-pink group-hover:border-pink rounded-e-sm active:shadow-ring"
- :class="[
- isHomeRoute
- ? 'bg-pink text-white border-tx'
- : 'border-dark-charcoal-20',
- ]"
- :icon-props="{ iconPath: searchIcon }"
- :aria-label="$t('search.search')"
- v-on="$listeners"
- />
- </div>
+ <VButton
+ v-bind="$attrs"
+ :aria-label="$t('search.search')"
+ size="disabled"
+ :variant="isIcon ? 'plain' : 'primary'"
+ class="transition-none inline-block rounded-s-none font-semibold text-2xl hover:text-white group-hover:text-white hover:bg-pink group-hover:bg-pink"
+ :class="[
+ isIcon ? 'search-button p-[0.5px] ps-[1.5px]' : 'py-6 px-10 h-full',
+ sizeClasses,
+ isHomeRoute
+ ? 'bg-pink border-b border-b-pink text-white border-tx'
+ : 'border-dark-charcoal-20',
+ ]"
+ v-on="$listeners"
+ >
+ <template v-if="isIcon">
+ <VIcon :icon-path="searchIcon" />
+ </template>
+ <template v-else>
+ <span>{{ $t('search.search') }}</span>
+ </template>
+ </VButton>
</template>
<script>
+import { defineComponent, computed } from '@nuxtjs/composition-api'
import { isMinScreen } from '~/composables/use-media-query'
+import { useBrowserIsMobile } from '~/composables/use-browser-detection'
-import VIconButton from '~/components/VIconButton/VIconButton.vue'
+import VButton from '~/components/VButton.vue'
+import VIcon from '~/components/VIcon/VIcon.vue'
import searchIcon from '~/assets/icons/search.svg'
-export default {
+/**
+ * @typedef Props
+ * @property {'small' | 'medium' | 'large' | 'standalone'} size
+ * @property {boolean} isHomeRoute
+ */
+
+export default defineComponent({
name: 'VSearchButton',
- components: { VIconButton },
+ components: { VIcon, VButton },
inheritAttrs: false,
props: {
size: {
type: String,
required: true,
+ /**
+ * @param {string} v
+ */
validator: (v) => ['small', 'medium', 'large', 'standalone'].includes(v),
},
isHomeRoute: {
@@ -47,20 +57,41 @@ export default {
default: false,
},
},
- setup() {
- const isMinScreenMd = isMinScreen('md', { shouldPassInSSR: true })
+ /**
+ * @param {Props} props
+ */
+ 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.size !== 'standalone' ||
+ (props.size === 'standalone' && !currentIsMinScreenMd)
+ )
+ })
+
+ const sizeClasses = computed(() => {
+ return isIcon
+ ? {
+ small: ['w-10', 'md:w-12', 'h-10', 'md:h-12'],
+ medium: ['w-12', 'h-12'],
+ large: ['w-14', 'h-14'],
+ standalone: ['w-14', 'md:w-[69px]', 'h-14', 'md:h-[69px]'],
+ }[props.size]
+ : undefined
+ })
- return { isMinScreenMd, searchIcon }
+ return { isMinScreenMd, searchIcon, sizeClasses, isIcon }
},
-}
+})
</script>
<style scoped>
-.cta-search-button {
- @apply py-6 px-10 rounded-s-none border-b border-b-pink;
-}
-
-/* @todo: Find a better way to override the VIconButton border styles */
.search-button {
/* Negative margin removes a tiny gap between the button and the input borders */
margin-inline-start: -1px;
diff --git a/src/components/VTeleport/VTeleportTarget.vue b/src/components/VTeleport/VTeleportTarget.vue
index 6b17f589c..978c52b37 100644
--- a/src/components/VTeleport/VTeleportTarget.vue
+++ b/src/components/VTeleport/VTeleportTarget.vue
@@ -8,6 +8,7 @@ export default defineComponent({
props: {
name: { type: String, required: true },
element: { type: String, default: 'div' },
+ forceDestroy: { type: Boolean, default: false },
},
data: () => ({ children: [] }),
created() {
@@ -20,11 +21,20 @@ export default defineComponent({
targets[this.name] = this
},
beforeDestroy() {
+ if (this.children.length > 0) {
+ if (this.forceDestroy) {
+ this.children.forEach((child) => {
+ child.$destroy()
+ child.$el.remove()
+ })
+ this.children = []
+ } else {
+ throw new Error(
+ `VTeleportTarget: ${this.name} beforeDestroy but still has children mounted`
+ )
+ }
+ }
delete targets[this.name]
- if (this.children.length > 0)
- throw new Error(
- `VTeleportTarget: ${this.name} beforeDestroy but still has children mounted`
- )
},
render(h) {
return h(
diff --git a/src/composables/use-browser-detection.js b/src/composables/use-browser-detection.js
index b6100a4fa..772bcba0e 100644
--- a/src/composables/use-browser-detection.js
+++ b/src/composables/use-browser-detection.js
@@ -9,3 +9,7 @@ export const useBrowserIsBlink = () => {
const browser = useBrowserDetection()
return browser.isChrome || browser.isEdge || browser.isOpera
}
+
+export const useBrowserIsMobile = () => {
+ return useBrowserDetection().isMobile
+}
diff --git a/src/composables/use-media-query.js b/src/composables/use-media-query.js
index 99ea42c8c..8afba42bc 100644
--- a/src/composables/use-media-query.js
+++ b/src/composables/use-media-query.js
@@ -1,26 +1,31 @@
/* this implementation is from https://github.com/vueuse/vueuse/packages/core/useMediaQuery/
which, in turn, is ported from https://github.com/logaretm/vue-use-web by Abdelrahman Awad */
import { onBeforeUnmount, ref } from '@nuxtjs/composition-api'
-import { SCREEN_SIZES } from '~/constants/screens.js'
+import { SCREEN_SIZES } from '~/constants/screens'
import { defaultWindow } from '~/composables/window'
+/**
+ * @typedef Options
+ * @property {boolean} [shouldPassInSSR]
+ * @property {Window} [window]
+ */
+
/**
* Reactive Media Query.
*
* @param {string} query
- * @param options
+ * @param {Options} [options]
*/
export function useMediaQuery(query, options = { shouldPassInSSR: false }) {
/** @type {import('@nuxtjs/composition-api').Ref<boolean>} */
const matches = ref(false)
const { window = defaultWindow } = options
if (!window) {
- matches.value = options.shouldPassInSSR
+ matches.value = Boolean(options.shouldPassInSSR)
return matches
}
const mediaQuery = window.matchMedia(query)
- /** @type {import('@nuxtjs/composition-api').Ref<boolean>} */
matches.value = mediaQuery.matches
const handler = (/** @type MediaQueryListEvent */ event) => {
@@ -49,10 +54,10 @@ export function useMediaQuery(query, options = { shouldPassInSSR: false }) {
* Check whether the current screen meets
* or exceeds the provided breakpoint size.
* @param {'sm'|'md'|'lg'|'xl'|'2xl'} breakpointName
- * @param {Parameters<typeof useMediaQuery>[1]} options
+ * @param {Options} [options]
* @returns {import('@nuxtjs/composition-api').Ref<boolean>}
*/
-export const isMinScreen = (breakpointName, options = {}) => {
+export const isMinScreen = (breakpointName, options) => {
return useMediaQuery(
`(min-width: ${SCREEN_SIZES.get(breakpointName)}px)`,
options
@@ -61,8 +66,9 @@ export const isMinScreen = (breakpointName, options = {}) => {
/**
* Check if the user prefers reduced motion or not.
+ * @param {Options} options
* @returns {import('@nuxtjs/composition-api').Ref<boolean>}
*/
-export function useReducedMotion(options = {}) {
+export function useReducedMotion(options) {
return useMediaQuery('(prefers-reduced-motion: reduce)', options)
}
diff --git a/src/constants/screens.js b/src/constants/screens.js
index 55653ca2c..79ef5d7e9 100644
--- a/src/constants/screens.js
+++ b/src/constants/screens.js
@@ -1,6 +1,8 @@
+/** @typedef {'2xl' | 'xl' | 'lg' | 'md' | 'sm' | 'xs' | 'mob'} Breakpoints */
+
/**
* Mapping of a breakpoint name to the lower-bound of its screen-width range
- * @type {Map<string, number>}
+ * @type {Map<Exclude<Breakpoints, 'mob'>, number>}
*/
const SCREEN_SIZES = new Map([
['2xl', 1536],
@@ -26,7 +28,7 @@ const VIEWPORTS = Object.fromEntries(
/**
* Get the breakpoint in which the screen with the given width lies.
* @param {number} screenWidth - the width of the screen
- * @returns {string} the breakpoint in which the screen lies
+ * @returns {Breakpoints} the breakpoint in which the screen lies
*/
const getBreakpointName = (screenWidth) => {
for (const [breakpointName, lowerLimit] of SCREEN_SIZES) {
@@ -37,6 +39,9 @@ const getBreakpointName = (screenWidth) => {
return 'mob' // smallest breakpoint
}
+/**
+ * This module is consumed by Nuxt and Tailwind config so it needs to use CJS modules
+ */
module.exports = {
SCREEN_SIZES,
VIEWPORTS,
diff --git a/src/layouts/default.vue b/src/layouts/default.vue
index 1e27eda87..634a9cf4b 100644
--- a/src/layouts/default.vue
+++ b/src/layouts/default.vue
@@ -1,7 +1,7 @@
<template>
<div class="app grid h-screen overflow-hidden relative">
<div>
- <VTeleportTarget name="skip-to-content" />
+ <VTeleportTarget name="skip-to-content" :force-destroy="true" />
<MigrationNotice v-show="isReferredFromCc" />
<VTranslationStatusBanner />
<VHeader />
diff --git a/test/unit/specs/components/VHeader/SearchBar/search-bar.spec.js b/test/unit/specs/components/VHeader/SearchBar/search-bar.spec.js
index 4434647cc..4ee55f414 100644
--- a/test/unit/specs/components/VHeader/SearchBar/search-bar.spec.js
+++ b/test/unit/specs/components/VHeader/SearchBar/search-bar.spec.js
@@ -14,6 +14,13 @@ describe('SearchBar', () => {
beforeEach(() => {
options = {
props: { placeholder: defaultPlaceholder, size: 'standalone' },
+ mocks: {
+ $nuxt: {
+ context: {
+ app: { $ua: {} },
+ },
+ },
+ },
}
})
diff --git a/tsconfig.json b/tsconfig.json
index 1669feb71..33381b6ae 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -36,6 +36,10 @@
"src/composables/use-event-listener-outside.js",
"src/composables/use-active-audio.js",
"src/composables/use-get-locale-formatted-number.js",
+ "src/composables/use-browser-detection.js",
+ "src/composables/use-media-query.js",
+ "src/composables/window.js",
+ "src/constants/screens.js",
"src/data/api-service.js",
"src/data/usage-data-service.js",
"src/store/user.js",
diff --git a/typings/nuxt__types/index.d.ts b/typings/nuxt__types/index.d.ts
index 96d701fa9..c00dc0d0a 100644
--- a/typings/nuxt__types/index.d.ts
+++ b/typings/nuxt__types/index.d.ts
@@ -1,7 +1,11 @@
import '@nuxt/types'
import type { IVueI18n } from 'vue-i18n'
+import type { Details as UADetails } from 'express-useragent'
declare module '@nuxt/types' {
+ export interface NuxtAppOptions {
+ $ua: UADetails
+ }
export interface Context {
i18n: IVueI18n
}
diff --git a/typings/openverse/index.d.ts b/typings/openverse/index.d.ts
new file mode 100644
index 000000000..317b45b47
--- /dev/null
+++ b/typings/openverse/index.d.ts
@@ -0,0 +1,9 @@
+declare module '*.svg' {
+ const SVG: string
+ export default SVG
+}
+
+declare module '*.svg!inline' {
+ const SVG: string
+ export default SVG
+}