/** * Create a pop-up dialog box using React. * * Calling this function with the same ID and React Root Component will trigger a re-render * * @param id The (hopefully) unique identifier for the popup container * @param rootComponent Root React Component for the content (NOT the popup containers themselves) */ import * as React from "react"; import * as ReactDOM from "react-dom"; import { Popup } from "./Popup"; import { createElement } from "../../../utils/uiHelpers/createElement"; import { removeElementById } from "../../../utils/uiHelpers/removeElementById"; type ReactComponent = new(...args: any[]) => React.Component; let gameContainer: HTMLElement; function getGameContainer() { let container = document.getElementById("entire-game-container"); if (container == null) { throw new Error(`Failed to find game container DOM element`) } gameContainer = container; document.removeEventListener("DOMContentLoaded", getGameContainer); } document.addEventListener("DOMContentLoaded", getGameContainer); export function createPopup(id: string, rootComponent: ReactComponent, props: object): HTMLElement | null { let container = document.getElementById(id); if (container == null) { container = createElement("div", { class: "popup-box-container", display: "flex", id: id, }); gameContainer.appendChild(container); } ReactDOM.render(, container); return container; } /** * Closes a popup created with the createPopup() function above */ export function removePopup(id: string): void { let content = document.getElementById(`${id}`); if (content == null) { return; } ReactDOM.unmountComponentAtNode(content); removeElementById(id); }