liveBook · Manning

Chapter 9: Advanced web components

What you will learn in chapter 9 of Vanilla Web

This introduction previews advanced web component topics aimed at making components more like native controls, easier to integrate, and simpler to ship.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction previews advanced web component topics aimed at making components more like native controls, easier to integrate, and simpler to ship.

9.1 Element internals

This section introduces ElementInternals as the way custom elements gain access to browser features they do not receive automatically.

Key terms: ElementInternals, form participation, constraint validation, default accessibility semantics, attachInternals(), constructor

9.1.1 Form participation

This section explains what form participation gives a custom element, how to enable it, and how to include its value in form submission.

Key terms: form-associated custom element, constraint validation, form resets, disabled state, <label>, static formAssociated = true, attachInternals(), constructor, ElementInternals, setFormValue()

9.1.2 Validation methods

This section explains how ElementInternals gives form-associated custom elements native-like validation behavior.

Key terms: ElementInternals, setValidity(), checkValidity(), reportValidity(), flags, invalid, native-like validation, form-associated custom elements, native form controls

9.1.3 Accessing form and labels

This subsection explains how a web component can inspect its form context through ElementInternals properties.

Key terms: ElementInternals, form, labels, willValidate, connectedCallback(), constraint validation

9.2 Web components lifecycle

The section introduces custom elements as having lifecycle phases and explains why callback timing matters before the detailed lifecycle discussion.

Key terms: custom element, callback, lifetime

9.2.1 Basic lifecycle flow

This section identifies the main custom-element lifecycle callbacks and explains why attribute-change handling may need a connection guard.

Key terms: constructor(), connectedCallback(), attributeChangedCallback(), disconnectedCallback(), adoptedCallback(), isConnected, upgrade, internally initialized flag

9.2.2 The adoptedCallback() in practice

Explains when adoptedCallback() runs, how it differs from connection in the target document, and when a component needs it.

Key terms: adoptedCallback(), connectedCallback(), adoption, document-scoped objects, document, window

9.2.3 Form-associated lifecycle

This section identifies the four additional lifecycle callbacks of form-associated custom elements and explains why they help keep component state aligned with form behavior.

Key terms: formAssociatedCallback(form), formDisabledCallback(disabled), formResetCallback(), formStateRestoreCallback(state, mode), internal state, form behavior

9.2.4 Restoring form state after navigation

This section explains why a form-associated component may store a restoration state separately from the value it submits.

Key terms: setFormValue(), formStateRestoreCallback(state, mode), submission value, restoration state, FormData

9.2.5 Cleanup in disconnectedCallback()

This section explains what a component should clean up when disconnected and how one AbortController can simplify part of that teardown.

Key terms: disconnectedCallback(), timers, pending fetches, observers, global listeners, AbortController, global listener, fetch, teardown

9.2.6 Waiting for element registration

This section shows how to wait for a custom element to be registered or check its registration synchronously.

Key terms: customElements.whenDefined(), customElements.get(), registered

9.2.7 Styling undefined elements with :defined

This section explains how CSS can style custom elements before they are defined, and why hiding them during that time requires care.

Key terms: :defined, :not(:defined), unupgraded elements

9.3 Advanced accessibility for web components

This section explains why custom elements need explicit accessibility semantics and recommends native elements first, with ElementInternals as a way to define defaults when needed.

Key terms: custom element, native semantics, generic host, native element, ElementInternals, default ARIA semantics, host element

9.3.1 ARIA via ElementInternals

This subsection shows how a web component can set default accessibility semantics through ElementInternals while still implementing the behavior those semantics describe.

Key terms: ElementInternals, ARIA reflection properties, role, ariaDisabled, semantics, behavior, assistive technology

9.3.2 Common ARIA properties

This section notes that ElementInternals exposes ARIA properties for custom widgets and advises setting only those that accurately describe the widget.

Key terms: ElementInternals, ARIA reflection properties, custom widgets

9.3.3 Building an accessible toggle

This section describes an accessible toggle-switch example that combines switch semantics, keyboard interaction, form participation, and a public attribute for styling.

Key terms: role="switch", checked, disabled, form participation, public checked attribute, aria-checked, styling hook

9.4 Focus and keyboard management

The section establishes that accessible interactive web components need deliberate focus and keyboard behavior, not just ARIA.

Key terms: ARIA, focus, keyboard behavior, custom element

9.4.1 Delegating focus into shadow DOM

This subsection explains how delegatesFocus directs focus into a shadow tree and why it does not replace keyboard behavior for complex widgets.

Key terms: delegatesFocus, shadow root, host, focusable descendant

9.4.2 Managing focus programmatically

Explains how an opening and closing component should manage focus, using a modal dialog as the example, and when to prefer native <dialog>.

Key terms: focus, modal dialog, previously focused element, native <dialog>, custom dialog component

9.4.3 Keyboard interaction patterns

This section sets out established keyboard conventions for web components and recommends modern keyboard event handling.

Key terms: widget conventions, WAI-ARIA Authoring Practices Guide, Enter, Space, radio groups, tabs, menus, dialogs, arrow keys, Escape

9.5 Custom state set

Introduces CustomStateSet as a way for autonomous custom elements to expose internal states to CSS without mirroring them as attributes or classes.

Key terms: CustomStateSet, autonomous custom elements, :state() pseudo-class, internal states

9.5.1 The states property

Explains how to manage custom states through ElementInternals.states and when to use them instead of host attributes.

Key terms: ElementInternals.states, add(), delete(), has(), custom states, host attributes, public API

9.5.2 The :state() pseudo-class

This section explains how CSS selects a custom element’s state and uses that selection to style its parts.

Key terms: :state(), ::part(), custom element, host, state

9.5.3 Building a stateful component

Illustrates a stateful button whose internal states can affect styling without appearing in the host element’s markup.

Key terms: CustomStateSet, custom states, host element, saveData, loading, success, <slot>, Save Changes, Submit

9.6 Declarative Shadow DOM

Introduces how Declarative Shadow DOM creates shadow content from HTML during parsing and why that helps server-rendered or static pages.

Key terms: Declarative Shadow DOM, DSD, <template>, shadowrootmode, shadow root, host

9.6.1 Basic syntax

This subsection explains that declarative shadow roots are created during HTML parsing, before a component upgrades, and identifies the standardized attribute name.

Key terms: declarative shadow root, component class upgrades, initial render, shadowrootmode, shadowroot

9.6.2 When to use Declarative Shadow DOM

This section explains when Declarative Shadow DOM is useful and when creating a shadow root in JavaScript is sufficient.

Key terms: Declarative Shadow DOM, DSD, server-side rendering, static site generation, progressive enhancement, attachShadow()

9.6.3 Hydrating a declarative shadow root

Explains how to hydrate a declarative shadow root without destroying server-rendered content, including how to access roots in open and closed mode.

Key terms: hydration, declarative shadow root, attachShadow(), server-rendered content, attachInternals().shadowRoot, this.shadowRoot, open, closed

9.6.4 Using CSS with DSD before hydration

Explains how to keep a declarative shadow root visible before hydration while limiting interaction until the custom element is ready.

Key terms: declarative shadow root, :defined, hydration, display: none

9.6.5 DSD configuration attributes

Explains three attributes that configure focus delegation, cloning, and serialization for declarative shadow roots.

Key terms: shadowrootdelegatesfocus, focus delegation, host, shadow tree, shadowrootclonable, cloneNode(true), importNode(), shadowrootserializable, getHTML(), serialization

9.6.6 Resource loading in declarative shadow roots

This section explains when resources load in declarative shadow roots, how that differs from ordinary templates, and why it helps server-side rendering.

