Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Add utilities for consistent focus rings (#2067)
Dhruv Bhanushalia2287380a848
diff --git a/.storybook/decorators/with-screenshot-area.js b/.storybook/decorators/with-screenshot-area.jsnew file mode 100644index 000000000..a6bc12817--- /dev/null+++ b/.storybook/decorators/with-screenshot-area.js@@ -0,0 +1,12 @@+export const WithScreenshotArea = (story) => {+ return {+ template: `+ <div+ class="screenshot-area"+ :style="{ display: 'inline-block', padding: '2rem' }"+ >+ <story />+ </div>`,+ components: { story },+ }+}diff --git a/nuxt.config.ts b/nuxt.config.tsindex 98e5716fd..16e8c9f15 100644--- a/nuxt.config.ts+++ b/nuxt.config.ts@@ -363,16 +363,23 @@ const config: NuxtConfig = {{name: "@storybook/addon-essentials",options: {- backgrounds: false,+ backgrounds: true,viewport: true,toolbars: true,},},],parameters: {+ backgrounds: {+ default: "White",+ values: [+ { name: "White", value: "#ffffff" },+ { name: "Dark charcoal", value: "#30272e" },+ ],+ },options: {storySort: {- order: ["Introduction", ["Openverse UI"]],+ order: ["Introduction", ["Openverse UI"], "Meta"],},},viewport: {diff --git a/src/assets/icons/check.svg b/src/assets/icons/check.svgindex 5da5ad29d..937c401b5 100644--- a/src/assets/icons/check.svg+++ b/src/assets/icons/check.svg@@ -1,3 +1,5 @@-<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none">- <path id="icon" stroke="currentColor" d="M16.7 7.1l-6.3 8.5-3.3-2.5-.9 1.2 4.5 3.4L17.9 8z" />+<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">+ <g id="icon">+ <path d="M17.9293 7.97825L10.731 17.6591L6.21469 14.3009L7.10972 13.0972L10.4223 15.5603L16.7255 7.08322L17.9293 7.97825Z" fill="currentColor"/>+ </g></svg>diff --git a/src/components/VAllResultsGrid/VImageCellSquare.vue b/src/components/VAllResultsGrid/VImageCellSquare.vueindex 482b37089..152519100 100644--- a/src/components/VAllResultsGrid/VImageCellSquare.vue+++ b/src/components/VAllResultsGrid/VImageCellSquare.vue@@ -3,7 +3,7 @@itemprop="contentUrl":title="image.title":href="'/image/' + image.id"- class="group block rounded-sm focus:outline-none focus:ring-[3px] focus:ring-pink focus:ring-offset-[3px]"+ class="group block rounded-sm focus-bold-filled"><figureitemprop="image"diff --git a/src/components/VAudioTrack/VAudioTrack.vue b/src/components/VAudioTrack/VAudioTrack.vueindex 6c68c84d8..53f30a1fd 100644--- a/src/components/VAudioTrack/VAudioTrack.vue+++ b/src/components/VAudioTrack/VAudioTrack.vue@@ -3,7 +3,7 @@<Component:is="isComposite ? 'VLink' : 'VWarningSuppressor'"v-bind="containerAttributes"- class="audio-track group block overflow-hidden rounded-sm ring-pink hover:no-underline focus:border-tx focus:bg-white focus:outline-none"+ class="audio-track group block overflow-hidden rounded-sm ring-pink hover:no-underline":aria-label="ariaLabel":role="isComposite ? 'application' : undefined"@keydown.native.shift.tab.exact="$emit('shift-tab', $event)"@@ -445,10 +445,8 @@ export default defineComponent({class: ["cursor-pointer",{- "focus:ring-offset-[3px] focus:ring-[3px]":- props.layout === "box",- "focus:ring-offset-0 focus:ring-[1.5px]":- props.layout === "row",+ "focus-bold-filled": props.layout === "box",+ "focus-slim-tx": props.layout === "row",},],}diff --git a/src/components/VCheckbox/VCheckbox.vue b/src/components/VCheckbox/VCheckbox.vueindex e0e0aedb1..936cea673 100644--- a/src/components/VCheckbox/VCheckbox.vue+++ b/src/components/VCheckbox/VCheckbox.vue@@ -1,17 +1,25 @@<template>- <label :for="id" class="checkbox-label" :class="labelClasses">+ <label+ :for="id"+ class="relative flex text-sm leading-5"+ :class="labelClasses"+ >+ <!--+ The checkbox focus style is a slight variation on the `focus-slim-tx` style.+ Because it becomes filled when checked, it also needs the+ `checked:focus-visible:border-white` class.+ --><input:id="id"type="checkbox"- class="checkbox bg-white"+ class="checkbox h-5 w-5 flex-shrink-0 appearance-none rounded-sm border border-dark-charcoal bg-white me-3 focus-slim-tx checked:bg-dark-charcoal checked:focus-visible:border-white disabled:border-dark-charcoal-40 disabled:bg-dark-charcoal-10 checked:disabled:border-dark-charcoal-40 checked:disabled:bg-dark-charcoal-40"v-bind="inputAttrs"@change="onChange"/><VIconv-show="localCheckedState"- class="absolute text-white start-0"- :icon-path="checkmark"- view-box="0 0 20 20"+ class="absolute transform text-white"+ :icon-path="checkIcon":size="5"/><!-- @slot The checkbox label --><slot />@@ -25,7 +33,7 @@ import { defineEvent } from "~/types/emits"import VIcon from "~/components/VIcon/VIcon.vue"-import checkmark from "~/assets/icons/checkmark.svg"+import checkIcon from "~/assets/icons/check.svg"type CheckboxAttrs = {name: string@@ -136,7 +144,7 @@ export default defineComponent({})}return {- checkmark,+ checkIcon,localCheckedState,labelClasses,inputAttrs,@@ -145,15 +153,3 @@ export default defineComponent({},})</script>-<style scoped>-.checkbox-label {- @apply relative flex text-sm leading-5;-}-.checkbox {- @apply relative h-5 w-5 flex-shrink-0 appearance-none rounded-sm border border-dark-charcoal me-3;- @apply focus:outline-none focus:ring focus:ring-pink focus:ring-offset-2;- @apply disabled:border-dark-charcoal-40 disabled:bg-dark-charcoal-10;- @apply checked:bg-dark-charcoal;- @apply checked:disabled:bg-dark-charcoal-40;-}-</style>diff --git a/src/components/VCheckbox/meta/VCheckbox.stories.mdx b/src/components/VCheckbox/meta/VCheckbox.stories.mdxindex fb043f9eb..d9b7216c6 100644--- a/src/components/VCheckbox/meta/VCheckbox.stories.mdx+++ b/src/components/VCheckbox/meta/VCheckbox.stories.mdx@@ -6,12 +6,15 @@ import {Story,} from "@storybook/addon-docs"+import { WithScreenshotArea } from "~~/.storybook/decorators/with-screenshot-area"+import VCheckbox from "~/components/VCheckbox/VCheckbox.vue"import VLicense from "~/components/VLicense/VLicense.vue"<Metatitle="Components/VCheckbox"components={VCheckbox}+ decorators={[WithScreenshotArea]}argTypes={{change: {action: "change",diff --git a/src/components/meta/Focus.stories.mdx b/src/components/meta/Focus.stories.mdxnew file mode 100644index 000000000..8b67cc656--- /dev/null+++ b/src/components/meta/Focus.stories.mdx@@ -0,0 +1,81 @@+import { Meta, Canvas, Story } from "@storybook/addon-docs"++import { WithScreenshotArea } from "~~/.storybook/decorators/with-screenshot-area"++<Meta title="Meta/Focus" decorators={[WithScreenshotArea]} />++export const GetTemplate = (irrelevantClassNames) => (args) => ({+ template: `+ <div+ class="h-30 w-30 flex items-center justify-center ${irrelevantClassNames}"+ :class="args.classNames"+ data-testid="focus-target"+ tabindex="0"+ >+ Focus on me+ </div>`,+ setup() {+ return { args }+ },+})++# Focus++Focus styles are defined across two axes: thickness (slim or bold) and style+(transparent or filled). However, there is no bold-transparent variant.++The transparent style works best if the element has a border and no background+color. On the other hand the filled style works with elements that have a+background color and no border.++Both the outer and inner strokes in filled border variants are box-shadows.++## Slim transparent++An outer stroke (colored) of 1.5px is applied to the component and the existing+border is turned transparent.++<Canvas>+ <Story name="Slim transparent" args={{ classNames: ["focus-slim-tx"] }}>+ {GetTemplate(+ "border border-dark-charcoal-40 hover:border-dark-charcoal"+ ).bind({})}+ </Story>+</Canvas>++## Slim filled++A colored outer stroke of 1.5px and a white inner stroke of 1.5px are both+applied to the component.++<Canvas>+ <Story name="Slim filled" args={{ classNames: ["focus-slim-filled"] }}>+ {GetTemplate("bg-pink text-white").bind({})}+ </Story>+</Canvas>++## Bold filled++A colored outer stroke of 3.0px and a white inner stroke of 3.0px are both+applied to the component.++<Canvas>+ <Story name="Bold filled" args={{ classNames: ["focus-bold-filled"] }}>+ {GetTemplate("bg-yellow text-dark-charcoal").bind({})}+ </Story>+</Canvas>++## Colored++The ring may be colored with a `-<color>` suffix after the appropriate class. If+no color is set, the default is assumed to be pink.++<Canvas>+ <Story+ name="Colored"+ args={{ classNames: ["focus-slim-tx-yellow"] }}+ parameters={{ backgrounds: { default: "Dark charcoal" } }}+ >+ {GetTemplate("bg-dark-charcoal text-white").bind({})}+ </Story>+</Canvas>diff --git a/src/styles/tailwind.css b/src/styles/tailwind.cssindex db1ba088b..3e420a7f5 100644--- a/src/styles/tailwind.css+++ b/src/styles/tailwind.css@@ -127,4 +127,48 @@ Time - 11px - xs semibold (default leading-[120%]).time {@apply text-xs font-semibold;}++ /**+ * Focus styles are defined across across two axes.+ *+ * Thickness:+ * - Slim: 1.5px outline+ * - Bold: 3.0px outline+ *+ * Style:+ * - Transparent: Replaces existing border on the element+ * - Filled: Adds a ring around element with some offset+ *+ * There is no bold-transparent variant.+ *+ * The color of the ring is set by a plugin defined in `tailwind.config.js`.+ */++ [class*="focus-slim-tx"] {+ @apply focus-visible:outline-none; /* UA styles: none */+ @apply focus-visible:ring; /* outer-stroke: box-shadow */+ @apply focus-visible:border-tx; /* inner-stroke: transparent border */++ /* It is up to the component to apply border utilities. */+ @apply focus-visible:hover:border-tx; /* focus prevails over hover */+ }++ [class*="focus-slim-filled"],+ [class*="focus-bold-filled"] {+ @apply relative;+ @apply after:pointer-events-none after:absolute after:inset-0 after:-z-10 after:rounded-inherit;++ @apply focus-visible:outline-none; /* UA styles: none */+ @apply focus-visible:after:z-10; /* inner-stroke: pseudo-element */+ }++ [class*="focus-slim-filled"] {+ @apply after:shadow-slim-filled;+ @apply focus-visible:ring; /* outer-stroke: box-shadow */+ }++ [class*="focus-bold-filled"] {+ @apply after:shadow-bold-filled;+ @apply focus-visible:ring-bold; /* outer-stroke: box-shadow */+ }}diff --git a/tailwind.config.js b/tailwind.config.jsindex 1ed862a19..ffe3a3412 100644--- a/tailwind.config.js+++ b/tailwind.config.js@@ -1,4 +1,5 @@const defaultTheme = require("tailwindcss/defaultTheme")+const plugin = require("tailwindcss/plugin")const { SCREENS } = require("./src/constants/screens")const { Z_INDICES } = require("./src/constants/z-indices")@@ -96,7 +97,8 @@ module.exports = {120: "30.00rem",},ringWidth: {- DEFAULT: "1.5px",+ DEFAULT: "1.5px", // aka slim+ bold: "3.0px",0: 0,},borderWidth: {@@ -197,6 +199,11 @@ module.exports = {ring: "inset 0 0 0 1px white","ring-1.5": "inset 0 0 0 1.5px white","el-2": "0 0.125rem 0.25rem rgba(0, 0, 0, 0.1)",+ "slim-filled": "inset 0 0 0 1.5px white",+ "bold-filled": "inset 0 0 0 3px white",+ },+ borderRadius: {+ inherit: "inherit",},typography: (theme) => ({DEFAULT: {@@ -220,5 +227,17 @@ module.exports = {require("@tailwindcss/line-clamp"),require("@tailwindcss/typography"),require("tailwindcss-labeled-groups")(["waveform"]),+ // Focus styles+ // This plugin has related stylesheets in `src/styles/tailwind.css`.+ plugin(({ matchUtilities, theme }) => {+ matchUtilities(+ Object.fromEntries(+ ["focus-slim-tx", "focus-slim-filled", "focus-bold-filled"].map(+ (item) => [item, (value) => ({ "--tw-ring-color": value })]+ )+ ),+ { values: { ...theme("colors"), DEFAULT: theme("colors.pink") } }+ )+ }),],}diff --git a/test/playwright/playwright.config.ts b/test/playwright/playwright.config.tsindex 065f8703d..fbc667232 100644--- a/test/playwright/playwright.config.ts+++ b/test/playwright/playwright.config.ts@@ -59,6 +59,7 @@ const config: PlaywrightTestConfig = {maxDiffPixelRatio: 0,},},+ retries: 2,}export default configdiff --git a/test/playwright/visual-regression/components/filters-sidebar.spec.ts-snapshots/filters-sidebar-filters-selected-ltr-linux.png b/test/playwright/visual-regression/components/filters-sidebar.spec.ts-snapshots/filters-sidebar-filters-selected-ltr-linux.pngindex d64de4d64..a7b30ae55 100644Binary files a/test/playwright/visual-regression/components/filters-sidebar.spec.ts-snapshots/filters-sidebar-filters-selected-ltr-linux.png and b/test/playwright/visual-regression/components/filters-sidebar.spec.ts-snapshots/filters-sidebar-filters-selected-ltr-linux.png differdiff --git a/test/playwright/visual-regression/components/filters-sidebar.spec.ts-snapshots/filters-sidebar-filters-selected-rtl-linux.png b/test/playwright/visual-regression/components/filters-sidebar.spec.ts-snapshots/filters-sidebar-filters-selected-rtl-linux.pngindex af2d2dd36..a880da461 100644Binary files a/test/playwright/visual-regression/components/filters-sidebar.spec.ts-snapshots/filters-sidebar-filters-selected-rtl-linux.png and b/test/playwright/visual-regression/components/filters-sidebar.spec.ts-snapshots/filters-sidebar-filters-selected-rtl-linux.png differdiff --git a/test/storybook/visual-regression/focus.spec.ts b/test/storybook/visual-regression/focus.spec.tsnew file mode 100644index 000000000..fb7da6a6a--- /dev/null+++ b/test/storybook/visual-regression/focus.spec.ts@@ -0,0 +1,23 @@+import { test, expect, Page, Locator } from "@playwright/test"++const goTo = async (page: Page, slug: string) => {+ await page.goto(`/iframe.html?id=meta-focus--${slug}`)+}++const expectSnapshot = async (name: string, elem: Locator) => {+ expect(await elem.screenshot()).toMatchSnapshot({ name: `${name}.png` })+}++const allSlugs = ["slim-transparent", "slim-filled", "bold-filled", "colored"]++test.describe.configure({ mode: "parallel" })++test.describe("Focus", () => {+ for (const slug of allSlugs) {+ test(`focus-${slug}`, async ({ page }) => {+ await goTo(page, slug)+ await page.focus('[data-testid="focus-target"]')+ await expectSnapshot(`focus-${slug}`, page.locator(".screenshot-area"))+ })+ }+})diff --git a/test/storybook/visual-regression/focus.spec.ts-snapshots/focus-bold-filled-linux.png b/test/storybook/visual-regression/focus.spec.ts-snapshots/focus-bold-filled-linux.pngnew file mode 100644index 000000000..adf310d9bBinary files /dev/null and b/test/storybook/visual-regression/focus.spec.ts-snapshots/focus-bold-filled-linux.png differdiff --git a/test/storybook/visual-regression/focus.spec.ts-snapshots/focus-colored-linux.png b/test/storybook/visual-regression/focus.spec.ts-snapshots/focus-colored-linux.pngnew file mode 100644index 000000000..bea74d514Binary files /dev/null and b/test/storybook/visual-regression/focus.spec.ts-snapshots/focus-colored-linux.png differdiff --git a/test/storybook/visual-regression/focus.spec.ts-snapshots/focus-slim-filled-linux.png b/test/storybook/visual-regression/focus.spec.ts-snapshots/focus-slim-filled-linux.pngnew file mode 100644index 000000000..e8db3988cBinary files /dev/null and b/test/storybook/visual-regression/focus.spec.ts-snapshots/focus-slim-filled-linux.png differdiff --git a/test/storybook/visual-regression/focus.spec.ts-snapshots/focus-slim-transparent-linux.png b/test/storybook/visual-regression/focus.spec.ts-snapshots/focus-slim-transparent-linux.pngnew file mode 100644index 000000000..006cfce72Binary files /dev/null and b/test/storybook/visual-regression/focus.spec.ts-snapshots/focus-slim-transparent-linux.png differdiff --git a/test/storybook/visual-regression/v-checkbox.spec.ts b/test/storybook/visual-regression/v-checkbox.spec.tsindex ceb361bcc..929676c03 100644--- a/test/storybook/visual-regression/v-checkbox.spec.ts+++ b/test/storybook/visual-regression/v-checkbox.spec.ts@@ -1,5 +1,7 @@import { test, expect } from "@playwright/test"+test.describe.configure({ mode: "parallel" })+test.describe("v-checkbox", () => {test.describe("default", () => {test.beforeEach(async ({ page }) => {@@ -7,21 +9,85 @@ test.describe("v-checkbox", () => {})test("default", async ({ page }) => {- expect(await page.screenshot()).toMatchSnapshot({ name: "default.png" })+ expect(+ await page.locator(".screenshot-area").screenshot()+ ).toMatchSnapshot({ name: "default.png" })+ })++ test("hover", async ({ page }) => {+ const checkbox = page.locator('input[type="checkbox"]')+ await checkbox.hover()+ expect(+ await page.locator(".screenshot-area").screenshot()+ ).toMatchSnapshot({ name: "hover.png" })+ })++ test("focused", async ({ page }) => {+ const checkbox = page.locator('input[type="checkbox"]')+ await checkbox.focus()+ expect(+ await page.locator(".screenshot-area").screenshot()+ ).toMatchSnapshot({+ name: "focused.png",+ })+ })++ test("disabled", async ({ page }) => {+ const checkbox = page.locator('input[type="checkbox"]')+ await checkbox.evaluate((checkbox) => {+ ;(checkbox as HTMLInputElement).disabled = true+ })+ expect(+ await page.locator(".screenshot-area").screenshot()+ ).toMatchSnapshot({+ name: "disabled.png",+ })})test("on", async ({ page }) => {- await page.click('input[type="checkbox"]')- expect(await page.screenshot()).toMatchSnapshot({ name: "on.png" })+ const checkbox = page.locator('input[type="checkbox"]')+ await checkbox.click()+ expect(+ await page.locator(".screenshot-area").screenshot()+ ).toMatchSnapshot({ name: "on.png" })+ })++ test("on focused", async ({ page }) => {+ const checkbox = page.locator('input[type="checkbox"]')+ await checkbox.focus()+ await page.keyboard.press("Space")+ expect(+ await page.locator(".screenshot-area").screenshot()+ ).toMatchSnapshot({+ name: "on-focused.png",+ })+ })++ test("on disabled", async ({ page }) => {+ const checkbox = page.locator('input[type="checkbox"]')+ await checkbox.click()+ await checkbox.evaluate((checkbox) => {+ ;(checkbox as HTMLInputElement).disabled = true+ })+ expect(+ await page.locator(".screenshot-area").screenshot()+ ).toMatchSnapshot({+ name: "on-disabled.png",+ })})- test("off", async ({ page }) => {+ test("on-and-off", async ({ page }) => {// toggle on and off again- await page.click('input[type="checkbox"]')+ const checkbox = page.locator('input[type="checkbox"]')+ await checkbox.click()// `force: true` is required because the `input`'s pointer events are actually intercepted by the visual SVG.// We still want to check that it works though as that does mimic the user behavior of checking directly on the checkbox.- await page.click('input[type="checkbox"]', { force: true })- expect(await page.screenshot()).toMatchSnapshot({ name: "off.png" })+ await checkbox.click({ force: true })+ expect(+ await page.locator(".screenshot-area").screenshot()+ ).toMatchSnapshot({+ name: "on-and-off.png",+ })})})})diff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/default-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/default-linux.pngindex ed7474e42..c260900ff 100644Binary files a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/default-linux.png and b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/default-linux.png differdiff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/disabled-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/disabled-linux.pngnew file mode 100644index 000000000..da69f9327Binary files /dev/null and b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/disabled-linux.png differdiff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/focused-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/focused-linux.pngnew file mode 100644index 000000000..0d85638c2Binary files /dev/null and b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/focused-linux.png differdiff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/hover-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/hover-linux.pngnew file mode 100644index 000000000..b9e3c9826Binary files /dev/null and b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/hover-linux.png differdiff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/off-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/off-linux.pngdeleted file mode 100644index 9fd0de4fb..000000000Binary files a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/off-linux.png and /dev/null differdiff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-and-off-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-and-off-linux.pngnew file mode 100644index 000000000..b9e3c9826Binary files /dev/null and b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-and-off-linux.png differdiff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-disabled-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-disabled-linux.pngnew file mode 100644index 000000000..0a9244044Binary files /dev/null and b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-disabled-linux.png differdiff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-focused-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-focused-linux.pngnew file mode 100644index 000000000..d14f786fdBinary files /dev/null and b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-focused-linux.png differdiff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-linux.pngindex 50107f2b8..553651f69 100644Binary files a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-linux.png and b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-linux.png differ