I'm creating a theme in Material-UI and have to use our own set of icons (SVG). However, the icons gets too small when inserted in a button with startIcon
/ endIcon
. Is there any way to increase the size of the icon and not the font. Preferably from the theme settings.
Here is an example of the button:
https://codesandbox.io/s/inspiring-spence-gsov0?file=/src/App.js
import React from "react";
import "./styles.css";
import Button from "@material-ui/core/Button";
import { SvgIcon } from "@material-ui/core";
function CloudIcon(props) {
return (
<SvgIcon {...props}>
<svg viewBox="0 0 94 94" {...props}>
<title>{"B Circle"}</title>
<circle cx={47} cy={47} r={47} fill="#1700ff" />
<title>{"\uF0C2"}</title>
<path
d="M61.9 61.6h-29c-6.3 0-11.3-5.1-11.3-11.3 0-5 3.2-9.3 7.9-10.8.2-7.2 6.2-13.1 13.5-13.1 4.4 0 8.4 2.1 10.9 5.6 1.2-.6 2.5-.9 3.9-.9 5.1 0 9.3 4.2 9.3 9.3 0 .5 0 1-.1 1.5 3.3 1.9 5.4 5.4 5.4 9.2 0 5.8-4.7 10.5-10.5 10.5zM43 28.4c-6.3 0-11.5 5.2-11.5 11.5V41l-.8.2c-4.2 1-7.2 4.7-7.2 9.1 0 5.2 4.2 9.3 9.3 9.3h28.9c4.7 0 8.5-3.8 8.5-8.5 0-3.3-1.9-6.3-4.9-7.7l-.6-.4.2-.8c.1-.6.2-1.2.2-1.8 0-4-3.3-7.3-7.3-7.3-1.3 0-2.6.3-3.7 1l-.8.5-.5-.8c-2.2-3.4-5.8-5.4-9.8-5.4z"
fill="#fff"
/>
</svg>
</SvgIcon>
);
}
export default function App() {
return (
<div className="App">
<Button startIcon={<CloudIcon />} variant="outlined">
Test
</Button>
</div>
);
}
The default styles for the icon size can be found here (and shown below): https://github.com/mui-org/material-ui/blob/v4.11.0/packages/material-ui/src/Button/Button.js#L249. Which of the three is used depends on the size
prop for the Button
. medium
is the default.
/* Styles applied to the icon element if supplied and `size="small"`. */
iconSizeSmall: {
'& > *:first-child': {
fontSize: 18,
},
},
/* Styles applied to the icon element if supplied and `size="medium"`. */
iconSizeMedium: {
'& > *:first-child': {
fontSize: 20,
},
},
/* Styles applied to the icon element if supplied and `size="large"`. */
iconSizeLarge: {
'& > *:first-child': {
fontSize: 22,
},
},
Below is an example of how to override this in the theme.
const theme = createMuiTheme({
overrides: {
MuiButton: {
iconSizeMedium: {
"& > *:first-child": {
fontSize: 22
}
}
}
}
});
I had the same question and here is a possible solution if you don't want to make changes to your Theme
. You can overwrite the nested iconSizeMedium
CSS style at component level directly. Here is an example where a Button
is wrapped with Typography
letting it control the font-size
through inherits
:
import Button from '@material-ui/core/Button';
import PhoneEnabledIcon from '@material-ui/icons/PhoneEnabled';
import { makeStyles } from '@material-ui/styles';
import Typography from '@material-ui/core/Typography';
const useStyles = makeStyles((theme) => ({
buttonRoot: {
fontSize: 'inherit', /* inherit from Typography */
},
myIconSizeMedium: {
"& > *:first-child": {
fontSize: 'inherit'
}
}
}));
export default function MyButton( {children} ) {
const classes = useStyles();
return (
<Typography color='textSecondary' variant="h6" >
<Button
startIcon={<PhoneEnabledIcon />}
className={classes.buttonRoot}
classes={{ iconSizeMedium: classes.myIconSizeMedium }}
>
{children}
</Button>
</Typography>
)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With