@brizy/cloud-media-upload
Version:
Upload media in Brizy Cloud
46 lines (45 loc) • 1.73 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModalController = void 0;
const react_1 = __importDefault(require("react"));
const client_1 = require("react-dom/client");
const ModalContainer_1 = require("../ModalContainer");
const constants_1 = require("../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();
class ModalController {
constructor(node) {
this.handleRef = react_1.default.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 = constants_1.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 = (0, client_1.createRoot)(this.rootContainer);
this.root.render(react_1.default.createElement(ModalContainer_1.ModalContainer, { ref: this.handleRef, rootContainer: this.rootContainer }));
}
update(props) {
this.handleRef.current?.update(props);
}
}
exports.ModalController = ModalController;