Skip to content

Instantly share code, notes, and snippets.

@DanielCardonaRojas
Created July 9, 2018 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielCardonaRojas/90601b935e021bf3aec23d87016e31f4 to your computer and use it in GitHub Desktop.
Save DanielCardonaRojas/90601b935e021bf3aec23d87016e31f4 to your computer and use it in GitHub Desktop.
CustomElement WebComponent Template
class MyCustomElement extends HTMLElement {
constructor(){
super();
}
static get observedAttributes() {
return [];
}
// Life Cycle
attributeChangedCallback(name, oldVal, newVal) {
console.log("Changed: " + name + " old: " + oldVal + " new: " + newVal);
}
connectedCallback() {
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.appendChild(this.template.content.cloneNode(true));
}
get template() {
if (this._template) {return this._template;}
this._template = document.createElement('template');
let styles = document.createElement('style');
let content = document.createElement('div');
let imports = document.createElement('head');
imports.innerHTML = `
`;
styles.innerHTML = `
`;
content.innerHTML = `
`;
this._template.content.appendChild(styles);
this._template.content.appendChild(imports);
this._template.content.appendChild(content);
return this._template;
}
}
customElements.define('my-custom-element', MyCustomElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment