Using MUI V5, how can I pass a custom style to a button component? Here is what I have been trying to merge the old way with the new MUI v5 but it’s not working.
import { Button } from "@mui/material"; import { styled } from "@mui/material/styles"; import React from "react"; const StyledButton = styled(Button)(({ theme }) => ({ root: { minWidth: 0, margin: theme.spacing(0.5), }, secondary: { backgroundColor: theme.palette.secondary.light, "& .MuiButton-label": { color: theme.palette.secondary.main, }, }, primary: { backgroundColor: theme.palette.primary.light, "& .MuiButton-label": { color: theme.palette.primary.main, }, }, })); export default function ActionButton(props) { const { color, children, onClick } = props; return ( <Button className={`${StyledButton["root"]} ${StyledButton[color]}`} onClick={onClick} > {children} </Button> ); }
Now I would like to call this Button and give it color=”secondary”
import ActionButton from "./ActionButton"; import { Close } from "@mui/icons-material"; export default function Header() { return ( <ActionButton color="secondary"> <Close /> </ActionButton> ) }
Advertisement
Answer
It looks like your code was an attempt to migrate from code using makeStyles/useStyles
, but styled
works quite a bit differently. You can’t use it to generate multiple CSS classes like makeStyles
does (StyledButton["root"]
and StyledButton[color]
will be undefined
). styled
will generate a single CSS class that is then passed in the className
prop to the wrapped component (e.g. Button
). Instead of trying to create multiple CSS classes with logic to decide which class to apply, with styled
you can leverage props (e.g. the color
prop) to generate dynamic styles within the single CSS class that is generated.
Below is an example that I think achieves the effect your code was aiming for. My example doesn’t do anything with MuiButton-label
because that class doesn’t exist in v5 (and the <span>
that the class was applied to inside the <button
in v4 also does not exist), and I believe the default Button styles set color
as desired when the color
prop is allowed to passed through to Button
.
import Button from "@mui/material/Button"; import { styled } from "@mui/material/styles"; const StyledButton = styled(Button)(({ theme, color }) => ({ minWidth: 0, margin: theme.spacing(0.5), backgroundColor: color ? theme.palette[color].light : undefined })); export default StyledButton;