blob: 85e8ca46541383b18065669d8b67fabe33509f54 (
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
|
import { ReactNode, useRef } from "react";
import ReactDOM from "react-dom";
import classNames from "classnames";
import { ThemeColor } from "../common";
import { useCloseDialog } from "./DialogProvider";
import "./Dialog.css";
const optionalPortalElement = document.getElementById("portal");
if (optionalPortalElement == null) {
throw new Error("Portal element not found");
}
const portalElement = optionalPortalElement;
interface DialogProps {
color?: ThemeColor;
children?: ReactNode;
disableCloseOnClickOnOverlay?: boolean;
}
export default function Dialog({
color,
children,
disableCloseOnClickOnOverlay,
}: DialogProps) {
const closeDialog = useCloseDialog();
const lastPointerDownIdRef = useRef<number | null>(null);
return ReactDOM.createPortal(
<div
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) closeDialog();
}
lastPointerDownIdRef.current = null;
}}
/>
<div className="cru-dialog-container">{children}</div>
</div>,
portalElement,
);
}
|