Hi everyone,
I’m developing a React-based UXP plugin for InDesign, and I’m using the standard HTML <dialog> element with showModal() to display modal dialogs.
The dialog appears correctly and works fine functionally, but it’s not centered on the screen — it often opens toward the bottom-left corner of the window, especially when the dialog’s size is small (e.g., width 390px, height 150px).
Here’s a simplified version of my code:
import React, { useEffect, useRef } from "react";
export default function DialogBase({ title, width = "400px", height = "auto", children, onClose }) {
const dialogRef = useRef(null);
useEffect(() => {
const dialog = dialogRef.current;
if (!dialog) return;
try {
dialog.showModal();
} catch {
dialog.setAttribute("open", "");
}
const handleKeyDown = (e) => {
if (e.key === "Escape") onClose?.("escape");
};
dialog.addEventListener("keydown", handleKeyDown);
return () => dialog.removeEventListener("keydown", handleKeyDown);
}, [onClose]);
return (
<dialog
ref={dialogRef}
style={{
width,
height,
border: "none",
borderRadius: "8px",
backgroundColor: "#F1F1F1",
boxShadow: "0 8px 20px rgba(0,0,0,0.4)",
}}
>
<form method="dialog">
{title && <div style={{ padding: "6px 10px", background: "#e0e0e0" }}>{title}</div>}
<div style={{ padding: "10px" }}>{children}</div>
</form>
</dialog>
);
}