You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
5 years ago
|
import "./Modal.scss";
|
||
5 years ago
|
|
||
|
import { createPortal } from "react-dom";
|
||
4 years ago
|
import clsx from "clsx";
|
||
5 years ago
|
import { KEYS } from "../keys";
|
||
4 years ago
|
import { AppState } from "../types";
|
||
2 years ago
|
import { useCreatePortalContainer } from "../hooks/useCreatePortalContainer";
|
||
5 years ago
|
|
||
3 years ago
|
export const Modal: React.FC<{
|
||
5 years ago
|
className?: string;
|
||
5 years ago
|
children: React.ReactNode;
|
||
|
maxWidth?: number;
|
||
|
onCloseRequest(): void;
|
||
5 years ago
|
labelledBy: string;
|
||
4 years ago
|
theme?: AppState["theme"];
|
||
3 years ago
|
closeOnClickOutside?: boolean;
|
||
3 years ago
|
}> = (props) => {
|
||
2 years ago
|
const { closeOnClickOutside = true } = props;
|
||
|
const modalRoot = useCreatePortalContainer({
|
||
|
className: "excalidraw-modal-container",
|
||
|
});
|
||
5 years ago
|
|
||
4 years ago
|
if (!modalRoot) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
5 years ago
|
const handleKeydown = (event: React.KeyboardEvent) => {
|
||
|
if (event.key === KEYS.ESCAPE) {
|
||
|
event.nativeEvent.stopImmediatePropagation();
|
||
4 years ago
|
event.stopPropagation();
|
||
5 years ago
|
props.onCloseRequest();
|
||
|
}
|
||
|
};
|
||
4 years ago
|
|
||
5 years ago
|
return createPortal(
|
||
5 years ago
|
<div
|
||
4 years ago
|
className={clsx("Modal", props.className)}
|
||
5 years ago
|
role="dialog"
|
||
|
aria-modal="true"
|
||
|
onKeyDown={handleKeydown}
|
||
|
aria-labelledby={props.labelledBy}
|
||
2 years ago
|
data-prevent-outside-click
|
||
5 years ago
|
>
|
||
3 years ago
|
<div
|
||
|
className="Modal__background"
|
||
|
onClick={closeOnClickOutside ? props.onCloseRequest : undefined}
|
||
2 years ago
|
/>
|
||
5 years ago
|
<div
|
||
|
className="Modal__content"
|
||
4 years ago
|
style={{ "--max-width": `${props.maxWidth}px` }}
|
||
4 years ago
|
tabIndex={0}
|
||
5 years ago
|
>
|
||
5 years ago
|
{props.children}
|
||
|
</div>
|
||
|
</div>,
|
||
5 years ago
|
modalRoot,
|
||
5 years ago
|
);
|
||
5 years ago
|
};
|