Fix color-contrast in React
React component libraries often introduce color-contrast regressions via shared <Badge>, <HelperText>, or <Button variant='secondary'> primitives that lower opacity (opacity: 0.5) on active interactive elements or read unverified theme tokens. Because axe-core computes the effective composited foreground against the painted background, fixing the shared React primitive eliminates every instance across the SPA.
Before and after
// Broken React component: low-contrast muted caption & pale badge
interface ShippingNoticeProps {
thresholdUsd: number;
}
export function ShippingNotice({ thresholdUsd }: ShippingNoticeProps) {
return (
<div style={{ backgroundColor: "#f9fafb", padding: "1rem" }}>
<span style={{ backgroundColor: "#34d399", color: "#ffffff", padding: "2px 8px", borderRadius: 4 }}>
In Stock
</span>
<p style={{ color: "#9ca3af", fontSize: "0.875rem", marginTop: "0.5rem" }}>
Free shipping on orders over ${thresholdUsd}.
</p>
</div>
);
}// Fixed React component: verified AA contrast tokens (5.48:1 badge, 9.86:1 text)
const CONTRAST_TOKENS = {
surface: "#f9fafb",
textSecondary: "#374151",
badgeSuccessBg: "#047857",
badgeSuccessFg: "#ffffff",
} as const;
interface ShippingNoticeProps {
thresholdUsd: number;
}
export function ShippingNotice({ thresholdUsd }: ShippingNoticeProps) {
return (
<div style={{ backgroundColor: CONTRAST_TOKENS.surface, padding: "1rem" }}>
<span
style={{
backgroundColor: CONTRAST_TOKENS.badgeSuccessBg,
color: CONTRAST_TOKENS.badgeSuccessFg,
padding: "2px 8px",
borderRadius: 4,
fontWeight: 600,
}}
>
In Stock
</span>
<p style={{ color: CONTRAST_TOKENS.textSecondary, fontSize: "0.875rem", marginTop: "0.5rem" }}>
Free shipping on orders over ${thresholdUsd}.
</p>
</div>
);
}Fixed vs. broken example
Inspect the foreground and background colors in Chrome DevTools Color Picker to verify the live WCAG AA (4.5:1 normal text, 3:1 large text) contrast badges.
Playwright + axe-core check
import { test, expect } from "@playwright/test";
import AxeBuilder from "@axe-core/playwright";
test("React checkout & shipping notices meet 4.5:1 contrast", async ({ page }) => {
await page.goto("/checkout");
await page.waitForSelector("[data-testid='shipping-notice']");
const results = await new AxeBuilder({ page })
.withRules(["color-contrast"])
.analyze();
expect(results.violations).toEqual([]);
});Template deduplication tip
A low-contrast <StatusBadge /> or <FormHelperText /> reused across 40 React routes produces dozens of duplicate page errors. A11yDrift normalizes CSS-module hashes (e.g. .Badge_muted__3xK9p) so all occurrences merge into a single component cluster.
Who this affects
Low-vision readers, older users with presbyopia, people with color-vision deficiencies, and anyone viewing a screen in bright sunlight struggle or fail to read muted gray captions, pale badges, and low-contrast CTA buttons.
NVDA & VoiceOver transcript
Screen readers announce the same copy for both versions; this rule primarily impacts low-vision readers, screen magnifiers, and visual legibility.
NVDA (Windows)
Screen readers still speak "Free shipping on orders over $75" and "Add to Cart, button", but screen magnifier and low-vision users cannot visually locate or read the faded copy and pale blue button.
Screen readers announce "Free shipping on orders over $75" and "Add to Cart, button", while sighted low-vision and ZoomText users clearly distinguish both the 7.58:1 body copy and 5.93:1 CTA.
VoiceOver (macOS / iOS)
VoiceOver speaks "Add to Cart, button", yet users combining VoiceOver with Zoom or macOS Increase Contrast experience severe glare washout on the 2.14:1 sky-blue fill.
VoiceOver speaks "Add to Cart, button", and the 5.93:1 darkened surface stays legible across macOS Zoom, mobile sunlight, and high-brightness displays.
Common questions
Why does axe-core flag my React text even though my base color is dark?
Are disabled React buttons exempt from WCAG 1.4.3 color-contrast?
Can jsdom + jest-axe reliably test color-contrast in React unit tests?
Test a URL for color-contrast
Enter any public URL for a free WCAG 2.2 AA check—no account or credit card required.
Shipping React weekly? Block new color-contrast regressions in PRs with our baseline-diff GitHub Action PR gate (≤20 URLs/PR).