DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. Web Components: Building Framework-Agnostic UI
XLinkedInReddit
MediumFrontend Engineering

Web Components: Building Framework-Agnostic UI

D
DevPrep Team
February 10, 2026·1 min read·0
Table of Contents
  • The Three Pillars
  • Creating a Custom Element
  • Usage
  • Shadow DOM Styling
  • Slots for Composition
  • When to Use Web Components

Web Components let you create reusable, encapsulated HTML elements that work everywhere — React, Vue, Angular, or vanilla JS.

The Three Pillars

  1. Custom Elements: Define new HTML tags
  2. Shadow DOM: Encapsulated DOM and styling
  3. HTML Templates: Reusable markup

Creating a Custom Element

class UserCard extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: "open" });
  }

  static get observedAttributes() {
    return ["name", "avatar", "role"];
  }

  attributeChangedCallback(name, oldVal, newVal) {
    if (oldVal !== newVal) this.render();
  }

  connectedCallback() {
    this.render();
  }

  render() {
    this.shadowRoot.innerHTML = `
      <style>
        :host { display: block; padding: 16px; border-radius: 8px; background: var(--card-bg, #fff); }
        .name { font-weight: bold; font-size: 1.1em; }
        .role { color: #666; font-size: 0.9em; }
      </style>
      <img src="${this.getAttribute("avatar")}" width="48" height="48" />
      <div class="name">${this.getAttribute("name")}</div>
      <div class="role">${this.getAttribute("role")}</div>
    `;
  }
}

customElements.define("user-card", UserCard);

Usage

<!-- HTML -->
<user-card name="Rahul" avatar="/avatar.jpg" role="Engineer"></user-card>

// React
function App() {
  return <user-card name="Rahul" avatar="/avatar.jpg" role="Engineer" />;
}

// Vue
<template>
  <user-card :name="user.name" :avatar="user.avatar" :role="user.role" />
</template>

Shadow DOM Styling

/* From outside: CSS custom properties pierce Shadow DOM */
user-card {
  --card-bg: #f0f0f0;
}

/* Inside Shadow DOM: ::part() exposes elements */
// In component: <div part="title">...</div>
// From outside: user-card::part(title) { color: blue; }

Slots for Composition

// Component template
<slot name="header">Default Header</slot>
<slot>Default content</slot>

// Usage
<user-card>
  <h2 slot="header">Custom Header</h2>
  <p>This goes in the default slot</p>
</user-card>

When to Use Web Components

  • Design system shared across frameworks
  • Micro-frontend integration points
  • Third-party embeddable widgets
  • When you need true style encapsulation

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • The Three Pillars
  • Creating a Custom Element
  • Usage
  • Shadow DOM Styling
  • Slots for Composition
  • When to Use Web Components

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.