Created
July 7, 2023 14:17
-
-
Save bitstarr/b1a5bfbd9f27060edc88ca4e858cf340 to your computer and use it in GitHub Desktop.
quick custom element with shadow dom #JavaScript
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
class Test extends HTMLElement { | |
constructor() { | |
super(); | |
const sheet = new CSSStyleSheet(); | |
sheet.replaceSync('a { color: green; }'); /* wins!? */ | |
this.attachShadow({mode: 'open'}); | |
this.shadowRoot.adoptedStyleSheets = [sheet]; | |
this.template = document.createElement('template'); | |
this.template.innerHTML = ` | |
<style> | |
a { | |
color: blue; /* doesn't win!? */ | |
} | |
</style> | |
<a href="#nowhere">Why is this not blue?</a> | |
`; | |
this.shadowRoot.replaceChildren(this.template.content.cloneNode(true)); | |
} | |
} | |
customElements.define('t-est', Test); |
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
<t-est></t-est> | |
see https://codepen.io/heydon/pen/XWyXVgy/bd624754dff688bdf5d9d435de73aef3?editors=1010 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment