File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,6 +15,14 @@ import ReactDOM from "react-dom";
1515import React from "react" ;
1616import ConfirmDialog from "./confirm-dialog" ;
1717
18+ // React 18+ uses createRoot from react-dom/client; React 17 does not have this module
19+ let createRoot ;
20+ try {
21+ ( { createRoot } = require ( "react-dom/client" ) ) ;
22+ } catch ( _ ) {
23+ // React 17 — createRoot not available, will fall back to ReactDOM.render
24+ }
25+
1826const showConfirmDialog = ( {
1927 title,
2028 text,
@@ -28,16 +36,22 @@ const showConfirmDialog = ({
2836 const container = document . createElement ( "div" ) ;
2937 document . body . appendChild ( container ) ;
3038
39+ let root = null ;
40+
3141 const close = ( answer ) => {
32- ReactDOM . unmountComponentAtNode ( container ) ;
42+ if ( root ) {
43+ root . unmount ( ) ;
44+ } else {
45+ ReactDOM . unmountComponentAtNode ( container ) ;
46+ }
3347 container . remove ( ) ;
3448 resolve ( answer ) ;
3549 } ;
3650
3751 const handleConfirm = ( ) => close ( true ) ;
3852 const handleCancel = ( ) => close ( false ) ;
3953
40- ReactDOM . render (
54+ const element = (
4155 < ConfirmDialog
4256 open
4357 title = { title }
@@ -49,9 +63,15 @@ const showConfirmDialog = ({
4963 cancelButtonColor = { cancelButtonColor }
5064 onConfirm = { handleConfirm }
5165 onCancel = { handleCancel }
52- /> ,
53- container
66+ />
5467 ) ;
68+
69+ if ( createRoot ) {
70+ root = createRoot ( container ) ;
71+ root . render ( element ) ;
72+ } else {
73+ ReactDOM . render ( element , container ) ;
74+ }
5575 } ) ;
5676
5777export default showConfirmDialog ;
You can’t perform that action at this time.
0 commit comments