* fix: Bind keyboard events to excalidraw container * fix cases around blurring * fix modal rendering so keyboard shortcuts work on modal as well * Revert "fix modal rendering so keyboard shortcuts work on modal as well" This reverts commit 2c8ec6be8eff7d308591467fe2c33cfbca16138f. * Attach keyboard event in react way so we need not handle portals separately (modals) * dnt propagate esc event when modal shown * focus the container when help dialog closed with shift+? * focus the help icon when help dialog on close triggered * move focusNearestTabbableParent to util * rename util to focusNearestParent and remove outline from excal and modal * Add prop bindKeyGlobally to decide if keyboard events should be binded to document and allow it in excal app, revert tests * fix * focus container after installing library, reset library and closing error dialog * fix tests and create util to focus container * Add excalidraw-container class to focus on the container * pass focus container to library to focus current instance of excal * update docs * remove util as it wont be used anywhere * fix propagation not being stopped for React keyboard handling * tweak reamde Co-authored-by: David Luzar <luzar.david@gmail.com> * tweak changelog * rename prop to handleKeyboardGlobally Co-authored-by: dwelle <luzar.david@gmail.com>
88 lines
2.1 KiB
TypeScript
88 lines
2.1 KiB
TypeScript
import "./Modal.scss";
|
|
|
|
import React, { useState, useLayoutEffect, useRef } from "react";
|
|
import { createPortal } from "react-dom";
|
|
import clsx from "clsx";
|
|
import { KEYS } from "../keys";
|
|
import { useIsMobile } from "../components/App";
|
|
|
|
export const Modal = (props: {
|
|
className?: string;
|
|
children: React.ReactNode;
|
|
maxWidth?: number;
|
|
onCloseRequest(): void;
|
|
labelledBy: string;
|
|
}) => {
|
|
const modalRoot = useBodyRoot();
|
|
|
|
if (!modalRoot) {
|
|
return null;
|
|
}
|
|
|
|
const handleKeydown = (event: React.KeyboardEvent) => {
|
|
if (event.key === KEYS.ESCAPE) {
|
|
event.nativeEvent.stopImmediatePropagation();
|
|
event.stopPropagation();
|
|
props.onCloseRequest();
|
|
}
|
|
};
|
|
|
|
return createPortal(
|
|
<div
|
|
className={clsx("Modal", props.className)}
|
|
role="dialog"
|
|
aria-modal="true"
|
|
onKeyDown={handleKeydown}
|
|
aria-labelledby={props.labelledBy}
|
|
>
|
|
<div className="Modal__background" onClick={props.onCloseRequest}></div>
|
|
<div
|
|
className="Modal__content"
|
|
style={{ "--max-width": `${props.maxWidth}px` }}
|
|
tabIndex={0}
|
|
>
|
|
{props.children}
|
|
</div>
|
|
</div>,
|
|
modalRoot,
|
|
);
|
|
};
|
|
|
|
const useBodyRoot = () => {
|
|
const [div, setDiv] = useState<HTMLDivElement | null>(null);
|
|
|
|
const isMobile = useIsMobile();
|
|
const isMobileRef = useRef(isMobile);
|
|
isMobileRef.current = isMobile;
|
|
|
|
useLayoutEffect(() => {
|
|
if (div) {
|
|
div.classList.toggle("excalidraw--mobile", isMobile);
|
|
}
|
|
}, [div, isMobile]);
|
|
|
|
useLayoutEffect(() => {
|
|
const isDarkTheme = !!document
|
|
.querySelector(".excalidraw")
|
|
?.classList.contains("theme--dark");
|
|
const div = document.createElement("div");
|
|
|
|
div.classList.add("excalidraw", "excalidraw-modal-container");
|
|
div.classList.toggle("excalidraw--mobile", isMobileRef.current);
|
|
|
|
if (isDarkTheme) {
|
|
div.classList.add("theme--dark");
|
|
div.classList.add("theme--dark-background-none");
|
|
}
|
|
document.body.appendChild(div);
|
|
|
|
setDiv(div);
|
|
|
|
return () => {
|
|
document.body.removeChild(div);
|
|
};
|
|
}, []);
|
|
|
|
return div;
|
|
};
|