liveBook · Manning

Vanilla web

As explained in Web Component Development with Modern Libraries and Tooling

A web component created directly with browser web-component APIs without relying on an implementation library.

A vanilla web component is a custom element built directly with browser APIs, without a helper library. It can be a lightweight, dependency-free choice, and understanding it makes library code easier to evaluate or debug. section 3.0 section 1.7

A greeting element

Once its defining script has loaded, <hello-world></hello-world> displays “Hello, World!” The custom tag needs both an opening and a closing tag, even though nothing is written between them. section 3.1.1 section 3.1

class HelloWorld extends HTMLElement {
  connectedCallback() { #1
    this.innerHTML = "<p>Hello, World!</p>"; #2
  }
}

customElements.define("hello-world", HelloWorld); #3

The complete greeting implementation shows the three essentials together: extend HTMLElement, populate the element in connectedCallback, and register its hyphenated name with customElements.define. section 3.1.1

The registered name must be lowercase and contain a hyphen; registering an existing name fails. connectedCallback runs when the element is inserted into the DOM, including after reattachment. Avoid putting context-dependent rendering in the constructor: it runs before attachment and only once. section 3.1.1 section 3.11

From greeting to form control

A <radio-group> inside a form accepts attributes for its legend, labels, values, name, and selected value. Its fuller implementation clones a template into a shadow DOM, responds to attribute changes, and uses element internals so the selected value participates in form submission. section 3.10

Direct browser APIs give you control, but you also write the update logic. Replacing the whole DOM can be costly; finding and changing individual nodes can be tedious. Sharing state across components has no built-in vanilla approach. Libraries such as Lit, Stencil, FAST, and wrec build on the same APIs and can reduce this boilerplate. section 3.11 section 3.0

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Works from a minimal registered element through lifecycle callbacks, shadow DOM, updates, and a form-associated radio group.
Examples
Uses a static greeting element and a configurable radio group that contributes its selected value to form submissions.
What it assumes
Introduces the DOM and custom-element naming while using JavaScript classes and HTML markup for the first implementation.
What you can do after
Build it from scratch with customElements.define and lifecycle callbacks, then judge when library-managed rendering or state would help.
How it connects
Connects custom elements to lifecycle callbacks, HTML templates, Shadow DOM, form submission, state sharing, and web component libraries.

Also explained in

Compare how 2 books explain vanilla web →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Component Development with Modern Libraries and Tooling. The section links point to the parts of the book it draws on.