Key terms: declarative shadow root, inert <template>, SSR, hydrates, inline and external stylesheets, declarative shadow roots

9.7 Testing web components

This section explains why web components are best tested in a real browser and introduces three complementary layers of testing.

Key terms: real browser, customElements, shadow DOM, slot distribution, form association, unit tests, integration tests, visual regression tests

9.7.1 A simple browser-based test page

This section introduces a simple browser test page for a custom element and explains when to move to an automated browser runner.

Key terms: test harness, assertions, runTests(), automated browser runner, Web Test Runner, Playwright, CI, shadow DOM

9.7.2 Testing shadow DOM and slots

This section explains how to test open-shadow components and verify slot assignments directly.

Key terms: Shadow DOM, shadowRoot, open-shadow components, slots, assigned nodes, assigned elements, default slots, outer HTML

9.7.3 Testing form-associated components

This subsection identifies submission and validation as the behaviors to test in form-associated custom elements.

Key terms: form-associated custom elements, submission, validation, checkValidity(), reportValidity(), validity state

9.7.4 Visual regression testing

This section explains when screenshot-based tests help web components and how to keep visual regression tests useful.

Key terms: logic tests, screenshot-based testing, spacing regressions, missing outlines, theme breakage, stable states, visual regression tests, state setup

9.8 Library integration

Explains why web components can be used across frameworks and states the basic rules for integrating them.

Key terms: web components, HTML elements, custom element, attributes, properties, native or custom events

9.8.1 Using web components in frameworks

This section explains how React, Angular, and Vue use custom elements and how to pass data and receive events across the framework boundary.

Key terms: custom element tags, CUSTOM_ELEMENTS_SCHEMA, dashed tags, attributes, properties, custom events, React 19, ref, on… props

9.8.2 Wrapping React components as web components

This section explains that a React component can be exposed to non-React pages through a custom element and notes when a small wrapper may need a helper layer.

Key terms: React, custom element, custom element host, helper layer, prop mapping, event forwarding

9.8.3 Wrapping Angular components as web components

This subsection shows how Angular components become custom elements and how their inputs, outputs, and tag names behave.

Key terms: @angular/elements, createCustomElement(), custom element, inputs, dash-cased attributes, outputs, custom events, component selector, double-instantiation

9.8.4 Wrapping Vue components as web components

This subsection explains how Vue 3 components become native custom elements and when the runtime cost of that approach is worthwhile.

Key terms: defineCustomElement(), native custom-element constructor, CustomEvent, shadow root, runtime size cost, component library

9.9 Deploying web components

The section frames deployment as part of building a web component and identifies the questions its distribution plan should answer.

Key terms: distribution story, import paths, side effect, registered

9.9.1 Shipping a standalone ES module

This section presents a browser-ready ES module as a simple, low-friction way to distribute a web component.

Key terms: browser-ready ES module, side effect, registers the element, adoption friction

9.9.2 Publishing as an npm package

This section explains how to package web components for bundler-based applications without losing element registration or obscuring import options.

Key terms: sideEffects, tree-shake, register the custom element, package metadata, root entry point, per-component entry points

9.9.3 Offering a CDN-friendly build

This section explains how a browser-ready ESM build lets developers try a web component through a CDN and why its public URL must remain stable.

Key terms: CDN-friendly ESM builds, browser-ready file, unpkg, jsDelivr, stable public paths, external contract, CDN URL

9.9.4 Bundling and transpiling

Explains why a web component package might use bundling and why its public entry points must remain easy to find.

Key terms: bundling, transpiling, ESM, CommonJS, public entry points, bundler

9.9.5 Distribution checklist

This section gives a pre-publish checklist for making a web component easy to try, install, and maintain.

Key terms: entry module, custom-element name, plain HTML usage example, package entry points, package.json, CDN paths, semantic versioning, public attributes, properties, events

9.10 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 8: Working with templates and slots Chapter 10: Navigation →

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.