Skip to content

Instantly share code, notes, and snippets.

@richstokoe
Created December 29, 2020 17:23
Show Gist options
  • Save richstokoe/3c9ff27ff0bfc02e3e27f6155c064238 to your computer and use it in GitHub Desktop.
Save richstokoe/3c9ff27ff0bfc02e3e27f6155c064238 to your computer and use it in GitHub Desktop.
WebComponent showing simple consumer switching between shadow vs non-shadow DOM
// 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