Skip to content

Commit 617dd30

Browse files
committed
[PR feedback] Extract WCAG 4.5 ratio const to shared export
1 parent 8d572eb commit 617dd30

3 files changed

Lines changed: 9 additions & 6 deletions

File tree

src/components/badge/badge.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
euiPaletteColorBlindBehindText,
2525
getSecureRelForTarget,
2626
isColorDark,
27+
wcagContrastMin,
2728
} from '../../services';
2829
import { EuiInnerText } from '../inner_text';
2930
import { EuiIcon, IconType } from '../icon';
@@ -149,8 +150,7 @@ export const EuiBadge: FunctionComponent<EuiBadgeProps> = ({
149150
};
150151

151152
let textColor = null;
152-
const wcagContrastBase = 4.5; // WCAG AA contrast level
153-
let wcagContrast = null;
153+
let contrastRatio = null;
154154
let colorHex = null;
155155

156156
try {
@@ -176,17 +176,17 @@ export const EuiBadge: FunctionComponent<EuiBadgeProps> = ({
176176
textColor = setTextColor(euiTheme, color);
177177

178178
// Check the contrast
179-
wcagContrast = getColorContrast(textColor, color);
179+
contrastRatio = getColorContrast(textColor, color);
180180

181-
if (wcagContrast < wcagContrastBase) {
181+
if (contrastRatio < wcagContrastMin) {
182182
// It's low contrast, so lets show a warning in the console
183183
console.warn(
184184
'Warning: ',
185185
color,
186186
' badge has low contrast of ',
187-
wcagContrast.toFixed(2),
187+
contrastRatio.toFixed(2),
188188
'. Should be above ',
189-
wcagContrastBase,
189+
wcagContrastMin,
190190
'.'
191191
);
192192
}

src/services/color/contrast.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import chroma from 'chroma-js';
1010
import { shade, tint, lightness as getLightness } from './manipulation';
1111
import { getOn } from '../theme/utils';
1212

13+
export const wcagContrastMin = 4.5; // WCAG AA minimum contrast ratio for normal (non-large) text
14+
1315
/**
1416
* Creates a new color that meets or exceeds WCAG level AA
1517
* @param foreground - Color to manipulate

src/services/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export {
6969
lightness,
7070
makeHighContrastColor,
7171
makeDisabledContrastColor,
72+
wcagContrastMin,
7273
} from './color';
7374

7475
export type { EuiSetColorMethod } from './color_picker';

0 commit comments

Comments
 (0)