Skip to content

Instantly share code, notes, and snippets.

@thescientist13
Last active September 1, 2018 19:53
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 thescientist13/fc5ae1e4a95baccf2ff4e21752a8f599 to your computer and use it in GitHub Desktop.
Save thescientist13/fc5ae1e4a95baccf2ff4e21752a8f599 to your computer and use it in GitHub Desktop.
Basic Custom Element Example
// 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