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

[EuiBadge] : Handle invalid color entry #4481

Merged
merged 4 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

**Bug fixes**

- Fixed invalid color entry passed to `EuiBadge` color prop ([#4481](https://github.com/elastic/eui/pull/4481))
- Fixed `id` usage throughout `EuiTreeView` to respect custom ids and stop conflicts in generated ids ([#4435](https://github.com/elastic/eui/pull/4435))
- Fixed `EuiTabs` `role` if no tabs are passed in ([#4435](https://github.com/elastic/eui/pull/4435))

Expand Down
91 changes: 45 additions & 46 deletions src/components/badge/badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ export const EuiBadge: FunctionComponent<EuiBadgeProps> = ({
style,
...rest
}) => {
checkValidColor(color);

const isHrefValid = !href || validateHref(href);
const isDisabled = _isDisabled || !isHrefValid;

Expand All @@ -162,48 +160,51 @@ export const EuiBadge: FunctionComponent<EuiBadgeProps> = ({
let colorHex = null;

// Check if a valid color name was provided
if (COLORS.indexOf(color) > -1) {
// Get the hex equivalent for the provided color name
colorHex = colorToHexMap[color];

// Set dark or light text color based upon best contrast
textColor = setTextColor(colorHex);

optionalCustomStyles = {
backgroundColor: colorHex,
color: textColor,
...optionalCustomStyles,
};
} else if (color !== 'hollow') {
// This is a custom color that is neither from the base palette nor hollow
// Let's do our best to ensure that it provides sufficient contrast

// Set dark or light text color based upon best contrast
textColor = setTextColor(color);

// Check the contrast
wcagContrast = getColorContrast(textColor, color);

if (wcagContrast < wcagContrastBase) {
// It's low contrast, so lets show a warning in the console
console.warn(
'Warning: ',
color,
' badge has low contrast of ',
wcagContrast.toFixed(2),
'. Should be above ',
wcagContrastBase,
'.'
);
}
try {
if (COLORS.indexOf(color) > -1) {
// Get the hex equivalent for the provided color name
colorHex = colorToHexMap[color];

// Set dark or light text color based upon best contrast
textColor = setTextColor(colorHex);

optionalCustomStyles = {
backgroundColor: colorHex,
color: textColor,
...optionalCustomStyles,
};
} else if (color !== 'hollow') {
// This is a custom color that is neither from the base palette nor hollow
// Let's do our best to ensure that it provides sufficient contrast

// Set dark or light text color based upon best contrast
textColor = setTextColor(color);

// Check the contrast
wcagContrast = getColorContrast(textColor, color);

if (wcagContrast < wcagContrastBase) {
// It's low contrast, so lets show a warning in the console
console.warn(
'Warning: ',
color,
' badge has low contrast of ',
wcagContrast.toFixed(2),
'. Should be above ',
wcagContrastBase,
'.'
);
}

optionalCustomStyles = {
backgroundColor: color,
color: textColor,
...optionalCustomStyles,
};
optionalCustomStyles = {
backgroundColor: color,
color: textColor,
...optionalCustomStyles,
};
}
} catch (err) {
handleInvalidColor(color);
}

const classes = classNames(
'euiBadge',
{
Expand Down Expand Up @@ -370,12 +371,10 @@ function setTextColor(bgColor: string) {
return textColor;
}

function checkValidColor(color: null | IconColor | string) {
const colorExists = !!color;
function handleInvalidColor(color: null | IconColor | string) {
const isNamedColor = (color && COLORS.includes(color)) || color === 'hollow';
const isValidColorString = color && chromaValid(parseColor(color) || '');

if (!colorExists && !isNamedColor && !isValidColorString) {
if (!isNamedColor && !isValidColorString) {
console.warn(
'EuiBadge expects a valid color. This can either be a three or six ' +
`character hex value, rgb(a) value, hsv value, hollow, or one of the following: ${COLORS}. ` +
Expand Down