-
Notifications
You must be signed in to change notification settings - Fork 855
Description
Summary
Within the Avatar component, for the purposes of rendering initials, the text colour is selected based either the selected or generated background colour:
eui/packages/eui/src/components/avatar/avatar.tsx
Lines 164 to 168 in 8e35923
const assignedColor = | |
color || visColors[Math.floor(name.length % visColors.length)]; | |
const textColor = isColorDark(...hexToRgb(assignedColor)) | |
? '#FFFFFF' | |
: '#000000'; |
Currently, the logic simplistically chooses white (for dark backgrounds) or black (for light backgrounds), this won't necessarily provide a sufficient contrast against some background colours. Neither is there support for explicitly setting the text colour.
A similar issue applies to the Badge component:
eui/packages/eui/src/components/badge/color_utils.ts
Lines 60 to 66 in 7fac1ce
export const getTextColor = ({ euiTheme }: UseEuiTheme, bgColor: string) => { | |
const textColor = isColorDark(...chroma(bgColor).rgb()) | |
? euiTheme.colors.ghost | |
: euiTheme.colors.ink; | |
return textColor; | |
}; |
Albeit inconsistent with Avatar, the Badge component does at least raise a contrast warning:
eui/packages/eui/src/components/badge/badge.tsx
Lines 142 to 153 in 7fac1ce
// Set dark or light text color based upon best contrast | |
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}.` | |
); | |
} |
Acceptance Criteria
- Calculate an appropriate high-contrasting colour (WCAG AA compliant) based on the background colour
- (Optionally) expose an additional property to allow the colour to be overridden
Fortunately, an existing utility function already exists within the EUI component library for calculating an appropriate contrasting colour: makeHighContrastColor.