Last active
September 1, 2018 19:53
-
-
Save thescientist13/fc5ae1e4a95baccf2ff4e21752a8f599 to your computer and use it in GitHub Desktop.
Basic Custom Element Example
This file contains 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
// https://jsfiddle.net/thegreenhouseio/qjj9hvv9/ | |
class HelloWorld extends HTMLElement { | |
constructor(name) { | |
super(); | |
this.subject = name || 'World'; | |
// creates a Shadow DOM tree | |
this.root = this.attachShadow({ mode: 'closed' }); | |
} | |
// run some code when the component is ready | |
// in this case insert our template into our component's DOM | |
connectedCallback() { | |
this.root.innerHTML = this.getTemplate(); | |
} | |
// our component's template to display | |
getTemplate() { | |
return `<h1>Hello ${this.subject}!</h1>`; | |
} | |
} | |
// register our custom element | |
customElements.define('hello-world', HelloWorld); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment