Hubert
9 min
April 29, 2024

What are Web Components?

Web Components are a set of standards that allow the creation of user interface components without relying on external libraries. They consist of custom elements, shadow DOM, and HTML templates. These technologies will enable the creation of universal components that work natively in browsers, meaning they can be used independently of libraries such as React, Vue, or Angular. As a result, web components offer greater flexibility and interoperability between different frameworks, limited only by the browser's support for these standards

Read more
What are Web Components?
Schedule a free consultation

    We process your data in accordance with our privacy policy.

    Historical context and applications of Web Components

    Web Components is a technology that dates back to around 2011, when Google began work on the Polymer project. Their goal was to create a standard that would allow developers to build their own fully isolated user interface components without depending on a particular framework. The standard was gradually developed by the W3C and today consists of four main components: Custom Elements, Shadow DOM, HTML Templates and ES Modules.

    Unlike traditional frontend frameworks(React, Angular, Vue), Web Components are fully natively supported by modern browsers. This means they can be used without additional libraries, and their performance is predictable and conforms to the specification regardless of the backend technology.

    Why were Web Components created and what are they used for?

    The main reason for the development of Web Components was to enable the creation of modular, reusable and easy-to-integrate UI components that work independently of the application context. This allows them to be used in a variety of projects – regardless of the frameworks or programming languages used.

    Most common applications:

    • Design Systems – building consistent libraries of UI components used across the organization (e.g., Google Material Web Components, Salesforce Lightning Web Components).
    • Micro-frontends – components that can be embedded in larger applications without the risk of style and logic conflicts.
    • Progressive Web Apps (PWA)lightweight, fast and framework-independent components ideal for modern web applications.
    • CMS, e-commerce integrations – easy to embed in systems such as WordPress, Magento, or Shopify.

    Due to its independence and flexibility, Web Components are used by companies that need to create universal components that work in different technology ecosystems, without the need for multiple code rewrites.

    Web components – technologies

    The following technologies enable developers to create complex user interfaces with high modularity and compatibility. With these tools, developers can design more personalized and efficient web applications.

    Custom Elements

    Custom Elements are JavaScript APIs that allow you to create and manage your own DOM elements. They allow you to extend existing HTML tags or create entirely new ones, greatly expanding the possibilities of personalizing user interfaces.

    Shadow DOM

    Shadow DOM introduces the concept of DOM encapsulation, meaning that DOM elements and their styles can be isolated from the rest of the page. This allows each component to have its own independent environment, without the risk of interference from external styles or scripts.

    HTML Templates

    TheThe <template> element allows you to define HTML code fragments that are kept in an inactive state until needed. This allows for efficient code reuse without putting a strain on page performance, which is crucial in large-scale projects.

    Looking for an IT contractor for your projects?
    Looking for an IT contractor for your projects?
    Looking for an IT contractor for your projects?
    Write to us!

    How to create a simple component using this technology? Below you will find a short guide

    First, define a component class that extends HTMLElement, which is a native class for HTML elements. In this class you will define all the functionality of your component.

    class MyComponent extends HTMLElement {
      constructor() {
        super(); // always call the superconstructor
        this.attachShadow({ mode: 'open' }); // add shadow DOM
      }
    }
    

    Step 2: Add Shadow DOM

    Shadow DOM allows CSS and HTML to be isolated in the component, so the styles do not affect the rest of the page.

    class MyComponent extends HTMLElement {
      constructor() {
        super();
        this.attachShadow({ mode: 'open' });
        this.shadowRoot.innerHTML = `
          <style>
            p {
              color: blue;
            }
          </style>
          <p>Hello from My Component!</p>
        `;
      }
    }
    

    Step 3: Register the component

    After defining the class, you need to register your component to use the new tag in HTML. Use the customElements.define() method.

    customElements.define('my-component', MyComponent);
    

    Step 4: Using the component

    Once registered, you can use your component like any other HTML element by adding it to your page.

    <my-component></my-component>
    

    Why use Web Components – advantages

    Web Components offer many benefits that make them an attractive choice for web developers:

    1. Isolation (encapsulation of styles and logic)

    Thanks to Shadow DOM technology, each component has its own isolated DOM and CSS context, effectively eliminating the problem of style conflicts between different parts of the application. This isolation extends not only to the appearance, but also to the logic of operation – events and selectors run locally inside the component. This makes it safe to use the same class names or id in different parts of the application without worrying about collisions. This greatly simplifies the development of large UI systems where components come from different teams or companies.

    2. Performance

    Web Components support mechanisms such as HTML Templates(<template>) and lazy rend ering – enabling delayed loading and rendering of content only when the component actually appears in the DOM or is requested by the user. This reduces the consumption of browser resources and speeds up page load times. What’s more, components are often compact, running natively without framework abstraction layers, which eliminates additional code overhead.

    3. Cooperation with other systems and frameworks

    Web Components follow universal browser standards and are not tied to any specific framework, which makes them easy to integrate into projects based on different technologies – such as React, Angular, Vue, or even traditional PHP or .NET applications. This makes it possible to build components independent of the hosting technology (so-called “framework agnostic”), making it easy to reuse them across multiple projects and even migrate between technologies without having to rewrite the UI from scratch.

    4. Customizable and expandable

    With the Custom Elements API, developers can create their own fully customizable HTML tags that behave like native DOM elements. This allows not only the creation of new, unique user interfaces, but also the extension of existing functionality in a way that is compatible with browser logic. Each such element can have its own methods, attributes and events, giving you full control over its behavior and facilitating integration with surrounding code.

    Best Practices – good practices in working with Web Components

    To realize the full potential of Web Components and ensure their high quality, it is worth following proven design and implementation practices. Here are the most important of them:

    1. Shadow DOM: mode: open vs mode: closed

    When creating components, it is a good idea to consciously choose the Shadow DOM isolation mode, which determines the accessibility of the internal DOM from external JavaScript code:

    mode: open
    This mode allows you to directly access the shadowRoot from outside using the element.shadowRoot property . This is useful for testing, debugging, or when external code needs to modify the content of the component.
    Recommendation: use open in library components where the integrator needs more control.

    mode: closed
    In this mode, shadowRoot is not accessible from outside – isolation is complete. Such a component behaves like a “black box”, which increases security and makes accidental tampering more difficult.
    Recommendation: use closed, when you want to guarantee full encapsulation and prevent tampering from outside the component.

    2. Lifecycle callbacks – component lifecycle

    Web Components have a set of native methods (called lifecycle callbacks) that make it easy to manage state and respond to changes in the DOM:

    MethodWhen you call
    connectedCallback()When a component is added to the DOM. Initialization of resources, observers, etc.
    disconnectedCallback()When a component is removed from the DOM. Clean up resources, listeners, etc.
    attributeChangedCallback()When the value of the observed attribute changes.
    adoptedCallback()When an item is moved between documents.

    Best practices:

    • Initialize EventListener, MutationObserver, external APIs in connectedCallback.
    • Remove resources in disconnectedCallback, to avoid memory leaks.
    • Use attributeChangedCallback, when the component reacts to attribute changes from outside (e.g. disabled, aria-*).

    3. Optimizing Web Components performance

    Creating efficient components requires attention to several key aspects:

    Lazy Loading

    Load component scripts only when they are needed (e.g., using dynamic import(), lazy hydration, Intersection Observer). This reduces the initial page load time.

    Avoid global CSS

    Do not use global styles within components. Each component should have its own styles located inside ShadowRoot. This reduces conflicts and improves readability. Avoid injecting large CSS libraries into multiple components — use minimal, dedicated styles.

    Minimal DOM manipulation

    Limit direct DOM manipulation and operations such as innerHTML or appendChild in favor of:

    • HTML Templates (<template>) – pre-prepared DOM fragments.
    • DocumentFragment – a lightweight, temporary DOM tree for building complex structures before inserting them.
    • Selector caching – avoid repeated searches for elements within a component.

    FAQ – Frequently Asked Questions

    How do I use Web Components?

    To use Web Components, define custom elements using Custom Elements, implement Shadow DOM for style and script isolation, and use HTML Templates to manage repetitive content.

    What are Lightning Web Components?

    Lightning Web Components (LWC) is a framework developed by Salesforce that allows you to build user interfaces on the Salesforce CRM platform using optimized Web Components.

    Do Web Components work with frameworks such as React, Angular, or Vue?

    Yes, Web Components are designed to be framework-agnostic. They can be used without any problems in applications based on React, Angular, Vue, or even older technologies such as jQuery. However, each framework requires a slightly different approach to integration, e.g., appropriate attribute passing and event handling.

    What are the limitations of Web Components?

    The biggest challenges are: lack of full integration with some older tools and frameworks, Shadow DOM limitations in the context of external styling, and a more complex state management structure for large applications. In addition, SEO and accessibility (a11y) may require additional attention when using a closed Shadow DOM.

    Are Web Components good for SEO?

    Web Components can be good for SEO, but it depends on how they are implemented. If the content inside the components is rendered server-side or made visible to search engine crawlers through proper hydration or prerendering, SEO performance can be strong. However, if the content relies heavily on client-side JavaScript and isn’t available in the initial HTML, search engines may have trouble indexing it properly. So, careful implementation is key.

    Connected articles
    See all
    Discover more topics