Skip to content

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

Add an updated `VAudioControl` component (#2929)

Olga Bulatfc1503220216
diff --git a/frontend/src/assets/svg/raw/pause.svg b/frontend/src/assets/svg/raw/pause.svg
index 1cf4e31fb..dfce4c84a 100644
--- a/frontend/src/assets/svg/raw/pause.svg
+++ b/frontend/src/assets/svg/raw/pause.svg
@@ -1,5 +1,5 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<g id="icon" fill="currentColor">
- <path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"/>
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M10 7H7V17H10V7ZM17 7H14V17H17V7Z" />
</g>
</svg>
diff --git a/frontend/src/assets/svg/raw/play.svg b/frontend/src/assets/svg/raw/play.svg
index 4989da2a3..b511e2300 100644
--- a/frontend/src/assets/svg/raw/play.svg
+++ b/frontend/src/assets/svg/raw/play.svg
@@ -1,5 +1,5 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<g id="icon" fill="currentColor">
- <path d="M8 5v14l11-7L8 5z"/>
+ <path d="M9 17L16 12L9 7V17Z" />
</g>
</svg>
diff --git a/frontend/src/assets/svg/raw/replay.svg b/frontend/src/assets/svg/raw/replay.svg
index 2998bac9d..bec742eeb 100644
--- a/frontend/src/assets/svg/raw/replay.svg
+++ b/frontend/src/assets/svg/raw/replay.svg
@@ -1,3 +1,5 @@
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
- <path id="icon" fill-rule="evenodd" clip-rule="evenodd" d="M12 8V11L8 7L12 3V6C15.3137 6 18 8.68629 18 12C18 15.3137 15.3137 18 12 18C8.68629 18 6 15.3137 6 12H8C8 14.2091 9.79086 16 12 16C14.2091 16 16 14.2091 16 12C16 9.79086 14.2091 8 12 8Z" fill="currentcolor"/>
+ <g id="icon" fill="currentColor">
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M12 8V11L8 7L12 3V6C15.3137 6 18 8.68629 18 12C18 15.3137 15.3137 18 12 18C8.68629 18 6 15.3137 6 12H8C8 14.2091 9.79086 16 12 16C14.2091 16 16 14.2091 16 12C16 9.79086 14.2091 8 12 8Z" />
+ </g>
</svg>
diff --git a/frontend/src/components/VAudioTrack/VAudioControl.vue b/frontend/src/components/VAudioTrack/VAudioControl.vue
new file mode 100644
index 000000000..5820c8cf2
--- /dev/null
+++ b/frontend/src/components/VAudioTrack/VAudioControl.vue
@@ -0,0 +1,196 @@
+<template>
+ <VIconButton
+ :tabindex="isTabbable ? 0 : -1"
+ class="play-pause"
+ :size="buttonSize"
+ variant="filled-dark"
+ :icon-props="icon === undefined ? undefined : { name: icon, size: iSize }"
+ :label="$t(label)"
+ :connections="connections"
+ @click.stop.prevent="handleClick"
+ @mousedown="handleMouseDown"
+ >
+ <template #default>
+ <svg
+ v-if="isLoading"
+ class="loading p-2"
+ :class="`w-${iSize} h-${iSize}`"
+ xmlns="http://www.w3.org/2000/svg"
+ overflow="visible"
+ viewBox="0 0 12 12"
+ >
+ <circle cx="6" cy="6" r="6" vector-effect="non-scaling-stroke" />
+ <path
+ d="m 6 0 a 6 6 0 0 1 6 6"
+ stroke="white"
+ vector-effect="non-scaling-stroke"
+ />
+ </svg>
+ </template>
+ </VIconButton>
+</template>
+
+<script lang="ts">
+import { computed, defineComponent, PropType } from "vue"
+
+import {
+ AudioLayout,
+ audioLayouts,
+ AudioStatus,
+ audioStatuses,
+ statusVerbMap,
+} from "~/constants/audio"
+import { defineEvent } from "~/types/emits"
+import type { ButtonConnections } from "~/types/button"
+
+import VIconButton from "~/components/VIconButton/VIconButton.vue"
+
+const statusIconMap = {
+ playing: "pause",
+ paused: "play",
+ played: "replay",
+ loading: undefined,
+} as const
+
+const layoutConnectionsMap: Record<AudioLayout, ButtonConnections> = {
+ row: "end",
+ global: "all",
+ box: "none",
+ full: "none",
+} as const
+
+/**
+ * The mapping of play-pause control sizes to the VIconButton sizes
+ * and the sizes of the contained icon.
+ */
+const sizes = {
+ small: { button: "small", icon: 6 },
+ medium: { button: "large", icon: 8 },
+ large: { button: "larger", icon: 10 },
+} as const
+
+/**
+ * Displays the control for switching between the playing and paused states of
+ * a media file.
+ */
+export default defineComponent({
+ name: "VAudioControl",
+ components: { VIconButton },
+ model: {
+ prop: "status",
+ event: "toggle",
+ },
+ props: {
+ /**
+ * the current play status of the audio
+ */
+ status: {
+ type: String as PropType<AudioStatus>,
+ required: true,
+ validator: (val: string) =>
+ (audioStatuses as readonly string[]).includes(val),
+ },
+ /**
+ * The size of the button. The size affects both the size of the button
+ * itself and the icon inside it.
+ */
+ size: {
+ type: String as PropType<"small" | "medium" | "large">,
+ required: true,
+ validator: (val: string) => ["small", "medium", "large"].includes(val),
+ },
+ /**
+ * The parent audio layout currently in use. The connections are determined
+ * by the layout and the size of the button.
+ */
+ layout: {
+ type: String as PropType<AudioLayout>,
+ default: "full",
+ validator: (val: string) =>
+ (audioLayouts as readonly string[]).includes(val),
+ },
+ /**
+ * Whether the play-pause button can be focused by using the `Tab` key
+ */
+ isTabbable: {
+ type: Boolean,
+ default: true,
+ },
+ },
+ emits: {
+ toggle: defineEvent<["paused" | "playing"]>(),
+ },
+ setup(props, { emit }) {
+ const isPlaying = computed(() => props.status === "playing")
+ const isLoading = computed(() => props.status === "loading")
+ /**
+ * Get the button label based on the current status of the player.
+ */
+ const label = computed(() => `playPause.${statusVerbMap[props.status]}`)
+ /**
+ * Get the button icon based on the current status of the player.
+ */
+ const icon = computed(() => statusIconMap[props.status])
+
+ /**
+ * Set the connections (none-rounded corners) for the button based on the layout.
+ */
+ const connections = computed(() => {
+ return props.layout === "row" && props.size === "small"
+ ? "none"
+ : layoutConnectionsMap[props.layout]
+ })
+
+ /** Convert the `play-pause` sizes to `VIconButton` sizes */
+ const buttonSize = computed(() => sizes[props.size].button)
+
+ const iSize = computed(() => sizes[props.size].icon)
+
+ const handleMouseDown = (event: MouseEvent) => {
+ if (!props.isTabbable) event.preventDefault() // to prevent focus
+ }
+ const handleClick = () => {
+ emit("toggle", isPlaying.value || isLoading.value ? "paused" : "playing")
+ }
+
+ return {
+ label,
+ icon,
+ connections,
+ buttonSize,
+ iSize,
+ isLoading,
+
+ handleClick,
+ handleMouseDown,
+ }
+ },
+})
+</script>
+
+<style scoped>
+@keyframes spinAnimation {
+ from {
+ transform: rotate(0deg);
+ }
+ to {
+ transform: rotate(360deg);
+ }
+}
+
+.loading circle,
+.loading path {
+ fill: transparent;
+ stroke-width: 2px;
+}
+
+.loading circle {
+ stroke: #595258;
+}
+
+.loading path {
+ stroke-linecap: round;
+ transform-origin: 50% 50%;
+ animation: spinAnimation 1.4s linear infinite both;
+}
+</style>
diff --git a/frontend/src/components/VAudioTrack/layouts/VBoxLayout.vue b/frontend/src/components/VAudioTrack/layouts/VBoxLayout.vue
index 6828ccdcd..d9426617a 100644
--- a/frontend/src/components/VAudioTrack/layouts/VBoxLayout.vue
+++ b/frontend/src/components/VAudioTrack/layouts/VBoxLayout.vue
@@ -93,7 +93,7 @@ export default defineComponent({
})
</script>
-<style>
+<style scoped>
.box-track .waveform {
@apply flex-grow;
--waveform-background-color: theme("colors.yellow");
diff --git a/frontend/src/components/VAudioTrack/meta/VAudioControl.stories.mdx b/frontend/src/components/VAudioTrack/meta/VAudioControl.stories.mdx
new file mode 100644
index 000000000..68978e73c
--- /dev/null
+++ b/frontend/src/components/VAudioTrack/meta/VAudioControl.stories.mdx
@@ -0,0 +1,107 @@
+import {
+ ArgsTable,
+ Canvas,
+ Description,
+ Meta,
+ Story,
+} from "@storybook/addon-docs"
+
+import { audioStatuses } from "~/constants/audio"
+import VAudioControl from "~/components/VAudioTrack/VAudioControl.vue"
+
+<Meta
+ title="Components/Audio track/Audio control"
+ components={VAudioControl}
+ argTypes={{
+ status: {
+ options: audioStatuses,
+ control: "select",
+ },
+ size: {
+ options: ["small", "medium", "large"],
+ control: "select",
+ },
+ toggle: {
+ action: "toggle",
+ },
+ }}
+/>
+
+export const Template = (args) => ({
+ template: `<VAudioControl :size="args.size" :status="args.status" v-bind="args" v-on="args"/>`,
+ components: { VAudioControl },
+ setup() {
+ return { args }
+ },
+})
+
+# Audio control
+
+<Description of={VAudioControl} />
+
+<ArgsTable of={VAudioControl} />
+
+It should be noted that the component emits the `toggle` event with the opposite
+value of its `status` state.
+
+## Size
+
+The button dimensions can be set by passing the `size` prop.
+
+<Canvas>
+ <Story
+ name="Default"
+ args={{
+ status: "playing",
+ size: "large",
+ }}
+ >
+ {Template.bind({})}
+ </Story>
+</Canvas>
+
+## Two-way binding
+
+The recommended way to use it is with `v-model` mapping to a `String` that can
+either be `'playing'`, `'paused'` or `'played'`.
+
+It should be noted that both paused and played states change to playing when
+toggled whereas the playing state toggles to paused. The played state can only
+be achieved by playing till the end of a track.
+
+export const vModelTemplate = () => ({
+ template: `
+ <div>
+ <VAudioControl v-model="status" size="small" status="paused" />
+ {{ status }}
+ </div>
+ `,
+ components: { VAudioControl },
+ data() {
+ return {
+ status: "played",
+ }
+ },
+})
+
+<Canvas>
+ <Story name="v-model">{vModelTemplate.bind({})}</Story>
+</Canvas>
+
+## Fall-through attributes
+
+The component is, indirectly, a transparent wrapper over `<button>` so all
+attributes of the button element can be applied to it, e.g. `disabled`.
+
+<Canvas>
+ <Story
+ name="Disabled"
+ args={{
+ disabled: true,
+ status: "playing",
+ size: "medium",
+ }}
+ >
+ {Template.bind({})}
+ </Story>
+</Canvas>
diff --git a/frontend/src/components/VButton.vue b/frontend/src/components/VButton.vue
index a8244289a..9a26eba5c 100644
--- a/frontend/src/components/VButton.vue
+++ b/frontend/src/components/VButton.vue
@@ -327,6 +327,11 @@ export default VButton
.icon-end-large {
@apply pe-4;
}
+
+.size-larger-icon-only {
+ @apply h-16 w-16;
+}
+
.icon-only {
@apply flex-none;
}
@@ -340,7 +345,7 @@ a.button {
}
.filled-dark {
- @apply border-tx bg-dark-charcoal text-white hover:bg-dark-charcoal-90 hover:text-white;
+ @apply border-tx bg-dark-charcoal text-white hover:bg-dark-charcoal-90 hover:text-white disabled:opacity-70;
}
.filled-gray {
diff --git a/frontend/src/components/VIconButton/VIconButton.vue b/frontend/src/components/VIconButton/VIconButton.vue
index 4744ec4e8..bd5ed502a 100644
--- a/frontend/src/components/VIconButton/VIconButton.vue
+++ b/frontend/src/components/VIconButton/VIconButton.vue
@@ -3,24 +3,29 @@
:aria-label="label"
:size="size"
:variant="variant"
+ :connections="connections"
class="icon-button"
icon-only
v-on="$listeners"
>
- <slot name="default" :icon-size="6" />
+ <slot name="default" :icon-size="iconSize" />
<VIcon
v-if="iconProps"
class="pointer-events-none"
- :size="6"
+ :size="iconSize"
v-bind="iconProps"
/>
</VButton>
</template>
<script lang="ts">
-import { defineComponent, PropType } from "vue"
+import { computed, defineComponent, PropType } from "vue"
-import type { ButtonSize, ButtonVariant } from "~/types/button"
+import type {
+ ButtonConnections,
+ ButtonSize,
+ ButtonVariant,
+} from "~/types/button"
import VIcon, { type IconProps } from "~/components/VIcon/VIcon.vue"
import VButton from "~/components/VButton.vue"
@@ -57,6 +62,12 @@ export default defineComponent({
type: Object as PropType<IconProps>,
required: false,
},
+ /**
+ * Connections to pass down to the `VButton` component nested inside the button.
+ */
+ connections: {
+ type: String as PropType<ButtonConnections>,
+ },
/**
* The label used for accessibility purposes.
*/
@@ -65,5 +76,11 @@ export default defineComponent({
required: true,
},
},
+ setup(props) {
+ const iconSize = computed(() => props.iconProps?.size ?? 6)
+ return {
+ iconSize,
+ }
+ },
})
</script>
diff --git a/frontend/src/types/button.ts b/frontend/src/types/button.ts
index 394076d81..ef8c3bb31 100644
--- a/frontend/src/types/button.ts
+++ b/frontend/src/types/button.ts
@@ -27,7 +27,10 @@ export type ButtonVariant = (typeof buttonVariants)[number]
export const baseButtonSizes = ["large", "medium", "small"] as const
export type BaseButtonSize = (typeof baseButtonSizes)[number]
-export const buttonSizes = [...baseButtonSizes, "disabled"] as const
+/**
+ * `larger` is only used for the `VAudioControl` button component.
+ */
+export const buttonSizes = [...baseButtonSizes, "disabled", "larger"] as const
export type ButtonSize = (typeof buttonSizes)[number]
export const buttonTypes = ["button", "submit", "reset"] as const
diff --git a/frontend/test/playwright/visual-regression/components/global-audio-player.spec.ts-snapshots/global-audio-player-on-search-ltr-png-xs-linux.png b/frontend/test/playwright/visual-regression/components/global-audio-player.spec.ts-snapshots/global-audio-player-on-search-ltr-png-xs-linux.png
index f1e83a954..b5220822c 100644
Binary files a/frontend/test/playwright/visual-regression/components/global-audio-player.spec.ts-snapshots/global-audio-player-on-search-ltr-png-xs-linux.png and b/frontend/test/playwright/visual-regression/components/global-audio-player.spec.ts-snapshots/global-audio-player-on-search-ltr-png-xs-linux.png differ
diff --git a/frontend/test/playwright/visual-regression/components/global-audio-player.spec.ts-snapshots/global-audio-player-on-search-rtl-png-xs-linux.png b/frontend/test/playwright/visual-regression/components/global-audio-player.spec.ts-snapshots/global-audio-player-on-search-rtl-png-xs-linux.png
index 32271e102..c71ca610a 100644
Binary files a/frontend/test/playwright/visual-regression/components/global-audio-player.spec.ts-snapshots/global-audio-player-on-search-rtl-png-xs-linux.png and b/frontend/test/playwright/visual-regression/components/global-audio-player.spec.ts-snapshots/global-audio-player-on-search-rtl-png-xs-linux.png differ
diff --git a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-page-rtl-lg-linux.png b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-page-rtl-lg-linux.png
index dcb4d405b..c39e1a3dd 100644
Binary files a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-page-rtl-lg-linux.png and b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-page-rtl-lg-linux.png differ
diff --git a/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-2xl-linux.png b/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-2xl-linux.png
index 88ddd40c7..277213649 100644
Binary files a/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-2xl-linux.png and b/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-2xl-linux.png differ
diff --git a/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-lg-linux.png b/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-lg-linux.png
index a9eaba5d3..cb94dde15 100644
Binary files a/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-lg-linux.png and b/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-lg-linux.png differ
diff --git a/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-md-linux.png b/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-md-linux.png
index bc3e23422..cddd084ec 100644
Binary files a/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-md-linux.png and b/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-md-linux.png differ
diff --git a/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-xl-linux.png b/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-xl-linux.png
index 4f2ab8fc4..4602c2496 100644
Binary files a/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-xl-linux.png and b/frontend/test/playwright/visual-regression/pages/search-with-banners.spec.ts-snapshots/page-with-all-banners-xl-linux.png differ