UNPKG

@react-slate/core

Version:

Write interactive CLI apps with React

40 lines 1.82 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const react_reconciler_1 = __importDefault(require("react-reconciler")); const reconcilerConfig_1 = __importDefault(require("./host/reconcilerConfig")); const View_1 = __importDefault(require("./host/nodes/View")); const Renderer_1 = __importDefault(require("./host/renderer/Renderer")); const utils_1 = require("./utils"); function renderToString(element, { width, height, maxRenders, } = {}) { let currentSnapshot = ''; const asyncIterator = new utils_1.AsyncIterator(maxRenders); const renderer = new Renderer_1.default(); const container = new View_1.default(); container.setLayoutStyle({ width: '100%', height: '100%' }); const reconciler = react_reconciler_1.default(reconcilerConfig_1.default(container, () => { const layout = container.layoutNode.computeLayout({ width: width || null, height: height || null, }); container.notifyOnLayoutHook(layout, { offsetX: 0, offsetY: 0 }); currentSnapshot = renderer.renderToString(container, layout); asyncIterator.nextValue(currentSnapshot); })); const node = reconciler.createContainer(container, false, false); reconciler.updateContainer(element, node, null, () => undefined); return { get snapshot() { return currentSnapshot; }, start: () => asyncIterator.makeIterator({}), stop: () => { asyncIterator.finish(undefined); reconciler.updateContainer(null, node, null, () => undefined); }, }; } exports.default = renderToString; //# sourceMappingURL=renderToString.js.map