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.
Responsive Full-Screen Dialog
We can make a full-screen dialog responsive with the useMediaQuery
hook.
For example, we can write:
import React from "react";
import Button from "@material-ui/core/Button";
import Dialog from "@material-ui/core/Dialog";
import DialogActions from "@material-ui/core/DialogActions";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogTitle from "@material-ui/core/DialogTitle";
import useMediaQuery from "@material-ui/core/useMediaQuery";
import { useTheme } from "@material-ui/core/styles";
export default function App() {
const [open, setOpen] = React.useState(false);
const theme = useTheme();
const fullScreen = useMediaQuery(theme.breakpoints.down("sm"));
const handleClickOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
};
return (
<div>
<Button variant="outlined" color="primary" onClick={handleClickOpen}>
Open dialog
</Button>
<Dialog fullScreen={fullScreen} open={open} onClose={handleClose}>
<DialogTitle>Dialog</DialogTitle>
<DialogContent>
<DialogContentText>lorem ipsum</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={handleClose} color="primary" autoFocus>
ok
</Button>
</DialogActions>
</Dialog>
</div>
);
}
to add a responsive dialog that’s also full screen.
We used the useTheme
hook to get the theme.
And with it, we use the useMediaQuery
to get the breakpoint
to pass in to the hook.
This means that the dialog box will be full screen if the screen’s width meets the sm breakpoint or smaller.
Otherwise, it’ll be a regular-sized dialog box.
Confirmation Dialogs
We can create a confirmation dialog to make use to select a choice before closing the dialog.
To create one, we can write:
import React from "react";
import PropTypes from "prop-types";
import { makeStyles } from "@material-ui/core/styles";
import Button from "@material-ui/core/Button";
import List from "@material-ui/core/List";
import ListItem from "@material-ui/core/ListItem";
import ListItemText from "@material-ui/core/ListItemText";
import DialogTitle from "@material-ui/core/DialogTitle";
import DialogContent from "@material-ui/core/DialogContent";
import DialogActions from "@material-ui/core/DialogActions";
import Dialog from "@material-ui/core/Dialog";
import RadioGroup from "@material-ui/core/RadioGroup";
import Radio from "@material-ui/core/Radio";
import FormControlLabel from "@material-ui/core/FormControlLabel";
const options = ["apple", "orange", "grape"];
function ConfirmationDialogRaw(props) {
const { onClose, value: valueProp, open, ...other } = props;
const [value, setValue] = React.useState(valueProp);
const radioGroupRef = React.useRef(null);
React.useEffect(() => {
if (!open) {
setValue(valueProp);
}
}, [valueProp, open]);
const handleEntering = () => {
if (radioGroupRef.current != null) {
radioGroupRef.current.focus();
}
};
const handleCancel = () => {
onClose();
};
const handleOk = () => {
onClose(value);
};
const handleChange = event => {
setValue(event.target.value);
};
return (
<Dialog
disableBackdropClick
disableEscapeKeyDown
maxWidth="xs"
onEntering={handleEntering}
open={open}
{...other}
>
<DialogTitle>Fruit</DialogTitle>
<DialogContent dividers>
<RadioGroup
ref={radioGroupRef}
name="ringtone"
value={value}
onChange={handleChange}
>
{options.map(option => (
<FormControlLabel
value={option}
key={option}
control={<Radio />}
label={option}
/>
))}
</RadioGroup>
</DialogContent>
<DialogActions>
<Button autoFocus onClick={handleCancel} color="primary">
Cancel
</Button>
<Button onClick={handleOk} color="primary">
Ok
</Button>
</DialogActions>
</Dialog>
);
}
export default function App() {
const [open, setOpen] = React.useState(false);
const [value, setValue] = React.useState("Done");
const onClick = () => {
setOpen(true);
};
const handleClose = newValue => {
setOpen(false);
if (newValue) {
setValue(newValue);
}
};
return (
<div>
<Button secondary={value} onClick={onClick}>
Choose fruit
</Button>
<ConfirmationDialogRaw
keepMounted
open={open}
onClose={handleClose}
value={value}
/>
</div>
);
}
We added the ConfirmationDialogRaw
component to display a dialog with various choices.
It has a list of choices rendered from the option
array as a set of radio buttons.
Also, we have an OK and Cancel button below the radio buttons.
In the App
component, we have the Button
to let us open the dialog box.
Draggable Dialog
We can make a dialog draggable with the react-draggable library.
For example, we can write:
import React from "react";
import Button from "@material-ui/core/Button";
import Dialog from "@material-ui/core/Dialog";
import DialogActions from "@material-ui/core/DialogActions";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogTitle from "@material-ui/core/DialogTitle";
import Paper from "@material-ui/core/Paper";
import Draggable from "react-draggable";
function PaperComponent(props) {
return (
<Draggable
handle="#draggable-dialog-title"
cancel={'[class*="MuiDialogContent-root"]'}
>
<Paper {...props} />
</Draggable>
);
}
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 open={open} onClose={handleClose} PaperComponent={PaperComponent}>
<DialogTitle style={{ cursor: "move" }} id="draggable-dialog-title">
Title
</DialogTitle>
<DialogContent>
<DialogContentText>lorem ipsum</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={handleClose} color="primary">
ok
</Button>
</DialogActions>
</Dialog>
</div>
);
}
to make a draggable dialog.
We created the PaperComponent
to make it draggable.
The Draggable
component from react-draggable lets us specify the draggable item by settle the handle
prop with the selector with the item we want to make draggable.
We pass that into the PaperComponent
prop of Dialog
.
The selector has to be in the DialogTitle
to make the dialog draggable.
Conclusion
We can make full-screen dialogs responsive.
They can also be made draggable.