I try to style mui Dialog but it doesnt acceppt className even inside PaperProps. Inline styles are working but I would like to import styles from styling sheet called Styles
<Styles>
<div>
{dialogOpen ? (
<Dialog
className="dialog"
onClose={() => setDialogOpen(false)}
open={dialogOpen}
PaperProps={{
className="paper"
}}
>
<DialogTitle className="title">TITLE</DialogTitle>
</Dialog>
) : null}
</div>
</Styles>
And this is styling file
import { styled } from "@mui/material";
const Styles = styled("div")(({ theme }) => ({
"& .text": {
backgroundColor: "red",
},
"& .dialog": {
backgroundColor: "green",
},
"& .text": {
color: "yellow",
},
}));
export { Styles };
2
Answers
To style a Material-UI
Dialog
component using theclassName
property with styles defined in a separate stylesheet, you can use themakeStyles
function provided by Material-UI. Here’s how you can do it:First, create a separate stylesheet (e.g.,
Styles.js
) to define your styles:Now, import the
Styles
function into your component and apply the styles to yourDialog
component:In this code, we import the
Styles
function from your separate stylesheet and initialize it within your component. Then, we apply the generated class names to the corresponding components within yourDialog
component.Make sure you have the necessary dependencies installed to use the
makeStyles
function and that you’ve set up your project to work with MUI version 5 or later, as the code provided is based on that version.Well you can try this
Now, import the Styles object into your component and apply the styles to your Dialog component: