Follow-ups from the reference site's first visual test: - data-theme="dark"/"light" switches the tokens on any element (LIGHT_SELECTOR, DARK_SELECTOR as selector lists); the base layer colours a themed panel's text - inverted sections re-map primary / primary-hover / primary-text to the new primary-inverse / primary-inverse-text roles; the contrast report checks them (inherited roles resolved on the ancestor, as the cascade does) - ipal-form formClasses.ts on token utilities + buttonClass(); theme.md: one-icon ThemeSwitcher pattern (the default two-option toggle stays) Co-Authored-By: Claude Opus 5.5 <[email protected]>
80 lines
3.7 KiB
TypeScript
80 lines
3.7 KiB
TypeScript
import { type ColorRole, INVERSE_SCOPE } from './catalog.js'
|
|
import { contrastRatio, parseColor } from './color.js'
|
|
import { resolveColors, type TokensInput } from './define.js'
|
|
|
|
/** One checked pair. `level: 'warn'` pairs are reported but do not fail the check. */
|
|
export type ContrastRow = {
|
|
bg: string
|
|
fg: string
|
|
level: 'error' | 'warn'
|
|
min: number
|
|
pass: boolean
|
|
/** null = a colour could not be parsed (reported, not failed). */
|
|
ratio: null | number
|
|
scope: 'default' | 'inverse'
|
|
theme: 'dark' | 'light'
|
|
}
|
|
|
|
type Pair = { bg: ColorRole; fg: ColorRole; level?: 'warn'; min: number }
|
|
|
|
/** Text 4.5:1 (WCAG 1.4.3 AA), focus rings and UI parts 3:1 (1.4.11). */
|
|
const PAIRS: Pair[] = [
|
|
...(['bg', 'surface', 'surface-muted', 'surface-raised'] as const).flatMap((bg) => [
|
|
{ bg, fg: 'text' as const, min: 4.5 },
|
|
{ bg, fg: 'text-strong' as const, min: 4.5 },
|
|
{ bg, fg: 'text-muted' as const, min: 4.5 },
|
|
{ bg, fg: 'link' as const, min: 4.5 },
|
|
]),
|
|
{ bg: 'primary', fg: 'primary-text', min: 4.5 },
|
|
{ bg: 'primary-hover', fg: 'primary-text', min: 4.5 },
|
|
{ bg: 'secondary', fg: 'secondary-text', min: 4.5 },
|
|
{ bg: 'accent', fg: 'accent-text', min: 4.5 },
|
|
{ bg: 'surface-inverse', fg: 'text-inverse', min: 4.5 },
|
|
{ bg: 'surface-inverse', fg: 'link-inverse', min: 4.5 },
|
|
{ bg: 'bg', fg: 'focus', min: 3 },
|
|
{ bg: 'surface-inverse', fg: 'focus-inverse', min: 3 },
|
|
...(['success', 'warning', 'danger', 'info'] as const).flatMap((state) => [
|
|
{ bg: 'bg' as const, fg: state, min: 4.5 },
|
|
{ bg: `${state}-surface` as const, fg: state, min: 4.5 },
|
|
]),
|
|
]
|
|
|
|
/** Content inside an inverted section: muted text and links on the inverse surface. */
|
|
const INVERSE_PAIRS: Pair[] = [
|
|
// primary is re-mapped to primary-inverse: a dark brand colour needs a lighter one here
|
|
{ bg: 'surface-inverse', fg: 'primary', level: 'warn', min: 3 },
|
|
{ bg: 'primary', fg: 'primary-text', min: 4.5 },
|
|
{ bg: 'surface-inverse', fg: 'text-muted', min: 4.5 },
|
|
{ bg: 'surface', fg: 'text', min: 4.5 },
|
|
{ bg: 'surface', fg: 'link', min: 4.5 },
|
|
]
|
|
|
|
/**
|
|
* WCAG contrast for every text/background pair the token roles promise, in each
|
|
* theme and inside the inverse scope. `ipal-tokens` prints it and fails on an error row.
|
|
*/
|
|
export function contrastReport(input: TokensInput = {}): ContrastRow[] {
|
|
const themes = input.theme === 'light' || input.theme === 'dark' ? [input.theme] : (['light', 'dark'] as const)
|
|
const resolved = resolveColors(input)
|
|
return themes.flatMap((theme) => {
|
|
const colors: Record<string, string> = resolved[theme]
|
|
const outer = (name: string) => (name.startsWith('ipal-') ? colors[name.slice(5)] : undefined)
|
|
// Roles the scope does not declare are inherited, i.e. computed on the ancestor: resolve them there first.
|
|
const inherited = Object.fromEntries(
|
|
Object.entries(colors).flatMap(([role, value]) => {
|
|
const rgb = parseColor(value, outer)
|
|
return rgb ? [[role, `rgb(${rgb.r * 255} ${rgb.g * 255} ${rgb.b * 255} / ${rgb.a})`]] : []
|
|
}),
|
|
)
|
|
const inverse: Record<string, string> = { ...inherited, ...INVERSE_SCOPE }
|
|
const check = (map: Record<string, string>, scope: ContrastRow['scope']) => (pair: Pair): ContrastRow => {
|
|
const lookup = (name: string) => (name.startsWith('ipal-') ? map[name.slice(5)] : undefined)
|
|
const fg = parseColor(map[pair.fg], lookup)
|
|
const bg = parseColor(map[pair.bg], lookup)
|
|
const ratio = fg && bg ? Math.round(contrastRatio(fg, bg) * 100) / 100 : null
|
|
return { bg: pair.bg, fg: pair.fg, level: pair.level ?? 'error', min: pair.min, pass: ratio === null || ratio >= pair.min, ratio, scope, theme }
|
|
}
|
|
return [...PAIRS.map(check(colors, 'default')), ...INVERSE_PAIRS.map(check(inverse, 'inverse'))]
|
|
})
|
|
}
|