UNPKG

tldraw

Version:

A tiny little drawing editor.

76 lines (75 loc) 2.38 kB
import { jsx } from "react/jsx-runtime"; import { useContainer, useValue } from "@tldraw/editor"; import { Dialog as _Dialog } from "radix-ui"; import { memo, useCallback, useRef } from "react"; import { useDialogs } from "../context/dialogs.mjs"; const TldrawUiDialog = ({ id, component: ModalContent, onClose, preventBackgroundClose }) => { const { removeDialog } = useDialogs(); const mouseDownInsideContentRef = useRef(false); const container = useContainer(); const handleOpenChange = useCallback( (isOpen) => { if (!isOpen) { if (onClose) { try { onClose(); } catch (err) { console.warn(err); } } removeDialog(id); } }, [id, onClose, removeDialog] ); return /* @__PURE__ */ jsx(_Dialog.Root, { onOpenChange: handleOpenChange, defaultOpen: true, children: /* @__PURE__ */ jsx(_Dialog.Portal, { container, children: /* @__PURE__ */ jsx( _Dialog.Overlay, { dir: "ltr", className: "tlui-dialog__overlay", onClick: (e) => { if (mouseDownInsideContentRef.current) return; if (!preventBackgroundClose && e.target === e.currentTarget) handleOpenChange(false); }, children: /* @__PURE__ */ jsx( _Dialog.Content, { dir: "ltr", className: "tlui-dialog__content", "aria-describedby": void 0, onMouseDown: () => mouseDownInsideContentRef.current = true, onMouseUp: () => mouseDownInsideContentRef.current = false, onInteractOutside: (e) => { mouseDownInsideContentRef.current = false; if (preventBackgroundClose) { e.preventDefault(); } }, children: /* @__PURE__ */ jsx( ModalContent, { onClose: () => { mouseDownInsideContentRef.current = false; handleOpenChange(false); } } ) } ) } ) }) }); }; const DefaultDialogs = memo(function DefaultDialogs2() { const { dialogs } = useDialogs(); const dialogsArray = useValue("dialogs", () => dialogs.get(), [dialogs]); return dialogsArray.map((dialog) => /* @__PURE__ */ jsx(TldrawUiDialog, { ...dialog }, dialog.id)); }); export { DefaultDialogs }; //# sourceMappingURL=Dialogs.mjs.map