Skip to content

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

Add Content Switcher component (#607)

Olga Bulatbe9dcf6056a5
diff --git a/package.json b/package.json
index 72e3b0f95..01bf1a477 100644
--- a/package.json
+++ b/package.json
@@ -64,6 +64,7 @@
"lodash.debounce": "^4.0.8",
"lodash.findindex": "^4.6.0",
"lodash.isempty": "^4.4.0",
+ "lodash.isequal": "^4.5.0",
"lodash.sortby": "^4.7.0",
"lodash.throttle": "^4.1.1",
"node-html-parser": "^5.1.0",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 6275e6cfe..2c88aca52 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -53,6 +53,7 @@ specifiers:
lodash.debounce: ^4.0.8
lodash.findindex: ^4.6.0
lodash.isempty: ^4.4.0
+ lodash.isequal: ^4.5.0
lodash.sortby: ^4.7.0
lodash.throttle: ^4.1.1
node-html-parser: ^5.1.0
@@ -99,6 +100,7 @@ dependencies:
lodash.debounce: 4.0.8
lodash.findindex: 4.6.0
lodash.isempty: 4.4.0
+ lodash.isequal: 4.5.0
lodash.sortby: 4.7.0
lodash.throttle: 4.1.1
node-html-parser: 5.1.0
@@ -12346,6 +12348,10 @@ packages:
resolution: {integrity: sha1-b4bL7di+TsmHvpqvM8loTbGzHn4=}
dev: false
+ /lodash.isequal/4.5.0:
+ resolution: {integrity: sha1-QVxEePK8wwEgwizhDtMib30+GOA=}
+ dev: false
+
/lodash.kebabcase/4.1.1:
resolution: {integrity: sha1-hImxyw0p/4gZXM7KRI/21swpXDY=}
dev: false
diff --git a/src/assets/icons/checkmark.svg b/src/assets/icons/checkmark.svg
index 04691f386..3a009af11 100644
--- a/src/assets/icons/checkmark.svg
+++ b/src/assets/icons/checkmark.svg
@@ -1,3 +1,5 @@
-<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
- <path d="M16.7 7.1l-6.3 8.5-3.3-2.5-.9 1.2 4.5 3.4L17.9 8z" fill="currentColor"/>
+<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
+ <g id="icon">
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M17.9293 7.97825L10.731 17.6591L6.2147 14.3009L7.10972 13.0972L10.4223 15.5603L16.7255 7.08322L17.9293 7.97825Z" fill="currentColor"/>
+ </g>
</svg>
diff --git a/src/components/VButton.vue b/src/components/VButton.vue
index f13b16178..f00a41d67 100644
--- a/src/components/VButton.vue
+++ b/src/components/VButton.vue
@@ -6,7 +6,7 @@
:class="[
$style.button,
$style[variant],
- pressed && $style[`${variant}-pressed`],
+ isActive && $style[`${variant}-pressed`],
$style[`size-${size}`],
]"
:aria-pressed="pressed"
@@ -23,7 +23,13 @@
</template>
<script>
-import { defineComponent, ref, watch, toRefs } from '@nuxtjs/composition-api'
+import {
+ defineComponent,
+ ref,
+ watch,
+ toRefs,
+ computed,
+} from '@nuxtjs/composition-api'
import { warn } from '~/utils/warn'
/**
@@ -57,7 +63,7 @@ const VButton = defineComponent({
as: {
type: String,
default: 'button',
- validate: (v) => ['a', 'button'].includes(v),
+ validate: (v) => ['a', 'button', 'NuxtLink'].includes(v),
},
/**
* The variant of the button.
@@ -143,6 +149,13 @@ const VButton = defineComponent({
const typeRef = ref(propsRef.type.value)
const supportsDisabledAttributeRef = ref(true)
+ const isActive = computed(() => {
+ return (
+ propsRef.pressed.value ||
+ attrs['aria-pressed'] ||
+ attrs['aria-expanded']
+ )
+ })
watch(
[propsRef.disabled, propsRef.focusableWhenDisabled],
([disabled, focusableWhenDisabled]) => {
@@ -162,18 +175,23 @@ const VButton = defineComponent({
watch(
propsRef.as,
(as) => {
- if (as === 'a') {
+ if (['a', 'NuxtLink'].includes(as)) {
typeRef.value = undefined
supportsDisabledAttributeRef.value = false
-
- // No need to decalare `href` as an explicit prop as Vue preserves
- // the `attrs` object reference between renders and updates the properties
- // meaning we'll always have the latest values for the properties on the
- // attrs object
- if (!attrs.href || attrs.href === '#') {
- warn(
- 'Do not use anchor elements without a valid `href` attribute. Use a `button` instead.'
- )
+ if (as === 'a') {
+ // No need to declare `href` as an explicit prop as Vue preserves
+ // the `attrs` object reference between renders and updates the properties
+ // meaning we'll always have the latest values for the properties on the
+ // attrs object
+ if (!attrs.href || attrs.href === '#') {
+ warn(
+ 'Do not use anchor elements without a valid `href` attribute. Use a `button` instead.'
+ )
+ }
+ } else {
+ if (!attrs.to) {
+ warn('NuxtLink needs a `to` attribute')
+ }
}
}
},
@@ -193,6 +211,7 @@ const VButton = defineComponent({
disabledAttributeRef,
ariaDisabledRef,
typeRef,
+ isActive,
}
},
})
@@ -239,11 +258,11 @@ a.button {
}
.secondary-pressed {
- @apply bg-dark-charcoal-80;
+ @apply bg-dark-charcoal-80 hover:border-tx;
}
.tertiary {
- @apply bg-white text-black hover:bg-dark-charcoal hover:text-white border border-dark-charcoal-20 hover:border-tx focus-visible:ring-pink ring-offset-0;
+ @apply bg-white text-black border border-dark-charcoal-20 focus-visible:border-tx focus-visible:ring-pink ring-offset-0;
}
.tertiary-pressed {
@@ -251,7 +270,7 @@ a.button {
}
.action-menu {
- @apply bg-white text-black border border-dark-charcoal-20 hover:border-dark-charcoal-20 focus-visible:ring-pink;
+ @apply bg-white text-black border border-tx hover:border-dark-charcoal-20 focus-visible:ring-pink;
}
.action-menu-secondary {
@@ -263,7 +282,7 @@ a.button {
}
.action-menu-pressed {
- @apply border-tx bg-dark-charcoal text-white;
+ @apply border-tx hover:border-tx bg-dark-charcoal text-white;
}
.action-menu-muted {
diff --git a/src/components/VContentLink/VContentLink.vue b/src/components/VContentLink/VContentLink.vue
index b96e6320a..e7504f12f 100644
--- a/src/components/VContentLink/VContentLink.vue
+++ b/src/components/VContentLink/VContentLink.vue
@@ -31,7 +31,11 @@
<script>
import { computed, defineComponent } from '@nuxtjs/composition-api'
import { resultsCount } from '~/composables/use-i18n-utilities'
-import { AUDIO, IMAGE, mediaTypes } from '~/constants/media'
+import {
+ AUDIO,
+ IMAGE,
+ supportedMediaTypes as mediaTypes,
+} from '~/constants/media'
import VIcon from '~/components/VIcon/VIcon.vue'
import audioIcon from '~/assets/icons/audio-wave.svg'
diff --git a/src/components/VContentSwitcher/VContentSwitcherButton.vue b/src/components/VContentSwitcher/VContentSwitcherButton.vue
new file mode 100644
index 000000000..09042091e
--- /dev/null
+++ b/src/components/VContentSwitcher/VContentSwitcherButton.vue
@@ -0,0 +1,85 @@
+<template>
+ <VButton
+ class="flex flex-row font-semibold px-3 py-2 text-sr md:text-base"
+ :class="{ 'w-12': isIconButton }"
+ :variant="buttonVariant"
+ size="disabled"
+ :aria-label="buttonLabel"
+ v-bind="a11yProps"
+ @click="$emit('click')"
+ >
+ <VIcon :icon-path="icon" />
+ <span v-show="showLabel" :class="{ 'ms-2': showLabel }">{{
+ buttonLabel
+ }}</span>
+ <VIcon
+ class="hidden md:block text-dark-charcoal-40 md:ms-2"
+ :icon-path="caretDownIcon"
+ />
+ </VButton>
+</template>
+<script>
+import { ALL_MEDIA, AUDIO, IMAGE } from '~/constants/media'
+import { computed, inject, useContext } from '@nuxtjs/composition-api'
+import useContentType from '~/composables/use-content-type'
+import { isMinScreen } from '~/composables/use-media-query'
+
+import caretDownIcon from '~/assets/icons/caret-down.svg'
+
+import VButton from '~/components/VButton.vue'
+import VIcon from '~/components/VIcon/VIcon.vue'
+
+export default {
+ name: 'VContentSwitcherButton',
+ components: { VButton, VIcon },
+ props: {
+ a11yProps: {
+ type: Object,
+ required: true,
+ },
+ activeItem: {
+ type: String,
+ default: ALL_MEDIA,
+ validator: (val) => [ALL_MEDIA, IMAGE, AUDIO].includes(val),
+ },
+ },
+ setup(props) {
+ const { i18n } = useContext()
+ const isHeaderScrolled = inject('isHeaderScrolled', null)
+ const isMinScreenMd = isMinScreen('md', { shouldPassInSSR: true })
+
+ const { icons, activeType: activeItem } = useContentType()
+ const isIconButton = computed(
+ () => isHeaderScrolled?.value && !isMinScreenMd.value
+ )
+ const buttonVariant = computed(() => {
+ return isMinScreenMd.value && !isHeaderScrolled?.value
+ ? 'tertiary'
+ : 'action-menu'
+ })
+ const buttonLabel = computed(() => {
+ const labelKey = {
+ image: 'search-type.image',
+ audio: 'search-type.audio',
+ all: 'search-type.all',
+ video: 'search-type.video',
+ }[props.activeItem]
+ return i18n.t(labelKey)
+ })
+ const showLabel = computed(
+ () => isMinScreenMd.value || !isHeaderScrolled.value
+ )
+
+ return {
+ buttonVariant,
+ isIconButton,
+ buttonLabel,
+ caretDownIcon,
+ showLabel,
+ isHeaderScrolled,
+ isMinScreenMd,
+ icon: computed(() => icons[activeItem.value]),
+ }
+ },
+}
+</script>
diff --git a/src/components/VContentSwitcher/VContentSwitcherPopover.vue b/src/components/VContentSwitcher/VContentSwitcherPopover.vue
new file mode 100644
index 000000000..8acec2b72
--- /dev/null
+++ b/src/components/VContentSwitcher/VContentSwitcherPopover.vue
@@ -0,0 +1,68 @@
+<template>
+ <VPopover
+ ref="contentMenuPopover"
+ class="flex mx-4 items-stretch"
+ :label="$t('search-type.label')"
+ >
+ <template #trigger="{ a11yProps }">
+ <VContentSwitcherButton
+ :a11y-props="a11yProps"
+ :active-item="activeItem"
+ />
+ </template>
+ <VContentTypes
+ :bordered="false"
+ :active-item="activeItem"
+ @select="selectItem"
+ />
+ </VPopover>
+</template>
+
+<script>
+import { ref } from '@nuxtjs/composition-api'
+import useContentType from '~/composables/use-content-type'
+import checkIcon from '~/assets/icons/checkmark.svg'
+
+import VPopover from '~/components/VPopover/VPopover.vue'
+import VContentSwitcherButton from '~/components/VContentSwitcher/VContentSwitcherButton.vue'
+
+export default {
+ name: 'VContentSwitcherPopover',
+ components: {
+ VContentSwitcherButton,
+ VPopover,
+ },
+ props: {
+ activeItem: {
+ type: String,
+ required: true,
+ },
+ },
+ setup(props, { emit }) {
+ const content = useContentType()
+
+ const contentMenuPopover = ref(null)
+
+ /**
+ * Only the contentMenuPopover needs to be closed programmatically
+ */
+ const closeMenu = () => {
+ if (contentMenuPopover.value) {
+ contentMenuPopover.value.close()
+ }
+ }
+
+ const selectItem = (item) => {
+ emit('select', item)
+ }
+
+ return {
+ content,
+ checkIcon,
+ selectItem,
+ contentMenuPopover,
+ closeMenu,
+ }
+ },
+}
+</script>
diff --git a/src/components/VContentSwitcher/VContentTypes.vue b/src/components/VContentSwitcher/VContentTypes.vue
new file mode 100644
index 000000000..053480f53
--- /dev/null
+++ b/src/components/VContentSwitcher/VContentTypes.vue
@@ -0,0 +1,59 @@
+<template>
+ <VItemGroup
+ direction="vertical"
+ :bordered="bordered"
+ type="radiogroup"
+ class="z-10"
+ >
+ <VItem
+ v-for="(item, idx) in content.types"
+ :key="idx"
+ :selected="item === activeItem"
+ :is-first="idx === 0"
+ @click.native="handleClick(item)"
+ >
+ <VIcon :icon-path="content.icons[item]" class="me-2 ms-4 my-4" />
+ <span class="pe-20 py-4 font-semibold">{{
+ $t(`search-type.${item}`)
+ }}</span>
+ </VItem>
+ </VItemGroup>
+</template>
+<script>
+import { supportedContentTypes } from '~/constants/media'
+import useContentType from '~/composables/use-content-type'
+
+import checkIcon from '~/assets/icons/checkmark.svg'
+
+import VIcon from '~/components/VIcon/VIcon.vue'
+import VItem from '~/components/VItemGroup/VItem.vue'
+import VItemGroup from '~/components/VItemGroup/VItemGroup.vue'
+
+export default {
+ name: 'VContentTypes',
+ components: { VIcon, VItem, VItemGroup },
+ props: {
+ bordered: {
+ type: Boolean,
+ default: true,
+ },
+ activeItem: {
+ type: String,
+ required: true,
+ validator: (val) => supportedContentTypes.includes(val),
+ },
+ },
+ setup(_, { emit }) {
+ const content = useContentType()
+
+ const handleClick = (item) => {
+ emit('select', item)
+ }
+ return {
+ content,
+ checkIcon,
+ handleClick,
+ }
+ },
+}
+</script>
diff --git a/src/components/VContentSwitcher/VMobileMenuModal.vue b/src/components/VContentSwitcher/VMobileMenuModal.vue
new file mode 100644
index 000000000..ad9e0bf91
--- /dev/null
+++ b/src/components/VContentSwitcher/VMobileMenuModal.vue
@@ -0,0 +1,142 @@
+<template>
+ <div ref="nodeRef" class="flex justify-center">
+ <div
+ ref="triggerContainerRef"
+ class="flex items-stretch"
+ @click="onTriggerClick"
+ >
+ <VContentSwitcherButton
+ :a11y-props="triggerA11yProps"
+ :active-item="activeItem"
+ />
+ </div>
+ <VMobileModalContent
+ :visible="visibleRef"
+ :trigger-element="triggerRef"
+ :hide="close"
+ :aria-label="$t('header.filter-button.simple')"
+ >
+ <nav class="p-6" aria-labelledby="content-switcher-heading">
+ <h2
+ id="content-switcher-heading"
+ class="md:sr-only text-sr pb-4 uppercase font-semibold"
+ >
+ {{ $t('search-type.heading') }}
+ </h2>
+ <VContentTypes
+ :bordered="false"
+ :active-item="content.activeType.value"
+ @select="selectItem"
+ />
+ <VPageList layout="columns" class="mt-10" />
+ </nav>
+ </VMobileModalContent>
+ </div>
+</template>
+
+<script>
+import { onMounted, reactive, ref, watch } from '@nuxtjs/composition-api'
+import { useBodyScrollLock } from '~/composables/use-body-scroll-lock'
+import useContentType from '~/composables/use-content-type'
+import usePages from '~/composables/use-pages'
+
+import externalLinkIcon from 'assets/icons/external-link.svg'
+
+import VMobileModalContent from '~/components/VModal/VMobileModalContent.vue'
+import VContentTypes from '~/components/VContentSwitcher/VContentTypes.vue'
+import VPageList from '~/components/VHeader/VPageMenu/VPageList.vue'
+import VContentSwitcherButton from '~/components/VContentSwitcher/VContentSwitcherButton'
+
+const externalLinkProps = { as: 'a', target: '_blank', rel: 'noopener' }
+
+export default {
+ name: 'VMobileContentSwitcher',
+ components: {
+ VMobileModalContent,
+ VContentTypes,
+ VPageList,
+ VContentSwitcherButton,
+ },
+ props: {
+ activeItem: {
+ type: String,
+ required: true,
+ },
+ },
+ setup(_, { emit }) {
+ const content = useContentType()
+ const pages = usePages()
+
+ const modalRef = ref(null)
+ const triggerContainerRef = ref(null)
+
+ const closeMenu = () => close()
+
+ const visibleRef = ref(false)
+ const nodeRef = ref(null)
+
+ const triggerA11yProps = reactive({
+ 'aria-expanded': false,
+ 'aria-haspopup': 'dialog',
+ })
+
+ const triggerRef = ref()
+ onMounted(() => (triggerRef.value = triggerContainerRef.value?.firstChild))
+
+ watch([visibleRef], ([visible]) => {
+ triggerA11yProps['aria-expanded'] = visible
+ })
+
+ const { lock, unlock } = useBodyScrollLock({ nodeRef })
+
+ const open = () => {
+ visibleRef.value = true
+ emit('open')
+ lock()
+ }
+
+ const close = () => {
+ visibleRef.value = false
+ emit('close')
+ unlock()
+ }
+
+ const onTriggerClick = () => {
+ if (visibleRef.value === true) {
+ close()
+ } else {
+ open()
+ }
+ }
+ const selectItem = (item) => {
+ emit('select', item)
+ }
+
+ const isLinkExternal = (item) => !item.link.startsWith('/')
+ const getLinkProps = (item) => {
+ return isLinkExternal(item)
+ ? { ...externalLinkProps, href: item.link }
+ : { as: 'NuxtLink', to: item.link }
+ }
+ return {
+ getLinkProps,
+ isLinkExternal,
+ externalLinkIcon,
+ pages,
+ content,
+ close,
+ modalRef,
+ nodeRef,
+ triggerContainerRef,
+ closeMenu,
+
+ triggerRef,
+ onTriggerClick,
+
+ triggerA11yProps,
+ visibleRef,
+ selectItem,
+ }
+ },
+}
+</script>
diff --git a/src/components/VHeader/VFilterButton.vue b/src/components/VHeader/VFilterButton.vue
index 9e7c9048e..2c7c8eceb 100644
--- a/src/components/VHeader/VFilterButton.vue
+++ b/src/components/VHeader/VFilterButton.vue
@@ -1,7 +1,8 @@
<template>
<VButton
:variant="variant"
- class="self-center gap-2 align-center font-semibold md:flex-shrink-0"
+ size="disabled"
+ class="self-center gap-2 align-center font-semibold px-3 py-2 md:flex-shrink-0"
:class="{ 'w-12': isHeaderScrolled && !isMinScreenMd }"
:pressed="pressed"
aria-controls="filter-sidebar"
diff --git a/src/components/VHeader/VHeader.vue b/src/components/VHeader/VHeader.vue
index 725dd0e75..b5f3cc552 100644
--- a/src/components/VHeader/VHeader.vue
+++ b/src/components/VHeader/VHeader.vue
@@ -1,16 +1,16 @@
<template>
<header
- class="flex py-4 px-4 md:px-7 items-stretch z-40 w-full bg-white md:gap-x-2"
+ class="flex py-4 px-4 md:px-7 items-stretch z-40 w-full bg-white md:gap-x-2 gap-y-4"
:class="{
+ 'flex-wrap': isSearchRoute,
'border-b border-white': !isHeaderScrolled && !isMenuOpen,
'border-b border-dark-charcoal-20':
isSearchRoute && (isHeaderScrolled || isMenuOpen),
- 'flex-wrap gap-y-4': !isMinScreenMd && !isHeaderScrolled,
'justify-between': isSearchRoute,
'justify-start': !isSearchRoute,
}"
>
- <div class="one-third items-stretch flex">
+ <div class="items-stretch flex" :class="{ 'one-third': isSearchRoute }">
<NuxtLink
to="/"
class="rounded-sm ring-offset-1 focus:outline-none focus-visible:ring focus-visible:ring-pink -ms-2 inline-flex items-center hover:bg-yellow mr-auto"
@@ -50,9 +50,16 @@
</span>
</VSearchBar>
+ <VHeaderMenu
+ :is-search-route="isSearchRoute"
+ :class="{ 'one-third': isSearchRoute }"
+ @open="openMenuModal(menus.CONTENT_SWITCHER)"
+ @close="close()"
+ />
<VHeaderFilter
v-if="isSearchRoute"
- class="one-third"
+ class="text-sr md:text-base"
+ :class="{ 'one-third': isSearchRoute }"
@open="openMenuModal(menus.FILTERS)"
@close="close()"
/>
@@ -85,9 +92,10 @@ import { useFilterSidebarVisibility } from '~/composables/use-filter-sidebar-vis
import closeIcon from '~/assets/icons/close.svg'
import OpenverseLogoText from '~/assets/icons/openverse-logo-text.svg?inline'
-import VSearchBar from '~/components/VHeader/VSearchBar/VSearchBar'
+import VHeaderMenu from '~/components/VHeader/VHeaderMenu.vue'
import VHeaderFilter from '~/components/VHeader/VHeaderFilter.vue'
-import VLogoLoader from '~/components/VLogoLoader/VLogoLoader'
+import VLogoLoader from '~/components/VLogoLoader/VLogoLoader.vue'
+import VSearchBar from '~/components/VHeader/VSearchBar/VSearchBar.vue'
const i18nKeys = {
[AUDIO]: {
@@ -110,6 +118,7 @@ const VHeader = defineComponent({
name: 'VHeader',
components: {
VHeaderFilter,
+ VHeaderMenu,
VLogoLoader,
VSearchBar,
OpenverseLogoText,
@@ -123,6 +132,7 @@ const VHeader = defineComponent({
const isHeaderScrolled = inject('isHeaderScrolled')
const isMinScreenMd = isMinScreen('md', { shouldPassInSSR: true })
+ const headerHasTwoRows = inject('headerHasTwoRows')
provide('isMinScreenMd', isMinScreenMd)
const menuModalRef = ref(null)
@@ -177,7 +187,9 @@ const VHeader = defineComponent({
}
/** @type {import('@nuxtjs/composition-api').ComputedRef<number>} */
- const resultsCount = computed(() => store.getters['media/results'].count)
+ const resultsCount = computed(
+ () => store.getters['media/results']?.count ?? 0
+ )
/**
* Status is hidden below the medium breakpoint.
@@ -268,6 +280,7 @@ const VHeader = defineComponent({
isHeaderScrolled,
isMinScreenMd,
+ headerHasTwoRows,
isSearchRoute,
isHomeRoute,
diff --git a/src/components/VHeader/VHeaderFilter.vue b/src/components/VHeader/VHeaderFilter.vue
index ef09abd60..9ae91c460 100644
--- a/src/components/VHeader/VHeaderFilter.vue
+++ b/src/components/VHeader/VHeaderFilter.vue
@@ -31,7 +31,7 @@ import VTeleport from '~/components/VTeleport/VTeleport.vue'
import VFilterButton from '~/components/VHeader/VFilterButton.vue'
import VSearchGridFilter from '~/components/VFilters/VSearchGridFilter.vue'
import VSidebarContent from '~/components/VHeader/VSidebarContent.vue'
-import VMobileModalContent from '@/components/VModal/VMobileModalContent'
+import VMobileModalContent from '~/components/VModal/VMobileModalContent.vue'
export default {
name: 'VHeaderFilter',
diff --git a/src/components/VHeader/VHeaderMenu.vue b/src/components/VHeader/VHeaderMenu.vue
new file mode 100644
index 000000000..7e7ab275a
--- /dev/null
+++ b/src/components/VHeader/VHeaderMenu.vue
@@ -0,0 +1,74 @@
+<script>
+import { inject, onMounted, ref } from '@nuxtjs/composition-api'
+import useContentType from '~/composables/use-content-type'
+
+import VMobileContentSwitcher from '~/components/VContentSwitcher/VMobileMenuModal.vue'
+import VContentSwitcherPopover from '~/components/VContentSwitcher/VContentSwitcherPopover.vue'
+import VDesktopPageMenu from '~/components/VHeader/VPageMenu/VDesktopPageMenu.vue'
+import VMobilePageMenu from '~/components/VHeader/VPageMenu/VMobilePageMenu.vue'
+
+export default {
+ name: 'VHeaderMenu',
+ components: {
+ VMobileContentSwitcher,
+ VContentSwitcherPopover,
+ VDesktopPageMenu,
+ VMobilePageMenu,
+ },
+ props: {
+ isSearchRoute: {
+ type: Boolean,
+ required: true,
+ },
+ },
+ setup() {
+ /** @type {import('@nuxtjs/composition-api').Ref<boolean>} */
+ const isMinScreenMd = inject('isMinScreenMd')
+ /** @type {import('@nuxtjs/composition-api').Ref<null|HTMLElement>} */
+ const menuModalRef = ref(null)
+ const content = useContentType()
+
+ const isMounted = ref(false)
+ onMounted(() => {
+ isMounted.value = true
+ })
+ const selectContentType = (val) => {
+ content.setActiveType(val)
+ menuModalRef.value?.closeMenu()
+ }
+
+ return {
+ isMinScreenMd,
+ menuModalRef,
+ isMounted,
+
+ content,
+ selectContentType,
+ }
+ },
+ render(h) {
+ if (!this.isSearchRoute) {
+ return this.isMinScreenMd && this.isMounted
+ ? h(VDesktopPageMenu)
+ : h(VMobilePageMenu)
+ } else if (this.isMinScreenMd && this.isMounted) {
+ return h('div', { class: 'flex flex-grow justify-between' }, [
+ h(VDesktopPageMenu),
+ h(VContentSwitcherPopover, {
+ props: { activeItem: this.content.activeType.value },
+ ref: 'menuModalRef',
+ on: { select: this.selectContentType },
+ }),
+ ])
+ } else {
+ return h(VMobileContentSwitcher, {
+ ref: 'menuModalRef',
+ props: { activeItem: this.content.activeType.value },
+ on: {
+ select: this.selectContentType,
+ },
+ })
+ }
+ },
+}
+</script>
diff --git a/src/components/VHeader/VPageMenu/VDesktopPageMenu.vue b/src/components/VHeader/VPageMenu/VDesktopPageMenu.vue
new file mode 100644
index 000000000..996f83624
--- /dev/null
+++ b/src/components/VHeader/VPageMenu/VDesktopPageMenu.vue
@@ -0,0 +1,46 @@
+<template>
+ <VPopover
+ ref="pageMenuPopover"
+ class="flex mx-2 items-stretch"
+ :label="$t('header.aria.menu')"
+ >
+ <template #trigger="{ a11yProps }">
+ <VPageMenuButton :a11y-props="a11yProps" />
+ </template>
+ <VPageList layout="vertical" @click="closeMenu" />
+ </VPopover>
+</template>
+
+<script>
+import { ref } from '@nuxtjs/composition-api'
+
+import VPopover from '~/components/VPopover/VPopover.vue'
+import VPageMenuButton from '~/components/VHeader/VPageMenu/VPageMenuButton.vue'
+import VPageList from '~/components/VHeader/VPageMenu/VPageList.vue'
+
+export default {
+ name: 'VDesktopPageMenu',
+ components: {
+ VPageMenuButton,
+ VPageList,
+ VPopover,
+ },
+ setup() {
+ const pageMenuPopover = ref(null)
+
+ /**
+ * Only the contentMenuPopover needs to be closed programmatically
+ */
+ const closeMenu = () => {
+ if (pageMenuPopover.value) {
+ pageMenuPopover.value.close()
+ }
+ }
+
+ return {
+ pageMenuPopover,
+ closeMenu,
+ }
+ },
+}
+</script>
diff --git a/src/components/VHeader/VPageMenu/VMobilePageMenu.vue b/src/components/VHeader/VPageMenu/VMobilePageMenu.vue
new file mode 100644
index 000000000..3b0fc166b
--- /dev/null
+++ b/src/components/VHeader/VPageMenu/VMobilePageMenu.vue
@@ -0,0 +1,89 @@
+<template>
+ <div ref="nodeRef" class="flex justify-center">
+ <div
+ ref="triggerContainerRef"
+ class="flex items-stretch flex-shrink-0"
+ @click="onTriggerClick"
+ >
+ <VPageMenuButton :a11y-props="triggerA11yProps" />
+ </div>
+ <VMobileModalContent
+ :visible="visibleRef"
+ :trigger-element="triggerRef"
+ :hide="close"
+ :aria-label="$t('header.filter-button.simple')"
+ >
+ <nav class="p-4">
+ <VPageList layout="columns" @click="closeMenu" />
+ </nav>
+ </VMobileModalContent>
+ </div>
+</template>
+
+<script>
+import { computed, reactive, ref, watch } from '@nuxtjs/composition-api'
+import { useBodyScrollLock } from '~/composables/use-body-scroll-lock'
+
+import VMobileModalContent from '~/components/VModal/VMobileModalContent.vue'
+import VPageMenuButton from '~/components/VHeader/VPageMenu/VPageMenuButton.vue'
+import VPageList from '~/components/VHeader/VPageMenu/VPageList.vue'
+
+export default {
+ name: 'VMobilePageMenu',
+ components: { VMobileModalContent, VPageMenuButton, VPageList },
+ setup(_, { emit }) {
+ const modalRef = ref(null)
+
+ const closeMenu = () => close()
+
+ const visibleRef = ref(false)
+ const nodeRef = ref(null)
+
+ const triggerA11yProps = reactive({
+ 'aria-expanded': false,
+ 'aria-haspopup': 'dialog',
+ })
+
+ const triggerRef = computed(() => nodeRef.value?.firstChild.firstChild)
+
+ watch([visibleRef], ([visible]) => {
+ triggerA11yProps['aria-expanded'] = visible
+ })
+
+ const { lock, unlock } = useBodyScrollLock({ nodeRef })
+
+ const open = () => {
+ visibleRef.value = true
+ emit('open')
+ lock()
+ }
+
+ const close = () => {
+ visibleRef.value = false
+ emit('close')
+ unlock()
+ }
+
+ const onTriggerClick = () => {
+ if (visibleRef.value === true) {
+ close()
+ } else {
+ open()
+ }
+ }
+
+ return {
+ close,
+ modalRef,
+ nodeRef,
+ closeMenu,
+
+ triggerRef,
+ onTriggerClick,
+
+ triggerA11yProps,
+ visibleRef,
+ }
+ },
+}
+</script>
diff --git a/src/components/VHeader/VPageMenu/VPageList.vue b/src/components/VHeader/VPageMenu/VPageList.vue
new file mode 100644
index 000000000..715578878
--- /dev/null
+++ b/src/components/VHeader/VPageMenu/VPageList.vue
@@ -0,0 +1,61 @@
+<template>
+ <VItemGroup :direction="layout" class="mt-10 md:mt-2" :bordered="false">
+ <VItem
+ v-for="(page, idx) in pages.all"
+ :key="page.id"
+ class="md:w-full w-1/2"
+ :selected="page.id === pages.current"
+ :is-first="idx === 0"
+ v-bind="getLinkProps(page)"
+ @click="$emit('click')"
+ >
+ <div class="flex flex-row">
+ <span class="pe-2">{{ $t(page.name) }}</span>
+ <VIcon
+ v-if="isLinkExternal(page)"
+ :icon-path="externalLinkIcon"
+ :size="5"
+ class="self-center mb-0.5"
+ />
+ </div>
+ </VItem>
+ </VItemGroup>
+</template>
+<script>
+import usePages from '~/composables/use-pages'
+
+import externalLinkIcon from '~/assets/icons/external-link.svg'
+
+import VIcon from '~/components/VIcon/VIcon.vue'
+import VItem from '~/components/VItemGroup/VItem.vue'
+import VItemGroup from '~/components/VItemGroup/VItemGroup.vue'
+
+const externalLinkProps = { as: 'a', target: '_blank', rel: 'noopener' }
+
+export default {
+ name: 'VPageMenuPopover',
+ components: { VIcon, VItem, VItemGroup },
+ props: {
+ layout: {
+ type: String,
+ default: 'vertical',
+ validator: (val) => ['vertical', 'columns'].includes(val),
+ },
+ },
+ setup() {
+ const pages = usePages()
+ const isLinkExternal = (item) => !item.link.startsWith('/')
+ const getLinkProps = (item) => {
+ return isLinkExternal(item)
+ ? { ...externalLinkProps, href: item.link }
+ : { as: 'NuxtLink', to: item.link }
+ }
+ return {
+ getLinkProps,
+ isLinkExternal,
+ externalLinkIcon,
+ pages,
+ }
+ },
+}
+</script>
diff --git a/src/components/VHeader/VPageMenu/VPageMenuButton.vue b/src/components/VHeader/VPageMenu/VPageMenuButton.vue
new file mode 100644
index 000000000..d7b0fdc40
--- /dev/null
+++ b/src/components/VHeader/VPageMenu/VPageMenuButton.vue
@@ -0,0 +1,36 @@
+<template>
+ <VButton
+ variant="action-menu"
+ size="disabled"
+ class="font-semibold p-2 w-12 leading-7"
+ :aria-label="buttonLabel"
+ v-bind="a11yProps"
+ @click="$emit('click')"
+ >
+ <VIcon :icon-path="ellipsisIcon" />
+ </VButton>
+</template>
+<script>
+import { computed, useContext } from '@nuxtjs/composition-api'
+
+import ellipsisIcon from '~/assets/icons/ellipsis.svg'
+
+import VIcon from '~/components/VIcon/VIcon.vue'
+import VButton from '~/components/VButton.vue'
+
+export default {
+ name: 'VPageMenuButton',
+ components: { VButton, VIcon },
+ props: {
+ a11yProps: {},
+ },
+ setup() {
+ const { i18n } = useContext()
+ const buttonLabel = computed(() => i18n.t('header.aria.menu'))
+ return {
+ buttonLabel,
+ ellipsisIcon,
+ }
+ },
+}
+</script>
diff --git a/src/components/VHeader/VSearchBar/VSearchButton.vue b/src/components/VHeader/VSearchBar/VSearchButton.vue
index 9107e277d..e07312db9 100644
--- a/src/components/VHeader/VSearchBar/VSearchButton.vue
+++ b/src/components/VHeader/VSearchBar/VSearchButton.vue
@@ -30,11 +30,14 @@ export default {
/* 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>
diff --git a/src/components/VItemGroup/VItem.vue b/src/components/VItemGroup/VItem.vue
index 551ed75df..4c32139d9 100644
--- a/src/components/VItemGroup/VItem.vue
+++ b/src/components/VItemGroup/VItem.vue
@@ -13,6 +13,7 @@
>
<VButton
data-item-group-item
+ :as="as"
class="flex justify-between focus-visible:ring-pink rounded min-w-full"
:class="[$style.button, $style[`${contextProps.direction}-button`]]"
variant="grouped"
@@ -26,6 +27,8 @@
@focus="isFocused = true"
@blur="isFocused = false"
@keydown="focusContext.onItemKeyPress"
+ @keydown.native="focusContext.onItemKeyPress"
+ @click.native="$emit('click')"
>
<div
class="flex-grow whitespace-nowrap"
@@ -35,7 +38,7 @@
</div>
<VIcon
v-if="!isInPopover && selected && contextProps.direction === 'vertical'"
- :icon-path="check"
+ :icon-path="checkmark"
/>
</VButton>
</div>
@@ -49,7 +52,7 @@ import {
computed,
watch,
} from '@nuxtjs/composition-api'
-import check from '~/assets/icons/check.svg'
+import checkmark from '~/assets/icons/checkmark.svg'
import VButton from '~/components/VButton.vue'
import VIcon from '~/components/VIcon/VIcon.vue'
import {
@@ -62,6 +65,7 @@ import { warn } from '~/utils/warn'
export default defineComponent({
name: 'VItem',
components: { VButton, VIcon },
+ inheritAttrs: false,
props: {
/**
* Whether the item is selected/checked.
@@ -77,7 +81,26 @@ export default defineComponent({
type: Boolean,
required: true,
},
+ /**
+ * To change the underlying component for the VButton,
+ * pass `as` prop.
+ * @variants 'button', 'a', 'NuxtLink'
+ */
+ as: {
+ type: String,
+ default: 'button',
+ validator: (val) => ['button', 'a', 'NuxtLink'].includes(val),
+ },
},
+ /**
+ * Setting `inheritAttrs` to false ensures that the $attrs are only passed to VButton component,
+ * and not the outer div. In Vue 3 this will also stops native events such as `click` from
+ * going up the tree. Adding `emits` should fix this:
+ * https://v3.vuejs.org/guide/migration/v-on-native-modifier-removed.html#_3-x-syntax
+ * However, with current Vue 2 setup, if VItem is a link (a or NuxtLink), it is
+ * necessary to add native modifier to handle click event: `@click.native='handler'`.
+ */
+ emits: ['click'],
setup(props) {
const focusContext = inject(VItemGroupFocusContextKey)
const isFocused = ref(false)
@@ -109,12 +132,12 @@ export default defineComponent({
// If the group is not focused but this is the selected item, then this should be the focusable item when focusing into the group
if (!focusContext.isGroupFocused.value && props.selected) return 0
- // Otherwise the item should not be tabbable. The logic above guarantees that at least one other item in the group will be tabbable.
+ // Otherwise, the item should not be tabbable. The logic above guarantees that at least one other item in the group will be tabbable.
return -1
})
return {
- check,
+ checkmark,
contextProps,
isInPopover,
isFocused,
diff --git a/src/components/VItemGroup/VItemGroup.vue b/src/components/VItemGroup/VItemGroup.vue
index d992c4bd4..bb7df99c8 100644
--- a/src/components/VItemGroup/VItemGroup.vue
+++ b/src/components/VItemGroup/VItemGroup.vue
@@ -3,7 +3,11 @@
ref="nodeRef"
class="w-full flex"
:role="type"
- :class="direction === 'vertical' ? 'flex-col' : 'flex-row'"
+ :class="{
+ 'flex-col': direction === 'vertical',
+ 'flex-row': direction !== 'vertical',
+ 'flex-wrap': direction === 'columns',
+ }"
@focusin="isFocused = true"
@focusout="isFocused = false"
>
@@ -60,15 +64,15 @@ export default defineComponent({
* @default 'vertical'
*/
direction: {
- type: /** @type {import('@nuxtjs/composition-api').PropType<'vertical' | 'horizontal'>} */ (
+ type: /** @type {import('@nuxtjs/composition-api').PropType<'vertical' | 'horizontal' | 'columns' >} */ (
String
),
default: 'vertical',
- validate: (v) => ['vertical', 'horizontal'].includes(v),
+ validate: (v) => ['vertical', 'horizontal', 'columns'].includes(v),
},
/**
* Whether to render a bordered, separated list of items. When false each
- * item will be have whitespace separating them instead of borders.
+ * item will have whitespace separating them instead of borders.
*
* @default true
*/
diff --git a/src/components/VModal/VMobileModalContent.vue b/src/components/VModal/VMobileModalContent.vue
index d687b84f4..70d223fc0 100644
--- a/src/components/VModal/VMobileModalContent.vue
+++ b/src/components/VModal/VMobileModalContent.vue
@@ -13,7 +13,7 @@
@keydown="onKeyDown"
@blur="onBlur"
>
- <div class="w-full flex justify-between h-20 px-6 py-6 shrink-0">
+ <div class="w-full flex justify-between h-20 px-4 py-6 shrink-0">
<NuxtLink
to="/"
class="rounded-sm ring-offset-1 focus:outline-none focus-visible:ring focus-visible:ring-pink -ms-2 inline-flex items-center hover:bg-yellow"
@@ -24,7 +24,7 @@
<VButton
size="disabled"
variant="plain"
- class="py-2 px-4"
+ class="py-2 px-4 font-semibold text-sr"
@click="hide()"
>
{{ $t('modal.close') }}
diff --git a/src/components/VPopover/VPopover.vue b/src/components/VPopover/VPopover.vue
index f2ec1508e..7483ce817 100644
--- a/src/components/VPopover/VPopover.vue
+++ b/src/components/VPopover/VPopover.vue
@@ -2,7 +2,7 @@
<div>
<div
ref="triggerContainerRef"
- class="w-min whitespace-nowrap"
+ class="w-min whitespace-nowrap flex items-stretch"
@click="onTriggerClick"
>
<!--
diff --git a/src/components/VTeleport/VTeleportTarget.vue b/src/components/VTeleport/VTeleportTarget.vue
index 6d19f91c2..6b17f589c 100644
--- a/src/components/VTeleport/VTeleportTarget.vue
+++ b/src/components/VTeleport/VTeleportTarget.vue
@@ -1,7 +1,7 @@
<script>
import { defineComponent } from '@nuxtjs/composition-api'
import { targets } from './meta/targets'
-import { warn } from '@/utils/warn'
+import { warn } from '~/utils/warn'
export default defineComponent({
name: 'VTeleportTarget',
diff --git a/src/composables/use-content-type.js b/src/composables/use-content-type.js
new file mode 100644
index 000000000..20cc551b6
--- /dev/null
+++ b/src/composables/use-content-type.js
@@ -0,0 +1,34 @@
+import { computed, ref, useContext } from '@nuxtjs/composition-api'
+import { supportedContentTypes } from '~/constants/media'
+import { SEARCH } from '~/constants/store-modules'
+import { UPDATE_QUERY } from '~/constants/action-types'
+import allIcon from '~/assets/icons/all-content.svg'
+import audioIcon from '~/assets/icons/audio-content.svg'
+import imageIcon from '~/assets/icons/image-content.svg'
+
+const icons = {
+ all: allIcon,
+ audio: audioIcon,
+ image: imageIcon,
+}
+export default function useContentType() {
+ const { store } = useContext()
+
+ const contentTypes = [...supportedContentTypes]
+
+ const activeType = computed(() => store.state.search.searchType)
+ const previousContentType = ref(activeType.value)
+ const setActiveType = async (contentType) => {
+ if (previousContentType.value === contentType) return
+ await store.dispatch(`${SEARCH}/${UPDATE_QUERY}`, {
+ searchType: contentType,
+ })
+ previousContentType.value = contentType
+ }
+ return {
+ setActiveType,
+ activeType,
+ types: contentTypes,
+ icons,
+ }
+}
diff --git a/src/composables/use-filter-sidebar-visibility.js b/src/composables/use-filter-sidebar-visibility.js
index acfa1ee93..02f3eb6e1 100644
--- a/src/composables/use-filter-sidebar-visibility.js
+++ b/src/composables/use-filter-sidebar-visibility.js
@@ -1,6 +1,6 @@
import { onMounted, ref } from '@nuxtjs/composition-api'
import local from '~/utils/local'
-import { isMinScreen } from '@/composables/use-media-query'
+import { isMinScreen } from '~/composables/use-media-query'
/** @type {import('@nuxtjs/composition-api').Ref<boolean>} */
const isVisible = ref(false)
diff --git a/src/composables/use-pages.js b/src/composables/use-pages.js
new file mode 100644
index 000000000..bce9a47e2
--- /dev/null
+++ b/src/composables/use-pages.js
@@ -0,0 +1,53 @@
+import { computed, useContext, useRoute } from '@nuxtjs/composition-api'
+
+export default function usePages() {
+ const { app } = useContext()
+
+ const pages = [
+ {
+ id: 'about',
+ name: 'header.about-nav-item',
+ link: app.localePath('/about'),
+ },
+ {
+ id: 'sources',
+ name: 'header.source-nav-item',
+ link: app.localePath('/sources'),
+ },
+ {
+ id: 'licenses',
+ name: 'header.licenses-nav-item',
+ link: 'https://creativecommons.org/about/cclicenses/',
+ },
+ {
+ id: 'search-help',
+ name: 'header.search-guide-nav-item',
+ link: app.localePath('/search-help'),
+ },
+ {
+ id: 'meta-search',
+ name: 'header.meta-search-nav-item',
+ link: app.localePath('/meta-search'),
+ },
+ {
+ id: 'feedback',
+ name: 'header.feedback-nav-item',
+ link: app.localePath('/feedback'),
+ },
+ {
+ id: 'api',
+ name: 'header.api-nav-item',
+ link: 'https://api.openverse.engineering/v1/',
+ },
+ {
+ id: 'extension',
+ name: 'header.extension-nav-item',
+ link: app.localePath('/extension'),
+ },
+ ]
+
+ const route = useRoute()
+ const currentPageId = computed(() => route.value.name)
+
+ return { all: pages, current: currentPageId }
+}
diff --git a/src/constants/media.js b/src/constants/media.js
index 6a7b7e570..cf6eec1e1 100644
--- a/src/constants/media.js
+++ b/src/constants/media.js
@@ -5,11 +5,15 @@ export const ALL_MEDIA = 'all'
/** @typedef {typeof AUDIO | typeof IMAGE | typeof VIDEO | typeof ALL_MEDIA} MediaType */
-// Media types
-/** @type {MediaType[]} */
-export const mediaTypes = [AUDIO, IMAGE]
-// Media types which support custom filters
-/** @type {MediaType[]} */
-export const supportedMediaTypes = [AUDIO, IMAGE, VIDEO]
-/** @type {MediaType[]} */
-export const allMediaTypes = [ALL_MEDIA, IMAGE, AUDIO, VIDEO]
+/**
+ * Media types that the API supports.
+ * These types also support custom filters.
+ * @type {MediaType[]}
+ */
+export const supportedMediaTypes = [AUDIO, IMAGE]
+
+/**
+ * The types of content that users can search. `All` is also an option here.
+ * @type {MediaType[]}
+ */
+export const supportedContentTypes = [ALL_MEDIA, IMAGE, AUDIO]
diff --git a/src/layouts/default.vue b/src/layouts/default.vue
index bc3f39e91..a513886a8 100644
--- a/src/layouts/default.vue
+++ b/src/layouts/default.vue
@@ -2,12 +2,10 @@
<div class="app grid h-screen overflow-hidden">
<MigrationNotice v-show="isReferredFromCc" />
<TranslationStatusBanner />
- <VHeader v-if="!isHomeRoute" />
+ <VHeader />
<main
class="main embedded overflow-x-hidden"
- :class="{
- 'has-sidebar': isSidebarVisible,
- }"
+ :class="{ 'has-sidebar': isSidebarVisible }"
>
<Nuxt ref="mainContentRef" class="min-w-0 main-page" />
<VSidebarTarget class="sidebar" />
@@ -29,10 +27,7 @@ import {
} from '@nuxtjs/composition-api'
import { useFilterSidebarVisibility } from '~/composables/use-filter-sidebar-visibility'
import { isMinScreen } from '~/composables/use-media-query'
-import {
- useMatchHomeRoute,
- useMatchSearchRoutes,
-} from '~/composables/use-match-routes'
+import { useMatchSearchRoutes } from '~/composables/use-match-routes'
import { useScroll } from '~/composables/use-scroll'
import MigrationNotice from '~/components/MigrationNotice.vue'
@@ -66,7 +61,6 @@ const embeddedPage = {
const { isVisible: isFilterVisible } = useFilterSidebarVisibility()
const isMinScreenMd = isMinScreen('md')
const { matches: isSearchRoute } = useMatchSearchRoutes()
- const { matches: isHomeRoute } = useMatchHomeRoute()
const isSidebarVisible = computed(
() => isSearchRoute.value && isMinScreenMd.value && isFilterVisible.value
@@ -87,13 +81,18 @@ const embeddedPage = {
provide('isHeaderScrolled', isHeaderScrolled)
provide('showScrollButton', showScrollButton)
+ const headerHasTwoRows = computed(
+ () =>
+ isSearchRoute.value && !isHeaderScrolled.value && !isMinScreenMd.value
+ )
+ provide('headerHasTwoRows', headerHasTwoRows)
return {
isHeaderScrolled,
isMinScreenMd,
isReferredFromCc,
isSidebarVisible,
isSearchRoute,
- isHomeRoute,
+ headerHasTwoRows,
mainContentRef,
mainRef,
}
diff --git a/src/locales/en.json b/src/locales/en.json
index 9c23c2108..60e7a1c44 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -705,6 +705,7 @@
"audio": "Audio",
"all": "All content",
"video": "Videos",
- "label": "Type of content to search"
+ "label": "Type of content to search",
+ "heading": "Content types"
}
}
diff --git a/src/pages/search.vue b/src/pages/search.vue
index 2cc94e88b..263782067 100644
--- a/src/pages/search.vue
+++ b/src/pages/search.vue
@@ -31,12 +31,12 @@ import {
SET_SEARCH_STATE_FROM_URL,
UPDATE_SEARCH_TYPE,
} from '~/constants/action-types'
-import { queryStringToSearchType } from '~/utils/search-query-transform'
import { ALL_MEDIA, AUDIO, IMAGE } from '~/constants/media'
+import isEqual from 'lodash.isequal'
import { mapActions, mapGetters, mapState } from 'vuex'
-import { inject } from '@nuxtjs/composition-api'
import { MEDIA, SEARCH } from '~/constants/store-modules'
+import { inject } from '@nuxtjs/composition-api'
import { isMinScreen } from '~/composables/use-media-query.js'
import { useFilterSidebarVisibility } from '~/composables/use-filter-sidebar-visibility'
@@ -123,28 +123,34 @@ const BrowsePage = {
},
watch: {
query: {
- deep: true,
- handler() {
+ handler(newQuery, oldQuery) {
+ console.log('query changed')
const newPath = this.localePath({
- path: this.$route.path,
+ path: `/search/${this.searchType === 'all' ? '' : this.searchType}/`,
query: this.searchQueryParams,
})
this.$router.push(newPath)
- if (this.supported) {
+ if (!isEqual(oldQuery, newQuery) && this.supported) {
this.getMediaItems(this.query)
}
},
},
- /**
- * Updates the search type only if the route's path changes.
- * @param newRoute
- * @param oldRoute
- */
- $route(newRoute, oldRoute) {
- if (newRoute.path !== oldRoute.path) {
- const searchType = queryStringToSearchType(newRoute.path)
- this.updateSearchType({ searchType })
- }
+ searchType: {
+ // This fix is necessary only until All search page is merged
+ handler(newQuery, oldQuery) {
+ if (
+ [ALL_MEDIA, IMAGE].includes(newQuery) &&
+ [ALL_MEDIA, IMAGE].includes(oldQuery)
+ ) {
+ const newPath = this.localePath({
+ path: `/search/${
+ this.searchType === 'all' ? '' : this.searchType
+ }/`,
+ query: this.searchQueryParams,
+ })
+ this.$router.push(newPath)
+ }
+ },
},
},
}
diff --git a/src/store/search.js b/src/store/search.js
index 4e0b1f9cf..6f8a0e446 100644
--- a/src/store/search.js
+++ b/src/store/search.js
@@ -346,7 +346,9 @@ const actions = {
}
commit(CLEAR_OTHER_MEDIA_TYPE_FILTERS, { searchType })
}
- await dispatch(UPDATE_QUERY_FROM_FILTERS, queryParams)
+ if (Object.keys(queryParams).length !== 0) {
+ await dispatch(UPDATE_QUERY_FROM_FILTERS, queryParams)
+ }
},
/**
* Toggles a filter's checked parameter
diff --git a/src/utils/search-query-transform.js b/src/utils/search-query-transform.js
index 925336d85..68e0c86ea 100644
--- a/src/utils/search-query-transform.js
+++ b/src/utils/search-query-transform.js
@@ -216,3 +216,12 @@ export const queryStringToQueryData = (queryString) => {
return queryDataObject
}
+
+export const areQueriesEqual = (oldQuery, newQuery) => {
+ for (let key of Object.keys(oldQuery)) {
+ if (oldQuery[key] !== newQuery[key]) {
+ return false
+ }
+ }
+ return true
+}
diff --git a/test/e2e/search-query-client.spec.js b/test/e2e/search-query-client.spec.js
index f68e82463..a0c921561 100644
--- a/test/e2e/search-query-client.spec.js
+++ b/test/e2e/search-query-client.spec.js
@@ -100,7 +100,13 @@ test.skip('url filter types not used by current mediaType are discarded', async
test('can search for a different term', async ({ page }) => {
await page.goto('/search/image?q=cat')
- await page.fill('input[type="search"]', 'dog')
+ await page.fill('header input[type="search"]', 'dog')
await page.keyboard.press('Enter')
await expect(page).toHaveURL('/search/image?q=dog')
})
+test('search for a different term keeps query parameters', async ({ page }) => {
+ await page.goto('/search/image?q=cat&license=by&extension=jpg')
+ await page.fill('header input[type="search"]', 'dog')
+ await page.keyboard.press('Enter')
+ await expect(page).toHaveURL('/search/image?q=dog&license=by&extension=jpg')
+})