Skip to content

Instantly share code, notes, and snippets.

@khola
Created April 27, 2018 11:40
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 khola/534b5718ced3874fc671c584fd5fe834 to your computer and use it in GitHub Desktop.
Save khola/534b5718ced3874fc671c584fd5fe834 to your computer and use it in GitHub Desktop.
Basic component
<template id="my-custom-component">
<span></span>
</template>
<script type="text/javascript">
class MyCustomComponent extends HTMLElement {
constructor() {
super();
const template = document.getElementById('my-custom-component');
const templateContent = template.content;
this.attachShadow({ mode: 'open' })
.appendChild(templateContent.cloneNode(true));
this.displayLabel = this.shadowRoot.querySelector('span');
}
static get observedAttributes() {
return ['my-label'];
}
attributeChangedCallback(name, oldValue, newValue) {
switch (name) {
case 'my-label':
this.displayLabel.innerText = newValue;
}
}
}
customElements.define('my-custom-component', MyCustomComponent);
</script>
<my-custom-component my-label="This is my custom component"></my-custom-component>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment