Web Components
Frontend
JavaScript
Componentes
Custom Elements
Shadow DOM

Web Components: Reusable Components for the Web

Web Components are browser-native patterns for creating reusable components. They work on any framework or without a framework. This guide introduces the technologies and how to use them.

What Are Web Components

Definition

Set of standards for encapsulated components.

Technologies

Custom Elements, Shadow DOM, HTML Templates.

Native

Supported directly by browsers.

Framework Agnostic

Works with React, Vue, Angular or pure.

Why Web Components

Reuse

One component, any project.

Encapsulation

Styles don't leak.

Patterns

It doesn't depend on the library.

Longevity

Browsers support it, not deprecate it.

Custom Elements

What is it

Defines new HTML elements.

Syntax

class MyElement extends HTMLElement { } customElements.define('my-element', MyElement);

###Lifecycle

connectedCallback, disconnectedCallback, attributeChangedCallback.

Usage

Shadow DOM

What is it

DOM encapsulated inside the element.

Encapsulation

CSS does not leak out or enter from the outside.

Creation

this.attachShadow({ mode: 'open' });

Slots

for content projection.

HTML Templates

What is it

HTML fragments that don't render until cloned.

Syntax

Usage

Clone and add to DOM.

Performance

Does not render until necessary.

Lifecycle Callbacks

connectedCallback

When added to the DOM.

disconnectedCallback

When removed from the DOM.

attributeChangedCallback

When attribute changes.

adoptedCallback

When moved to new document.

Attributes and Properties

Attributes

Strings in HTML.

Properties

JavaScript values in the object.

Synchronization

Reflect between both.

observedAttributes

static get observedAttributes() { return ['value']; }

Slots

Default Slot

receives all content.

Named Slots

Usage

...

Fallback

Content inside the slot is fallback.

Styling

Shadow DOM Styles

Styles within the shadow root.

:host

Styles the host element.

::slotted

Styles slotted content.

CSS Parts

::part() for external customization.

CSS Variables

They pass through the shadow boundary.

With Frameworks

###React

Can be used with a wrapper.

Vue

Native support for custom elements.

Angular

CUSTOM_ELEMENTS_SCHEMA.

###Lit

Library to facilitate creation.

Lit Element

What is it

Google Library for Web Components.

Benefits

Less boilerplate, reactive properties.

Templating

Tagged template literals.

Adoption

Popular for Web Components.

Stencil

What is it

Compiler for Web Components.

JSX

Familiar syntax for React devs.

TypeScript

Native support.

Lazy Loading

Automatic optimization.

Browser Support

Modern

All modern browsers support it.

Polyfills

For old browsers.

Verification

https://caniuse.com/custom-elementsv1

Testing

Unit Tests

Jest, Mocha with DOM.

Web Test Runner

Modern testing for web components.

Open WC

Testing tools.

##Performance

Initial Cost

Shadow DOM overhead.

Scaling

Many components are efficient.

Bundle

Components load on demand.

Common Errors

ForgetSuper

super() in the constructor.

Attribute vs Property

Confuse the two.

Heavy Constructor

Lots of work in the constructor.

Style Leaking

Without using Shadow DOM.

Use Cases

Design Systems

Shared components.

Microfrontends

Integration of independent parts.

Third-Party Widgets

Embed on any website.

Legacy Integration

Gradually modernize.

Conclusion

Web Components are powerful standards for truly reusable components. They work today in all modern browsers and complement any stack.

##FAQs

1) Web Components replace React? Not necessarily. They can coexist.

2) Do I need Lit or Stencil? They help, but pure vanilla works.

3) Does SEO work with Shadow DOM? Google renders JavaScript. It usually works.

4) Is performance good? Yes. Browsers have optimized.

5) When to use Web Components? Components shared between projects/frameworks.

Also read