Back to Blog
Web Platform2026-04-11

Web Components: A Modern Guide

Author framework-free reusable elements with custom elements, shadow DOM, and slots.

Web Components have outlasted countless framework cycles. They are the safest bet for shared design system primitives.

Define a Custom Element

``js class StarRating extends HTMLElement { static observedAttributes = ['value']; connectedCallback() { this.render(); } attributeChangedCallback() { this.render(); } render() { const v = +this.getAttribute('value') || 0; this.innerHTML = '★'.repeat(v) + '☆'.repeat(5 - v); } } customElements.define('star-rating', StarRating); `

Use anywhere: . Works in React, Vue, vanilla HTML.

Shadow DOM

Encapsulates styles and DOM:

`js this.attachShadow({ mode: 'open' }).innerHTML = ; `

Outside CSS does not leak in; :host styles the element itself.

Slots and Composition

`html Hello

Body content

`

Inside the component, and a default project content.

Form-Associated Custom Elements

ElementInternals (broadly shipped by 2026) lets custom elements participate in forms:

`js static formAssociated = true; constructor() { super(); this._internals = this.attachInternals(); } `

The element submits with the form, supports validation, and integrates with FormData.

CSS Parts

Expose styling hooks without breaking encapsulation:

`html `

Consumers style via my-comp::part(trigger) { color: red; }.

Declarative Shadow DOM

Server-rendered shadow DOM via