import React from 'react'; import { renderToString } from 'react-dom/server'; interface PrintOptions { styles?: string; printTimeout?: number; popupFeatures?: object; } export default function(element: React.ReactElement, opts: PrintOptions = {}) { const options = { styles: '', printTimeout: 500, popupFeatures: {}, ...opts }; // Convert popupFeatures into a key=value,key=value string. See // https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Window_features // for more information. const featuresStr = Object.entries(options.popupFeatures) .map(([key, value]) => `${key}=${value}`) .join(','); const popup = window.open('about:blank', 'printWindow', featuresStr); if (popup) { popup.document.open(); popup.document.write(` ${renderToString(element)} `); } }