-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccessibility.ts
More file actions
148 lines (128 loc) · 3.89 KB
/
accessibility.ts
File metadata and controls
148 lines (128 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Accessibility checker tool
* Validates WCAG contrast ratios for token combinations
*/
import { DesignToken } from '../optics-data.js';
import { checkContrast, ContrastResult } from '../utils/color.js';
export interface ContrastCheckResult {
foregroundToken: string;
backgroundToken: string;
foregroundValue: string;
backgroundValue: string;
contrast: ContrastResult | null;
passes: boolean;
recommendation?: string;
}
/**
* Check contrast between two tokens
*/
export function checkTokenContrast(
foregroundToken: string,
backgroundToken: string,
tokens: DesignToken[]
): ContrastCheckResult {
const fgToken = tokens.find(t => t.name === foregroundToken);
const bgToken = tokens.find(t => t.name === backgroundToken);
if (!fgToken || !bgToken) {
return {
foregroundToken,
backgroundToken,
foregroundValue: '',
backgroundValue: '',
contrast: null,
passes: false,
recommendation: 'Token not found'
};
}
const contrast = checkContrast(fgToken.value, bgToken.value);
if (!contrast) {
return {
foregroundToken,
backgroundToken,
foregroundValue: fgToken.value,
backgroundValue: bgToken.value,
contrast: null,
passes: false,
recommendation: 'Unable to calculate contrast (non-color tokens?)'
};
}
const passes = contrast.wcagAA;
let recommendation = '';
if (!passes) {
recommendation = findBetterTokenCombination(fgToken, tokens, bgToken.value);
}
return {
foregroundToken,
backgroundToken,
foregroundValue: fgToken.value,
backgroundValue: bgToken.value,
contrast,
passes,
recommendation
};
}
/**
* Find better token combination with sufficient contrast
*/
function findBetterTokenCombination(
currentToken: DesignToken,
allTokens: DesignToken[],
backgroundValue: string
): string {
const colorTokens = allTokens.filter(t => t.category === 'color');
for (const token of colorTokens) {
const contrast = checkContrast(token.value, backgroundValue);
if (contrast && contrast.wcagAA) {
return `Try using ${token.name} (${token.value}) for better contrast`;
}
}
return 'No alternative tokens found with sufficient contrast';
}
/**
* Format contrast check result
*/
export function formatContrastResult(result: ContrastCheckResult): string {
const lines: string[] = [
'# Contrast Check Result',
'',
`**Foreground**: ${result.foregroundToken} (\`${result.foregroundValue}\`)`,
`**Background**: ${result.backgroundToken} (\`${result.backgroundValue}\`)`,
''
];
if (result.contrast) {
lines.push(`**Contrast Ratio**: ${result.contrast.ratio}:1`);
lines.push(`**WCAG AA**: ${result.contrast.wcagAA ? '✓ Pass' : '✗ Fail'}`);
lines.push(`**WCAG AAA**: ${result.contrast.wcagAAA ? '✓ Pass' : '✗ Fail'}`);
lines.push(`**Score**: ${result.contrast.score}`);
lines.push('');
if (!result.passes && result.recommendation) {
lines.push('## Recommendation');
lines.push(result.recommendation);
}
} else {
lines.push('✗ Unable to calculate contrast');
if (result.recommendation) {
lines.push(`**Reason**: ${result.recommendation}`);
}
}
return lines.join('\n');
}
/**
* Check all color token combinations for a given background
*/
export function checkAllCombinations(
backgroundToken: string,
tokens: DesignToken[]
): ContrastCheckResult[] {
const bgToken = tokens.find(t => t.name === backgroundToken);
if (!bgToken) return [];
const colorTokens = tokens.filter(t => t.category === 'color' && t.name !== backgroundToken);
const results: ContrastCheckResult[] = [];
for (const fgToken of colorTokens) {
results.push(checkTokenContrast(fgToken.name, backgroundToken, tokens));
}
return results.sort((a, b) => {
if (!a.contrast || !b.contrast) return 0;
return b.contrast.ratio - a.contrast.ratio;
});
}