Modal.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import React from "react";
  2. import { useTranslation } from "react-i18next";
  3. import styled from "styled-components";
  4. import { hasClass } from "../utils";
  5. const StyledModal = styled.div.attrs(() => ({ className: "overlay" }))`
  6. position: fixed;
  7. z-index: 20;
  8. left: 0;
  9. top: 0;
  10. width: 100%;
  11. height: 100%;
  12. overflow: auto;
  13. background-color: rgba(0, 0, 0, 0.4);
  14. border-radius: 5px;
  15. .modal-content {
  16. max-width: 50%;
  17. position: relative;
  18. margin: 10% auto;
  19. padding: 8px 8px 8px 8px;
  20. padding: 2em;
  21. border-radius: 2px;
  22. background-color: var(--color-blueGrey);
  23. box-shadow: rgba(0, 0, 0, 0.19) 0px 10px 20px,
  24. rgba(0, 0, 0, 0.23) 0px 6px 6px;
  25. }
  26. .close {
  27. position: absolute;
  28. top: 0.2em;
  29. right: 0.2em;
  30. padding: 0.5rem;
  31. margin: 0;
  32. }
  33. footer {
  34. margin-top: 1em;
  35. }
  36. @media screen and (max-width: 640px) {
  37. & .modal-content {
  38. max-width: 90%;
  39. }
  40. }
  41. `;
  42. export const Modal = ({ setShow, show, children, footer, title }) => {
  43. const { t } = useTranslation();
  44. if (!show) {
  45. return null;
  46. }
  47. return (
  48. <StyledModal
  49. onClick={(e) => {
  50. if (hasClass(e.target, "overlay")) setShow(false);
  51. }}
  52. >
  53. <div className="modal-content">
  54. <article>
  55. <header>
  56. <h3 className="title">{title}</h3>
  57. <button
  58. className="button clear icon-only close"
  59. onClick={() => {
  60. setShow(false);
  61. }}
  62. >
  63. <img
  64. src="https://icongr.am/feather/x.svg?size=30&color=ffffff"
  65. alt={t("Close")}
  66. />
  67. </button>
  68. </header>
  69. <section className="content">{children}</section>
  70. <footer>{footer}</footer>
  71. </article>
  72. </div>
  73. </StyledModal>
  74. );
  75. };
  76. export default Modal;