Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/cli/src/linter/linter/rules/contrast-ratio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,34 @@ describe('contrastCheck', () => {
const contrastWarnings = contrastCheck(state).filter(d => d.message.includes('contrast'));
expect(contrastWarnings.length).toBe(0);
});

it('does not judge a translucent backgroundColor against the opaque text color', () => {
// #195: a 10% tint of the text color was compared as if it were opaque
// and reported 1.00:1. What shows through depends on the surface beneath.
const state = buildState({
colors: { warn: '#f2b33d', 'warn-tint': '#f2b33d1a' },
components: {
alert: { backgroundColor: '{colors.warn-tint}', textColor: '{colors.warn}' },
},
});
const findings = contrastCheck(state);
expect(findings.length).toBe(1);
expect(findings[0]!.severity).toBe('info');
expect(findings[0]!.path).toBe('components.alert');
expect(findings[0]!.message).toMatch(/translucent/);
expect(findings[0]!.message).not.toMatch(/below WCAG AA/);
});

it('still checks a backgroundColor whose alpha is fully opaque', () => {
const state = buildState({
colors: { yellow: '#ffff00ff', white: '#ffffff' },
components: {
'button-bad': { backgroundColor: '{colors.yellow}', textColor: '{colors.white}' },
},
});
const findings = contrastCheck(state);
expect(findings.length).toBe(1);
expect(findings[0]!.severity).toBeUndefined();
expect(findings[0]!.message).toMatch(/below WCAG AA/);
});
});
11 changes: 11 additions & 0 deletions packages/cli/src/linter/linter/rules/contrast-ratio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ export function contrastCheck(state: DesignSystemState): RuleFinding[] {
const textColor = resolveToColor(textValue);
if (!bgColor || !textColor) continue;

// A translucent background shows whatever surface sits beneath it, and
// the component does not say what that is, so the ratio cannot be judged.
if (bgColor.a !== undefined && bgColor.a < 1) {
findings.push({
path: `components.${compName}`,
severity: 'info',
message: `backgroundColor (${bgColor.hex}) is translucent, so the contrast of textColor (${textColor.hex}) depends on the surface beneath it and was not checked.`,
});
continue;
}

const ratio = contrastRatio(bgColor, textColor);
if (ratio < WCAG_AA_MINIMUM) {
findings.push({
Expand Down