Skip to content

Examples

Every example below runs as a standalone page at demo.webcomponent.io ↗, a live gallery with the source alongside each demo.

DemoShows
Boolean props ↗presence/absence reflection, toggleAttribute, [flag] selectors
Custom attribute converters ↗toAttribute/fromAttribute for Date and array props
Props blueprint ↗static props as the single source of defaults and types
Prop type enforcement ↗static strictProps and the log-not-throw default
Compile-time prop types ↗typing this.props in TypeScript
Typed props ↗attribute round-trips restoring the declared type
Templating ↗string vs html tagged-template rendering
Render reconciliation ↗in-place patching preserving focus, caret and input state
Style objects ↗calculated and conditional styles via the style prop
Shadow DOM ↗static shadowRootInit
Constructable styles ↗static styles, including composing several sheets
Lifecycle order ↗each hook logged as it fires
Attribute lifecycle ↗how attribute changes drive the hooks
onChanges payload ↗camelCase property vs kebab-case attribute
Just the parts ↗using html/createElement without the base class
Kitchen sink ↗several features together
Single-file pen ↗counter and toggle in one HTML file

A simple app that allows adding / completing tasks: View on CodePen ↗

To-Do App screen recording

Here is an example of using a custom element in a single .html file.

<!doctype html>
<html lang="en">
<head>
<title>WC Base Test</title>
<script type="module">
import { WebComponent } from 'https://esm.sh/web-component-base@latest'
class HelloWorld extends WebComponent {
static props = {
myName: 'World',
}
get template() {
return `<h1>Hello ${this.props.myName}!</h1>`
}
}
customElements.define('hello-world', HelloWorld)
</script>
</head>
<body>
<hello-world my-name="Ayo"></hello-world>
<script>
const helloWorld = document.querySelector('hello-world')
setTimeout(() => {
helloWorld.props.myName = 'Ayo zzzZzzz'
}, 2500)
</script>
</body>
</html>

Some feature-specific demos:

  1. Context-Aware Post-Apocalyptic Human
  2. Simple reactive property
  3. Counter & Toggle
  4. Using custom templating (lit-html)
  5. Using dynamic style objects
  6. Using the Shadow DOM
  7. Using tagged templates in your vanilla custom element