Created
December 29, 2020 17:23
-
-
Save richstokoe/3c9ff27ff0bfc02e3e27f6155c064238 to your computer and use it in GitHub Desktop.
WebComponent showing simple consumer switching between shadow vs non-shadow DOM
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
// Encapsulated | |
// <my-webcomponent shadow></my-webcomponent> | |
// Inherits page styles | |
// <my-webcomponent></my-webcomponent> | |
class MyWebComponent extends HTMLElement { | |
constructor() { | |
let self = super(); | |
if(this.hasAttribute("shadow")){ | |
this.init(this.attachShadow({mode: 'open'})); | |
} | |
else { | |
this.init(self); | |
} | |
} | |
init(self){ | |
// build DOM | |
} | |
} | |
customElements.define('my-webcomponent', MyWebComponent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment