Created
April 7, 2019 10:51
-
-
Save mistyharsh/c4f756abe986670a8a0bfafde06f1404 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 sheet = new CSSStyleSheet(); | |
// Replace all styles synchronously for this style sheet | |
sheet.replaceSync('p { color: green; }'); | |
class FancyComponent1 extends HTMLElement { | |
constructor() { | |
super(); | |
const shadowRoot = this.attachShadow({ mode: 'open' }); | |
// Attaching the style sheet to the Shadow DOM of this component | |
shadowRoot.adoptedStyleSheets = [sheet]; | |
shadowRoot.innerHTML = ` | |
<div> | |
<p>Hello World</p> | |
</div> | |
`; | |
} | |
} | |
class FancyComponent2 extends HTMLElement { | |
constructor() { | |
super(); | |
const shadowRoot = this.attachShadow({ mode: 'open' }); | |
// Same style sheet can also be used by another web component | |
shadowRoot.adoptedStyleSheets = [sheet]; | |
// You can even manipulate the style sheet with plain JS manipulations | |
setTimeout(() => shadowRoot.adoptedStyleSheets = [], 2000); | |
shadowRoot.innerHTML = ` | |
<div> | |
<p>Hello World</p> | |
</div> | |
`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment