Last active
October 4, 2022 11:56
-
-
Save blargism/76d018a8b5979fd814383a71ff29e364 to your computer and use it in GitHub Desktop.
Basic Web Component with lit-html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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