Define a Lit component by creating a class extending LitElement
and registering your class with the browser:
@customElement('word-viewer')
export class WordViewer extends LitElement { /* ... */ }
export class WordViewer extends LitElement { /* ... */ }
customElements.define('word-viewer', WordViewer);
The TypeScript @customElement
decorator is a shorthand for the JavaScript equivalent of calling customElements.define
, which registers the custom element class with the browser and associates it with the custom element name word-viewer
.
customElements.define
registers the custom element class with the browser and associates it with the custom element name word-viewer
.
A valid custom element name must contain a hyphen.
This ensures forward compatibility with future tags added to HTML or SVG which won't add hyphen containing elements.
A Lit component's render()
method returns a template. Lit templates use the html
tagged template literal to run without a build step in JavaScript.
Add a template to define what word-viewer
should render:
import { html, LitElement } from 'lit';
...
render() {
return html`<pre>A super expressive and efficient template!</pre>`
}
If using TypeScript, you can register your custom element on the HTMLElementTagNameMap
.
declare global {
interface HTMLElementTagNameMap {
"word-viewer": WordViewer;
}
}
Doing this allows TypeScript to infer the type of document.createElement('word-viewer')
as your WordViewer
class.
You should see A super expressive and efficient template!
rendered in the preview.