Reputation: 792
import { styled } from "@mui/system";
import DeleteIcon from "@mui/icons-material/Delete";
import SendIcon from "@mui/icons-material/Send";
import { Button } from "@mui/material";
const Button = styled("button")({
color: "red",
backgroundColor: "black",
padding: "1rem",
});
function App() {
return (
<div>
<Button
variant="contained"
size="medium"
startIcon={<DeleteIcon />}
endIcon={<SendIcon />}
>
Material UI
</Button>
</div>
);
}
export default App;
I think styled is now a new way in MUI to create make and use custom styles. I don't think I can use className property in this case. Previously I could use makeStyles and useStyles and assign classname to the Button component to customize it, How can I use Button Component from MUI and customize it using styled . Right now I should define at the top what kind of component is. I want to use Button component, its variant color properties and also customize it using styled.
Upvotes: 1
Views: 3432
Reputation: 140
I see a couple of problems here:
import { styled } from '@mui/material/styles';
https://mui.com/material-ui/migration/migrating-from-jss/
Button
to styled
and rename the componentconst CustomButton = styled(Button)({
color: "red",
backgroundColor: "black",
padding: "1rem",
});
Then you can use it
function App() {
return (
<CustomButton
variant="contained"
size="medium"
startIcon={<DeleteIcon />}
endIcon={<SendIcon />}
>
Material UI
</CustomButton>
);
}
Upvotes: 2
Reputation: 2733
you need to use !important
to overwrite styles and styled(Button)
as it is clear in code. complete version is here in sandbox
import * as React from "react";
import { styled } from "@mui/styles";
import Button from "@mui/material/Button";
import DeleteIcon from "@mui/icons-material/Delete";
import SendIcon from "@mui/icons-material/Send";
const MyButton = styled(Button)({
backgroundColor: "#000 !important",
color: "red !important",
padding: "1rem !important"
});
export default function App() {
return (
<MyButton
variant="contained"
size="medium"
startIcon={<DeleteIcon />}
endIcon={<SendIcon />}
>
Styled Components
</MyButton>
);
}
Upvotes: 0
Reputation: 334
You can also use sx
to add css to components in MUI 5
<Button
sx={{color: "red",
backgroundColor: "black",
padding: "1rem"}}
variant="contained"
size="medium"
startIcon={<DeleteIcon />}
endIcon={<SendIcon />}
>
Material UI
</Button>
Upvotes: 1
Reputation: 696
Try to use the code below
const StyledButton = styled(Button)(() => ({
color: "red",
backgroundColor: "black",
padding: "1rem",
}));
In the first ()
you need to pass the material ui component as the named export and then you can use the const-name.
So in your code in stead of <Button></Button>
you will use the <StyledButton></StyledButton>
Usage:
<StyledButton
variant="contained"
size="medium"
startIcon={<DeleteIcon />}
endIcon={<SendIcon />}
>
Material UI
</StyledButton>
Upvotes: 0