import {createPortal, render} from 'react-dom'; import {useEffect, useRef} from 'react'; import './styles.scss'; /** * @since 3.14.0 * Creates a portal to the Top Level document, rendering children elements within an iframe. */ export default function createIframePortal(children, targetElement = window.top.document.body) { const iframeRef = useRef(null); useEffect(() => { const iframe = iframeRef.current; const iframeDoc = iframe.contentDocument || iframe.contentWindow.document; if (iframe) { // Clear existing content. iframeDoc.head.innerHTML = ''; iframeDoc.body.innerHTML = ''; async function renderContent() { try { await fetchStylesheets(iframeDoc); render(children, iframeDoc.body); } catch (error) { console.error('Error loading stylesheets:', error); } } renderContent(); } }, []); return createPortal(