2020-03-13 15:32:47 -04:00
|
|
|
import "./Modal.scss";
|
2020-01-15 20:42:02 +05:00
|
|
|
|
2021-04-08 19:54:50 +02:00
|
|
|
import React, { useState, useLayoutEffect, useRef } from "react";
|
2020-01-15 20:42:02 +05:00
|
|
|
import { createPortal } from "react-dom";
|
2020-10-19 17:14:28 +03:00
|
|
|
import clsx from "clsx";
|
2020-01-25 19:37:58 -03:00
|
|
|
import { KEYS } from "../keys";
|
2021-04-13 23:02:57 +05:30
|
|
|
import { useExcalidrawContainer, useIsMobile } from "./App";
|
|
|
|
import { AppState } from "../types";
|
2021-10-14 14:15:57 +05:30
|
|
|
import { THEME } from "../constants";
|
2020-01-15 20:42:02 +05:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const Modal = (props: {
|
2020-03-13 15:32:47 -04:00
|
|
|
className?: string;
|
2020-01-15 20:42:02 +05:00
|
|
|
children: React.ReactNode;
|
|
|
|
maxWidth?: number;
|
|
|
|
onCloseRequest(): void;
|
2020-01-25 19:37:58 -03:00
|
|
|
labelledBy: string;
|
2021-04-13 23:02:57 +05:30
|
|
|
theme?: AppState["theme"];
|
2021-11-17 23:53:43 +05:30
|
|
|
closeOnClickOutside?: boolean;
|
2020-05-20 16:21:37 +03:00
|
|
|
}) => {
|
2021-11-17 23:53:43 +05:30
|
|
|
const { theme = THEME.LIGHT, closeOnClickOutside = true } = props;
|
2021-04-13 23:02:57 +05:30
|
|
|
const modalRoot = useBodyRoot(theme);
|
2020-01-25 19:37:58 -03:00
|
|
|
|
2020-10-15 21:39:24 +02:00
|
|
|
if (!modalRoot) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-02-28 23:03:53 +01:00
|
|
|
const handleKeydown = (event: React.KeyboardEvent) => {
|
|
|
|
if (event.key === KEYS.ESCAPE) {
|
|
|
|
event.nativeEvent.stopImmediatePropagation();
|
2021-04-13 01:29:25 +05:30
|
|
|
event.stopPropagation();
|
2020-01-25 19:37:58 -03:00
|
|
|
props.onCloseRequest();
|
|
|
|
}
|
|
|
|
};
|
2020-10-15 21:39:24 +02:00
|
|
|
|
2020-01-15 20:42:02 +05:00
|
|
|
return createPortal(
|
2020-01-25 19:37:58 -03:00
|
|
|
<div
|
2020-10-19 17:14:28 +03:00
|
|
|
className={clsx("Modal", props.className)}
|
2020-01-25 19:37:58 -03:00
|
|
|
role="dialog"
|
|
|
|
aria-modal="true"
|
|
|
|
onKeyDown={handleKeydown}
|
|
|
|
aria-labelledby={props.labelledBy}
|
|
|
|
>
|
2021-11-17 23:53:43 +05:30
|
|
|
<div
|
|
|
|
className="Modal__background"
|
|
|
|
onClick={closeOnClickOutside ? props.onCloseRequest : undefined}
|
|
|
|
></div>
|
2020-04-05 15:58:00 +03:00
|
|
|
<div
|
|
|
|
className="Modal__content"
|
2021-01-03 11:50:41 +02:00
|
|
|
style={{ "--max-width": `${props.maxWidth}px` }}
|
2021-04-13 01:29:25 +05:30
|
|
|
tabIndex={0}
|
2020-04-05 15:58:00 +03:00
|
|
|
>
|
2020-01-15 20:42:02 +05:00
|
|
|
{props.children}
|
|
|
|
</div>
|
|
|
|
</div>,
|
2020-01-24 12:04:54 +02:00
|
|
|
modalRoot,
|
2020-01-15 20:42:02 +05:00
|
|
|
);
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-15 20:42:02 +05:00
|
|
|
|
2021-04-13 23:02:57 +05:30
|
|
|
const useBodyRoot = (theme: AppState["theme"]) => {
|
2020-10-15 21:39:24 +02:00
|
|
|
const [div, setDiv] = useState<HTMLDivElement | null>(null);
|
|
|
|
|
2021-04-08 19:54:50 +02:00
|
|
|
const isMobile = useIsMobile();
|
|
|
|
const isMobileRef = useRef(isMobile);
|
|
|
|
isMobileRef.current = isMobile;
|
|
|
|
|
2021-06-10 02:46:56 +05:30
|
|
|
const { container: excalidrawContainer } = useExcalidrawContainer();
|
2021-04-13 23:02:57 +05:30
|
|
|
|
2021-04-08 19:54:50 +02:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (div) {
|
|
|
|
div.classList.toggle("excalidraw--mobile", isMobile);
|
|
|
|
}
|
|
|
|
}, [div, isMobile]);
|
|
|
|
|
2020-10-15 21:39:24 +02:00
|
|
|
useLayoutEffect(() => {
|
2021-04-13 23:02:57 +05:30
|
|
|
const isDarkTheme =
|
|
|
|
!!excalidrawContainer?.classList.contains("theme--dark") ||
|
|
|
|
theme === "dark";
|
2020-01-15 20:42:02 +05:00
|
|
|
const div = document.createElement("div");
|
2020-09-26 02:48:45 +05:30
|
|
|
|
2021-02-14 18:18:34 +05:30
|
|
|
div.classList.add("excalidraw", "excalidraw-modal-container");
|
2021-04-08 19:54:50 +02:00
|
|
|
div.classList.toggle("excalidraw--mobile", isMobileRef.current);
|
2020-09-26 02:48:45 +05:30
|
|
|
|
|
|
|
if (isDarkTheme) {
|
2021-03-13 18:58:06 +05:30
|
|
|
div.classList.add("theme--dark");
|
|
|
|
div.classList.add("theme--dark-background-none");
|
2020-09-26 02:48:45 +05:30
|
|
|
}
|
2020-01-15 20:42:02 +05:00
|
|
|
document.body.appendChild(div);
|
2020-10-15 21:39:24 +02:00
|
|
|
|
|
|
|
setDiv(div);
|
|
|
|
|
2020-01-15 20:42:02 +05:00
|
|
|
return () => {
|
|
|
|
document.body.removeChild(div);
|
|
|
|
};
|
2021-04-13 23:02:57 +05:30
|
|
|
}, [excalidrawContainer, theme]);
|
2020-10-15 21:39:24 +02:00
|
|
|
|
2020-01-15 20:42:02 +05:00
|
|
|
return div;
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|