Skip to content

Commit

Permalink
Re-adding custom colors as style
Browse files Browse the repository at this point in the history
  • Loading branch information
miukimiu committed Jun 28, 2022
1 parent c4861d0 commit 3b57233
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
12 changes: 8 additions & 4 deletions src/components/icon/__snapshots__/icon.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ exports[`EuiIcon is rendered 1`] = `
exports[`EuiIcon props color #885522 is rendered 1`] = `
<svg
aria-hidden="true"
class="euiIcon euiIcon-isLoaded emotion-euiIcon-m-customColor-isLoaded"
class="euiIcon euiIcon-isLoaded emotion-euiIcon-m-isLoaded"
data-icon-type="search"
focusable="false"
height="16"
role="img"
style="color: rgb(136, 85, 34);"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
Expand All @@ -40,11 +41,12 @@ exports[`EuiIcon props color #885522 is rendered 1`] = `
exports[`EuiIcon props color #fde is rendered 1`] = `
<svg
aria-hidden="true"
class="euiIcon euiIcon-isLoaded emotion-euiIcon-m-customColor-isLoaded"
class="euiIcon euiIcon-isLoaded emotion-euiIcon-m-isLoaded"
data-icon-type="search"
focusable="false"
height="16"
role="img"
style="color: rgb(255, 221, 238);"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
Expand Down Expand Up @@ -130,11 +132,12 @@ exports[`EuiIcon props color ghost is rendered 1`] = `
exports[`EuiIcon props color hsla(270, 60%, 70%, 0.9) is rendered 1`] = `
<svg
aria-hidden="true"
class="euiIcon euiIcon-isLoaded emotion-euiIcon-m-customColor-isLoaded"
class="euiIcon euiIcon-isLoaded emotion-euiIcon-m-isLoaded"
data-icon-type="search"
focusable="false"
height="16"
role="img"
style="color: hsla(270, 60%, 70%, 0.9);"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
Expand Down Expand Up @@ -184,11 +187,12 @@ exports[`EuiIcon props color primary is rendered 1`] = `
exports[`EuiIcon props color rgb(100, 150, 200) is rendered 1`] = `
<svg
aria-hidden="true"
class="euiIcon euiIcon-isLoaded emotion-euiIcon-m-customColor-isLoaded"
class="euiIcon euiIcon-isLoaded emotion-euiIcon-m-isLoaded"
data-icon-type="search"
focusable="false"
height="16"
role="img"
style="color: rgb(100, 150, 200);"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
Expand Down
9 changes: 1 addition & 8 deletions src/components/icon/icon.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import { css, keyframes } from '@emotion/react';
import { logicalCSS, euiCanAnimate } from '../../global_styling';
import { UseEuiTheme } from '../../services';
import { EuiIconProps } from './icon';

const _iconSize = (size: string) => {
return `
Expand All @@ -30,10 +29,7 @@ const _iconLoading = keyframes`
}
`;

export const euiIconStyles = (
{ euiTheme }: UseEuiTheme,
color: EuiIconProps['color']
) => ({
export const euiIconStyles = ({ euiTheme }: UseEuiTheme) => ({
// Base
euiIcon: css`
flex-shrink: 0; // Ensures it never scales down below its intended size
Expand Down Expand Up @@ -94,9 +90,6 @@ export const euiIconStyles = (
color: inherit;
`,
default: css``,
customColor: css`
color: ${color};
`,
logoElasticOutline: css`
// Elastic logo specific colors
*[fill] {
Expand Down
14 changes: 12 additions & 2 deletions src/components/icon/icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import React, {
ImgHTMLAttributes,
ComponentType,
SVGAttributes,
CSSProperties,
} from 'react';
import classNames from 'classnames';

Expand Down Expand Up @@ -240,13 +241,21 @@ export class EuiIconClass extends PureComponent<
title,
onIconLoad,
theme,
style,
...rest
} = this.props;

const { isLoading, neededLoading } = this.state;

const isCustomColor = color && !isNamedColor(color);

const optionalCustomStyles: CSSProperties | undefined = isCustomColor
? {
color: color,
...style,
}
: style;

// These icons are a little special and get some extra CSS flexibility
const isAppIcon = getIsAppIcon(type);
// App color styles are only applied if no color is passed or if color="default" is passed
Expand All @@ -264,12 +273,12 @@ export class EuiIconClass extends PureComponent<
className
);

const styles = euiIconStyles(theme, color);
// Emotion styles
const styles = euiIconStyles(theme);
const cssStyles = [
styles.euiIcon,
styles[size],
color && isNamedColor(color) && styles[color as NamedColor],
isCustomColor && styles.customColor,

This comment was marked as resolved.

Copy link
@cee-chen

cee-chen Jun 28, 2022

Member

IMO we should still keep customColor in with a blank css`` for the actual styles, so that euiIcon-[...]-customColor shows up as the className. It's not super necessary but I think it's helpful to be explicit

isElasticLogoOutline && styles.logoElasticOutline,
isAppIcon && !appIconHasColor && styles.app,
isLoading && styles.isLoading,
Expand Down Expand Up @@ -326,6 +335,7 @@ export class EuiIconClass extends PureComponent<
return (
<Svg
className={classes}
style={optionalCustomStyles}
css={cssStyles}
tabIndex={tabIndex}
focusable={focusable}
Expand Down

0 comments on commit 3b57233

Please sign in to comment.