diff --git a/.changeset/selectable-card-forced-colors-selected.md b/.changeset/selectable-card-forced-colors-selected.md new file mode 100644 index 0000000000000..41ee2289e2cb0 --- /dev/null +++ b/.changeset/selectable-card-forced-colors-selected.md @@ -0,0 +1,7 @@ +--- +'@astryxdesign/core': patch +--- + +[fix] SelectableCard: in forced colors (Windows High Contrast), a selected card now draws a 2px ring in the system Highlight color inside its edge, so the selection stays visible. Before, a selected card painted the same as an unselected one. The ring takes no space, so a toggle moves nothing, and it keeps Highlight under a theme that sets the card's border color. On a fixed-height card whose content scrolls, the ring scrolls with that content. Normal color modes are unchanged. + +@cixzhang diff --git a/docs/specs/components/SelectableCard/SelectableCard.spec.md b/docs/specs/components/SelectableCard/SelectableCard.spec.md index 28f534a5bc22b..856779b1a3b22 100644 --- a/docs/specs/components/SelectableCard/SelectableCard.spec.md +++ b/docs/specs/components/SelectableCard/SelectableCard.spec.md @@ -141,14 +141,14 @@ Consumer migration instructions belong in consumer docs and release notes. ## Behavioral and layout contract -| ID | Invariant | Basis | -| --- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ | -| FR1 | SelectableCard renders `component:Card` with the given `variant`, `elevation`, `padding`, and sizing. The root also carries the `selectable-card` target reflecting `variant`, `selected`, and, while disabled, `disabled`, and the first child is a visually hidden native `input type="checkbox"` named by `label` with `checked` equal to `isSelected`. | Current source and tests | -| FR2 | While enabled, a click on the card surface, a click or Space on the checkbox, or Enter on the checkbox calls `onChange(!isSelected)` exactly once. A click that belongs to nested interactive content, an open Tooltip or HoverCard surface (`component:Tooltip` FR11), or selected text does not toggle. A caller `onClick` runs after the toggle for a surface click, before it for a click or Space on the checkbox, and not at all for Enter; it also runs for clicks that do not toggle, including a click inside an open Tooltip or HoverCard surface. At the next minor, staged as `[breaking]`, a click inside an open layer stops at the layer root and the caller `onClick` no longer receives it (`architecture:layer-runtime` INV17); implementation gap: the current source still delivers that click to `onClick`. A caller `onMouseUp` runs after the card's own handling. | Current source and tests | -| FR3 | Selection adds a 2px inset ring through Card's ring variable, so layout does not move, and composes with any elevation shadow. `default`, `transparent`, and `muted` ring in `--color-accent`; each palette variant rings in its `--color-border-`; a theme-added variant rings in `--selectable-card-ring-color`. Selection also colors the border in the ring color where Card draws one, which is only the `default` variant. | Current source and tests | -| FR4 | While enabled, the surface paints the shared hover overlay under a hover-capable pointer and the pressed overlay for mouse and touch presses; a press from any pointer that starts on a nested interactive element shows no pressed paint on the card, since it does not activate the card (`architecture:press-feedback` INV7). A touch press shows at once. Under the touch presentation, its release fades on the theme's exit motion tokens (`--duration-exit`, `--ease-exit`), per `spec:AST-094` FR5; a mouse press keeps the card's desktop press styling. Keyboard focus on the hidden checkbox shows one indicator on the card root (`architecture:interaction-modality` INV3). | Current source and tests | -| FR5 | `isDisabled` dims the card to half opacity, removes the card's own click and mouseup handling and the caller's `onClick` and `onMouseUp` (other caller event props still reach the root) and the hover and pressed overlays (`spec:AST-085` FR6), and keeps the checkbox focusable with `aria-disabled="true"`; no click, Space, or Enter calls `onChange`. The disabled state is reflected on the owning target as `data-disabled` (`architecture:component-theming-surface` INV6). | Current source and tests | -| FR6 | `ref` reaches the Card root. Supported DOM, ARIA, data, and event props reach the root; `xstyle`, `className`, and `style` merge after the component's interactive styles. | Current source and tests | +| ID | Invariant | Basis | +| --- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------ | +| FR1 | SelectableCard renders `component:Card` with the given `variant`, `elevation`, `padding`, and sizing. The root also carries the `selectable-card` target reflecting `variant`, `selected`, and, while disabled, `disabled`, and the first child is a visually hidden native `input type="checkbox"` named by `label` with `checked` equal to `isSelected`. | Current source and tests | +| FR2 | While enabled, a click on the card surface, a click or Space on the checkbox, or Enter on the checkbox calls `onChange(!isSelected)` exactly once. A click that belongs to nested interactive content, an open Tooltip or HoverCard surface (`component:Tooltip` FR11), or selected text does not toggle. A caller `onClick` runs after the toggle for a surface click, before it for a click or Space on the checkbox, and not at all for Enter; it also runs for clicks that do not toggle, including a click inside an open Tooltip or HoverCard surface. At the next minor, staged as `[breaking]`, a click inside an open layer stops at the layer root and the caller `onClick` no longer receives it (`architecture:layer-runtime` INV17); implementation gap: the current source still delivers that click to `onClick`. A caller `onMouseUp` runs after the card's own handling. | Current source and tests | +| FR3 | Selection adds a 2px inset ring through Card's ring variable, so layout does not move, and composes with any elevation shadow. `default`, `transparent`, and `muted` ring in `--color-accent`; each palette variant rings in its `--color-border-`; a theme-added variant rings in `--selectable-card-ring-color`. Selection also colors the border in the ring color where Card draws one, which is only the `default` variant. Under forced colors, which drop the ring's shadow and repaint the border, a selected card, enabled or disabled, draws a 2px ring in the system `Highlight` color inside its edge instead, on a layer that takes no space and that a theme's border color on the card does not reach, so the selection stays visible, a toggle moves nothing, and the ring keeps `Highlight` under a theme that sets the card's border color; an unselected card draws none. On a fixed-height card whose content scrolls, the ring scrolls with that content. | Current source and tests | +| FR4 | While enabled, the surface paints the shared hover overlay under a hover-capable pointer and the pressed overlay for mouse and touch presses; a press from any pointer that starts on a nested interactive element shows no pressed paint on the card, since it does not activate the card (`architecture:press-feedback` INV7). A touch press shows at once. Under the touch presentation, its release fades on the theme's exit motion tokens (`--duration-exit`, `--ease-exit`), per `spec:AST-094` FR5; a mouse press keeps the card's desktop press styling. Keyboard focus on the hidden checkbox shows one indicator on the card root (`architecture:interaction-modality` INV3). | Current source and tests | +| FR5 | `isDisabled` dims the card to half opacity, removes the card's own click and mouseup handling and the caller's `onClick` and `onMouseUp` (other caller event props still reach the root) and the hover and pressed overlays (`spec:AST-085` FR6), and keeps the checkbox focusable with `aria-disabled="true"`; no click, Space, or Enter calls `onChange`. The disabled state is reflected on the owning target as `data-disabled` (`architecture:component-theming-surface` INV6). | Current source and tests | +| FR6 | `ref` reaches the Card root. Supported DOM, ARIA, data, and event props reach the root; `xstyle`, `className`, and `style` merge after the component's interactive styles. | Current source and tests | ### Allowed variation @@ -159,13 +159,13 @@ Consumer migration instructions belong in consumer docs and release notes. ### Representative states -| State | Required invariant | Allowed variation | -| -------------- | ------------------------------------------------------------------------------------------------- | ----------------- | -| Unselected | Unchecked named checkbox; no ring. | Variant. | -| Selected | Checked; inset ring in the variant's color, and a matching border on `default`; layout unchanged. | Elevation. | -| Keyboard focus | Focus outline on the card root. | — | -| Disabled | Focusable checkbox with `aria-disabled`; no toggle; half opacity. | Selection. | -| Nested control | Activating the nested control does not toggle the card. | Control type. | +| State | Required invariant | Allowed variation | +| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | ----------------- | +| Unselected | Unchecked named checkbox; no ring. | Variant. | +| Selected | Checked; inset ring in the variant's color, and a matching border on `default`; a `Highlight` ring under forced colors; layout unchanged. | Elevation. | +| Keyboard focus | Focus outline on the card root. | — | +| Disabled | Focusable checkbox with `aria-disabled`; no toggle; half opacity. | Selection. | +| Nested control | Activating the nested control does not toggle the card. | Control type. | ### Transformation and precedence order @@ -239,17 +239,18 @@ Consumer migration instructions belong in consumer docs and release notes. ## Verification map -| Contract | Verification | Representative states | Mutation or failure expectation | Audit section | -| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------- | -| FR1, FR2 | `SelectableCard.test.tsx` surface, checkbox, Enter, and Space tests | Unselected, selected | A missed toggle, a wrong `onChange` value, or a doubled toggle from Space fails. Target-reflection and handler-order tests fail on a lost target, variant, selection, or disabled reflection, or a changed order of `onChange` and a caller `onClick`. Gap: no test asserts the order of an enabled caller `onMouseUp`. | `audit:SelectableCard/behavior` | -| FR3 | `SelectableCard.test.tsx` elevation tests; stable visual baseline `core-selectablecard--single-select`; `SelectableCard.stories.tsx` | Selected with elevation; one selected default card beside unselected ones | A lost elevation while selected fails; a changed default ring or border comes back changed for review. Gap: palette and theme-added ring colors have no automated check. | `audit:SelectableCard/theming` | -| FR4 | `SelectableCard.test.tsx` touch-press test; `CardPress.a11y.chromium.spec.ts` on `core-selectablecard--single-select` | Rest; desktop hover and mouse press; touch press | A hover that does not paint over rest, a mouse press that does not paint over hover, or a touch press that does not match the desktop press or fade fails. `FocusRingOwner.a11y.chromium.spec.ts` fails if a focused nested button shows other than exactly one ring. | `audit:SelectableCard/design` | -| FR5, AR3 | `SelectableCard.test.tsx` disabled tests; `Checkbox.a11y.chromium.spec.ts` `card-disabled` state | Disabled click, Enter, Space | A toggle while disabled, or a disabled card that leaves keyboard reach, fails. `SelectableCardAudit.a11y.chromium.spec.ts` fails on a lost half opacity, a hover overlay on a disabled card, or a missing `data-disabled`. Gap: no test asserts the absent pressed overlay on a disabled card. | `audit:SelectableCard/a11y` | -| FR2 (nested) | `SelectableCard.test.tsx` nested-button test; `SelectableCardAudit.a11y.chromium.spec.ts` on `core-selectablecard--with-nested-button` | Card with a nested control or selected text | A click on a nested button that toggles the card fails. Gap: no test covers selected-text suppression. | `audit:SelectableCard/behavior` | -| Theming anatomy map | `scripts/check-knowledge.mjs` | Consumer anatomy and current target | Missing, extra, or stale mappings fail repository validation. | `audit:SelectableCard/theming` | -| AR1, AR2, AR4 | `SelectableCard.test.tsx` checkbox tests; `Checkbox.a11y.test.tsx` and `Checkbox.a11y.chromium.spec.ts` SelectableCard states | Unchecked, checked, disabled (`card-unchecked`, `card-checked`, `card-disabled`) | A lost checkbox role or name, a wrong checked state, or a disabled card that is not exposed as unavailable or not reachable fails, including at the accessibility tree. The passthrough test fails if the root gains a role or tab stop; `SelectableCardAudit.a11y.chromium.spec.ts` fails on a missing root focus outline or a ring that changes under hover. | `audit:SelectableCard/a11y` | -| FR6, PR1 | `SelectableCard.test.tsx` passthrough test; source review | Ref; caller props | A `ref`, `data-*`, `className`, or `style` that stops reaching the root fails. Gap: no test asserts `xstyle` or the merge order of the styling inputs, or fails if SelectableCard adds its own listener or timer. | `audit:SelectableCard/behavior` | -| AR5 | `Checkbox.a11y.chromium.spec.ts` aborted-press check on the SelectableCard states | Press that slides off the card before the lift | A toggle after a slide-off lift fails. | `audit:SelectableCard/a11y` | +| Contract | Verification | Representative states | Mutation or failure expectation | Audit section | +| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------- | +| FR1, FR2 | `SelectableCard.test.tsx` surface, checkbox, Enter, and Space tests | Unselected, selected | A missed toggle, a wrong `onChange` value, or a doubled toggle from Space fails. Target-reflection and handler-order tests fail on a lost target, variant, selection, or disabled reflection, or a changed order of `onChange` and a caller `onClick`. Gap: no test asserts the order of an enabled caller `onMouseUp`. | `audit:SelectableCard/behavior` | +| FR3 | `SelectableCard.test.tsx` elevation tests; stable visual baseline `core-selectablecard--single-select`; `SelectableCard.stories.tsx` | Selected with elevation; one selected default card beside unselected ones | A lost elevation while selected fails; a changed default ring or border comes back changed for review. Gap: palette and theme-added ring colors have no automated check. | `audit:SelectableCard/theming` | +| FR3 (forced colors) | `SelectableCard.test.tsx` forced-colors test; `SelectableCardForcedColors.a11y.chromium.spec.ts` on the multi-select, color-variants and disabled stories, the probe theme, a toggle, and a zero-padding card | Selected and unselected cards under forced colors; a selected card under a theme that sets the card's border color; a toggle; a selected card with zero padding; the same cards outside forced colors | A selected card without a 2px `Highlight` ring inside its edge under forced colors, a themed card whose ring loses `Highlight`, a toggle that moves a card, an unselected card with a ring, or a ring outside forced colors fails. Gap: no test covers the ring on a fixed-height card whose content scrolls. | `audit:SelectableCard/a11y` | +| FR4 | `SelectableCard.test.tsx` touch-press test; `CardPress.a11y.chromium.spec.ts` on `core-selectablecard--single-select` | Rest; desktop hover and mouse press; touch press | A hover that does not paint over rest, a mouse press that does not paint over hover, or a touch press that does not match the desktop press or fade fails. `FocusRingOwner.a11y.chromium.spec.ts` fails if a focused nested button shows other than exactly one ring. | `audit:SelectableCard/design` | +| FR5, AR3 | `SelectableCard.test.tsx` disabled tests; `Checkbox.a11y.chromium.spec.ts` `card-disabled` state | Disabled click, Enter, Space | A toggle while disabled, or a disabled card that leaves keyboard reach, fails. `SelectableCardAudit.a11y.chromium.spec.ts` fails on a lost half opacity, a hover overlay on a disabled card, or a missing `data-disabled`. Gap: no test asserts the absent pressed overlay on a disabled card. | `audit:SelectableCard/a11y` | +| FR2 (nested) | `SelectableCard.test.tsx` nested-button test; `SelectableCardAudit.a11y.chromium.spec.ts` on `core-selectablecard--with-nested-button` | Card with a nested control or selected text | A click on a nested button that toggles the card fails. Gap: no test covers selected-text suppression. | `audit:SelectableCard/behavior` | +| Theming anatomy map | `scripts/check-knowledge.mjs` | Consumer anatomy and current target | Missing, extra, or stale mappings fail repository validation. | `audit:SelectableCard/theming` | +| AR1, AR2, AR4 | `SelectableCard.test.tsx` checkbox tests; `Checkbox.a11y.test.tsx` and `Checkbox.a11y.chromium.spec.ts` SelectableCard states | Unchecked, checked, disabled (`card-unchecked`, `card-checked`, `card-disabled`) | A lost checkbox role or name, a wrong checked state, or a disabled card that is not exposed as unavailable or not reachable fails, including at the accessibility tree. The passthrough test fails if the root gains a role or tab stop; `SelectableCardAudit.a11y.chromium.spec.ts` fails on a missing root focus outline or a ring that changes under hover. | `audit:SelectableCard/a11y` | +| FR6, PR1 | `SelectableCard.test.tsx` passthrough test; source review | Ref; caller props | A `ref`, `data-*`, `className`, or `style` that stops reaching the root fails. Gap: no test asserts `xstyle` or the merge order of the styling inputs, or fails if SelectableCard adds its own listener or timer. | `audit:SelectableCard/behavior` | +| AR5 | `Checkbox.a11y.chromium.spec.ts` aborted-press check on the SelectableCard states | Press that slides off the card before the lift | A toggle after a slide-off lift fails. | `audit:SelectableCard/a11y` | ## Decision log diff --git a/packages/core/src/SelectableCard/SelectableCard.test.tsx b/packages/core/src/SelectableCard/SelectableCard.test.tsx index e17eee48f642f..b9f3dc9a40553 100644 --- a/packages/core/src/SelectableCard/SelectableCard.test.tsx +++ b/packages/core/src/SelectableCard/SelectableCard.test.tsx @@ -11,6 +11,7 @@ import {describe, it, expect, vi} from 'vitest'; import {render, screen, fireEvent} from '@testing-library/react'; import {SelectableCard} from './SelectableCard'; +import {getForcedColorsRules} from '../__tests__/forcedColors'; import { afterPaintsPress, hasReleaseFade, @@ -349,3 +350,19 @@ describe('SelectableCard target, handler order and passthrough (FR1, FR2, FR5, F expect(root).not.toHaveAttribute('tabindex'); }); }); + +// jsdom cannot emulate forced-colors rendering, so this asserts that the +// compiled output holds the forced-colors ring; the browser spec +// SelectableCardForcedColors.a11y.chromium.spec.ts checks the paint. +describe('SelectableCard forced colors (FR3)', () => { + it('compiles a Highlight ring on the selected card for forced colors', () => { + render( + {}}> + React + , + ); + const css = getForcedColorsRules(); + expect(css).toMatch(/::after\s*\{[^}]*border-color: highlight/); + expect(css).toMatch(/::after\s*\{[^}]*border-width: 2px/); + }); +}); diff --git a/packages/core/src/SelectableCard/SelectableCard.tsx b/packages/core/src/SelectableCard/SelectableCard.tsx index feb60860f0f1f..d34981d08ff0a 100644 --- a/packages/core/src/SelectableCard/SelectableCard.tsx +++ b/packages/core/src/SelectableCard/SelectableCard.tsx @@ -191,6 +191,34 @@ const styles = stylex.create({ }, }, }, + // Forced colors drop the ring's shadow and repaint the border, so selection + // needs its own ring. The layer takes no space, so a toggle moves nothing. A + // theme's border color on the card does not reach the layer. On a + // fixed-height card whose content scrolls, the layer scrolls with it. + selectedForcedColors: { + '::after': { + borderWidth: {default: null, '@media (forced-colors: active)': '2px'}, + borderStyle: {default: null, '@media (forced-colors: active)': 'solid'}, + borderColor: { + default: null, + '@media (forced-colors: active)': 'Highlight', + }, + }, + }, + // A disabled card has no overlay layer, so the forced-colors ring brings + // its own. + selectedForcedColorsLayer: { + '::after': { + content: {default: null, '@media (forced-colors: active)': '""'}, + position: {default: null, '@media (forced-colors: active)': 'absolute'}, + inset: {default: null, '@media (forced-colors: active)': 0}, + borderRadius: { + default: null, + '@media (forced-colors: active)': 'inherit', + }, + pointerEvents: {default: null, '@media (forced-colors: active)': 'none'}, + }, + }, disabled: { cursor: 'default', opacity: 0.5, @@ -564,6 +592,8 @@ export function SelectableCard({ styles.interactive, isSelected && selectedStyleForVariant(variant), !isDisabled && styles.overlay, + isSelected && styles.selectedForcedColors, + isSelected && isDisabled && styles.selectedForcedColorsLayer, // The touch press's strength and release, on the element the // controller writes to; the `::after` above paints off it. !isDisabled && interactionOverlayStyles.pressedAlpha, diff --git a/packages/core/src/SelectableCard/__tests__/SelectableCardForcedColors.a11y.chromium.spec.ts b/packages/core/src/SelectableCard/__tests__/SelectableCardForcedColors.a11y.chromium.spec.ts new file mode 100644 index 0000000000000..56da44ad508d8 --- /dev/null +++ b/packages/core/src/SelectableCard/__tests__/SelectableCardForcedColors.a11y.chromium.spec.ts @@ -0,0 +1,226 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. +// @pr-a11y scope ^packages/core/src/(SelectableCard|Card)/ ^apps/storybook/stories/SelectableCard + +/** + * @file SelectableCardForcedColors.a11y.chromium.spec.ts + * @input The built SelectableCard multi-select, color-variants and disabled + * stories in real Chromium, with forced colors emulated and without + * @output Browser evidence that, in forced colors, a selected card paints a + * 2px ring in the system Highlight color inside its edge, also with zero + * padding and under a theme that sets the card's border color, that a + * toggle moves no card, and that an unselected card paints none; outside + * forced colors the layer paints no ring + * @position Regression proof for SelectableCard's forced-colors selection + * (Chromium only: Playwright's WebKit matches the forced-colors query but + * forces nothing) + */ + +// @ts-expect-error -- pngjs ships no declarations; runtime support is pinned. +import {PNG} from 'pngjs'; +import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright'; +import { + DEFAULT_STORYBOOK_DIR, + serveStorybook, + type StaticServer, +} from '@astryxdesign/a11y-spec/storybook'; + +let storybook: StaticServer; +test.beforeAll(async () => { + storybook = await serveStorybook( + process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR, + ); +}); +test.afterAll(async () => { + await storybook?.close(); +}); + +type Ring = { + label: string; + selected: boolean; + width: number; + style: string; + color: string; +}; + +async function open( + page: Page, + story: string, + forcedColors: 'active' | 'none', + theme?: string, +): Promise { + await page.emulateMedia({forcedColors}); + const globals = theme ? `&globals=astryxTheme:${theme}` : ''; + await page.goto( + `${storybook.origin}/iframe.html?id=${story}&viewMode=story${globals}`, + ); + await page.locator('.astryx-selectable-card').first().waitFor(); +} + +/** Each card's `::after` border, and the system Highlight color. */ +async function readRings( + page: Page, +): Promise<{rings: Ring[]; highlight: string}> { + return page.evaluate(() => { + const probe = document.createElement('span'); + probe.style.color = 'Highlight'; + document.body.append(probe); + const highlight = getComputedStyle(probe).color; + probe.remove(); + const rings = [ + ...document.querySelectorAll('.astryx-selectable-card'), + ].map(card => { + const layer = getComputedStyle(card, '::after'); + const input = card.querySelector('input'); + return { + label: input?.getAttribute('aria-label') ?? '', + selected: card.getAttribute('data-selected') === 'true', + width: parseFloat(layer.borderTopWidth), + style: layer.borderTopStyle, + color: layer.borderTopColor, + }; + }); + return {rings, highlight}; + }); +} + +function expectRings({rings, highlight}: {rings: Ring[]; highlight: string}) { + const selected = rings.filter(ring => ring.selected); + const unselected = rings.filter(ring => !ring.selected); + expect(selected.length).toBeGreaterThan(0); + expect(unselected.length).toBeGreaterThan(0); + for (const ring of selected) { + expect(ring, ring.label).toMatchObject({ + width: 2, + style: 'solid', + color: highlight, + }); + } + for (const ring of unselected) { + expect(ring.width, ring.label).toBe(0); + } +} + +for (const story of [ + 'core-selectablecard--multi-select', + 'core-selectablecard--color-variants', + 'core-selectablecard--disabled', +]) { + test(`${story}: in forced colors a selected card rings in Highlight and an unselected card does not`, async ({ + page, + }) => { + await open(page, story, 'active'); + expectRings(await readRings(page)); + }); +} + +test('in forced colors the ring keeps Highlight under a theme that sets the card border color', async ({ + page, +}) => { + // The probe theme sets `borderColor` on the `selectable-card` target. + await open(page, 'core-selectablecard--multi-select', 'active', 'probe'); + expectRings(await readRings(page)); +}); + +test('in forced colors the selected card paints Highlight inside its edge', async ({ + page, +}) => { + await open(page, 'core-selectablecard--multi-select', 'active'); + const pixel = async ( + clip: {x: number; y: number; width: number; height: number}, + x: number, + ) => { + const png = PNG.sync.read(await page.screenshot({clip})); + const i = (Math.floor(png.height / 2) * png.width + x) * 4; + return [png.data[i], png.data[i + 1], png.data[i + 2]]; + }; + // The system Highlight as it paints over the page: a swatch beside the cards. + await page.evaluate(() => { + const swatch = document.createElement('div'); + swatch.id = 'highlight-swatch'; + Object.assign(swatch.style, { + position: 'fixed', + right: '0', + bottom: '0', + width: '20px', + height: '20px', + backgroundColor: 'Highlight', + }); + document.body.append(swatch); + }); + const swatch = (await page.locator('#highlight-swatch').boundingBox())!; + const highlight = await pixel(swatch, 10); + const edge = async (selected: boolean) => { + const card = page + .locator(`.astryx-selectable-card[data-selected="${selected}"]`) + .first(); + // One pixel inside the card's own 1px edge, halfway down: on the ring. + return pixel((await card.boundingBox())!, 2); + }; + expect(await edge(true)).toEqual(highlight); + expect(await edge(false)).not.toEqual(highlight); +}); + +test('in forced colors a toggle moves no card', async ({page}) => { + await open(page, 'core-selectablecard--multi-select', 'active'); + const boxes = async () => + page.evaluate(() => + [ + ...document.querySelectorAll('.astryx-selectable-card'), + ].map(card => { + const {x, y, width, height} = card.getBoundingClientRect(); + return {x, y, width, height}; + }), + ); + const before = await boxes(); + // The story selects only its first card. Turning it off leaves no + // selected card, so a ring that took space would change the row's height. + const card = page.locator('.astryx-selectable-card').first(); + await expect(card).toHaveAttribute('data-selected', 'true'); + await card.click(); + await expect(card).toHaveAttribute('data-selected', 'false'); + expect(await boxes()).toEqual(before); +}); + +test('in forced colors a selected card with zero padding still rings in Highlight', async ({ + page, +}) => { + await open(page, 'core-selectablecard--multi-select', 'active'); + const {highlight} = await readRings(page); + const ring = await page.evaluate(() => { + // `padding={0}` sets the container padding variables to zero. + const card = document.querySelector( + '.astryx-selectable-card[data-selected="true"]', + )!; + for (const side of [ + 'inline-start', + 'inline-end', + 'block-start', + 'block-end', + ]) { + card.style.setProperty(`--container-padding-${side}`, '0px'); + } + const layer = getComputedStyle(card, '::after'); + return { + width: parseFloat(layer.borderTopWidth), + style: layer.borderTopStyle, + color: layer.borderTopColor, + padding: parseFloat(getComputedStyle(card).paddingInlineStart), + }; + }); + expect(ring).toMatchObject({width: 2, style: 'solid', color: highlight}); + expect(ring.padding).toBe(0); +}); + +test('outside forced colors the layer paints no ring', async ({page}) => { + for (const story of [ + 'core-selectablecard--multi-select', + 'core-selectablecard--color-variants', + 'core-selectablecard--disabled', + ]) { + await open(page, story, 'none'); + const {rings} = await readRings(page); + for (const ring of rings) { + expect(ring.width, `${story} ${ring.label}`).toBe(0); + } + } +});