UNPKG

@brizy/cloud-media-upload

Version:
39 lines (38 loc) 1.37 kB
import React from "react"; import { createRoot } from "react-dom/client"; import { ModalContainer } from "../ModalContainer"; import { MEDIA_UPLOAD_ROOT_CLASSNAME } from "../constants"; // Defined outside the class since NewMediaUpload runs twice: first with undefined node, // then with the correct one—creating two root container. const rootContainer = new Map(); export class ModalController { constructor(node) { this.handleRef = React.createRef(); this.node = node; this.makeRoot(); } makeContainer() { if (this.node) { rootContainer.set(this.node, this.node); return this.node; } const container = document.createElement("div"); container.className = MEDIA_UPLOAD_ROOT_CLASSNAME; const _node = window.document.body.appendChild(container); rootContainer.set(this.node, _node); return _node; } get rootContainer() { return rootContainer.get(this.node) ?? this.makeContainer(); } makeRoot() { if (typeof document === "undefined") { return; } this.root = createRoot(this.rootContainer); this.root.render(React.createElement(ModalContainer, { ref: this.handleRef, rootContainer: this.rootContainer })); } update(props) { this.handleRef.current?.update(props); } }