aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/views/common/dialog/Dialog.tsx
blob: 31dd113b8e802fd69f0e3819ea62dbf07a316dc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { ReactNode } from "react";
import ReactDOM from "react-dom";
import { CSSTransition } from "react-transition-group";
import classNames from "classnames";

import { ThemeColor } from "../common";

import "./Dialog.css";

const optionalPortalElement = document.getElementById("portal");
if (optionalPortalElement == null) {
  throw new Error("Portal element not found");
}
const portalElement = optionalPortalElement;

interface DialogProps {
  open: boolean;
  onClose: () => void;
  color?: ThemeColor;
  children?: ReactNode;
  disableCloseOnClickOnOverlay?: boolean;
}

export default function Dialog({
  open,
  onClose,
  color,
  children,
  disableCloseOnClickOnOverlay,
}: DialogProps) {
  color = color ?? "primary";

  return ReactDOM.createPortal(
    <CSSTransition
      mountOnEnter
      unmountOnExit
      in={open}
      timeout={300}
      classNames="cru-dialog"
    >
      <div
        className={classNames("cru-dialog-overlay", `cru-${color}`)}
        onPointerDown={
          disableCloseOnClickOnOverlay
            ? undefined
            : () => {
                onClose();
              }
        }
      >
        <div className="cru-dialog-background" />
        <div
          className="cru-dialog-container"
          onPointerDown={(e) => e.stopPropagation()}
        >
          {children}
        </div>
      </div>
    </CSSTransition>,
    portalElement,
  );
}