Open In App

React MUI Modal API

MUI or Material-UI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google.

In this article, we will discuss the React MUI Modal API. Modals are the foundation that provides the framework for popups, dialogs, lightboxes, etc. The API provides a lot of functionality and we will learn to implement them.



Import Modal API

import Modal from '@mui/material/Modal';
// or
import { Modal } from '@mui/material';

Props List: Here is the list of different props used with this component. We can access them and modify them according to our needs.



CSS Rules:

Syntax: Create a Modal as follows:

<Modal open={open} onClose={handleClose}>
    Content
</Modal>

Installing and Creating React app, and adding the MUI dependencies:

Step 1: Create a react project using the following command.

npx create-react-app gfg_tutorial

Step 2: Get into the project directory

cd gfg_tutorial

Step 3: Install the MUI dependencies as follows:

npm install @mui/material @emotion/react @emotion/styled @mui/lab

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have a basic Modal.




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import Button from "@mui/material/Button";
import Typography from "@mui/material/Typography";
import Modal from "@mui/material/Modal";
  
const style = {
    position: "absolute",
    top: "50%",
    left: "50%",
    transform: "translate(-50%, -50%)",
    width: 400,
    bgcolor: "background.paper",
    border: "2px solid #000",
    boxShadow: 24,
    p: 4,
};
  
function App() {
    const [open, setOpen] = React.useState(false);
    const handleOpen = () => setOpen(true);
    const handleClose = () => setOpen(false);
  
    return (
        <div className="App">
            <div
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI Modal API</strong>
            </div>
            <br />
            <div
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <Button onClick={handleOpen}>
                    Open modal
                </Button>
            </div>
            <Modal
                open={open}
                onClose={handleClose}
                aria-labelledby="modal-modal-title"
                aria-describedby="modal-modal-description"
            >
                <Box sx={style}>
                    <Typography id="modal-modal-title" 
                        variant="h6" component="h2">
                        Welcome to GeeksforGeeks
                    </Typography>
                    <Typography id="modal-modal-description" 
                        sx={{ mt: 2 }}>
                        A computer science portal for geeks
                    </Typography>
                </Box>
            </Modal>
        </div>
    );
}
  
export default App;

Output:

 

Example 2: In the following example, we have animated the backdrop component.




import "./App.css";
import * as React from "react";
import Backdrop from "@mui/material/Backdrop";
import Box from "@mui/material/Box";
import Modal from "@mui/material/Modal";
import Fade from "@mui/material/Fade";
import Button from "@mui/material/Button";
import Typography from "@mui/material/Typography";
  
const style = {
    position: "absolute",
    top: "50%",
    left: "50%",
    transform: "translate(-50%, -50%)",
    width: 400,
    bgcolor: "background.paper",
    border: "2px solid #000",
    boxShadow: 24,
    p: 4,
};
function App() {
    const [open, setOpen] = React.useState(false);
    const handleOpen = () => setOpen(true);
    const handleClose = () => setOpen(false);
  
    return (
        <div className="App">
            <div
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI Modal API</strong>
            </div>
            <br />
            <div
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <Button onClick={handleOpen}>
                    Open modal
                </Button>
            </div>
            <Modal
                open={open}
                onClose={handleClose}
                closeAfterTransition
                BackdropComponent={Backdrop}
                BackdropProps={{
                    timeout: 1500,
                }}
            >
                <Box sx={style}>
                    <Typography id="modal-modal-title" 
                        variant="h6" component="h2">
                        Welcome to GeeksforGeeks
                    </Typography>
                    <Typography id="modal-modal-description"
                        sx={{ mt: 2 }}>
                        A computer science portal for geeks
                    </Typography>
                </Box>
            </Modal>
        </div>
    );
}
  
export default App;

Output:

 

Reference: https://mui.com/material-ui/api/modal/


Article Tags :