liveBook · Manning

Web Components

As explained in Web Component Development with Modern Libraries and Tooling

A reusable web interface element implemented with encapsulated HTML, CSS, and JavaScript and exposed through the browser platform.

A web component is a reusable custom HTML element that combines content, styling, and behavior. It can replace repeated interface markup with a meaningful tag while keeping the component’s implementation in one place. Unlike a component tied to one framework, it can be used in framework-based applications or pages with no framework at all. section 1.0 section 1.2

A radio group in one element

A <radio-group> can take a name, option labels and values, and an initial selection. It renders the radio inputs and their labels, so each new group does not require the same structure to be written again. section 1.1

Listing 1.1 radio-group custom element
<radio-group
  name="color"
  labels="Red,Green,Blue"
  values="red,green,blue"
  value="red"
></radio-group>

The custom-element markup shows how a color choice is configured without writing three separate radio inputs and labels. section 1.1

To create a web component without a helper library, define a JavaScript class extending HTMLElement and register it with customElements.define. Load that code into the page and use the registered element name in HTML. Once registered, matching elements are upgraded by the browser. A component can attach a shadow DOM to contain its markup and styles, helping prevent unintended interactions with page CSS. section 1.3 section 1.4

Listing 1.4 hello-world.js
class HelloWorld extends HTMLElement { #1
  constructor() { #2
    super();
    this.attachShadow({ mode: "open" });
  }

  connectedCallback() { #3
    const name = this.getAttribute("name") ?? "World";
    this.shadowRoot.innerHTML = `
      <style>
        button {
          color: blue;
        }
      </style>
      <button type="button">Hello, ${name}!</button>
    `;
    this.addEventListener("click", () => {
      const button = this.shadowRoot.querySelector("button");
      const { style } = button;
      style.color = style.color === "red" ? "blue" : "red";
    });
  }
}

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

The greeting component shows the class, shadow DOM, lifecycle callback, click behavior, and registration together in one small implementation. section 1.3

Vanilla components use browser APIs directly, but rendering and state management can require boilerplate. Libraries such as Lit, Stencil, FAST, and wrec offer other authoring approaches. Existing component libraries can also save you from implementing every element yourself. Choose according to the features and simplicity you need. section 1.6 section 1.7 section 3.0

Framework independence does not mean identical setup everywhere: applications may need custom-element type declarations or configuration. For reliable integration, attend to HTML and CSS semantics, attribute and property changes, and dispatched events. Framework-specific components may still be preferable when their built-in capabilities suit the task. section 4.0 section 9.9 section 1.2

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Works from element markup through vanilla class registration and browser upgrade, then compares libraries and framework integration.
Examples
Shows a radio-group replacing repeated inputs and labels, plus a greeting button implemented with a JavaScript class.
What it assumes
Assumes basic HTML, CSS, JavaScript, TypeScript, npm, and package.json familiarity while introducing custom-element implementation.
What you can do after
Build it from scratch with browser APIs, or evaluate libraries and integrate existing components into framework applications.
How it connects
Connects custom elements with HTMLElement, Shadow DOM, lifecycle callbacks, attributes, properties, events, and web frameworks.

Also explained in

Compare how 3 books explain Web Components →

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.