Skip to content

Why Web Components

This lesson explains why Web Components matter, where they fit in modern frontend architecture, and how reusable custom elements help you build maintainable UI across projects and frameworks.

Why Web Components Matter

Web Components are a set of browser-native standards that let you create reusable custom HTML elements with encapsulated structure, style, and behavior. Instead of rebuilding the same UI in every framework, you can define a component once and use it anywhere.

They are especially valuable for design systems, shared UI libraries, micro frontends, and long-lived products that need components to work across React, Vue, Angular, Astro, and plain HTML pages.

Feature Description
Core Standards Custom Elements, Shadow DOM, HTML Templates
Main Benefit Reusable and encapsulated UI
Framework Support Works with any framework or no framework
Browser Native Built into modern browsers
Common Uses Design systems, widgets, shared UI, micro frontends
Key Idea Write once, use anywhere in the browser

Basic Reusable Component Example

class AlertBox extends HTMLElement {
  connectedCallback() {
    this.innerHTML = `
      <div class="alert">
        <strong>Notice:</strong>
        <span>${this.getAttribute("message")}</span>
      </div>
    `;
  }
}

customElements.define(
  "alert-box",
  AlertBox
);
<alert-box message="Profile saved successfully"></alert-box>

Once registered, a custom element behaves like native HTML. You can drop <alert-box> into any page without importing a framework-specific component.

How Web Components Solve UI Reuse

Web Components combine three browser features to create portable UI building blocks that are easier to maintain than copy-pasted markup and scripts.

Standard Purpose Example
Custom Elements Define new HTML tags with JavaScript. <user-card>
Shadow DOM Encapsulate internal markup and CSS. attachShadow()
HTML Templates Store reusable markup safely. <template>
Slots Allow flexible external content. <slot>
Custom Events Communicate with parent apps. new CustomEvent()

Reusability Across Pages

<!-- Marketing site -->
<primary-button label="Get Started"></primary-button>

<!-- Admin dashboard -->
<primary-button label="Save Changes"></primary-button>

<!-- Documentation page -->
<primary-button label="Read Docs"></primary-button>

The same custom element can appear in many applications with different attributes and content, reducing duplicated UI code and inconsistent styling.

Encapsulation with Shadow DOM

class ProfileCard extends HTMLElement {
  connectedCallback() {
    const shadow =
      this.attachShadow({ mode: "open" });

    shadow.innerHTML = `
      <style>
        .card {
          padding: 1rem;
          border: 1px solid #dbe5f1;
          border-radius: 0.75rem;
        }
      </style>
      <div class="card">
        <slot></slot>
      </div>
    `;
  }
}

customElements.define(
  "profile-card",
  ProfileCard
);

Shadow DOM keeps a component's internal styles and structure separate from the rest of the page, which helps prevent accidental CSS conflicts.

Framework-Agnostic Usage

// React
function App() {
  return (
    <user-badge name="Alex" role="admin" />
  );
}

// Vue template
// <user-badge name="Sam" role="editor"></user-badge>

// Plain HTML
// <user-badge name="Riya" role="viewer"></user-badge>

Web Components are especially useful when teams use multiple frontend frameworks or migrate between them over time.

Design Systems and Shared UI Libraries

<app-header logo="Acme"></app-header>
<search-input placeholder="Search docs"></search-input>
<status-badge type="success">Active</status-badge>
<app-footer year="2026"></app-footer>

Companies often use Web Components to publish a shared design system that product teams consume across many apps with consistent behavior and branding.

Top Reasons to Use Web Components

  • Create reusable UI that works across projects.
  • Encapsulate styles and DOM to reduce conflicts.
  • Build framework-independent component libraries.
  • Improve long-term maintainability of shared widgets.
  • Support micro frontend and multi-team frontend architectures.
  • Use native browser APIs instead of heavy abstractions.
  • Publish components that feel like built-in HTML elements.

Web Components vs Framework Components

Feature Web Components Framework Components
Portability Works across frameworks Mostly tied to one ecosystem
Encapsulation Shadow DOM built in Depends on framework patterns
Learning Curve Browser standards focused Framework APIs and tooling
Best For Shared libraries and design systems App-level UI and state management
Runtime Native browser support Framework runtime required

When Web Components Are a Good Choice

  • You need reusable UI across multiple apps or frameworks.
  • You are building a company-wide design system.
  • You want encapsulated widgets such as modals, tabs, or cards.
  • You maintain legacy and modern apps at the same time.
  • You want custom HTML elements with predictable APIs.
  • You need long-lived components that outlive one framework choice.

When Framework Components May Be Better

  • The entire app lives in one framework with no reuse needs.
  • Most UI depends heavily on framework-specific state systems.
  • You need advanced framework devtools and ecosystem features first.
  • The team has no plan for accessibility or component APIs.
  • A simple static page does not need custom element abstraction.

Common Web Components Use Cases

  • Buttons, badges, alerts, and cards in a design system.
  • Embeddable widgets for third-party websites.
  • Shared navigation, footer, and auth UI across products.
  • Form controls with custom validation and styling.
  • Video players, date pickers, and complex UI widgets.
  • Micro frontend shells and cross-team UI packages.
  • Progressive enhancement for existing HTML applications.

Web Components Best Practices

  • Design components as if they will be reused in many apps.
  • Keep public APIs small with clear attributes and properties.
  • Use semantic HTML inside components for accessibility.
  • Document events, slots, and styling extension points.
  • Prefer open Shadow DOM unless you have a strong reason not to.
  • Test keyboard interaction, focus, and screen reader behavior.
  • Start with focused components instead of large all-in-one widgets.

Common Web Components Mistakes

  • Assuming Shadow DOM alone solves accessibility.
  • Creating components that are too large and hard to reuse.
  • Ignoring attribute and property synchronization.
  • Using unclear custom event names and payloads.
  • Over-engineering simple UI that could stay as plain HTML.
  • Not checking browser support or fallback strategies.
  • Building components without a documented public API.

Key Takeaways

  • Web Components provide reusable, encapsulated custom HTML elements.
  • They are built on Custom Elements, Shadow DOM, and Templates.
  • They work across frameworks and plain HTML applications.
  • They are ideal for design systems and shared UI libraries.
  • Good component APIs, accessibility, and documentation matter most.

Pro Tip

Use Web Components when you want UI that survives framework changes. Define a clean element such as <status-badge> once, then reuse it everywhere with attributes, slots, and events.