Replies: 1 comment
-
|
@pearcake emotion has no native the cleanest way to bridge emotion into import createCache from '@emotion/cache'
const adoptable = new CSSStyleSheet()
const cache = createCache({
key: 'shared',
container: document.createElement('div'), // detached, avoids polluting document
speedy: false, // avoids tag.sheet being null on detached containers
})
const origInsert = cache.sheet.insert.bind(cache.sheet)
cache.sheet.insert = (rule: string) => {
origInsert(rule)
try { adoptable.insertRule(rule, adoptable.cssRules.length) } catch {}
}then in your web components: class MyWidget extends HTMLElement {
connectedCallback() {
const shadow = this.attachShadow({ mode: 'open' })
shadow.adoptedStyleSheets = [adoptable] // shared across all instances
// render React with <CacheProvider value={cache}>
}
}this is better than stylis plugins (which run at compile time, before insertion, so they can't produce a few gotchas:
if shadow DOM is a core requirement and you're not locked into emotion, Lit's ref: @emotion/cache docs | shadow DOM support #388 | MDN adoptedStyleSheets |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, basically the title. I need to share Emotion styles between two web components with shadow dom, but the closest I've got to getting to style values and syncing their updates with adopted stylesheet was hooking into "stylis plugins", but that doesnt seem to be a robust solution. Is there any way I can pull this off, or does that require changes to the library?
Beta Was this translation helpful? Give feedback.
All reactions