Skip to content

Commit

Permalink
[IconColor] Distinguish between named and arbitrary colors (#1842)
Browse files Browse the repository at this point in the history
* distinguish between named colors and arbitrary strings

* clean up

* CL
  • Loading branch information
thompsongl authored Apr 16, 2019
1 parent 2b71d39 commit 8360f4a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `10.0.0`.
- Convert `EuiText`, `EuiTextColor` and `EuiTextAlign` to TS ([#1791](https://github.com/elastic/eui/pull/1791))
- Updated `IconColor` type to better distinguish between accepted types ([#1842](https://github.com/elastic/eui/pull/1842))

## [`10.0.0`](https://github.com/elastic/eui/tree/v10.0.0)

Expand All @@ -9,7 +10,6 @@ No public interface changes since `10.0.0`.
- Updated the overflow shadows for `EuiModal` and `EuiFlyout` ([#1829](https://github.com/elastic/eui/pull/1829))
- Added `confirmButtonDisabled` prop to `EuiConfirmModal` ([#1829](https://github.com/elastic/eui/pull/1829))
- Fixed `EuiNavDrawer` overflow scroll behavior on Firefox ([#1837](https://github.com/elastic/eui/pull/1837))
- Convert `EuiText`, `EuiTextColor` and `EuiTextAlign` to TS ([#1791](https://github.com/elastic/eui/pull/1791))

**Bug fixes**

Expand Down
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

0 comments on commit 8360f4a

Please sign in to comment.