Insights / Development

Native Web Components in Design Systems: A Comprehensive Guide

Dive into native web components - their pros, cons, adoption by big tech, framework integration, design system use, and challenges like lack of SSR support
6 min read
Line drawing of a each step for the assembly of a car motor
Line drawing assembly steps representing the evolution of a web component (Source: Midjourney)

Web development has seen a significant evolution, with native web components becoming a core aspect of modern front-end development. Native web components enable encapsulation of HTML, CSS, and JavaScript, providing a highly modular way of building web applications. In this post, we’ll explore the pros and cons of using native web components, their adoption by large businesses, support from major JavaScript frameworks, help from NWC frameworks, integrating them into design systems and end with the challenges such as Server-Side Rendering (SSR) support.

Pros of Using Native Web Components

  • Reusability – Native web components can be reused across various parts of a website or different projects. It’s like having a favorite recipe you can use again and again.
  • Interoperability – They work across all major browsers, just like your favorite cat videos. Enjoy the seamlessness!
<!-- This component can be used anywhere, regardless of technology stack -->
<my-component></my-component>
  • Encapsulation – Much like keeping your chocolate stash hidden from your roommates, web components encapsulate styles and behaviors.
// Shadow DOM ensures styles and behaviors are encapsulated (hands off roomies!)
class EncapsulatedComponent extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.shadowRoot.innerHTML = '<style>h1 { color: red; }</style><h1>Encapsulated Component</h1>';
  }
}
customElements.define('encapsulated-component', EncapsulatedComponent);

Cons of Using Native Web Components

  • Learning Curve – Although powerful, native web components come with a learning curve, particularly for developers who are new to the concept. The learning curve might feel steep, but it’s worth the climb!
  • Lack of SSR Support – Server-side rendering (SSR) is not as straightforward with native web components, leading to potential challenges in SEO and performance. SSR support is a bit like putting together IKEA furniture – challenging but not impossible.

Adoption by Large Businesses

Companies like Google, Microsoft, and Salesforce have actively adopted web components in their applications, demonstrating the viability and scalability of this approach. Their usage serves as an endorsement of native web components as a forward-thinking practice. Some of these companies have even opensourced their NWC libraries, making them available for public use!

Support from Major JavaScript Frameworks

Angular, React, and Vue support them, making it a unanimous decision – a rarity in the world of development. This ubiquity of support enhances the appeal of web components, making it easier for developers to integrate them into existing projects.

  • Lit – Lit is a simple and powerful library for building web components. It provides a lightweight layer to work with native web components, enhancing their capabilities.

Example using Lit:

import { LitElement, html, css } from 'lit';

class MyElement extends LitElement {
  static styles = css`p { color: blue; }`;

  render() {
    return html`<p>Lit makes web components easy!</p>`;
  }
}
customElements.define('my-element', MyElement);
  • Stencil – Stencil is a compiler that generates web components with optimized performance. It’s designed to be used not just as a web component builder but as a complete toolchain.

Example using Stencil:

import { Component, h } from '@stencil/core';

@Component({
  tag: 'my-stencil-component',
  styleUrl: 'my-stencil-component.css',
})
export class MyStencilComponent {
  render() {
    return <div>Stencil components are super optimized!</div>;
  }
}

These frameworks help developers leverage the power of native web components with more ease and efficiency. By abstracting some complexities, they facilitate the adoption of web components in modern front-end development.

Integrating with Design Systems and CSS Tokens

In today’s world of front-end development, a cohesive design system is more than just a fancy perk; it’s a necessity. Native web components play nicely with this need, especially when we throw CSS tokens into the mix.

  • Design Systems Design systems offer a unified language and structure for design across projects. Integrating native web components with a design system means that you’re not just putting pieces together but building a well-oiled machine where everything fits.

With web components, you can create reusable components that adhere to the design guidelines set by your design system. This ensures consistency across different parts of the application or even across different projects.

Here’s how you might create a button component:

<custom-button variant="primary"></custom-button>
class CustomButton extends HTMLElement {
  // Define properties, methods, and styles to align with the design system
}
customElements.define('custom-button', CustomButton);
  • CSS tokens, also known as custom properties, allow developers to define reusable values for their CSS. It’s like creating a palette of colors, spacing, or typography that you can refer to throughout your CSS.

In the context of native web components, CSS tokens enable a more flexible and maintainable approach to styling. They allow you to define a set of design variables that can be reused across components, making them a powerful ally in maintaining consistency with your design system.

Example using CSS tokens:

:root {
  --primary-color: #007bff;
  --secondary-color: #6c757d;
}

my-component {
  background-color: var(--primary-color);
}

By using CSS tokens, you can change the values in a single place, and those changes will be reflected across all components using those variables. It’s like having a remote control for your styles! B/c of the concept of encapsulation, CSS tokens are your best bet to maintain consistency across your design system.

Challenges: SSR Support

The lack of seamless support for SSR is one of the notable challenges with native web components. While there are workarounds and tools that address this issue, it remains an area where improvements can be made. I suspect that as the adoption of native web components continues to grow, we’ll see more elegant tools and solutions that will all but make this challenge disappear.

The Future of Native Web Components

With the continuous evolution of web technologies, native web components are poised to play a more vital role in web development. The ongoing development to enhance features, the alignment with other emerging standards, and solving existing challenges like SSR support, are factors that will shape the future of native web components.

Native web components provide a robust, reusable, and scalable solution for modern web development. Their adoption by large businesses and support across major frameworks are testaments to their effectiveness. However, challenges such as the lack of straightforward SSR support do exist, requiring attention and improvement. As we continue to see advancements in web technology, the role of native web components will likely grow, contributing to the ever-changing landscape of front-end web development.