Created
September 22, 2023 03:40
-
-
Save fimars/037390ab3babb8984c0367a498314bd6 to your computer and use it in GitHub Desktop.
This file contains 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
const stylesheets = {}; | |
export const injectStyles = ({ fileScopeId, css, mountNode = document.head }) => { | |
let stylesheet = stylesheets[fileScopeId]; | |
if (!stylesheet) { | |
const styleEl = document.createElement('style'); | |
styleEl.setAttribute('type', 'text/css'); | |
stylesheet = stylesheets[fileScopeId] = styleEl; | |
if (mountNode !== document.head) { | |
styleEl.setAttribute('scoped', ''); | |
} | |
mountNode.appendChild(styleEl); | |
} | |
stylesheet.innerHTML = css; | |
for (let i = 0; i < stylesheet.sheet.cssRules.length; i++) { | |
const rule = stylesheet.sheet.cssRules[i]; | |
rule.selectorText = `.${mountNode.className} ${rule.selectorText}`; | |
stylesheet.sheet.deleteRule(i); | |
stylesheet.sheet.insertRule(rule.cssText); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment