webstd-ui / view

Define the visual elements of your web app using a hierarchy of views
0 stars 0 forks source link

Client-side rendering #2

Closed markmals closed 1 year ago

markmals commented 1 year ago

Using the html tagged template literal from lit-html, we can dynamically render HTML into our web components' shadow root.

lit-html lets you define UI components as functions that take state and return templates. You can then render those templates into an HTMLElement:

import { html, render } from "lit-html"
const ui = (title) => html`<h1>${title}</h1>`
render(ui("Example Title"), document.body)

Lit also provides support for declaratively setting event handlers (@event="${handler}" attributes) and IDL properties (.someProp="${someValue}").

By using the shadow root, we get access to all of the features the shadow DOM brings along with it. This includes:

Lit template results are small, optimized representations of the DOM rather than actualized DOM nodes, so re-rendering is expected and generally a cheap operation.

markmals commented 1 year ago

Implemented in 2a699ee