Skip to content

Commit

Permalink
feat(icon): add support to size and color
Browse files Browse the repository at this point in the history
Co-authored-by: @jimmyandrade @lauralucca
  • Loading branch information
Jimmy Andrade authored and arielwb committed Sep 4, 2020
1 parent 45a1f3f commit a388255
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/common/themeSelectors/colors/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ export const getColorHighEmphasis = (theme: Theme) => getColors(theme).colorHigh
export const getColorSurface = (theme: Theme) => getColors(theme).colorSurface;

export const getColorHighlight = (theme: Theme) => getColors(theme).colorHighlight;

export const getColorByName = (theme: Theme, colorName: string) => getColors(theme)[colorName];
49 changes: 48 additions & 1 deletion src/components/Icon/Icon.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { select, text } from '@storybook/addon-knobs';

import { ContainerRow } from '../../common/HelperComponents/ThemeHelper.styles';
import { Icon, IconColors } from './Icon';
import { Icon, IconColors, IconSizes } from './Icon';

export default {
component: Icon,
Expand All @@ -14,22 +14,69 @@ export default {

export const all = () => (
<ContainerRow style={{ justifyContent: 'space-around', padding: 20 }}>
<Icon />
<Icon color="primary" />
<Icon color="primary" name="filled-action-add" />
<Icon color="default" name="filled-action-add" />
</ContainerRow>
);

/* eslint-disable sort-keys */
const iconColors = {
default: 'default',
primary: 'primary',
onPrimary: 'onPrimary',
primaryLight: 'primaryLight',
onPrimaryLight: 'onPrimaryLight',
primaryDark: 'primaryDark',
onPrimaryDark: 'onPrimaryDark',
secondary: 'secondary',
onSecondary: 'onSecondary',
secondaryLight: 'secondaryLight',
onSecondaryLight: 'onSecondaryLight',
secondaryDark: 'secondaryDark',
onSecondaryDark: 'onSecondaryDark',
background: 'background',
onBackground: 'onBackground',
surface: 'surface',
onSurface: 'onSurface',
highlight: 'highlight',
highEmphasis: 'highEmphasis',
mediumEmphasis: 'mediumEmphasis',
lowEmphasis: 'lowEmphasis',
link: 'link',
onLink: 'onLink',
success: 'success',
onSuccess: 'onSuccess',
warning: 'warning',
onWarning: 'onWarning',
alert: 'alert',
onAlert: 'onAlert',
};
/* eslint-enable sort-keys */

/* eslint-disable sort-keys */
const iconSizes = {
micro: 'micro',
tiny: 'tiny',
small: 'small',
standard: 'standard',
medium: 'medium',
large: 'large',
largex: 'largex',
largexx: 'largexx',
huge: 'huge',
hugex: 'hugex',
hugexx: 'hugexx',
};
/* eslint-enable sort-keys */

export const interactive = () => (
<ContainerRow style={{ padding: 20 }} >
<Icon
color={select('Colors', iconColors, 'default') as IconColors}
name={text('Icon name', 'outlined-default-mockup')}
size={select('Sizes', iconSizes, 'standard') as IconSizes}
/>
</ContainerRow>
);
57 changes: 44 additions & 13 deletions src/components/Icon/Icon.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
import React from 'react';
import { withTheme } from 'styled-components/native';
import { Text } from 'react-native';

import iconNames from '@naturacosmeticos/natds-icons/dist/natds-icons.json';
import { Theme } from '../../common/themeSelectors';
import { ISizes } from '@naturacosmeticos/natds-styles';
import { Theme, getColorByName } from '../../common/themeSelectors';

export type IconColors = 'default' | 'primary'
export type IconSizes = 'small' | 'medium'
export type IconColors = 'default'
| 'primary'
| 'onPrimary'
| 'primaryLight'
| 'onPrimaryLight'
| 'primaryDark'
| 'onPrimaryDark'
| 'secondary'
| 'onSecondary'
| 'secondaryLight'
| 'onSecondaryLight'
| 'secondaryDark'
| 'onSecondaryDark'
| 'background'
| 'onBackground'
| 'surface'
| 'onSurface'
| 'highlight'
| 'highEmphasis'
| 'mediumEmphasis'
| 'lowEmphasis'
| 'link'
| 'onLink'
| 'success'
| 'onSuccess'
| 'warning'
| 'onWarning'
| 'alert'
| 'onAlert'
export type IconSizes = keyof ISizes

export interface IconProps {
/**
Expand All @@ -21,13 +49,17 @@ export interface IconProps {
*/
accessibilityLabel?: string
/**
* Icon variants `default` | `primary`
* Icon color tokens
*/
color?: IconColors,
/**
* Icon name
*/
name?: string
/**
* Icon size
*/
size?: IconSizes
/**
* Optional ID for testing
*/
Expand All @@ -40,15 +72,13 @@ export interface IconProps {

const defaultIconName = 'outlined-default-mockup';

const getIconSize = theme => theme.sizes.standard;
const getIconSize = (theme: Theme, size: IconSizes) => theme.sizes[size];

const getFontColor = (theme, color) => {
const translatedColor = {
default: 'colorHighEmphasis',
primary: 'colorPrimary',
}[color];
const getFontColor = (theme: Theme, color: IconColors) => {
const colorName = color === 'default' ? 'highEmphasis' : color;
const colorToken = `color${colorName.charAt(0).toUpperCase()}${colorName.slice(1)}`;

return theme.colorTokens[translatedColor];
return getColorByName(theme, colorToken);
};

const IconComponent = ({
Expand All @@ -58,6 +88,7 @@ const IconComponent = ({
name = defaultIconName,
testID = `icon-${name}`,
theme,
size = 'standard',
}: IconProps) => {
const unicodeName = iconNames[name]
? iconNames[name].replace('%', '\\')
Expand All @@ -73,7 +104,7 @@ const IconComponent = ({
style={{
color: getFontColor(theme, color),
fontFamily: 'natds-icons',
fontSize: getIconSize(theme),
fontSize: getIconSize(theme, size),
}}
testID={testID}
>
Expand Down

0 comments on commit a388255

Please sign in to comment.