Skip to content

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

Extract `use-audio-snackbar` component (#2722)

Olga Bulat31a3bde96ea1
diff --git a/frontend/src/components/VSearchResultsGrid/VAllResultsGrid.vue b/frontend/src/components/VSearchResultsGrid/VAllResultsGrid.vue
index ce0484ef2..662874d59 100644
--- a/frontend/src/components/VSearchResultsGrid/VAllResultsGrid.vue
+++ b/frontend/src/components/VSearchResultsGrid/VAllResultsGrid.vue
@@ -13,7 +13,7 @@
:to="contentLinkPath(mediaType)"
/>
</div>
- <VSnackbar size="large" :is-visible="isSnackbarVisible">
+ <VSnackbar size="large" :is-visible="snackbar.isVisible.value">
<i18n path="allResults.snackbar.text" tag="p">
<template #spacebar>
<kbd class="font-sans">{{ $t(`allResults.snackbar.spacebar`) }}</kbd>
@@ -50,7 +50,7 @@
:audio="item"
:search-term="searchTerm"
@interacted="handleInteraction"
- @focus="showSnackbar"
+ @focus="snackbar.show"
/>
</template>
</ol>
@@ -70,6 +70,7 @@ import { isDetail } from "~/types/media"
import type { AudioInteractionData } from "~/types/analytics"
import { useAnalytics } from "~/composables/use-analytics"
+import { useAudioSnackbar } from "~/composables/use-audio-snackbar"
import { useI18n } from "~/composables/use-i18n"
import type { SupportedMediaType } from "~/constants/media"
@@ -128,19 +129,14 @@ export default defineComponent({
() => fetchState.value.isFinished && allMedia.value.length === 0
)
+ const snackbar = useAudioSnackbar()
+
const uiStore = useUiStore()
- const isSnackbarVisible = computed(() => uiStore.areInstructionsVisible)
- const showSnackbar = () => {
- uiStore.showInstructionsSnackbar()
- }
- const hideSnackbar = () => {
- uiStore.hideInstructionsSnackbar()
- }
const isSidebarVisible = computed(() => uiStore.isFilterVisible)
const handleInteraction = (data: AudioInteractionData) => {
- hideSnackbar()
+ snackbar.hide()
sendCustomEvent("AUDIO_INTERACTION", {
...data,
component: "VAllResultsGrid",
@@ -161,9 +157,8 @@ export default defineComponent({
isSidebarVisible,
- isSnackbarVisible,
- showSnackbar,
handleInteraction,
+ snackbar,
isDetail,
}
diff --git a/frontend/src/components/VSnackbar.vue b/frontend/src/components/VSnackbar.vue
index 41cec5b36..168bb5aef 100644
--- a/frontend/src/components/VSnackbar.vue
+++ b/frontend/src/components/VSnackbar.vue
@@ -1,6 +1,6 @@
<template>
<div
- role="alert"
+ :role="isVisible ? 'alert' : undefined"
:class="[
$style.snackbar,
$style[size],
diff --git a/frontend/src/composables/use-audio-snackbar.ts b/frontend/src/composables/use-audio-snackbar.ts
new file mode 100644
index 000000000..18a541eb8
--- /dev/null
+++ b/frontend/src/composables/use-audio-snackbar.ts
@@ -0,0 +1,39 @@
+import { computed, ref } from "vue"
+
+import { useUiStore } from "~/stores/ui"
+
+/**
+ * Composable that handles showing and hiding of the snackbar
+ * with instructions for audio interactions.
+ *
+ * Shows the snackbar when the user focuses the first audio track.
+ * Hides the snackbar when the user interacts with it (seeks or clicks on play button).
+ */
+export const useAudioSnackbar = () => {
+ const isMouseDown = ref(false)
+ const handleMouseDown = () => {
+ isMouseDown.value = true
+ }
+
+ const show = () => {
+ if (isMouseDown.value) {
+ // The audio player was clicked to open the single result view, not
+ // focused via keyboard.
+ isMouseDown.value = false
+ } else {
+ useUiStore().showInstructionsSnackbar()
+ }
+ }
+ const hide = () => {
+ useUiStore().hideInstructionsSnackbar()
+ }
+
+ const isVisible = computed(() => useUiStore().areInstructionsVisible)
+
+ return {
+ isVisible,
+ show,
+ hide,
+ handleMouseDown,
+ }
+}
diff --git a/frontend/src/pages/search/audio.vue b/frontend/src/pages/search/audio.vue
index eb3ea1761..014aafabf 100644
--- a/frontend/src/pages/search/audio.vue
+++ b/frontend/src/pages/search/audio.vue
@@ -1,7 +1,7 @@
<template>
<!-- Negative margin compensates for the `p-4` padding in row layout. -->
<section class="-mx-2 md:-mx-4">
- <VSnackbar size="large" :is-visible="isSnackbarVisible">
+ <VSnackbar size="large" :is-visible="snackbar.isVisible.value">
<i18n path="audioResults.snackbar.text" tag="p">
<template
v-for="keyboardKey in ['spacebar', 'left', 'right']"
@@ -26,8 +26,8 @@
layout="row"
:search-term="searchTerm"
@interacted="handleInteraction"
- @mousedown="handleMouseDown"
- @focus="showSnackbar"
+ @mousedown="snackbar.handleMouseDown"
+ @focus="snackbar.show"
/>
</li>
</ol>
@@ -36,12 +36,13 @@
</template>
<script lang="ts">
-import { computed, defineComponent, ref, inject } from "vue"
+import { computed, defineComponent, inject } from "vue"
import { useSearchStore } from "~/stores/search"
import { useUiStore } from "~/stores/ui"
import { useAnalytics } from "~/composables/use-analytics"
+import { useAudioSnackbar } from "~/composables/use-audio-snackbar"
import { IsSidebarVisibleKey } from "~/types/provides"
import type { AudioInteractionData } from "~/types/analytics"
@@ -79,26 +80,10 @@ export default defineComponent({
!isDesktopLayout.value ? "s" : filterVisibleRef.value ? "l" : "m"
)
- const isMouseDown = ref(false)
- const handleMouseDown = () => {
- isMouseDown.value = true
- }
-
- const isSnackbarVisible = computed(() => uiStore.areInstructionsVisible)
- const showSnackbar = () => {
- if (isMouseDown.value) {
- // The audio player was clicked to open the single result view, not
- // focused via keyboard.
- isMouseDown.value = false
- } else {
- uiStore.showInstructionsSnackbar()
- }
- }
- const hideSnackbar = () => {
- uiStore.hideInstructionsSnackbar()
- }
+ const snackbar = useAudioSnackbar()
const handleInteraction = (data: AudioInteractionData) => {
+ snackbar.hide()
sendCustomEvent("AUDIO_INTERACTION", {
...data,
component: "AudioSearch",
@@ -110,12 +95,8 @@ export default defineComponent({
results,
audioTrackSize,
- handleMouseDown,
-
- isSnackbarVisible,
- showSnackbar,
- hideSnackbar,
handleInteraction,
+ snackbar,
}
},
})
diff --git a/frontend/test/playwright/e2e/all-results-keyboard.spec.ts b/frontend/test/playwright/e2e/all-results-keyboard.spec.ts
index 950402e19..e6a81f93b 100644
--- a/frontend/test/playwright/e2e/all-results-keyboard.spec.ts
+++ b/frontend/test/playwright/e2e/all-results-keyboard.spec.ts
@@ -1,50 +1,21 @@
-import { test, expect, Page } from "@playwright/test"
+import { expect, test } from "@playwright/test"
import audio from "~~/test/playwright/utils/audio"
+import { preparePageForTests } from "~~/test/playwright/utils/navigation"
-import { keycodes } from "~/constants/key-codes"
-
-const walkToNextOfType = async (type: "image" | "audio", page: Page) => {
- const isActiveElementOfType = () => {
- return page.evaluate(
- ([contextType]) => {
- const regex = new RegExp(`^${contextType}:`, "i")
- const element = document.activeElement as HTMLAnchorElement | null
- const toTest = element?.title || element?.ariaLabel || ""
- return regex.test(toTest)
- },
- [type]
- )
- }
-
- while (!(await isActiveElementOfType())) {
- await page.keyboard.press(keycodes.Tab)
- }
-}
-
-const walkToType = async (type: "image" | "audio", page: Page) => {
- // Go to skip to content button
- await page.keyboard.press(keycodes.Tab)
- // Skip to content
- await page.keyboard.press(keycodes.Enter)
-
- await walkToNextOfType(type, page)
-}
+import {
+ locateFocusedResult,
+ walkToNextOfType,
+ walkToType,
+} from "~~/test/playwright/utils/search-results"
-const locateFocusedResult = async (page: Page) => {
- const href = await page.evaluate(
- () => (document.activeElement as HTMLAnchorElement | null)?.href
- )
- expect(href).toBeDefined()
- const url = new URL(href ?? "")
-
- return page.locator(`[href="${url.pathname}?q=birds"]`)
-}
+import { keycodes } from "~/constants/key-codes"
test.describe.configure({ mode: "parallel" })
test.describe("all results grid keyboard accessibility test", () => {
test.beforeEach(async ({ page }) => {
+ await preparePageForTests(page, "xl")
await page.goto("/search/?q=birds")
})
@@ -76,6 +47,28 @@ test.describe("all results grid keyboard accessibility test", () => {
)
})
+ test("should show instructions snackbar when focusing first audio", async ({
+ page,
+ }) => {
+ await walkToType("audio", page)
+
+ await expect(page.locator("[role=alert]")).toBeVisible()
+ })
+
+ test("should hide the instructions snackbar when interacted with audio", async ({
+ page,
+ }) => {
+ await walkToType("audio", page)
+
+ await expect(page.locator("[role=alert]")).toBeVisible()
+
+ const focusedResult = await locateFocusedResult(page)
+ const playButton = await audio.getInactive(focusedResult)
+ await playButton.click()
+
+ await expect(page.locator("[role=alert]")).toBeHidden()
+ })
+
test("should allow toggling audio playback via play/pause click", async ({
page,
}) => {
diff --git a/frontend/test/playwright/e2e/audio-results.spec.ts b/frontend/test/playwright/e2e/audio-results.spec.ts
new file mode 100644
index 000000000..ce35bb165
--- /dev/null
+++ b/frontend/test/playwright/e2e/audio-results.spec.ts
@@ -0,0 +1,38 @@
+import { expect, test } from "@playwright/test"
+
+import { preparePageForTests } from "~~/test/playwright/utils/navigation"
+import {
+ locateFocusedResult,
+ walkToType,
+} from "~~/test/playwright/utils/search-results"
+
+test.describe.configure({ mode: "parallel" })
+
+test.describe("all results grid keyboard accessibility test", () => {
+ test.beforeEach(async ({ page }) => {
+ await preparePageForTests(page, "xl")
+ await page.goto("/search/?q=birds")
+ })
+
+ test("should show instructions snackbar when focusing first audio", async ({
+ page,
+ }) => {
+ await walkToType("audio", page)
+
+ await expect(page.locator("[role=alert]")).toBeVisible()
+ })
+
+ test("should hide the instructions snackbar when interacted with audio", async ({
+ page,
+ }) => {
+ await walkToType("audio", page)
+
+ await expect(page.locator("[role=alert]")).toBeVisible()
+
+ const focusedResult = await locateFocusedResult(page)
+
+ await focusedResult.press("Space")
+
+ await expect(page.locator("[role=alert]")).toBeHidden()
+ })
+})
diff --git a/frontend/test/playwright/utils/search-results.ts b/frontend/test/playwright/utils/search-results.ts
new file mode 100644
index 000000000..ccfefeaa0
--- /dev/null
+++ b/frontend/test/playwright/utils/search-results.ts
@@ -0,0 +1,38 @@
+import { expect, Page } from "@playwright/test"
+
+import { keycodes } from "~/constants/key-codes"
+
+export const walkToNextOfType = async (type: "image" | "audio", page: Page) => {
+ const isActiveElementOfType = () => {
+ return page.evaluate(
+ ([contextType]) => {
+ const regex = new RegExp(`^${contextType}:`, "i")
+ const element = document.activeElement as HTMLAnchorElement | null
+ const toTest = element?.title || element?.ariaLabel || ""
+ return regex.test(toTest)
+ },
+ [type]
+ )
+ }
+
+ while (!(await isActiveElementOfType())) {
+ await page.keyboard.press(keycodes.Tab)
+ }
+}
+export const walkToType = async (type: "image" | "audio", page: Page) => {
+ // Go to skip to content button
+ await page.keyboard.press(keycodes.Tab)
+ // Skip to content
+ await page.keyboard.press(keycodes.Enter)
+
+ await walkToNextOfType(type, page)
+}
+export const locateFocusedResult = async (page: Page) => {
+ const href = await page.evaluate(
+ () => (document.activeElement as HTMLAnchorElement | null)?.href
+ )
+ expect(href).toBeDefined()
+ const url = new URL(href ?? "")
+
+ return page.locator(`[href="${url.pathname}?q=birds"]`)
+}