modal 2.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import React from 'react';
  2. import { makeStyles } from '@material-ui/core/styles';
  3. import Modal from '@material-ui/core/Modal';
  4. import Fade from '@material-ui/core/Fade';
  5. function rand() {
  6. return Math.round(Math.random() * 20) - 10;
  7. }
  8. function getModalStyle() {
  9. const top = 50 + rand();
  10. const left = 50 + rand();
  11. return {
  12. top: `${top}%`,
  13. left: `${left}%`,
  14. transform: `translate(-${top}%, -${left}%)`,
  15. };
  16. }
  17. const useStyles = makeStyles((theme) => ({
  18. paper: {
  19. position: 'absolute',
  20. width: 400,
  21. backgroundColor: theme.palette.background.paper,
  22. border: '2px solid #000',
  23. boxShadow: theme.shadows[5],
  24. padding: theme.spacing(2, 4, 3),
  25. },
  26. }));
  27. export default function SimpleModal() {
  28. const classes = useStyles();
  29. // getModalStyle is not a pure function, we roll the style only on the first render
  30. const [modalStyle] = React.useState(getModalStyle);
  31. const [open, setOpen] = React.useState(false);
  32. const handleOpen = () => {
  33. setOpen(true);
  34. };
  35. const handleClose = () => {
  36. setOpen(false);
  37. };
  38. /*
  39. return ( <
  40. div >
  41. <
  42. button type = "button"
  43. onClick = { handleOpen } >
  44. Open Modal <
  45. /button> <
  46. Modal open = { open }
  47. onClose = { handleClose } >
  48. <
  49. div style = { modalStyle }
  50. className = { classes.paper } > 123 < /div>< /
  51. Modal > < /
  52. div >
  53. );
  54. */
  55. return ( <
  56. div >
  57. <
  58. button type = "button"
  59. onClick = { handleOpen } >
  60. Open Modal <
  61. /button> <
  62. Modal open = { open }
  63. onClose = { handleClose } >
  64. <
  65. div >
  66. <
  67. Fade in = { open } >
  68. <
  69. div > 123 < SimpleModal / > < /div>
  70. <
  71. /Fade> < /
  72. div > < /
  73. Modal > < /
  74. div >
  75. );
  76. }