41
loading...
This website collects cookies to deliver better user experience
@mui/icons-material
package can easily understand MUI theming and, they can simply communicate with other MUI components. Thanks to the MUI SvgIcon
component you can easily create your icon component that looks like MUI icons.@mui/icons-material
isn't necessary. So as a minimum, you need a react app as well as @mui/material
.@emotion
as it's the default style library used in MUI v5.1 import * as React from 'react';
SvgIcon
component and styled
utility from @mui/matarial
, So the code will be like this:1 import * as React from 'react';
2 import { SvgIcon as MuiSvgIcon, styled } from '@mui/material';
SvgIconProps
type to create our new component properly.1 import * as React from 'react';
2 import { SvgIcon as MuiSvgIcon, SvgIconProps, styled } from '@mui/material';
SvgIcon
to MuiSvgIcon
is that in the next step we're going to create a new styled SvgIcon
and we'll name that new component SvgIcon. You'll get it better in the next step.SvgIcon
with our custom styles. Each path may need several CSS like fill
or stroke
etc. This SvgIcon
in HTML will become a <svg></svg>
tag with our styles.const SvgIcon = styled(component, options)((props)=>(styles))
const SvgIcon = styled(component, options)<PropsType>((props)=>(styles))
styled
function and then pass a component to that. This component can be one of MUI components or even simple HTML tags like an a
or a button
etc. Here we want to create a svg
tag, and we want to make it in the MUI way. So we pass the SvgIcon
component as the first prop to the styled
function.name
and shouldForwardProp
options to set a name for our new SvgIcon Component and also shouldForwardProp to say which property should or shouldn't forward to the styles. You also can ignore these options as they're optional. MUI docs explain these two options like this:options.shouldForwardProp
((prop: string) => bool
[optional]): Indicates whether the prop
should be forwarded to the Component
.
options.name
(string [optional]): The key used under theme.components
for specifying styleOverrides
and variants
. Also used for generating the label
.
...
3
4 const SvgIcon = styled(MuiSvgIcon, {
5 name: 'MopeimIcon',
6 shouldForwardProp: (prop) => prop !== 'fill',
7 })(() => ({
8 fill: 'none',
9 stroke: 'currentColor',
10 strokeLinecap: 'round',
11 strokeLinejoin: 'round',
12 strokeWidth: '2.25px',
13 }));
...
3
4 const SvgIcon = styled(MuiSvgIcon, {
5 name: 'MopeimIcon',
6 shouldForwardProp: (prop) => prop !== 'fill',
7 })<SvgIconProps>(() => ({
8 fill: 'none',
9 stroke: 'currentColor',
10 strokeLinecap: 'round',
11 strokeLinejoin: 'round',
12 strokeWidth: '2.25px',
13 }));
shouldForwardProp
we have to wrap the style prop in quotes. So this is NOT true:...
6 shouldForwardProp: (prop) => prop !== fill, //Cannot find name 'fill'.
...
...
7 })<SvgIconProps>(({theme, anotherProp}) => ({
8 fill: theme.palette.primary.main,
9 borderRadius: theme.shape.borderRadius,
10 anotherStyle: anotherProp,
...
defaultProps
property on SvgIcon to set some defaults for our svg
. So:...
14
15 SvgIcon.defaultProps = {
16 viewBox: '0 0 24 24',
17 focusable: 'false',
18 'aria-hidden': 'true',
19 };
aria-hidden="true"
to an element removes that element and all of its children from the accessibility tree.
Read more about this attribute here
SvgIcon
that we've modified before, and a path. M15,19.16V15.07a4.27,4.27,0,0,0,6,0h0a4.27,4.27,0,0,0,0-6h0a4.27,4.27,0,0,0-6,0l-3,3-3,3a4.27,4.27,0,0,1-6,0h0a4.27,4.27,0,0,1,0-6h0A4.27,4.27,0,0,1,9,9
...
20
21 const Mopeim = (props) => {
22 return (
23 <SvgIcon {...props}>
24 <path d="M15,19.16V15.07a4.27,4.27,0,0,0,6,0h0a4.27,4.27,0,0,0,0-6h0a4.27,4.27,0,0,0-6,0l-3,3-3,3a4.27,4.27,0,0,1-6,0h0a4.27,4.27,0,0,1,0-6h0A4.27,4.27,0,0,1,9,9" />
25 </SvgIcon>
26 );
27 };
28
29 export default Mopeim;
30
...
20
21 const Mopeim: React.FunctionComponent<SvgIconProps> = (props) => {
22 return (
23 <SvgIcon {...props}>
24 <path d="M15,19.16V15.07a4.27,4.27,0,0,0,6,0h0a4.27,4.27,0,0,0,0-6h0a4.27,4.27,0,0,0-6,0l-3,3-3,3a4.27,4.27,0,0,1-6,0h0a4.27,4.27,0,0,1,0-6h0A4.27,4.27,0,0,1,9,9" />
25 </SvgIcon>
26 );
27 };
28
29 export default Mopeim;
30
1 import * as React from 'react';
2 import { SvgIcon as MuiSvgIcon, styled } from '@mui/material';
3
4 const SvgIcon = styled(MuiSvgIcon, {
5 name: 'MopeimIcon',
6 shouldForwardProp: (prop) => prop !== 'fill',
7 })(() => ({
8 fill: 'none',
9 stroke: 'currentColor',
10 strokeLinecap: 'round',
11 strokeLinejoin: 'round',
12 strokeWidth: '2.25px',
13 }));
14
15 SvgIcon.defaultProps = {
16 viewBox: '0 0 24 24',
17 focusable: 'false',
18 'aria-hidden': 'true',
19 };
20
21 const Mopeim = (props) => {
22 return (
23 <SvgIcon {...props}>
24 <path d="M15,19.16V15.07a4.27,4.27,0,0,0,6,0h0a4.27,4.27,0,0,0,0-6h0a4.27,4.27,0,0,0-6,0l-3,3-3,3a4.27,4.27,0,0,1-6,0h0a4.27,4.27,0,0,1,0-6h0A4.27,4.27,0,0,1,9,9" />
25 </SvgIcon>
26 );
27 };
28
29 export default Mopeim;
30
1 import * as React from 'react';
2 import { SvgIcon as MuiSvgIcon, SvgIconProps, styled } from '@mui/material';
3
4 const SvgIcon = styled(MuiSvgIcon, {
5 name: 'MopeimIcon',
6 shouldForwardProp: (prop) => prop !== 'fill',
7 })<SvgIconProps>(() => ({
8 fill: 'none',
9 stroke: 'currentColor',
10 strokeLinecap: 'round',
11 strokeLinejoin: 'round',
12 strokeWidth: '2.25px',
13 }));
14
15 SvgIcon.defaultProps = {
16 viewBox: '0 0 24 24',
17 focusable: 'false',
18 'aria-hidden': 'true',
19 };
20
21 const Mopeim: React.FunctionComponent<SvgIconProps> = (props) => {
22 return (
23 <SvgIcon {...props}>
24 <path d="M15,19.16V15.07a4.27,4.27,0,0,0,6,0h0a4.27,4.27,0,0,0,0-6h0a4.27,4.27,0,0,0-6,0l-3,3-3,3a4.27,4.27,0,0,1-6,0h0a4.27,4.27,0,0,1,0-6h0A4.27,4.27,0,0,1,9,9" />
25 </SvgIcon>
26 );
27 };
28
29 export default Mopeim;
30