Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/components/mui/showConfirmDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import ReactDOM from "react-dom";
import React from "react";
import ConfirmDialog from "./confirm-dialog";

// React 18+ uses createRoot from react-dom/client; React 17 does not have this module
let createRoot;
try {
({ createRoot } = require("react-dom/client"));
} catch (_) {
// React 17 — createRoot not available, will fall back to ReactDOM.render
}

const showConfirmDialog = ({
title,
text,
Expand All @@ -28,16 +36,22 @@ const showConfirmDialog = ({
const container = document.createElement("div");
document.body.appendChild(container);

let root = null;

const close = (answer) => {
ReactDOM.unmountComponentAtNode(container);
if (root) {
root.unmount();
} else {
ReactDOM.unmountComponentAtNode(container);
}
container.remove();
resolve(answer);
};

const handleConfirm = () => close(true);
const handleCancel = () => close(false);

ReactDOM.render(
const element = (
<ConfirmDialog
open
title={title}
Expand All @@ -49,9 +63,15 @@ const showConfirmDialog = ({
cancelButtonColor={cancelButtonColor}
onConfirm={handleConfirm}
onCancel={handleCancel}
/>,
container
/>
);

if (createRoot) {
root = createRoot(container);
root.render(element);
} else {
ReactDOM.render(element, container);
}
});

export default showConfirmDialog;
Loading