Skip to content

Instantly share code, notes, and snippets.

@blargism
Last active October 4, 2022 11:56
Show Gist options
  • Save blargism/76d018a8b5979fd814383a71ff29e364 to your computer and use it in GitHub Desktop.
Save blargism/76d018a8b5979fd814383a71ff29e364 to your computer and use it in GitHub Desktop.
Basic Web Component with lit-html
import { html, render } from 'lit-html';
export default class ComponentClass extends HTMLElement {
constructor() {
super();
// set sane defaults if desired.
this._value = null;
}
get values() {
return this._value;
}
set values(value) {
this._value = value;
this.render();
}
connectedCallback() {
this.template = () => html`<div></div>`;
this.init().then(() => {
this.render();
}).catch((e) => {
// handle errors
});
}
async init() {
// do fetches etc.
}
render() {
if (!this.template)
return;
// other captures
render(this.template(), this);
}
}
customElements.define("component-name", ComponentClass);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment