Skip to content

fix(theme-neutral): Badge error label meets WCAG AA (#4414) - #4446

Merged
cixzhang merged 2 commits into
meta:mainfrom
AKnassa:rocky/issue-4414-badge-error-contrast
Aug 24, 2026
Merged

cixzhang merged 2 commits into
meta:mainfrom
AKnassa:rocky/issue-4414-badge-error-contrast

Conversation

@AKnassa

@AKnassa AKnassa commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #4414.

<Badge variant="error"> renders white on the neutral theme's light-mode red #e33f4a at 4.14:1. The label is 12px at weight 500, so WCAG 2.1 AA asks for 4.5:1 — the 3:1 large-text allowance doesn't apply. This is the color-contrast violation the a11y audit reports as "Badge, affects 2/5 stories" on essentially every PR that touches packages/core/src/Badge/; those two stories are exactly the ones containing an error badge.

Flipping the label to dark doesn't rescue it either — #171717 on #e33f4a is 4.33:1, still short. So the fill moves one tonal step down.

fill label ratio
before #e33f4a #ffffff 4.14:1
after #c9303a #ffffff 5.29:1

#c9303a holds the hue — OKLCH H 21.9 → 22.8, C 0.200 → 0.189. Dark mode is untouched: dark text on #ff705d is 6.60:1.

What else moved, and what didn't

StatusDot and the ProgressBar --color-error rebinding move with it. Both are commented in neutralTheme.ts as tracking the badge fill so "a dot and its badge read as one status language". Leaving them on the old red would have broken that stated invariant.

--shadow-inset-error deliberately keeps #e33f4a4D. It's a 30%-alpha focus ring, not a text surface, so the 4.5:1 threshold never applied to it and changing it would be a visual change with no accessibility gain.

The file header claimed "All 9 saturated badge values pass WCAG AA (5.6–9.6 contrast range)", which wasn't true of error. It now points at the guard instead of restating a number.

The guard

scripts/check-badge-contrast.test.mjs resolves every theme's badge label/fill pair through light-dark(), var() indirection (recursively, with a cycle guard) and alpha compositing over the theme's own surface token, then holds all of them to 4.5:1.

Resolution has to go through the component block: each theme re-binds the fill inside its own components.badge, so the pairing that actually ships cannot be read off the global --color-* tokens. Reading the tokens is what makes this bug easy to misdiagnose.

Pre-existing shortfalls this surfaced

The issue asked for the check to be run per theme. Doing that found four more, in themes I have not touched:

theme / scheme variant pair ratio
butter, both info #ffffff on #4883fd 3.54:1
butter, both neutral #225BFF on #ffee7b 4.43:1
butter, both error #ffffff on #fc473b 3.44:1
gothic, both warning #6c5010 on #d3c490 4.32:1

Both themes pin those fills to brand values from a spec ("pinned to the brand colors from the spec"), so moving them is a design call rather than a bug fix — left for maintainers. They're recorded as an exact known-gap list, so a new shortfall fails the build and a fixed one fails until it's removed from the list. Happy to follow up with values if you want them changed.

chocolate and matcha define no badge block and inherit core's defaults, so they have no pair to resolve; the guard asserts that set explicitly rather than silently covering fewer themes than it looks like.

Note on the regenerated template

packages/cli/assets/templates/themes/neutral/neutralTheme.ts is generated by pnpm bundle:cli-themes. After merging main (which moved the templates into assets/ and regenerated them, picking up the previously-drifted statusdot block), the template diff mirrors the source change exactly.

Tests

scripts/check-badge-contrast.test.mjs — 4 tests. Mutation-checked: restoring #e33f4a fails 3 of them.
Badge, StatusDot and scripts suites green. pnpm check:repo clean.

`<Badge variant="error">` rendered white on the neutral theme's light-mode
red `#e33f4a` at 4.14:1. The label is 12px at weight 500, so WCAG 2.1 AA
asks for 4.5:1 — the 3:1 large-text allowance does not apply. This is the
`color-contrast` violation the a11y audit reported as "Badge, affects 2/5
stories" on every PR touching `packages/core/src/Badge/`; those two stories
are exactly the ones containing an error badge.

Flipping the label to dark does not work either — `#171717` on `#e33f4a` is
4.33:1, still short. So the fill moves one tonal step down to `#c9303a`,
which reaches 5.29:1 against white while holding the hue (OKLCH H 21.9 ->
22.8, C 0.200 -> 0.189).

StatusDot and the ProgressBar `--color-error` rebinding move with it. Both
are commented in this file as tracking the badge fill so a dot and its badge
read as one status language; leaving them behind would have broken that
stated invariant. Dark mode is untouched — dark text on `#ff705d` is 6.60:1.

`--shadow-inset-error` deliberately keeps `#e33f4a4D`: it is a 30%-alpha
focus ring, not a text surface, so the 4.5:1 threshold never applied to it.

The file header claimed "All 9 saturated badge values pass WCAG AA (5.6-9.6
contrast range)", which was not true of error. It now points at the guard.

