Skip to content

Instantly share code, notes, and snippets.

@enjikaka
Created August 6, 2017 17:43
Show Gist options
  • Select an option

  • Save enjikaka/c46621a259f80ecbe72c582f66a6a057 to your computer and use it in GitHub Desktop.

Select an option

Save enjikaka/c46621a259f80ecbe72c582f66a6a057 to your computer and use it in GitHub Desktop.
class TemplateComponent extends HTMLElement {
constructor (tagName) {
super();
if (!tagName) {
throw new Error('You did now specify a tag name. You need to call super with the tag name in you constructor.');
}
this._tagName = tagName;
}
connectedCallback () {
this.render();
}
async render () {
let state;
if (this.getState) {
state = await this.getState();
}
const template = await this.getTemplate(state);
const shadowDOM = this.attachShadow({ mode: 'open' });
const clone = document.importNode(template.content, true);
this._shadowDOM = shadowDOM;
shadowDOM.appendChild(clone);
if (this.postRender) {
this.postRender();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment