2020-03-13 15:32:47 -04:00
|
|
|
import "./Modal.scss";
|
2020-01-15 20:42:02 +05:00
|
|
|
|
2020-10-15 21:39:24 +02:00
|
|
|
import React, { useState, useLayoutEffect } 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";
|
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;
|
2020-05-20 16:21:37 +03:00
|
|
|
}) => {
|
2020-01-15 20:42:02 +05:00
|
|
|
const modalRoot = useBodyRoot();
|
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();
|
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}
|
|
|
|
>
|
2020-01-15 20:42:02 +05:00
|
|
|
<div className="Modal__background" onClick={props.onCloseRequest}></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` }}
|
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
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const useBodyRoot = () => {
|
2020-10-15 21:39:24 +02:00
|
|
|
const [div, setDiv] = useState<HTMLDivElement | null>(null);
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
2020-09-26 02:48:45 +05:30
|
|
|
const isDarkTheme = !!document
|
|
|
|
.querySelector(".excalidraw")
|
2021-03-13 18:58:06 +05:30
|
|
|
?.classList.contains("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");
|
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);
|
|
|
|
};
|
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
|
|
|
};
|