Adds `scripts/check-badge-contrast.test.mjs`: it resolves every theme's
badge label/fill pair through `light-dark()`, `var()` indirection and alpha
compositing over the theme's own surface, then holds all of them to 4.5:1.
The pairing cannot be read off the global `--color-*` tokens, because each
theme re-binds the fill inside its own `components.badge` block.

Running it across all seven themes surfaced four pre-existing shortfalls
beyond the reported one, recorded as an exact known-gap list so no new
shortfall can land and no fixed one can linger:

  butter/light+dark variant:info    #ffffff on #4883fd  3.54:1
  butter/light+dark variant:neutral #225BFF on #ffee7b  4.43:1
  butter/light+dark variant:error   #ffffff on #fc473b  3.44:1
  gothic/light+dark variant:warning #6c5010 on #d3c490  4.32:1

Those fills are pinned to brand values from a spec, so moving them is a
design call rather than a bug fix and is left to maintainers.

The regenerated CLI theme template is larger than the source change because
it had already drifted: it was missing the `statusdot` block entirely.

Fixes meta#4414
@vercel

vercel Bot commented Jul 28, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Aug 7, 2026 2:44am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 28, 2026
@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge needs:design-review Affects visuals — Design should review labels Jul 28, 2026
@github-actions

github-actions Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

No new or modified components detected.

Bundle Size Summary

No component packages changed.

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@AKnassa
AKnassa marked this pull request as ready for review July 30, 2026 04:57
Regenerated packages/cli/assets/templates/themes/neutral/neutralTheme.ts
via pnpm bundle:cli-themes from the merged theme source (templates moved
to assets/ in the cli restructure; upstream's regenerated template had
the statusdot block with the pre-fix red).

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — 4.14:1 to 5.29:1 with the hue held, dark mode untouched, and a test that holds every theme to it. Landing.

[Reviewed by Robohands]

@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Aug 24, 2026
@cixzhang
cixzhang merged commit 1abeec7 into meta:main Aug 24, 2026
21 checks passed
@github-actions
github-actions Bot deleted the rocky/issue-4414-badge-error-contrast branch August 24, 2026 06:53
cixzhang pushed a commit that referenced this pull request Aug 24, 2026
* fix(theme): give syntax punctuation its own AA-passing colour

Fixes #5386.

--color-syntax-punctuation borrowed --color-text-disabled, a token WCAG
deliberately exempts from the normal-text contrast requirement. Three
themes measured under 4.5:1 against their syntax background: neutral
(2.42:1 light, 2.53:1 dark), chocolate (3.06:1/2.56:1), matcha
(3.83:1/2.73:1). butter/gothic/stone/y2k already had a passing punctuation
colour of their own.

The shared default now points at --color-text-secondary (already used
for --color-syntax-comment, already AA). The three failing themes get a
dedicated punctuation colour instead of inheriting the default, since
each defines its own syntax palette:
  neutral   #6e6e6e / #a0a0a0  (4.89:1 / 7.57:1)
  chocolate #9e622e / #cb884d  (4.84:1 / 6.12:1)
  matcha    #566a39 / #92af6a  (5.19:1 / 7.02:1)

Adds scripts/check-syntax-punctuation-contrast.test.mjs, resolving
--color-syntax-punctuation/background through light-dark()/var()
indirection (the pattern from #4446's badge contrast guard) and holding
every theme to AA in both colour schemes.

Regenerated the CLI theme template bundle (pnpm bundle:cli-themes) for
the three changed themes.

* fix(theme): fail loud on an unresolvable colour format in the contrast guard

Per @gonzoblasco's review on #5414: resolve() returned null for anything
that wasn't hex, so a theme using oklch()/hsl()/rgb() would silently
drop out of the resolved rows with no clue why — the length guard
would just report the wrong count with no hint which theme or format
broke it. Throw with the actual unresolvable value instead.

All seven themes still resolve cleanly through hex/light-dark()/var()
today, so this is a no-op change in current behavior.

This branch was successfully deployed

1 active deployment
Preview — 6f60885f Deployed Aug 7, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team) needs:design-review Affects visuals — Design should review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Badge variant="error" label fails WCAG AA contrast (4.14:1) in the neutral theme

2 participants