Skip to content

Instantly share code, notes, and snippets.

@bitstarr
Created July 7, 2023 14:17
Show Gist options
  • Save bitstarr/b1a5bfbd9f27060edc88ca4e858cf340 to your computer and use it in GitHub Desktop.
Save bitstarr/b1a5bfbd9f27060edc88ca4e858cf340 to your computer and use it in GitHub Desktop.
quick custom element with shadow dom #JavaScript
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);
<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