aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/components/dialog/Dialog.tsx
blob: 2d0b34db75ace248ac13e0bb09058878fb76cd51 (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
63
import { ReactNode, useRef } from "react";
import classNames from "classnames";
import { CSSTransition } from "react-transition-group";

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

import InPortal from "../InPortal";

import "./Dialog.css";

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

export default function Dialog({
  open,
  onClose,
  color,
  children,
  disableCloseOnClickOnOverlay,
}: DialogProps) {
  const nodeRef = useRef(null);
  const lastPointerDownIdRef = useRef<number | null>(null);

  return (
    <InPortal>
      <CSSTransition
        nodeRef={nodeRef}
        mountOnEnter
        unmountOnExit
        in={open}
        timeout={300}
        classNames="cru-dialog"
      >
        <div
          ref={nodeRef}
          className={classNames(
            `cru-theme-${color ?? "primary"}`,
            "cru-dialog-overlay",
          )}
        >
          <div
            className="cru-dialog-background"
            onPointerDown={(e) => {
              lastPointerDownIdRef.current = e.pointerId;
            }}
            onPointerUp={(e) => {
              if (lastPointerDownIdRef.current === e.pointerId) {
                if (!disableCloseOnClickOnOverlay) onClose();
              }
              lastPointerDownIdRef.current = null;
            }}
          />
          <div className="cru-dialog-container">{children}</div>
        </div>
      </CSSTransition>
    </InPortal>
  );
}