From afa4b3890449202bd694b619c2154fcc8ebaf4ba Mon Sep 17 00:00:00 2001 From: yousefed Date: Tue, 3 Oct 2023 13:10:40 +0200 Subject: [PATCH] support CSSStyleSheet export --- .../components/DefaultOutputVisualizer.tsx | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/frame/src/runtime/executor/components/DefaultOutputVisualizer.tsx b/packages/frame/src/runtime/executor/components/DefaultOutputVisualizer.tsx index 385dae71..e6a87524 100644 --- a/packages/frame/src/runtime/executor/components/DefaultOutputVisualizer.tsx +++ b/packages/frame/src/runtime/executor/components/DefaultOutputVisualizer.tsx @@ -36,13 +36,27 @@ export const DefaultOutputVisualizer = (props: { if (styleElement) { styleElement.ownerNode?.remove(); setStyleElement(undefined); + document.adoptedStyleSheets = document.adoptedStyleSheets.filter( + (sheet) => sheet !== styleElement, + ); } - if (mainExport instanceof HTMLStyleElement) { - document.head.appendChild(mainExport); - const sheet = findStyleSheet(mainExport); - if (!sheet) { - throw new Error("css sheet not found"); + if ( + mainExport instanceof HTMLStyleElement || + mainExport instanceof CSSStyleSheet + ) { + let sheet = mainExport; + + if (sheet instanceof HTMLStyleElement) { + document.head.appendChild(sheet); + const foundSheet = findStyleSheet(sheet); + if (!foundSheet) { + throw new Error("css sheet not found"); + } + sheet = foundSheet; + } else { + // add CSSSTyleSheet sheet to document + document.adoptedStyleSheets.push(sheet); } // based on: https://stackoverflow.com/a/33237161/194651 const rules = sheet.cssRules;