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