Skip to content

Commit 777213a

Browse files
feat(SvgIcon): make default color 'inherit'; use in @prenda/spark-icons; expand colors; improve props (#174)
* feat(SvgIcon)!: revert default color to 'inherit'; improve props * feat(SvgIcon): expand color options and improve handling / classes * change @prenda/spark-icons dependency to spark * specify color for lone icon in NavBar story
1 parent c14d532 commit 777213a

File tree

7 files changed

+368
-62
lines changed

7 files changed

+368
-62
lines changed

libs/spark-icons/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"@babel/runtime": "^7.10.2"
77
},
88
"peerDependencies": {
9-
"@material-ui/core": "^4.0.0",
9+
"@prenda/spark": "^0.8.0",
1010
"react": "^16.8.0 || ^17.0.0"
1111
},
1212
"devDependencies": {

libs/spark-icons/src/utils/createSvgIcon.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// Changes made since
33

44
import * as React from 'react';
5-
import SvgIcon from '@material-ui/core/SvgIcon';
6-
import { SvgIconProps as MuiSvgIconProps } from '@material-ui/core';
5+
import { SvgIcon, SvgIconProps } from '@prenda/spark';
76

87
export default function createSvgIcon(
98
path: React.ReactNode,
@@ -12,7 +11,7 @@ export default function createSvgIcon(
1211
width?: string,
1312
height?: string
1413
): typeof SvgIcon {
15-
const Component = (props: MuiSvgIconProps, ref: React.ForwardedRef<SVGSVGElement>) => (
14+
const Component = (props: SvgIconProps, ref: React.ForwardedRef<SVGSVGElement>) => (
1615
<SvgIcon ref={ref} viewBox={viewBox} width={width} height={height} {...props}>
1716
{path}
1817
</SvgIcon>

libs/spark/src/NavBarButton.tsx

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ export const NavBarButton = withStyles((theme) => ({
1111
padding: 6,
1212
textTransform: 'none',
1313
color: theme.palette.text.onLightLowContrast,
14-
'& .MuiSvgIcon-root': {
15-
color: theme.palette.text.onLightLowContrast,
16-
'& .MuiSvgIcon-colorPrimary': {
17-
fill: theme.palette.text.onLightLowContrast,
18-
},
19-
},
2014
'&:hover': {
2115
backgroundColor: theme.palette.grey.light,
2216
border: `2px solid ${theme.palette.grey.light}`,
@@ -31,23 +25,17 @@ export const NavBarButton = withStyles((theme) => ({
3125
border: `2px solid ${theme.palette.grey.light}`,
3226
backgroundColor: theme.palette.grey.light,
3327
color: theme.palette.text.onLight,
34-
'& .MuiSvgIcon-colorPrimary': {
35-
color: theme.palette.text.onLight,
36-
'& > *:first-child': {
37-
borderColor: theme.palette.blue[5],
38-
fill: theme.palette.blue[3],
39-
fillOpacity: '0.24',
40-
},
28+
'& .MuiSvgIcon-root > *[fill="#F0F1F2"]': {
29+
fill: theme.palette.blue[3],
30+
fillOpacity: '0.24',
4131
},
4232
},
4333
label: { fontWeight: 700 },
4434
},
4535
iconSizeLarge: {
36+
// override Mui selector
4637
'& > *:first-child': {
47-
fontSize: '2rem', // small/medium/large for icons is 16px/24px/32px
48-
'&.MuiSvgIcon-fontSizeLarge': {
49-
fontSize: '2rem',
50-
},
38+
fontSize: '2rem',
5139
},
5240
},
5341
disabled: {
@@ -56,12 +44,6 @@ export const NavBarButton = withStyles((theme) => ({
5644
color: theme.palette.grey.dark,
5745
backgroundColor: 'transparent',
5846
borderColor: 'transparent',
59-
'& .MuiSvgIcon-colorPrimary': {
60-
color: theme.palette.grey.dark,
61-
'& > *:first-child': {
62-
color: theme.palette.grey.dark,
63-
},
64-
},
6547
},
6648
},
6749
focusVisible: {

libs/spark/src/SvgIcon.tsx

Lines changed: 86 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,59 @@ import { palette } from './styles/palette';
44
import clsx from 'clsx';
55
import { capitalize } from './utils';
66

7-
export interface SvgIconProps extends MuiSvgIconProps {
7+
export interface SvgIconProps extends Omit<MuiSvgIconProps, 'color'> {
88
contrast?: 'high' | 'low';
9+
color?:
10+
| 'inherit'
11+
| 'onLight'
12+
| 'onDark'
13+
| 'disabled'
14+
| 'error'
15+
| 'success'
16+
| 'warning'
17+
| 'info'
18+
| 'white';
919
}
1020

11-
export const MuiSvgIconDefaultProps = {
12-
color: 'primary' as const,
13-
};
21+
export const MuiSvgIconDefaultProps = {};
1422

23+
// :NOTE: Duotone fill selector is & > *[fill="#F0F1F2"]
1524
export const MuiSvgIconStyleOverrides = {
1625
root: {
17-
// fontSizeDefault
26+
// fontSizeDefault/Medium
1827
fontSize: '1.5rem', // 24px
19-
'&.SparkSvgIcon-contrastHigh': {
20-
opacity: 0.72,
21-
},
28+
// contrastHigh is `opacity: 1` which is just the default CSS value
2229
'&.SparkSvgIcon-contrastLow': {
2330
opacity: 0.72,
2431
},
32+
// custom colors
33+
'&.SparkSvgIcon-colorSuccess': {
34+
color: palette.green[2],
35+
'& > *[fill="#F0F1F2"]': {
36+
fill: palette.green[1],
37+
opacity: 0.72,
38+
},
39+
},
40+
'&.SparkSvgIcon-colorWarning': {
41+
color: palette.yellow[2],
42+
'& > *[fill="#F0F1F2"]': {
43+
fill: palette.yellow[1],
44+
opacity: 0.72,
45+
},
46+
},
47+
'&.SparkSvgIcon-colorInfo': {
48+
color: palette.blue[2],
49+
'& > *[fill="#F0F1F2"]': {
50+
fill: palette.blue[1],
51+
opacity: 0.72,
52+
},
53+
},
54+
'&.SparkSvgIcon-colorWhite': {
55+
color: palette.common.white,
56+
'& > *[fill="#F0F1F2"]': {
57+
opacity: 0.72,
58+
},
59+
},
2560
},
2661
fontSizeSmall: {
2762
fontSize: '1rem', // 16px
@@ -34,23 +69,56 @@ export const MuiSvgIconStyleOverrides = {
3469
},
3570
colorSecondary: {
3671
color: palette.text.onDark,
72+
'& > *[fill="#F0F1F2"]': {
73+
opacity: 0.72,
74+
},
75+
},
76+
colorError: {
77+
color: palette.red[2],
78+
'& > *[fill="#F0F1F2"]': {
79+
fill: palette.red[1],
80+
opacity: 0.72,
81+
},
3782
},
3883
};
3984

40-
function SparkSvgIcon({
41-
contrast = 'high',
42-
className,
43-
...other
44-
}: SvgIconProps) {
85+
const SparkSvgIcon = React.forwardRef(function SparkSvgIcon(
86+
{ contrast = 'high', color = 'inherit', className, ...other }: SvgIconProps,
87+
ref: React.ForwardedRef<SVGSVGElement>
88+
) {
89+
let muiColor;
90+
let sparkColor = 'inherit';
91+
if (color === 'onLight') {
92+
muiColor = 'primary';
93+
} else if (color === 'onDark') {
94+
muiColor = 'secondary';
95+
} else if (color === 'success') {
96+
sparkColor = 'success';
97+
muiColor = 'inherit';
98+
} else if (color === 'warning') {
99+
sparkColor = 'warning';
100+
muiColor = 'inherit';
101+
} else if (color === 'info') {
102+
sparkColor = 'info';
103+
muiColor = 'inherit';
104+
} else if (color === 'white') {
105+
sparkColor = 'white';
106+
muiColor = 'inherit';
107+
} else {
108+
muiColor = color;
109+
}
110+
45111
return (
46112
<SvgIcon
47-
className={clsx(
48-
className,
49-
`SparkSvgIcon-contrast${capitalize(contrast)}`
50-
)}
113+
color={muiColor}
114+
className={clsx(className, {
115+
[`SparkSvgIcon-contrast${capitalize(contrast)}`]: contrast !== 'high',
116+
[`SparkSvgIcon-color${capitalize(sparkColor)}`]:
117+
sparkColor !== 'inherit',
118+
})}
51119
{...other}
52120
/>
53121
);
54-
}
122+
});
55123

56124
export { SparkSvgIcon as SvgIcon };

libs/spark/stories/icons.stories.tsx

Lines changed: 0 additions & 16 deletions
This file was deleted.

libs/spark/stories/navbar.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const Template: Story<NavBarProps> = (args) => (
7777
0
7878
</InboxNavBarButton>
7979
<UserMenu>
80-
<UserDuotone fontSize="large" />
80+
<UserDuotone color="onLight" fontSize="large" />
8181
</UserMenu>
8282
</CustomToolbar>
8383
</NavBar>

0 commit comments

Comments
 (0)