Skip to content

Instantly share code, notes, and snippets.

@web-padawan
Last active November 10, 2020 13:15
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/791d067ee9ce27c4f34690ca5235d0bc to your computer and use it in GitHub Desktop.
Save web-padawan/791d067ee9ce27c4f34690ca5235d0bc to your computer and use it in GitHub Desktop.
const $value = Symbol('value');
const $updating = Symbol('updating');
class MyElement extends HTMLElement {
static get observedAttributes() {
return ['value'];
}
set value(value) {
this[$value] = value;
this.render();
}
get value() {
return this[$value];
}
async render() {
if (!this[$updating]) {
this[$updating] = true;
// Ensure that changes are batched.
this[$updating] = await false;
this.textContent = `Value: ${this.value}`;
}
}
attributeChangedCallback(attr, oldValue, newValue) {
if (oldValue !== newValue) {
this[attr] = newValue;
}
}
}
customElements.define('my-element', MyElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment