liveBook · Manning

Custom Elements

As explained in Web Component Development with Modern Libraries and Tooling

A developer-defined HTML element registered with the browser's Custom Elements API.

A custom element is a developer-defined HTML element registered with the browser. It gives a reusable UI component its own tag, so a page can use concise, meaningful markup instead of repeating the same HTML, CSS, and JavaScript wherever that UI appears. Custom elements work in applications with or without a web framework. section 1.3 section 1.2

A radio group in one element

`<radio-group name="color" labels="Red,Green,Blue" values="red,green,blue" value="red"></radio-group>` can represent three labeled radio buttons. Without the component, each use needs separate radio inputs, labels, and surrounding markup. The attributes supply the choices and initial selection; the element handles rendering them. section 1.1

To define a vanilla custom element, create a JavaScript class extending `HTMLElement`, then register its tag with `customElements.define("hello-world", HelloWorld)`. The browser uses that association when it encounters `<hello-world></hello-world>`. A custom element name must be lowercase and contain a hyphen, distinguishing it from built-in tags. Even an empty custom element needs a closing tag; it cannot be self-closing. section 1.3 section 3.1

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 complete `hello-world` example shows registration, a `name` attribute with a default, a lifecycle callback, shadow DOM markup and styling, and a click handler that changes button color. section 1.3

Custom elements can bundle their rendering, styling, and behavior, while shadow DOM can help protect internal structure and styles from unintended outside changes. The browser APIs need no helper library, though libraries can reduce boilerplate and ease rendering and state management. For reliable integration, design elements to respond correctly to attribute and property changes and to dispatch events appropriately. section 1.8 section 3.0 section 4.0

Framework use may need a little configuration. In Vue, custom elements can be imported and used in templates, with refs for property updates and event bindings for custom events. Configure Vue to recognize their tags; otherwise it may warn that it cannot resolve them as components. section 9.7

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Works through an HTMLElement subclass, registration, attributes, lifecycle rendering, and tag rules; also notes library trade-offs and integration practices.
Examples
Shows a radio-group replacing repeated inputs and labels, a clickable hello-world element, and custom elements used in Vue.
What it assumes
Assumes basic HTML, CSS, JavaScript, TypeScript, npm, and package.json familiarity while introducing the browser component APIs through examples.
What you can do after
Build it from scratch with HTMLElement and customElements.define, then use its tag in HTML and integrate its properties and events where needed.
How it connects
Connects custom elements to web components, shadow DOM, lifecycle callbacks, attributes, properties, custom events, and framework interoperability.

Also explained in

Compare how 4 books explain Custom Elements →

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.