Overview

1 The framework without a framework

Modern web development faces rapid tool churn and “framework fatigue,” which complicate both individual learning and team-wide decisions about long-term maintainability. Many developers want the simplicity of plain HTML, JavaScript, and CSS, but have lacked a scalable way to add structure without committing to a full framework. Web Components address this gap by offering a standards-based path to build reusable, encapsulated UI without framework lock-in, providing organization and longevity grounded in the platform itself.

At their core, Web Components mirror the strengths of native elements: they package markup, style, behavior, and an API. The Custom Elements API lets you define new elements, while the Shadow DOM provides encapsulation so external CSS and JS don’t inadvertently break internals. Early turbulence—most notably the failure of HTML Imports and uneven Shadow DOM support—pushed developers toward stopgap libraries like Polymer and X-Tags with polyfills such as Shady DOM. The modern, stable approach centers on ES2015 modules, template literals, and the template tag, with Custom Elements as the foundation and Shadow DOM now broadly available and optional. Lightweight utilities like lit-html and LitElement exemplify an opt-in, no-framework workflow that builds on web standards.

Because custom elements behave like any DOM element, you interact with them via properties, attributes, methods, and events, with reflection between properties and attributes considered a best practice. Components compose naturally—from small controls to entire applications—and communicate through native or custom events; when needed, simple patterns like an event bus or carefully chosen microframeworks can coordinate larger systems without heavy abstractions. Looking ahead, frameworks increasingly compile their components to standalone Web Components, improving interoperability across ecosystems, and even other languages can participate via WebAssembly. The upshot: choose the tools that fit your project, but Web Components now offer a practical, future-friendly way to build and share UI—framework-free or framework-assisted—on a foundation of stable web standards.

Expanded date picker UI
Enabling shadow root settings in the Chrome dev tools allows us to see the input tag’s hidden Shadow DOM.
The Shadow DOM protects your component from unintended consequences when CSS or JS might affect styles and nodes inside that aren’t meant to be altered. Instead, your component would have a custom-defined API to interact with using methods and properties.
With HTML Imports, a file containing your component definition and your component’s markup could be imported right into your document.
Web Components could bridge the gap in the future between popular frameworks. Not only can no-framework Web Components be used in these frameworks, but there are already experimental projects to compile a component in React, Angular, or Vue to independently run components that can be used anywhere.
DOM elements have various properties, methods, events, and attributes that are used to tell the element how to act and communicate with the outside world.
Example web application comprised of Web Components, which are themselves made up of more Web Components. The hierarchy can extend to something small, like a custom button, or be as large as the entire application wrapped as a Web Component.
Events naturally bubble from the inside out of nested elements.
If normal event bubbling is not desirable, with a bit of code, you can create an event bus system to route events where you want.

   Summary

In this chapter, you learned

  • How Web Components have evolved in the past few years from a Google-owned working draft to a real web standard adopted by all the modern browsers
  • The Shadow DOM as an optional yet important feature, while being on the verge of widespread browser adoption
  • Web Components’ place in modern frameworks, as well as an agnostic part of any ecosystem
  • The potential future of Web Components with an ever-expanding community of JS modules in the spirit of Polymer Project libraries like lit-html and lit-element, as well as non-Polymer Project ones like hyperHTML
  • The individual Web Component versus an entire Web Component application

FAQ

What are Web Components and what problems do they solve?Web Components are standards-based, reusable UI building blocks created with Custom Elements and, optionally, the Shadow DOM and the template tag. They let you package markup, style, and behavior behind a clean public API so you can reuse components across projects without a framework and without CSS/JS conflicts.
How does the Shadow DOM work and why is it useful?The Shadow DOM creates an encapsulated subtree for your component so outside CSS and DOM queries can’t accidentally affect its internals. It prevents style leakage in both directions and encourages interacting with the component only through its public properties, attributes, methods, and events.
What are Custom Elements?Custom Elements let you define your own HTML tags by extending HTMLElement (or specific element types) and hooking into lifecycle callbacks. They establish the component’s public API—properties, attributes, methods, and events—whether or not you use the Shadow DOM.
Do I have to use the Shadow DOM to build Web Components?No. Shadow DOM is optional. It provides strong encapsulation and is great when you need isolation, but you can ship perfectly valid Custom Elements without it and adopt it later if needed.
What happened to HTML Imports, and what should I use instead?HTML Imports didn’t become a web standard. Today, ES modules (import/export), the template tag, and template literals are the recommended way to organize, load, and compose a component’s code, markup, and styles.
Which ES6/ES2015 features make working with Web Components easier?ES modules enable dependable, per-component dependencies; template literals and the template tag simplify rendering and composition; arrow functions help with lexical this; and classes make defining element behavior straightforward.
How do properties, attributes, and events form a component’s API?Use properties and methods to read/change state and behavior, reflect important state to attributes (and vice versa) to keep markup and JS in sync, and dispatch custom events to notify the outside world about changes or actions.
How do multiple Web Components scale into a full application?Compose small components into larger ones and build a hierarchy up to a single root component if you like. Use DOM event bubbling for local communication, and introduce patterns like an event bus when distant parts of the tree need to talk without tight coupling.
Where do Polymer, X-Tags, LitElement, and similar tools fit in?Early libraries like Polymer and X-Tags helped pioneer workflows when standards and browser support were in flux. The modern trend favors smaller, opt-in tools—such as lit-html/LitElement—that complement standards rather than replace them, letting you stay “no framework” while adding just what you need.
How do Web Components relate to popular frameworks going forward?Components built in React, Angular, Vue, or tools like Stencil/Svelte can be compiled to framework-agnostic Custom Elements. This enables cross-framework reuse—dropping a component into any app—while still letting teams choose their preferred authoring stack.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Web Components in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Web Components in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Web Components in Action ebook for free