Modal.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { Flex } from "rebass/styled-components";
  2. import styled from "styled-components";
  3. import React, { FC } from "react";
  4. import ReactDOM from "react-dom";
  5. import Animation from "./Animation";
  6. interface Props extends React.ComponentProps<typeof Flex> {
  7. show: boolean;
  8. id?: string;
  9. closeHandler?: () => unknown;
  10. }
  11. const Wrapper = styled.div`
  12. position: fixed;
  13. width: 100%;
  14. height: 100%;
  15. top: 0;
  16. left: 0;
  17. display: flex;
  18. justify-content: center;
  19. align-items: center;
  20. background-color: rgba(50, 50, 50, 0.8);
  21. z-index: 1000;
  22. `;
  23. const Modal: FC<Props> = ({ children, id, show, closeHandler, ...rest }) => {
  24. if (!show) return null;
  25. const onClickOutside = (e) => {
  26. if (e.target.id === id) closeHandler();
  27. };
  28. return ReactDOM.createPortal(
  29. <Wrapper id={id} onClick={onClickOutside}>
  30. <Animation
  31. offset="-20px"
  32. duration="0.2s"
  33. minWidth={[400, 450]}
  34. maxWidth="90%"
  35. py={[32, 32, 48]}
  36. px={[24, 24, 32]}
  37. style={{ borderRadius: 8, backgroundColor: "white" }}
  38. flexDirection="column"
  39. {...rest}
  40. >
  41. {children}
  42. </Animation>
  43. </Wrapper>,
  44. document.body
  45. );
  46. };
  47. Modal.defaultProps = {
  48. show: false
  49. };
  50. export default Modal;