UNPKG

@wener/console

Version:

Base console UI toolkit

67 lines (66 loc) 2.26 kB
import React, { useEffect, useState } from "react"; import { createPortal } from "react-dom"; import { useStore } from "zustand"; import { useShallow } from "zustand/react/shallow"; import { getRootWindow } from "./ReactWindow.js"; import { WindowGuest } from "./WindowGuest.js"; (function (Window) { function closeAll() { getRootWindow().windows.forEach((v) => v.close()); } Window.closeAll = closeAll; function minimizeAll() { getRootWindow().windows.forEach((v) => v.minimize(true)); } Window.minimizeAll = minimizeAll; function createWindowContainer(win) { let id = `react-window-container-${win.id}`; let host = document.getElementById(id); if (host) { return host; } host = document.createElement("div"); // host.setAttribute('data-react-window-id', win.id); host.id = id; host.className = "fixed overflow-hidden w-screen h-screen left-0 top-0 pointer-events-none isolate z-40"; document.body.appendChild(host); return host; } Window.Host = () => { const window = getRootWindow(); const [container, setContainer] = useState(null); let store = window.store; // works in ssr useEffect(() => { let ele = createWindowContainer(window); setContainer(ele); store.setState({ childrenElement: ele }); return () => { ele.remove(); }; }, [ window.id ]); const windows = useStore(store, useShallow(({ windows }) => { return windows; })); return /*#__PURE__*/ React.createElement(React.Fragment, null, container && windows.map((win) => { return /*#__PURE__*/ createPortal(/*#__PURE__*/ React.createElement(WindowGuest, { key: win.id, win: win }), container, win.id); })); }; function getRoot() { return getRootWindow(); } Window.getRoot = getRoot; function open(opts) { getRootWindow().open(opts); } Window.open = open; })(Window || (Window = {})); export var Window; //# sourceMappingURL=Window.js.map