|
1 | 1 | import * as React from 'react';
|
2 |
| -import { makeStyles } from '@material-ui/core/styles'; |
| 2 | +import Box from '@material-ui/core/Box'; |
3 | 3 | import Modal from '@material-ui/core/Modal';
|
4 |
| - |
5 |
| -function rand() { |
6 |
| - return Math.round(Math.random() * 20) - 10; |
7 |
| -} |
8 |
| - |
9 |
| -function getModalStyle() { |
10 |
| - const top = 50 + rand(); |
11 |
| - const left = 50 + rand(); |
12 |
| - |
13 |
| - return { |
14 |
| - top: `${top}%`, |
15 |
| - left: `${left}%`, |
16 |
| - transform: `translate(-${top}%, -${left}%)`, |
17 |
| - }; |
18 |
| -} |
19 |
| - |
20 |
| -const useStyles = makeStyles((theme) => ({ |
21 |
| - paper: { |
22 |
| - position: 'absolute', |
23 |
| - width: 400, |
24 |
| - backgroundColor: theme.palette.background.paper, |
25 |
| - border: '2px solid #000', |
26 |
| - boxShadow: theme.shadows[5], |
27 |
| - padding: theme.spacing(2, 4, 3), |
28 |
| - }, |
29 |
| -})); |
| 4 | +import Button from '@material-ui/core/Button'; |
| 5 | +import Typography from '@material-ui/core/Typography'; |
| 6 | + |
| 7 | +const style = { |
| 8 | + position: 'absolute', |
| 9 | + top: '50%', |
| 10 | + left: '50%', |
| 11 | + transform: 'translate(-50%, -50%)', |
| 12 | + width: 400, |
| 13 | + bgcolor: 'background.paper', |
| 14 | + border: '2px solid #000', |
| 15 | + boxShadow: 24, |
| 16 | + p: 4, |
| 17 | +}; |
30 | 18 |
|
31 | 19 | export default function KeepMountedModal() {
|
32 |
| - const classes = useStyles(); |
33 |
| - // getModalStyle is not a pure function, we roll the style only on the first render |
34 |
| - const [modalStyle] = React.useState(getModalStyle); |
35 | 20 | const [open, setOpen] = React.useState(false);
|
36 |
| - |
37 |
| - const handleOpen = () => { |
38 |
| - setOpen(true); |
39 |
| - }; |
40 |
| - |
41 |
| - const handleClose = () => { |
42 |
| - setOpen(false); |
43 |
| - }; |
| 21 | + const handleOpen = () => setOpen(true); |
| 22 | + const handleClose = () => setOpen(false); |
44 | 23 |
|
45 | 24 | return (
|
46 | 25 | <div>
|
47 |
| - <button type="button" onClick={handleOpen}> |
48 |
| - Open Modal |
49 |
| - </button> |
| 26 | + <Button onClick={handleOpen}>Open modal</Button> |
50 | 27 | <Modal
|
51 | 28 | keepMounted
|
52 | 29 | open={open}
|
53 | 30 | onClose={handleClose}
|
54 | 31 | aria-labelledby="keep-mounted-modal-title"
|
55 | 32 | aria-describedby="keep-mounted-modal-description"
|
56 | 33 | >
|
57 |
| - <div style={modalStyle} className={classes.paper}> |
58 |
| - <h2 id="keep-mounted-modal-title">Text in a modal</h2> |
59 |
| - <p id="keep-mounted-modal-description"> |
| 34 | + <Box sx={style}> |
| 35 | + <Typography id="keep-mounted-modal-title" variant="h6" component="h2"> |
| 36 | + Text in a modal |
| 37 | + </Typography> |
| 38 | + <Typography id="keep-mounted-modal-description" sx={{ mt: 2 }}> |
60 | 39 | Duis mollis, est non commodo luctus, nisi erat porttitor ligula.
|
61 |
| - </p> |
62 |
| - </div> |
| 40 | + </Typography> |
| 41 | + </Box> |
63 | 42 | </Modal>
|
64 | 43 | </div>
|
65 | 44 | );
|
|
0 commit comments