Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft PR: Working on issue #7824 #7903

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
22 changes: 13 additions & 9 deletions packages/eui/src/components/badge/badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import { CommonProps, ExclusiveUnion, PropsOf } from '../common';
import { EuiInnerText } from '../inner_text';
import { EuiIcon, IconType } from '../icon';

import { getTextColor, getColorContrast, getIsValidColor } from './color_utils';
import { getTextColor, getIsValidColor } from './color_utils'; // getColorContrast commented out
import { euiBadgeStyles } from './badge.styles';
import { warnIfContrastBelowMin } from '../../services/color/contrast';

export const ICON_SIDES = ['left', 'right'] as const;
type IconSide = (typeof ICON_SIDES)[number];
Expand Down Expand Up @@ -143,14 +144,17 @@ export const EuiBadge: FunctionComponent<EuiBadgeProps> = ({
const textColor = getTextColor(euiTheme, color);

// Check the contrast ratio. If it's low contrast, emit a console awrning
const contrastRatio = getColorContrast(textColor, color);
if (contrastRatio < wcagContrastMin) {
console.warn(
`Warning: ${color} badge has a low contrast of ${contrastRatio.toFixed(
2
)}. Should be above ${wcagContrastMin}.`
);
}
// const contrastRatio = getColorContrast(textColor, color);
// if (contrastRatio < wcagContrastMin) {
// console.warn(
// `Warning: ${color} badge has a low contrast of ${contrastRatio.toFixed(
// 2
// )}. Should be above ${wcagContrastMin}.`
// );
// }

// Call the centralized utility function
warnIfContrastBelowMin(textColor, color, wcagContrastMin);

return {
'--euiBadgeBackgroundColor': color,
Expand Down
30 changes: 30 additions & 0 deletions packages/eui/src/services/color/contrast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,33 @@ export const makeDisabledContrastColor =
}
) =>
makeHighContrastColor(color, ratio)(themeOrBackground);

// packages/eui/src/services/color/contrast.ts


/**
* Gets the contrast ratio between two colors.
* @param {string} textColor - The text color in hexadecimal format.
* @param {string} backgroundColor - The background color in hexadecimal format.
* @returns {number} - The contrast ratio.
*/
export const getColorContrast = (textColor: string, backgroundColor: string): number => {
return chroma.contrast(textColor, backgroundColor);
};

/**
* Warns if the contrast ratio is below a minimum threshold.
* @param {string} textColor - The text color.
* @param {string} backgroundColor - The background color.
* @param {number} wcagContrastMin - Minimum contrast ratio threshold.
*/
export const warnIfContrastBelowMin = (textColor: string, backgroundColor: string, wcagContrastMin: number): void => {
const contrastRatio = getColorContrast(textColor, backgroundColor);
if (contrastRatio < wcagContrastMin) {
console.warn(
`Warning: ${backgroundColor} background with ${textColor} text has a low contrast ratio of ${contrastRatio.toFixed(
2
)}. Should be above ${wcagContrastMin}.`
);
}
};
Loading