Skip to content

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

Add search type button (#1860)

Olga Bulate44fb703f0f5
diff --git a/src/components/VButton.vue b/src/components/VButton.vue
index 4ba785a25..d6f1ffb8b 100644
--- a/src/components/VButton.vue
+++ b/src/components/VButton.vue
@@ -311,7 +311,7 @@ a.button {
@apply border-tx bg-tx text-dark-charcoal hover:border-dark-charcoal-20;
}
.action-menu-pressed {
- @apply border-tx bg-dark-charcoal text-white hover:bg-dark-charcoal-90;
+ @apply border-tx bg-dark-charcoal text-white hover:border-tx hover:bg-dark-charcoal-90;
}
/**
@@ -329,7 +329,7 @@ https://www.figma.com/file/GIIQ4sDbaToCfFQyKMvzr8/Openverse-Design-Library?node-
@apply border-tx bg-dark-charcoal-10 text-dark-charcoal hover:border-dark-charcoal-20;
}
.action-menu-muted-pressed {
- @apply border-tx bg-dark-charcoal text-white;
+ @apply border-tx bg-dark-charcoal text-white hover:border-tx hover:bg-dark-charcoal-90;
}
.action-menu[disabled='disabled'],
@@ -341,6 +341,14 @@ https://www.figma.com/file/GIIQ4sDbaToCfFQyKMvzr8/Openverse-Design-Library?node-
@apply border-dark-charcoal-10 bg-dark-charcoal-10 text-dark-charcoal-40;
}
+.action-menu-secondary {
+ @apply border border-tx bg-white text-dark-charcoal hover:border-dark-charcoal-20;
+}
+
+.action-menu-secondary-pressed {
+ @apply border-tx bg-dark-charcoal text-white;
+}
+
.full {
@apply w-full bg-dark-charcoal-06 font-semibold hover:bg-dark-charcoal-40 hover:text-white;
}
diff --git a/src/components/VContentSwitcher/VMobileMenuModal.vue b/src/components/VContentSwitcher/VMobileMenuModal.vue
index 0f57e76ed..4ebef2175 100644
--- a/src/components/VContentSwitcher/VMobileMenuModal.vue
+++ b/src/components/VContentSwitcher/VMobileMenuModal.vue
@@ -9,7 +9,6 @@
<VSearchTypeButton
:a11y-props="a11yProps"
:visible="visible"
- :active-item="activeItem"
aria-controls="content-switcher-modal"
/>
</template>
diff --git a/src/components/VContentSwitcher/VSearchTypeButton.vue b/src/components/VContentSwitcher/VSearchTypeButton.vue
index 98814afdb..e9e26591e 100644
--- a/src/components/VContentSwitcher/VSearchTypeButton.vue
+++ b/src/components/VContentSwitcher/VSearchTypeButton.vue
@@ -1,11 +1,7 @@
<template>
<VButton
- class="group flex flex-row py-2 text-sr font-semibold md:text-base"
- :class="[
- sizeClasses,
- isHeaderScrolled ? 'max-w-[10rem] sm:max-w-[20rem] md:max-w-[16rem]' : '',
- ]"
- :variant="buttonVariant"
+ class="group flex h-12 w-12 flex-row xl:w-auto xl:px-2 xl:ps-3"
+ variant="action-menu"
size="disabled"
:aria-label="buttonLabel"
v-bind="a11yProps"
@@ -13,97 +9,61 @@
>
<VIcon :icon-path="icon" />
<span
- class="md:block md:truncate md:ms-2 md:text-start"
- :class="isHeaderScrolled ? 'hidden' : 'block truncate ms-2 text-start'"
+ class="label-regular hidden truncate xl:block xl:ms-2 xl:text-start"
>{{ buttonLabel }}</span
>
- <VIcon
- class="hidden text-dark-charcoal-40 group-hover:text-white md:block md:ms-2"
- :icon-path="caretDownIcon"
- />
+ <VIcon class="hidden xl:block xl:ms-2" :icon-path="caretDownIcon" />
</VButton>
</template>
<script lang="ts">
-import {
- computed,
- defineComponent,
- inject,
- PropType,
- ref,
-} from '@nuxtjs/composition-api'
+import { computed, defineComponent } from '@nuxtjs/composition-api'
-import { ALL_MEDIA, type SearchType } from '~/constants/media'
-import type { ButtonVariant } from '~/types/button'
+import { ALL_MEDIA, AUDIO, IMAGE, MODEL_3D, VIDEO } from '~/constants/media'
import useSearchType from '~/composables/use-search-type'
import { useI18n } from '~/composables/use-i18n'
-import { isMinScreen } from '~/composables/use-media-query'
import VIcon from '~/components/VIcon/VIcon.vue'
import VButton from '~/components/VButton.vue'
import caretDownIcon from '~/assets/icons/caret-down.svg'
+const labels = {
+ [ALL_MEDIA]: 'search-type.all',
+ [IMAGE]: 'search-type.image',
+ [AUDIO]: 'search-type.audio',
+ [VIDEO]: 'search-type.video',
+ [MODEL_3D]: 'search-type.model-3d',
+}
+
+/**
+ * This is the search type button that appears in the header, not on the homepage.
+ */
export default defineComponent({
name: 'VSearchTypeButton',
components: { VButton, VIcon },
props: {
a11yProps: {
type: Object,
- required: true,
- },
- activeItem: {
- type: String as PropType<SearchType>,
- default: ALL_MEDIA,
- },
- type: {
- type: String as PropType<'header' | 'searchbar'>,
- default: 'header',
+ default: () => ({
+ 'aria-expanded': false,
+ 'aria-haspopup': 'dialog',
+ }),
},
},
- setup(props) {
+ setup() {
const i18n = useI18n()
- const isHeaderScrolled = inject('isHeaderScrolled', ref(null))
- const isMinScreenMd = isMinScreen('md', { shouldPassInSSR: true })
+ const { icons, activeType } = useSearchType()
+
+ const activeItem = computed(() => activeType.value)
- const { icons, activeType: activeItem } = useSearchType()
- const isIconButton = computed(
- () => isHeaderScrolled?.value && !isMinScreenMd.value
- )
- const sizeClasses = computed(() => {
- if (props.type === 'searchbar') {
- return 'h-12 px-2'
- } else if (isIconButton.value) {
- return 'w-10 h-10'
- } else {
- /**
- When there is a caret down icon (on 'md' screens), paddings are balanced,
- without it, paddings need to be adjusted.
- */
- return 'ps-2 pe-3 md:px-2'
- }
- })
+ const buttonLabel = computed(() => i18n.t(labels[activeItem.value]))
- const buttonVariant = computed<ButtonVariant>(() => {
- if (props.type === 'searchbar') {
- return 'action-menu'
- } else {
- return isMinScreenMd.value && !isHeaderScrolled?.value
- ? 'action-menu-bordered'
- : 'action-menu'
- }
- })
- const buttonLabel = computed(() => {
- return i18n.t(`search-type.${props.activeItem}`)
- })
+ const icon = computed(() => icons[activeItem.value])
return {
- buttonVariant,
- sizeClasses,
buttonLabel,
caretDownIcon,
- isHeaderScrolled,
- isMinScreenMd,
- icon: computed(() => icons[activeItem.value]),
+ icon,
}
},
})
diff --git a/src/components/VContentSwitcher/meta/VSearchTypeButton.stories.mdx b/src/components/VContentSwitcher/meta/VSearchTypeButton.stories.mdx
new file mode 100644
index 000000000..5b8244e47
--- /dev/null
+++ b/src/components/VContentSwitcher/meta/VSearchTypeButton.stories.mdx
@@ -0,0 +1,102 @@
+import {
+ ArgsTable,
+ Canvas,
+ Description,
+ Meta,
+ Story,
+} from '@storybook/addon-docs'
+
+import allIcon from '~/assets/icons/all-content.svg'
+import audioIcon from '~/assets/icons/audio-content.svg'
+import imageIcon from '~/assets/icons/image-content.svg'
+import videoIcon from '~/assets/icons/video-content.svg'
+import model3dIcon from '~/assets/icons/model-3d.svg'
+
+export const icons = {
+ [ALL_MEDIA]: allIcon,
+ [AUDIO]: audioIcon,
+ [IMAGE]: imageIcon,
+ [VIDEO]: videoIcon,
+ [MODEL_3D]: model3dIcon,
+}
+
+import VSearchTypeButton from '~/components/VContentSwitcher/VSearchTypeButton.vue'
+import {
+ ALL_MEDIA,
+ AUDIO,
+ IMAGE,
+ MODEL_3D,
+ supportedSearchTypes,
+ VIDEO,
+} from '~/constants/media'
+import useSearchType from '~/composables/use-search-type'
+
+<Meta
+ title="Components/VContentSwitcher/VSearchTypeButton"
+ components={VSearchTypeButton}
+ argTypes={{
+ activeItem: {
+ defaultValue: 'all',
+ options: supportedSearchTypes,
+ control: 'select',
+ },
+ pressed: {
+ control: 'boolean',
+ },
+ click: {
+ action: 'click',
+ },
+ }}
+/>
+
+export const Template = (args) => ({
+ template: `<VSearchTypeButton v-bind="args" v-on="args" />`,
+ components: { VSearchTypeButton },
+ setup() {
+ const searchType = useSearchType()
+ searchType.setActiveType(args.activeItem)
+ const a11yProps = { 'aria-haspopup': 'dialog' }
+ if (args.pressed) {
+ a11yProps['aria-expanded'] = true
+ }
+ args.a11yProps = a11yProps
+ return { args }
+ },
+})
+
+# VSearchTypeButton
+
+<Description of={VSearchTypeButton} />
+
+<ArgsTable of={VSearchTypeButton} />
+
+<Canvas>
+ <Story
+ height="480px"
+ name="Default"
+ parameters={{
+ viewport: { defaultViewport: 'lg' },
+ }}
+ args={{
+ activeItem: ALL_MEDIA,
+ }}
+ >
+ {Template.bind({})}
+ </Story>
+</Canvas>
+
+<Canvas>
+ <Story
+ height="480px"
+ name="Large with text label"
+ parameters={{
+ viewport: { defaultViewport: 'lx' },
+ }}
+ args={{
+ activeItem: ALL_MEDIA,
+ a11yProps: { 'aria-pressed': true },
+ }}
+ >
+ {Template.bind({})}
+ </Story>
+</Canvas>
diff --git a/src/composables/use-search-type.ts b/src/composables/use-search-type.ts
index 150734a6b..6ba76343a 100644
--- a/src/composables/use-search-type.ts
+++ b/src/composables/use-search-type.ts
@@ -26,7 +26,7 @@ const icons = {
[IMAGE]: imageIcon,
[VIDEO]: videoIcon,
[MODEL_3D]: model3dIcon,
-}
+} as const
export default function useSearchType() {
const activeType = computed(() => useSearchStore().searchType)
@@ -54,3 +54,5 @@ export default function useSearchType() {
additionalTypes,
}
}
+
+export type UseSearchTypeReturn = ReturnType<typeof useSearchType>
diff --git a/test/storybook/playwright.config.ts b/test/storybook/playwright.config.ts
index 5aafd9e12..df0d1f896 100644
--- a/test/storybook/playwright.config.ts
+++ b/test/storybook/playwright.config.ts
@@ -21,6 +21,12 @@ const config: PlaywrightTestConfig = {
trace: 'retain-on-failure',
},
timeout: 2 * 60 * 1e3,
+ expect: {
+ toMatchSnapshot: {
+ // To avoid flaky tests, we allow a small amount of pixel difference.
+ maxDiffPixelRatio: 0.02,
+ },
+ },
}
export default config
diff --git a/test/storybook/visual-regression/v-search-type-button.spec.ts b/test/storybook/visual-regression/v-search-type-button.spec.ts
new file mode 100644
index 000000000..3db2779eb
--- /dev/null
+++ b/test/storybook/visual-regression/v-search-type-button.spec.ts
@@ -0,0 +1,83 @@
+import { test } from '@playwright/test'
+
+import breakpoints from '~~/test/playwright/utils/breakpoints'
+
+const defaultUrl =
+ '/iframe.html?id=components-vcontentswitcher-vsearchtypebutton--default-story'
+const pressedUrl = `${defaultUrl}&args=pressed:true`
+
+const searchTypeButtonLocator = 'button[aria-haspopup="dialog"]'
+
+test.describe('VSearchTypeButton', () => {
+ breakpoints.describeLg(({ expectSnapshot }) => {
+ test('resting', async ({ page }) => {
+ await page.goto(defaultUrl)
+ await expectSnapshot(
+ 'v-search-type-button-at-rest',
+ page.locator(searchTypeButtonLocator)
+ )
+ })
+
+ test('hovered', async ({ page }) => {
+ await page.goto(defaultUrl)
+ await page.hover(searchTypeButtonLocator)
+ await expectSnapshot(
+ 'v-search-type-button-hovered',
+ page.locator(searchTypeButtonLocator)
+ )
+ })
+
+ test('pressed', async ({ page }) => {
+ await page.goto(pressedUrl)
+ await expectSnapshot(
+ 'v-search-type-button-pressed',
+ page.locator(searchTypeButtonLocator)
+ )
+ })
+
+ test('pressed-hovered', async ({ page }) => {
+ await page.goto(pressedUrl)
+ await page.hover(searchTypeButtonLocator)
+ await expectSnapshot(
+ 'v-search-type-button-pressed-hovered',
+ page.locator(searchTypeButtonLocator)
+ )
+ })
+ })
+
+ breakpoints.describeXl(({ expectSnapshot }) => {
+ test('resting', async ({ page }) => {
+ await page.goto(defaultUrl)
+ await expectSnapshot(
+ 'v-search-type-button-with-text-at-rest',
+ page.locator(searchTypeButtonLocator)
+ )
+ })
+
+ test('hovered', async ({ page }) => {
+ await page.goto(defaultUrl)
+ await page.hover(searchTypeButtonLocator)
+ await expectSnapshot(
+ 'v-search-type-button-with-text-hovered',
+ page.locator(searchTypeButtonLocator)
+ )
+ })
+
+ test('pressed', async ({ page }) => {
+ await page.goto(pressedUrl)
+ await expectSnapshot(
+ 'v-search-type-button-with-text-pressed',
+ page.locator(searchTypeButtonLocator)
+ )
+ })
+
+ test('pressed-hovered', async ({ page }) => {
+ await page.goto(pressedUrl)
+ await page.hover(searchTypeButtonLocator)
+ await expectSnapshot(
+ 'v-search-type-button-with-text-pressed-hovered',
+ page.locator(searchTypeButtonLocator)
+ )
+ })
+ })
+})
diff --git a/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-at-rest-lg-linux.png b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-at-rest-lg-linux.png
new file mode 100644
index 000000000..d981144bb
Binary files /dev/null and b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-at-rest-lg-linux.png differ
diff --git a/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-hovered-lg-linux.png b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-hovered-lg-linux.png
new file mode 100644
index 000000000..096376af8
Binary files /dev/null and b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-hovered-lg-linux.png differ
diff --git a/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-pressed-hovered-lg-linux.png b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-pressed-hovered-lg-linux.png
new file mode 100644
index 000000000..739ba19ce
Binary files /dev/null and b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-pressed-hovered-lg-linux.png differ
diff --git a/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-pressed-lg-linux.png b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-pressed-lg-linux.png
new file mode 100644
index 000000000..bfcd1b4f4
Binary files /dev/null and b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-pressed-lg-linux.png differ
diff --git a/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-with-text-at-rest-xl-linux.png b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-with-text-at-rest-xl-linux.png
new file mode 100644
index 000000000..e6b0a3844
Binary files /dev/null and b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-with-text-at-rest-xl-linux.png differ
diff --git a/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-with-text-hovered-xl-linux.png b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-with-text-hovered-xl-linux.png
new file mode 100644
index 000000000..f6356a862
Binary files /dev/null and b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-with-text-hovered-xl-linux.png differ
diff --git a/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-with-text-pressed-hovered-xl-linux.png b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-with-text-pressed-hovered-xl-linux.png
new file mode 100644
index 000000000..3ec88bc6b
Binary files /dev/null and b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-with-text-pressed-hovered-xl-linux.png differ
diff --git a/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-with-text-pressed-xl-linux.png b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-with-text-pressed-xl-linux.png
new file mode 100644
index 000000000..ba8c795de
Binary files /dev/null and b/test/storybook/visual-regression/v-search-type-button.spec.ts-snapshots/v-search-type-button-with-text-pressed-xl-linux.png differ