Created
October 14, 2025 04:44
-
-
Save imamkhaira/03d8474af3922dcc3e0cf778cc83d275 to your computer and use it in GitHub Desktop.
Create a single combined CSSStyleSheet from global styles. Convenient to add global styles to Shadow DOM.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const combiledStyleSheet = new CSSStyleSheet(); | |
| const allRules = [...document.styleSheets] | |
| .flatMap((ss) => { | |
| try { | |
| return [...ss.cssRules].map((rule) => rule.cssText); | |
| } catch { | |
| return []; // Skip cross-origin stylesheets | |
| } | |
| }) | |
| .filter(Boolean); | |
| combiledStyleSheet.replaceSync(allRules.join("")); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example on how to use with Web Component: