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

[IconColor] Distinguish between named and arbitrary colors #1842

Merged
merged 4 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions src/components/icon/icon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ describe('EuiIcon', () => {
});

describe('color', () => {
COLORS.concat([
[
...COLORS,
'#fde',
'#885522',
'rgb(100, 150, 200)',
'hsla(270, 60%, 70%, 0.9)',
]).forEach(color => {
].forEach(color => {
test(`${color} is rendered`, () => {
const component = render(<EuiIcon color={color} />);

Expand Down
14 changes: 10 additions & 4 deletions src/components/icon/icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ export const TYPES: IconType[] = keysOf(typeToIconMap);

export type IconType = keyof typeof typeToIconMap;

const colorToClassMap: { [color: string]: string | null } = {
const colorToClassMap = {
default: null,
primary: 'euiIcon--primary',
secondary: 'euiIcon--secondary',
Expand All @@ -616,10 +616,16 @@ const colorToClassMap: { [color: string]: string | null } = {
ghost: 'euiIcon--ghost',
};

export const COLORS: IconColor[] = keysOf(colorToClassMap);
export const COLORS: NamedColor[] = keysOf(colorToClassMap);

type NamedColor = keyof typeof colorToClassMap;

function isNamedColor(name: string): name is NamedColor {
return colorToClassMap.hasOwnProperty(name);
}

// We accept arbitrary color strings, which are impossible to type.
export type IconColor = string | keyof typeof colorToClassMap;
export type IconColor = string | NamedColor;

const sizeToClassNameMap = {
original: null,
Expand Down Expand Up @@ -659,7 +665,7 @@ export const EuiIcon: FunctionComponent<Props> = ({
let optionalCustomStyles = null;

if (color) {
if (COLORS.indexOf(color) > -1) {
if (isNamedColor(color)) {
optionalColorClass = colorToClassMap[color];
} else {
optionalCustomStyles = { fill: color };
Expand Down