Skip to content

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

Improve focus styles for audio subcomponents (#459)

Dhruv Bhanushali630e295858f1
diff --git a/src/components/AudioTrack/PlayPause.vue b/src/components/AudioTrack/PlayPause.vue
index c0c01388a..1dfd8e3a6 100644
--- a/src/components/AudioTrack/PlayPause.vue
+++ b/src/components/AudioTrack/PlayPause.vue
@@ -1,16 +1,15 @@
<template>
- <button
- type="button"
- class="play-pause flex-shrink-0 flex items-center justify-center bg-dark-charcoal text-white transition-shadow duration-100 ease-linear disabled:opacity-70 focus:outline-none focus-visible:ring focus-visible:ring-offset-2 focus-visible:ring-pink"
+ <VIconButton
+ v-bind="$attrs"
+ class="play-pause flex-shrink-0 bg-dark-charcoal border-dark-charcoal text-white disabled:opacity-70"
+ :icon-props="{ iconPath: icon }"
+ :aria-label="$t(label)"
@click="handleClick"
- >
- <span class="sr-only">{{ $t(label) }}</span>
- <VIcon :icon-path="icon" />
- </button>
+ />
</template>
<script>
-import VIcon from '~/components/VIcon/VIcon.vue'
+import VIconButton from '~/components/VIconButton/VIconButton.vue'
import playIcon from '~/assets/icons/play.svg'
import pauseIcon from '~/assets/icons/pause.svg'
@@ -22,7 +21,8 @@ import replayIcon from '~/assets/icons/replay.svg'
*/
export default {
name: 'PlayPause',
- components: { VIcon },
+ components: { VIconButton },
+ inheritAttrs: false,
model: {
prop: 'status',
event: 'toggle',
diff --git a/src/components/AudioTrack/Waveform.vue b/src/components/AudioTrack/Waveform.vue
index c1bc2cb8d..cde9f936b 100644
--- a/src/components/AudioTrack/Waveform.vue
+++ b/src/components/AudioTrack/Waveform.vue
@@ -1,7 +1,7 @@
<template>
<div
ref="el"
- class="waveform relative bg-dark-charcoal-06 overflow-hidden focus:outline-none focus-visible:ring focus-visible:ring-offset-2 focus-visible:ring-pink"
+ class="waveform relative bg-dark-charcoal-06 border-1.5 border-tx focus:border-pink focus:outline-none focus:shadow-ring overflow-hidden"
:style="{ '--unusable-height': `${Math.floor((1 - usableFrac) * 100)}%` }"
tabIndex="0"
role="slider"
diff --git a/src/components/AudioTrack/layouts/Full.vue b/src/components/AudioTrack/layouts/Full.vue
index 4abe57cc1..d06bc1cb3 100644
--- a/src/components/AudioTrack/layouts/Full.vue
+++ b/src/components/AudioTrack/layouts/Full.vue
@@ -4,7 +4,7 @@
<div class="flex flex-row justify-between items-top mx-16 my-6">
<div class="left-content flex flex-row items-top gap-6">
- <slot name="play-pause" />
+ <slot name="play-pause" size="medium" />
<div class="audio-info">
<h1 class="text-3xl font-heading font-semibold">{{ audio.title }}</h1>
@@ -108,6 +108,6 @@ export default {
}
.full-track .play-pause {
- @apply h-14 w-14 rounded-sm;
+ @apply rounded-sm;
}
</style>
diff --git a/src/components/AudioTrack/layouts/Global.vue b/src/components/AudioTrack/layouts/Global.vue
index 5eb56f5fb..5e7c9a79c 100644
--- a/src/components/AudioTrack/layouts/Global.vue
+++ b/src/components/AudioTrack/layouts/Global.vue
@@ -2,7 +2,7 @@
<div class="global-track flex flex-row w-full">
<div class="flex-shrink-0">
<AudioThumbnail :audio="audio" />
- <slot name="play-pause" />
+ <slot name="play-pause" size="medium" />
</div>
<div class="relative flex-grow">
@@ -28,7 +28,6 @@ export default {
</script>
<style>
-.global-track .play-pause,
.global-track .thumbnail {
@apply h-14 w-14;
}
diff --git a/src/components/AudioTrack/layouts/Row.vue b/src/components/AudioTrack/layouts/Row.vue
index d4a91b823..d2f5b9454 100644
--- a/src/components/AudioTrack/layouts/Row.vue
+++ b/src/components/AudioTrack/layouts/Row.vue
@@ -6,7 +6,7 @@
>
<AudioThumbnail :audio="audio" />
<div v-if="isSmall" class="absolute bottom-0 end-0">
- <slot name="play-pause" />
+ <slot name="play-pause" size="tiny" />
</div>
</div>
@@ -70,7 +70,7 @@
'flex-grow': isMedium,
}"
>
- <slot name="play-pause" />
+ <slot name="play-pause" :size="isLarge ? 'medium' : 'large'" />
<slot name="controller" />
</div>
</div>
@@ -131,18 +131,10 @@ export default {
@apply rounded-te-sm rounded-be-sm;
}
-.row-track.size-m .play-pause {
- @apply h-20 w-20;
-}
-
.row-track.size-m .waveform {
@apply h-20;
}
-.row-track.size-l .play-pause {
- @apply h-14 w-14;
-}
-
.row-track.size-l .waveform {
@apply h-14;
}
diff --git a/src/components/AudioTrack/meta/PlayPause.stories.mdx b/src/components/AudioTrack/meta/PlayPause.stories.mdx
index bc2f1ec76..68d9fd41c 100644
--- a/src/components/AudioTrack/meta/PlayPause.stories.mdx
+++ b/src/components/AudioTrack/meta/PlayPause.stories.mdx
@@ -12,6 +12,15 @@ import PlayPause from '~/components/AudioTrack/PlayPause'
title="Components/Audio track/Play-pause toggle"
components={PlayPause}
argTypes={{
+ status: {
+ options: ['playing', 'paused', 'played'],
+ control: 'select',
+ },
+ size: {
+ defaultValue: 'medium',
+ options: ['tiny', 'small', 'medium', 'large'],
+ control: 'select',
+ },
toggle: {
action: 'toggle',
},
@@ -31,21 +40,28 @@ export const Template = (args, { argTypes }) => ({
<ArgsTable of={PlayPause} />
It should be noted that the component emits the `toggle` event with the opposite
-value of its `status` state. The buttons don't have any dimensions so a pair
-of `h-*` and `w-*` classes are needed.
+value of its `status` state.
+
+## Size
+
+The button dimensions can be set by passing the
+`size` prop (see
+[`VIconButton` docs](?path=/docs/components-viconbutton--default-story)).
<Canvas>
<Story
name="Default"
args={{
status: 'playing',
- class: ['h-16', 'w-16'],
+ 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'`.
@@ -56,7 +72,7 @@ be achieved by playing till the end of a track.
export const vModelTemplate = () => ({
template: `
<div>
- <PlayPause class="h-16 w-16" v-model="status" />
+ <PlayPause v-model="status" />
{{ status }}
</div>
`,
@@ -72,15 +88,14 @@ export const vModelTemplate = () => ({
<Story name="v-model">{vModelTemplate.bind({})}</Story>
</Canvas>
-The component is a transparent wrapper over `<button>` so all attributes of the
-button element can be applied to it, e.g. `disabled`.
+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,
- class: ['h-16', 'w-16'],
}}
>
{Template.bind({})}
diff --git a/src/components/Header/SearchBar/SearchButton.vue b/src/components/Header/SearchBar/SearchButton.vue
index 5eb918e57..86c8394cb 100644
--- a/src/components/Header/SearchBar/SearchButton.vue
+++ b/src/components/Header/SearchBar/SearchButton.vue
@@ -1,42 +1,25 @@
<template>
- <button
- :type="type"
- class="search-button flex items-center justify-center h-12 w-12 hover:text-white group-hover:text-white hover:bg-pink group-hover:bg-pink p-0.5px ps-1.5px focus:p-0 border border-s-0 focus:border-1.5 border-dark-charcoal-20 hover:border-pink group-hover:border-pink focus:border-pink rounded-e-sm focus:outline-none"
+ <VIconButton
+ v-bind="$attrs"
+ size="search"
+ 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 border-s-0 focus:border-1.5 border-dark-charcoal-20 hover:border-pink group-hover:border-pink rounded-e-sm active:shadow-ring"
+ :icon-props="{ iconPath: searchIcon }"
:aria-label="$t('search.search')"
v-on="$listeners"
- >
- <svg
- class="h-6 w-6"
- xmlns="http://www.w3.org/2000/svg"
- viewBox="0 0 24 24"
- aria-hidden="true"
- focusable="false"
- >
- <use :href="`${searchIcon}#icon`" />
- </svg>
- <span class="sr-only">{{ $t('search.search') }}</span>
- </button>
+ />
</template>
<script>
+import VIconButton from '~/components/VIconButton/VIconButton.vue'
+
import searchIcon from '~/assets/icons/search.svg'
export default {
name: 'SearchButton',
- setup(props, { attrs }) {
- const type = attrs['type'] ?? 'button'
-
- return {
- searchIcon,
-
- type,
- }
+ components: { VIconButton },
+ inheritAttrs: false,
+ setup() {
+ return { searchIcon }
},
}
</script>
-
-<style scoped>
-.search-button:active {
- box-shadow: 0 0 0 1px inset white;
-}
-</style>
diff --git a/src/components/VIconButton/VIconButton.vue b/src/components/VIconButton/VIconButton.vue
new file mode 100644
index 000000000..f21bc8161
--- /dev/null
+++ b/src/components/VIconButton/VIconButton.vue
@@ -0,0 +1,69 @@
+<template>
+ <button
+ class="icon-button flex items-center justify-center border-1.5 focus:border-pink focus:outline-none focus:shadow-ring active:shadow-ring"
+ :class="[...buttonSizeClasses]"
+ :type="type"
+ v-on="$listeners"
+ >
+ <VIcon :class="[...iconSizeClasses]" v-bind="iconProps" />
+ </button>
+</template>
+
+<script>
+import { computed } from '@nuxtjs/composition-api'
+
+import VIcon from '~/components/VIcon/VIcon.vue'
+
+export default {
+ name: 'VIconButton',
+ components: { VIcon },
+ props: {
+ /**
+ * the size of the button; The size affects both the size of the button
+ * itself and the icon inside it.
+ */
+ size: {
+ type: String,
+ default: 'medium',
+ validator: (val) =>
+ ['tiny', 'small', 'search', 'medium', 'large'].includes(val),
+ },
+ /**
+ * props to pass down to the `VIcon` component nested inside the button; See
+ * documentation on `VIcon`.
+ */
+ iconProps: {},
+ },
+ setup(props, { attrs }) {
+ const type = attrs['type'] ?? 'button'
+
+ const buttonSizeClasses = computed(
+ () =>
+ ({
+ tiny: ['w-6', 'h-6'],
+ small: ['w-10', 'h-10'],
+ search: ['w-12', 'h-12'],
+ medium: ['w-14', 'h-14'],
+ large: ['w-20', 'h-20'],
+ }[props.size])
+ )
+ const iconSizeClasses = computed(
+ () =>
+ ({
+ tiny: ['w-6', 'h-6'],
+ small: ['w-6', 'h-6'],
+ search: ['w-6', 'h-6'],
+ medium: ['w-8', 'h-8'],
+ large: ['w-12', 'h-12'],
+ }[props.size])
+ )
+
+ return {
+ type,
+
+ buttonSizeClasses,
+ iconSizeClasses,
+ }
+ },
+}
+</script>
diff --git a/src/components/VIconButton/meta/VIconButton.stories.mdx b/src/components/VIconButton/meta/VIconButton.stories.mdx
new file mode 100644
index 000000000..2ca48749c
--- /dev/null
+++ b/src/components/VIconButton/meta/VIconButton.stories.mdx
@@ -0,0 +1,50 @@
+import {
+ ArgsTable,
+ Canvas,
+ Description,
+ Meta,
+ Story,
+} from '@storybook/addon-docs'
+
+import VIconButton from '~/components/VIconButton/VIconButton.vue'
+
+import replayIcon from '~/assets/icons/replay.svg'
+
+<Meta
+ title="Components/VIconButton"
+ component={VIconButton}
+ argTypes={{
+ size: {
+ defaultValue: 'medium',
+ options: ['tiny', 'small', 'search', 'medium', 'large'],
+ control: 'select',
+ },
+ }}
+/>
+
+export const Template = (args, { argTypes }) => ({
+ template: `
+ <VIconButton class="text-white bg-dark-charcoal border-dark-charcoal" v-bind="$props"/>
+ `,
+ components: { VIconButton },
+ props: Object.keys(argTypes),
+})
+
+# Icon Button
+
+<Description of={VIconButton} />
+
+<ArgsTable of={VIconButton} />
+
+<Canvas>
+ <Story
+ name="Default"
+ args={{
+ iconProps: {
+ iconPath: replayIcon,
+ },
+ }}
+ >
+ {Template.bind({})}
+ </Story>
+</Canvas>
diff --git a/src/composables/use-media-query.js b/src/composables/use-media-query.js
index 4156a3498..d253064d6 100644
--- a/src/composables/use-media-query.js
+++ b/src/composables/use-media-query.js
@@ -1,6 +1,7 @@
/* 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'
/**
* Reactive Media Query.
@@ -38,6 +39,16 @@ export function useMediaQuery(query, options = {}) {
return matches
}
+/**
+ * Check whether the screen meets the current breakpoint size.
+ */
+export const isScreen = (breakpointName, options = {}) => {
+ return useMediaQuery(
+ `(min-width: ${SCREEN_SIZES.get(breakpointName)}px)`,
+ options
+ )
+}
+
/**
* Check if the user prefers reduced motion or not.
*/
diff --git a/src/constants/screens.js b/src/constants/screens.js
new file mode 100644
index 000000000..745ea9fcf
--- /dev/null
+++ b/src/constants/screens.js
@@ -0,0 +1,24 @@
+/**
+ * Mapping of a breakpoint name to the lower-bound of its screen-width range
+ * @type {Map<string, number>}
+ */
+export const SCREEN_SIZES = new Map()
+SCREEN_SIZES.set('2xl', 1536)
+SCREEN_SIZES.set('xl', 1280)
+SCREEN_SIZES.set('lg', 1024)
+SCREEN_SIZES.set('md', 768)
+SCREEN_SIZES.set('sm', 640)
+
+/**
+ * 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
+ */
+export const getBreakpointName = (screenWidth) => {
+ for (const [breakpointName, lowerLimit] of SCREEN_SIZES) {
+ if (screenWidth >= lowerLimit) {
+ return breakpointName
+ }
+ }
+ return 'mob' // smallest breakpoint
+}
diff --git a/src/pages/search.vue b/src/pages/search.vue
index fb6fe270f..91ef4e684 100644
--- a/src/pages/search.vue
+++ b/src/pages/search.vue
@@ -34,11 +34,9 @@ import { mapActions, mapGetters, mapMutations, mapState } from 'vuex'
import { MEDIA, SEARCH } from '~/constants/store-modules'
import debounce from 'lodash.debounce'
import SearchGridFilter from '~/components/Filters/SearchGridFilter.vue'
-import { useMediaQuery } from '~/composables/use-media-query.js'
+import { isScreen } from '~/composables/use-media-query.js'
import AppModal from '~/components/AppModal'
-const MIN_SCREEN_WIDTH_FILTER_VISIBLE_BY_DEFAULT = 768
-
const BrowsePage = {
name: 'browse-page',
layout: 'default',
@@ -47,12 +45,9 @@ const BrowsePage = {
},
setup() {
const defaultWindow = typeof window !== 'undefined' ? window : undefined
- const isLargeScreen = useMediaQuery(
- `(min-width: ${MIN_SCREEN_WIDTH_FILTER_VISIBLE_BY_DEFAULT}px)`,
- defaultWindow
- )
+ const isMdScreen = isScreen('md', defaultWindow)
return {
- isLargeScreen,
+ isMdScreen,
}
},
scrollToTop: false,
@@ -79,7 +74,7 @@ const BrowsePage = {
? local.get(process.env.filterStorageKey) === 'true'
: true
this.setFilterVisibility({
- isFilterVisible: this.isLargeScreen && localFilterState(),
+ isFilterVisible: this.isMdScreen && localFilterState(),
})
window.addEventListener('scroll', this.debounceScrollHandling)
},
@@ -103,10 +98,9 @@ const BrowsePage = {
searchFilter() {
return {
classes: {
- 'column is-narrow grid-sidebar max-w-full bg-white': this
- .isLargeScreen,
+ 'column is-narrow grid-sidebar max-w-full bg-white': this.isMdScreen,
},
- as: this.isLargeScreen ? 'aside' : AppModal,
+ as: this.isMdScreen ? 'aside' : AppModal,
}
},
},
diff --git a/tailwind.config.js b/tailwind.config.js
index 2c15e3601..9aa62a78c 100644
--- a/tailwind.config.js
+++ b/tailwind.config.js
@@ -1,3 +1,5 @@
+const { SCREEN_SIZES } = require('./src/constants/screens')
+
module.exports = {
purge: {
content: ['src/**/*.{vue,js,jsx,ts,tsx,mdx}', './nuxt.config.js'],
@@ -10,6 +12,9 @@ module.exports = {
safelist: ['w-4', 'w-5', 'w-6', 'h-4', 'h-5', 'h-6'],
},
theme: {
+ screens: Object.fromEntries(
+ Array.from(SCREEN_SIZES, ([name, width]) => [name, `${width}px`])
+ ),
colors: {
// Accents
tomato: '#e23600',
@@ -189,6 +194,9 @@ module.exports = {
scale: {
'-100': '-1',
},
+ boxShadow: {
+ ring: 'inset 0 0 0 1px white',
+ },
},
},
variants: {
@@ -202,6 +210,7 @@ module.exports = {
ringWidth: ['focus-visible'],
borderWidth: ['focus', 'focus-within'],
padding: ['focus', 'focus-within'],
+ boxShadow: ['focus', 'active'],
},
},
plugins: [require('tailwindcss-rtl')],
diff --git a/test/unit/specs/components/Header/SearchBar/search-bar.spec.js b/test/unit/specs/components/Header/SearchBar/search-bar.spec.js
index 269cbfab4..265121ff4 100644
--- a/test/unit/specs/components/Header/SearchBar/search-bar.spec.js
+++ b/test/unit/specs/components/Header/SearchBar/search-bar.spec.js
@@ -24,6 +24,6 @@ describe('SearchBar', () => {
expect(btnElement.tagName).toBe('BUTTON')
expect(btnElement).toHaveAttribute('type', 'submit')
- expect(btnElement.textContent.trim()).toEqual('search.search')
+ expect(btnElement).toHaveAttribute('aria-label', 'search.search')
})
})