Material UI is a Material Design library made for React.
It’s a set of React components that have Material Design styles.
In this article, we’ll look at how to customize dialog boxes with Material UI.
Customized Dialogs
We can create our own dialog components by putting into our own components and passing in various styles to it.
For example, we can write:
import React from "react";
import { withStyles } from "@material-ui/core/styles";
import Button from "@material-ui/core/Button";
import Dialog from "@material-ui/core/Dialog";
import MuiDialogTitle from "@material-ui/core/DialogTitle";
import MuiDialogContent from "@material-ui/core/DialogContent";
import MuiDialogActions from "@material-ui/core/DialogActions";
import IconButton from "@material-ui/core/IconButton";
import CloseIcon from "@material-ui/icons/Close";
import Typography from "@material-ui/core/Typography";
const styles = theme => ({
root: {
margin: 0,
padding: theme.spacing(3)
},
closeButton: {
position: "absolute",
right: theme.spacing(1),
top: theme.spacing(2),
color: theme.palette.grey[500]
}
});
const DialogTitle = withStyles(styles)(props => {
const { children, classes, onClose, ...other } = props;
return (
<MuiDialogTitle disableTypography className={classes.root} {...other}>
<Typography variant="h6">{children}</Typography>
{onClose ? (
<IconButton className={classes.closeButton} onClick={onClose}>
<CloseIcon />
</IconButton>
) : null}
</MuiDialogTitle>
);
});
const DialogContent = withStyles(theme => ({
root: {
padding: theme.spacing(2)
}
}))(MuiDialogContent);
const DialogActions = withStyles(theme => ({
root: {
margin: 0,
padding: theme.spacing(1)
}
}))(MuiDialogActions);
export default function App() {
const [open, setOpen] = React.useState(false);
const handleClickOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
};
return (
<div>
<Button variant="outlined" color="primary" onClick={handleClickOpen}>
Open dialog
</Button>
<Dialog onClose={handleClose} open={open}>
<DialogTitle onClose={handleClose}>Modal</DialogTitle>
<DialogContent dividers>
<Typography gutterBottom>lorem ipsum.</Typography>
</DialogContent>
<DialogActions>
<Button autoFocus onClick={handleClose} color="primary">
ok
</Button>
</DialogActions>
</Dialog>
</div>
);
}
We create a styles
function that we pass into the withStyles
higher-order components with various styles.
We moved the close button by setting some styles for the closeButton
class.
To create the DialogTitle
component, we passed a component that uses the classes and children from the props.
The classes are applied to icon buttons and dialog titles.
Also, we created the DialogContent
and DialogActions
in similar ways.
We changed the padding slightly to create both components.
Then we used them all in the App
component.
Full-Screen Dialogs
We can make dialogs full screen by adding the fullscreen
prop to Dialog
.
For example, we can write:
import React from "react";
import Button from "@material-ui/core/Button";
import Dialog from "@material-ui/core/Dialog";
import DialogTitle from "@material-ui/core/DialogTitle";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogActions from "@material-ui/core/DialogActions";
export default function App() {
const [open, setOpen] = React.useState(false);
const handleClose = () => {
setOpen(false);
};
return (
<div>
<Button variant="outlined" color="primary" onClick={() => setOpen(true)}>
Open dialog
</Button>
<Dialog fullScreen open={open} onClose={handleClose}>
<DialogTitle>Title</DialogTitle>
<DialogContent>
<DialogContentText>lorem ipsum</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={handleClose} color="primary" autoFocus>
ok
</Button>
</DialogActions>
</Dialog>
</div>
);
}
to create a dialog that’s full screen with the fullScreen
prop.
Optional Sizes
We can change the size of the dialog with the maxWidth
prop to set the max width of the dialog.
fullWidth
has the full width.
For example, we can write:
import React from "react";
import Button from "@material-ui/core/Button";
import Dialog from "@material-ui/core/Dialog";
import DialogTitle from "@material-ui/core/DialogTitle";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogActions from "@material-ui/core/DialogActions";
export default function App() {
const [open, setOpen] = React.useState(false);
const handleClose = () => {
setOpen(false);
};
return (
<div>
<Button variant="outlined" color="primary" onClick={() => setOpen(true)}>
Open dialog
</Button>
<Dialog fullWidth maxWidth="sm" open={open} onClose={handleClose}>
<DialogTitle>Title</DialogTitle>
<DialogContent>
<DialogContentText>lorem ipsum</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={handleClose} color="primary" autoFocus>
ok
</Button>
</DialogActions>
</Dialog>
</div>
);
}
to add the fullWidth
and maxWidth
props to the Dialog
.
The fullWidth
prop will display the dialog that stretches to the width of the screen if it’s narrower than the breakpoint given in maxWidth
.
Otherwise, it’ll display with the max-width of the given breakpoint.
Conclusion
We can add dialogs with its own styles, full-screen dialogs, and also width a max-width.