liveBook · Manning

Chapter 7: Web components

What you will learn in chapter 7 of Vanilla Web

Introduces web components as the chapter’s approach to building maintainable, reusable user interfaces and previews the topics ahead.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces web components as the chapter’s approach to building maintainable, reusable user interfaces and previews the topics ahead.

7.1 The component design pattern

This section explains how the component pattern packages reusable UI, what web components are, and why their encapsulation and interoperability matter.

Key terms: component design pattern, structure, behavior, style, Web Components, custom HTML elements, DOM, attributes, properties, events

7.2 Use cases

This section distinguishes reusable and application-specific web component use cases, then shows how a custom element becomes usable in HTML.

Key terms: generalizable components, project-specific components, web component, custom element, HTMLElement, customElements.define()

7.3 Advantages

This section explains how web components improve on ad hoc script-driven markup through encapsulation, reuse across projects and frameworks, and integration with browser features.

Key terms: encapsulation, shadow DOM, private subtree, global CSS, reusability, interoperability, custom element, framework dependencies, event flow, CSS selectors

7.4 Challenges

This section explains the extra implementation work, uneven API support, and use-case limits of Web Components.

Key terms: Custom Elements API, attributes, state, DOM, events, reactivity, Baseline, form-associated custom elements, generalizable elements, reactive templating

7.5 Vanilla web components

Introduces the browser APIs that provide vanilla web components and explains how libraries build on them without being required.

Key terms: Custom Elements, CustomElementRegistry, Shadow DOM, HTML templates, ElementInternals, component pattern, framework, Lit, Stencil, templating syntax

7.5.1 Architecture

This section explains the parts of a web component, its public contract with the host page, and the basic sequence from creation to later lifecycle callbacks.

Key terms: host element, custom tag, HTMLElement, shadow DOM tree, shadow DOM root, public interface, attributes, properties, methods, events

7.5.2 The puzzle

This section defines the main pieces of a web component and distinguishes content inside its shadow DOM from content supplied by its consumer.

Key terms: custom element, interface, Document DOM, Page DOM, Shadow DOM, host element, Light DOM, slot, part, template

7.5.3 Abilities

Lists capabilities that Web Components add beyond plain HTML, without explaining how to implement them.

Key terms: Web Components, shadow DOM, attributes, properties

7.6 Custom Elements API

Introduces what the Custom Elements API provides, the two kinds of custom elements, and how registered elements begin responding to the browser.

Key terms: Custom Elements API, custom element, web component, autonomous custom elements, HTMLElement, customised built-in custom elements, is attribute, class, register, constructor

7.6.1 Creation

This section explains how to create an autonomous custom-element class, when to initialize it versus work with its DOM, and why defining the class alone does not make it usable as a custom element.

Key terms: autonomous elements, HTMLElement, constructor, super(), connectedCallback(), this, DOM APIs, registered, custom element

7.6.2 Rendering content

This section compares ways to render a custom element’s content and explains how to handle user-supplied content safely.

Key terms: Imperative DOM creation, innerHTML, HTML templates, External HTML file, shadow root, textContent, XSS, sanitize, shadow DOM, page DOM

7.6.3 Registration

Explains how to register a custom element, use it in HTML or JavaScript, and choose a valid name.

Key terms: customElements.define(), constructor, HTMLElement, connectedCallback, attributes, properties, customElements.upgrade(container), module, race conditions, custom element name

7.6.4 Simplifying the syntax

This subsection shows that custom element definitions can use shorter syntax, including literal classes or a function-style helper, while distinguishing the helper from a built-in API.

Key terms: literal classes, constructor function, simple helper, micro-library, custom element

7.6.5 The Custom Element Registry

This section introduces CustomElementRegistry helpers for checking registration, waiting for it, and upgrading elements created before registration.

Key terms: CustomElementRegistry, customElements, get(name), whenDefined(name), customElements.upgrade(root), subtree, registration

7.7 Our first component

The section uses a counter-button example to show independent custom-element instances, then explains how their hosts can be styled and how CSS can handle elements awaiting registration.

Key terms: autonomous custom element, <counter-button>, instance, internal count, CSS cascade, host element, inline elements, :defined, :not(:defined), customElements.define()

7.8 Shadow DOM

This section explains how Shadow DOM gives web components an isolated DOM boundary and what that boundary means for styles, queries, events, and root access.

Key terms: Shadow DOM, shadow root, shadow boundary, light DOM, page DOM, IDs, events, retargeted, host element, open root

7.8.1 Creation

Explains how to attach a shadow root, render into it, and choose options that control access, focus, and slot assignment.

Key terms: attachShadow(), ShadowRoot, this.shadowRoot, host, light DOM, mode, open, closed, host.shadowRoot, encapsulation

7.8.2 Styles with the page DOM

This section shows how a custom element that uses the page DOM shares CSS with the rest of the page, allowing styles to affect elements in both directions.

Key terms: page DOM, shadow root, cascade

7.8.3 Styles with Shadow DOM

This section shows how attaching a shadow DOM isolates a component’s styles, how open and closed modes differ, and how :host can set the component’s display behavior.

Key terms: shadow DOM, innerHTML, global scope, style declarations, mode: 'open', mode: 'closed', host.shadowRoot, attachShadow(), :host, host element

7.8.4 Styling web components

This section compares ways to style web components, recommends shadow-root encapsulation, and explains how adopted stylesheets let components share parsed CSS.

Key terms: shadow root, <style>, external style sheets, constructible style sheets, global styles, encapsulate styles, Adopted Stylesheets, CSSStyleSheet, replaceSync(), replace()

7.9 Element parameterization

This section introduces the ways consumers can parameterize a custom element and highlights practical distinctions and cautions when exposing its API.

Key terms: attributes, properties, getAttribute(), setAttribute(), CSS custom properties, visual styling, media queries, in sync, slots, <slot>

7.9.1 data-* vs standard attributes

Explains when a custom element should use standard attributes instead of data-* attributes and how JavaScript accesses each kind.

Key terms: standard attributes, data attributes, core configuration, metadata, dataset API, this.dataset.attributeName, getAttribute, setAttribute

7.9.2 Boolean attributes

This subsection explains how to interpret boolean attributes on custom elements by checking whether the attribute is present.

Key terms: boolean attributes, hasAttribute(), getAttribute(), readonly

7.9.3 Update the component while observing attributes

This section explains how a custom element notices changes to selected attributes and updates its content after initial rendering.

Key terms: observedAttributes, attributeChangedCallback, observed attributes, connectedCallback, render

7.9.4 Update the component observing properties

This section explains that a web component with a property-based API should update when a property changes, and introduces a setter as a way to do that.

Key terms: attributes, properties, property-based API, getters and setters, setter function

7.10 Creating a rating component

This section illustrates a registered star-rating web component whose value attribute controls how many of its five stars appear active.

Key terms: star-rating, value attribute, active

7.11 Working with events

The section explains how custom elements communicate through events and uses an editable star-rating component to show an event in response to user input.

Key terms: CustomEvent, dispatchEvent(), detail, bubbles, composed, addEventListener(), star-rating, editable, boolean attribute, change event

7.12 Connecting the page DOM and shadow DOM with CSS

Introduces the CSS techniques that the following subsections use to connect the page DOM and shadow DOM.

7.12.1 Read custom properties for theming

Explains how shadow-DOM components can use page-defined CSS custom properties for theming, with defaults and a reusability caution.

Key terms: CSS custom properties, page DOM, shadow DOM barrier, theme token, default value, global CSS custom properties, reusable

7.12.2 Expose custom properties for parametrization

This section shows how CSS custom properties let a page customize a web component’s shadow DOM styles while the component supplies defaults.

Key terms: CSS custom properties, shadow DOM, :host, var(), page DOM, CSS selector, custom elements, tokens, read-only star ratings, editable star ratings

7.12.3 Expose elements to the page DOM

This section shows how a web component can expose selected shadow DOM elements for styling from the page.

Key terms: part attribute, ::part() pseudo-element, shadow DOM, exportparts attribute

7.12.4 Read state from the page DOM

This section explains how shadow DOM CSS can respond to attributes and classes set on a component’s host element in the page DOM.

Key terms: :host, :host(selector), host element, shadow DOM, page DOM

7.13 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 6: The document object model API Chapter 8: Working with templates and slots →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.