There seems to be an oddity in isolating Shadow DOM styles from the Main DOM styles, see the below.
In essence if I am specific about the tagName then there is isolation, but if I use * as the CSS selector then the style flows into the Shadow DOM. The issue here is that the Main DOM is using a style library which appears to do this, I’m not sure if there are other cases of the main DOM styling overflowing into the Shadow DOM, but this clearly is and everything I’ve read about Shadow DOM suggests there is full isolation.
class myTest extends HTMLElement {
constructor () {
super();
}
connectedCallback () {
const shadow = this.attachShadow({ mode: 'closed' });
const h1 = document.createElement('h1')
h1.innerHTML = 'In Shadow DOM'
shadow.appendChild(h1)
}
}
customElements.define('my-test', myTest);
h1 { color: red }
* {
font-family: sans-serif
}
<h1>In main DOM</h1>
<my-test></my-test>
2
Answers
Thanks to @Danny '365CSI' Engelman is seems it expected that the isolation is not complete, so I've looked at the link @Danny sent and edited the snipped to show what might be done if you get this problem.
Re: other answer
the
connectedCallback
will run multiple times when the Custom Element is moved in the DOM.The
constructor
is better for creating all shadowDOM; it will execute only onceThere hardly ever is a need for a
closed
shadowRoot, it has nothing to do with styling.So an
open
shadowRoot is fine; returningthis.shadowRoot
, thus can be chained.Note: using
innerHTML
for setting<style>
(text)content; makes for better GZIP/Brotli compression… we want small components after all.Live edit
<style>
<style>
(and<script>
) content are HTML, and can be displayed as HTML content.The style is still applied, run below code and edit
red
to any HTML color you know