Created
July 9, 2018 19:54
-
-
Save DanielCardonaRojas/90601b935e021bf3aec23d87016e31f4 to your computer and use it in GitHub Desktop.
CustomElement WebComponent Template
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
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