Files
Ekklesia/packages/design-tokens/tests/contrast.test.ts
ekklesiaandClaude Opus 5.5 d42b64a093
CI / verify (pull_request) Successful in 1m10s
CI / infrastructure-smoke (pull_request) Successful in 34s
CI / package-release (pull_request) Successful in 52s
CI / migration-smoke (pull_request) Successful in 2m20s
CI / image-build (pull_request) Successful in 1m5s
CI / deploy-smoke (same-origin) (pull_request) Successful in 50s
CI / package-publication (pull_request) Successful in 58s
CI / deploy-smoke (split) (pull_request) Successful in 2m38s
feat(theme): Light/Dark/System, reduced motion, and one radius scale
The theme foundation DESIGN.md requires (ADR 0060):

- A design token may carry a `dark` value and a `reduced` value. The
  token stylesheet selects them in CSS alone: dark under
  prefers-color-scheme unless the host chose light, or when it chose
  dark; motion durations zero under prefers-reduced-motion unless it
  allowed motion, or when it chose reduce. `color-scheme` travels with
  the tokens. System is live, an explicit choice wins, and every module
  root follows through custom-property inheritance, with no module
  contract change.
- @ekklesia/design-tokens adds a small appearance API: system/light/dark
  and system/reduce/allow, parsers for untrusted stored values,
  resolvers, and a controller that applies the attributes immediately.
  The shell applies it before React renders. That is the hook the
  account preferences work will feed; nothing is persisted. The
  Dashboard page declares `color-scheme: light dark` so a dark system
  never flashes white. Displays stay light.
- A designed dark theme: teal-tinted charcoal, never pure black or
  white, the same teal accent.
- WCAG AA contrast is tested for 41 element pairings in both schemes.
  Two light pairings failed and are fixed visibly: text.muted darkens,
  and form controls get a new border.control (3:1).
  background.placeholder keeps skeletons visible in dark.
- Motion tokens (duration-short, duration-ambient) replace literal
  durations and every per-element reduced-motion rule. radius.scale
  multiplies sm/md/lg, unchanged at its default of 1.
- The sign-in card and module loading/error text move from literal
  colours to tokens. Guards keep raw colours and literal radii out of
  Dashboard product source and @ekklesia/ui.
- Fixes ConfirmationDialog's phrase input, which used a class that
  does not exist (`ek-input`) and so was unstyled.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 20:00:39 -05:00

130 lines
6.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
type DesignTokenColorScheme,
type EkklesiaDesignTokenName,
ekklesiaDesignTokens,
} from "../src/index.js";
/**
* WCAG 2.2 AA contrast for every text, control, and focus pairing Ekklesia's elements actually use,
* in both colour schemes (ADR 0060). 4.5:1 for text (§1.4.3); 3:1 for the boundary that identifies a
* form control and for focus indication (§1.4.11). These are computed from the token values, which
* is necessary but not sufficient: representative screens are also rendered in a real browser.
*/
function channels(hex: string): readonly [number, number, number] {
const match = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/u.exec(hex);
if (!match) {
throw new Error(`not an opaque hex colour: ${hex}`);
}
return [match[1], match[2], match[3]].map((part) => Number.parseInt(part ?? "0", 16) / 255) as [
number,
number,
number,
];
}
function luminance(hex: string): number {
const [r, g, b] = channels(hex).map((value) =>
value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4,
) as [number, number, number];
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
function contrastRatio(first: string, second: string): number {
const [lighter, darker] = [luminance(first), luminance(second)].sort((a, b) => b - a) as [
number,
number,
];
return (lighter + 0.05) / (darker + 0.05);
}
type Pairing = readonly [EkklesiaDesignTokenName, EkklesiaDesignTokenName, number, string];
const TEXT = 4.5;
const NON_TEXT = 3;
/** Foreground, background, required ratio, and where it appears. */
const PAIRINGS: readonly Pairing[] = [
["text.primary", "background.page", TEXT, "body text on the page"],
["text.primary", "background.canvas", TEXT, "sidebar and empty states"],
["text.primary", "background.surface", TEXT, "panels, inputs"],
["text.primary", "background.raised", TEXT, "dialogs, menus, toasts"],
["text.primary", "background.inset", TEXT, "technical details"],
["text.secondary", "background.page", TEXT, "descriptions on the page"],
["text.secondary", "background.canvas", TEXT, "quiet navigation items"],
["text.secondary", "background.surface", TEXT, "panel descriptions"],
["text.secondary", "background.raised", TEXT, "dialog descriptions, toast bodies"],
["text.secondary", "background.inset", TEXT, "technical detail text"],
["text.secondary", "state.hover-background", TEXT, "hovered quiet button"],
["text.primary", "state.pressed-background", TEXT, "pressed button"],
["text.muted", "background.page", TEXT, "hints and metadata on the page"],
["text.muted", "background.canvas", TEXT, "group labels in the sidebar"],
["text.muted", "background.surface", TEXT, "field hints, placeholders"],
["text.muted", "background.raised", TEXT, "menu captions, phrase status"],
["accent.foreground", "accent.base", TEXT, "primary button"],
["accent.foreground", "accent.hover", TEXT, "primary button, hovered"],
["accent.foreground", "accent.active", TEXT, "primary button, pressed"],
["accent.strong", "background.page", TEXT, "eyebrow and accent text"],
["accent.strong", "background.surface", TEXT, "accent text in a panel"],
["state.selected-foreground", "state.selected-background", TEXT, "the open page in navigation"],
["text.danger", "status.danger-background", TEXT, "danger button and badge"],
["text.danger", "background.surface", TEXT, "field error"],
["text.danger", "background.canvas", TEXT, "error state title"],
["text.danger", "background.raised", TEXT, "error in a dialog"],
["text.warning", "status.warning-background", TEXT, "warning badge and notice"],
["text.success", "status.success-background", TEXT, "success badge and notice"],
["text.primary", "status.info-background", TEXT, "information notice"],
["text.primary", "status.warning-background", TEXT, "warning notice body"],
["text.primary", "status.danger-background", TEXT, "danger notice body"],
["text.primary", "status.success-background", TEXT, "success notice body"],
["text.primary", "accent.subtle", TEXT, "selected tab"],
["border.control", "background.surface", NON_TEXT, "an input's boundary"],
["border.control", "background.page", NON_TEXT, "an input on the page"],
["border.control", "background.raised", NON_TEXT, "an input in a dialog"],
["text.danger", "background.surface", NON_TEXT, "an invalid input's boundary"],
["accent.focus", "background.page", NON_TEXT, "focus ring on the page"],
["accent.focus", "background.canvas", NON_TEXT, "focus ring in the sidebar"],
["accent.focus", "background.surface", NON_TEXT, "focus ring in a panel"],
["accent.focus", "background.raised", NON_TEXT, "focus ring in a dialog"],
];
describe.each([
"light",
"dark",
] as const satisfies readonly DesignTokenColorScheme[])("WCAG 2.2 AA contrast, %s", (scheme) => {
it.each(PAIRINGS)("%s on %s ≥ %s:1 (%s)", (foreground, background, required) => {
const ratio = contrastRatio(
ekklesiaDesignTokens.valueFor(foreground, scheme),
ekklesiaDesignTokens.valueFor(background, scheme),
);
expect(Number(ratio.toFixed(2))).toBeGreaterThanOrEqual(required);
});
});
describe("the contrast calculator", () => {
it("agrees with WCAG's reference values", () => {
expect(contrastRatio("#000000", "#ffffff")).toBeCloseTo(21, 5);
expect(contrastRatio("#ffffff", "#ffffff")).toBeCloseTo(1, 5);
expect(contrastRatio("#767676", "#ffffff")).toBeCloseTo(4.54, 2);
});
});
describe("decorative placeholders stay visible in both schemes", () => {
// A skeleton is hidden from assistive technology, so WCAG sets no ratio for it; it still has to be
// seen against what it sits on, or it is a blank page rather than a shape of what is coming.
it.each(["light", "dark"] as const)("%s", (scheme) => {
for (const background of [
"background.page",
"background.surface",
"background.canvas",
] as const) {
const ratio = contrastRatio(
ekklesiaDesignTokens.valueFor("background.placeholder", scheme),
ekklesiaDesignTokens.valueFor(background, scheme),
);
expect(ratio, background).toBeGreaterThanOrEqual(1.1);
}
});
});