import { html, render, TemplateResult } from 'lit-html'; export class UtilLib extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); } static get observedAttributes(): string[] { return ['name']; } connectedCallback() { this.upgradeProperties(); this.render(); } disconnectedCallback() { } attributeChangedCallback(_name: string, _oldValue: any, _newValue: any) { this.render(); } private upgradeProperties() { // Support lazy properties https://developers.google.com/web/fundamentals/web-components/best-practices#lazy-properties (this).constructor['observedAttributes'].forEach((prop: string) => { if (this.hasOwnProperty(prop)) { let value = (this)[prop]; delete (this)[prop]; (this)[prop] = value; } }); } get name(): string { return this.getAttribute('name'); } set name(value: string) { if (value) { this.setAttribute('name', value); } else { this.removeAttribute('name'); } } private get styles(): TemplateResult { return html` `; } private get template(): TemplateResult { return html` ${this.styles}
Welcome to <util-lib>
  • name: ${this.name === null ? 'N/A' : this.name}
`; } render() { render(this.template, this.shadowRoot); } } window.customElements.define('util-lib', UtilLib);