Skip to content

Instantly share code, notes, and snippets.

@web-padawan
Created November 10, 2020 13:05
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 web-padawan/08ad2d8199d695cd5810920e6559dcd9 to your computer and use it in GitHub Desktop.
Save web-padawan/08ad2d8199d695cd5810920e6559dcd9 to your computer and use it in GitHub Desktop.
const $value = Symbol('value');
class MyElement extends HTMLElement {
static get observedAttributes() {
return ['value'];
}
set value(value) {
this[$value] = value;
}
get value() {
return this[$value];
}
attributeChangedCallback(attr, oldValue, newValue) {
if (oldValue !== newValue) {
this[attr] = newValue;
}
}
connectedCallback() {
// run stored value through setter
if (this.hasOwnProperty('value')) {
const value = this.value;
delete this.value;
this.value = value;
}
}
}
customElements.define('my-element', MyElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment