Interactivated logo

How to Build Custom JavaScript Web Components for Your E-Commerce Store

04 Dec
All blog posts

The world of e-commerce is incredibly competitive. Statistics show that there are already millions of online stores in operation, with more being made each day. Standing out in such a crowded industry isn’t easy, and business owners have to do whatever they can to get ahead of the game and draw customers.

One of the best ways to make your mark on the world of e-commerce is by having the most fully-functional, well-designed store. And JavaScript web components can massively help with that, giving you new ways to strengthen your site’s navigation and user experience. This guide will cover the basics of web components and how to make them.

An Introduction to JavaScript Web Components

To begin, a quick definition: web components are custom JavaScript elements that users can customize and define to their own desires. Another way to look at them is like reusable, encapsulated pieces of code, customizable with the precise styling and functionality you want. They’re very versatile and offer an array of benefits not only for e-commerce stores but for other websites as well.

The Benefits of Web Components for E-Commerce

There are lots of advantages associated with JavaScript web components for all sorts of sites and online businesses, including e-commerce stores. Here are some of the biggest benefits and reasons to consider using web components in your store:

  • Reusability: As explained above, web components are infinitely reusable across different sites and projects. So, once you’ve made one, you can get a lot of value out of it, using it for multiple sites and pages – particularly useful if you operate on more than one e-commerce platform.
  • Ease-of-use: Making web components requires a certain level of skill and experience. But, compared to other kinds of coding and site design, they’re relatively easy to work with, allowing big, complicated pages to be broken down into smaller and easier to understand chunks.
  • Independent testing: You can both develop and test web components individually. And that’s very handy when it comes to troubleshooting issues or making little adjustments or tweaks, as you won’t run the risk of breaking or disrupting your entire site.
  • Enhancing user experience: Web components can be utilized in various ways to improve the user experience of visitors to your site. They can help to make the store easier to navigate, for example, and even boost conversions when used the right way.
  • Versatility: Web components are also endlessly versatile. They can be as simple or as complicated as you like. One might display a little custom greeting for each user, for example, and others could completely change the way people navigate your e-commerce store.

How to Build Web Components

With the basics and benefits of web components covered, let’s dig into the actual process of making them, step-by-step. For the purposes of this guide, we’ll use a simple “Hello World” example. It is useful for illustrating the basic concept of how web components work, but there are much more advanced components you can start working on, once you know the basics.

Defining the Class and Extending the HTMLElement Interface

Web components typically begin by defining an ES2015 class, which can be used later on to control the element. You can name it anything you like, but for the purposes of this guide, we’ll use “HelloWorld.”

Next, given that the whole purpose of web components is to extend the HTMLElement interface, the code needs to introduce this concept. For that, we simply use the phrase “extends HTMLElement”. Thus, with the new addition, our web component definition looks like this:

class HelloWorld extends HTMLElement {

Adding the Function or Purpose of the Web Component

With the basic intro out of the way, you can then get to work on the main function of your component. In our “HelloWorld” example, we simply want the component to be used for displaying a “Hello World!” message on the page. For that, we need to define the desired text and use a “connectedCallback()”, which is used whenever the web component is appended to a Document Object Model, or DOM.

The code now looks like this:

class HelloWorld extends HTMLElement {
// connect component
connectedCallback()
{ this.textContent = ’Hello World!’;
}
}

Defining the Custom Web Component

Finally, in order to actually use the web component, it needs to be registered with the CustomElementRegistry. This is done using the “customElements.define()” piece of code, giving us the following:

class HelloWorld extends HTMLElement {
// connect component
connectedCallback()
{ this.textContent = ’Hello World!’;
}
}
customElements.define( ’hello-world’, HelloWorld );

With this last line added in, our “hello-world” element will now be associated with the “HelloWorld” class. Any time we want to use this web component, we can simply use the HTML <hello-world>. Note that a dash is always needed when naming and defining your custom web components so that they don’t cause any clashes or confusion with existing HTML elements.

Improving the Custom Web Component

How to Build Custom JavaScript Web Components for Your E-Commerce Store 2

The steps above show how to make a very basic web component, but it’s not much use. All it does is display a single output message, without any kind of personalization or variation. To make it useful for sites like e-commerce stores, we need to improve it. This can be done by adding various HTML attributes to customize the function and alter the output text.

We could, for instance, adjust the message to say “Hello Mary!” instead of “Hello World!” This involves adding a new HTML attribute, like so:

<hello-world name="Mary"></hello-world>

We also need to change the JavaScript accordingly, using the constructor() function on the HelloWorld class and the super() method, as well as defining a default name property:

class HelloWorld extends HTMLElement {
constructor() {
super();
this.name = ’World’;
}

We can then add in the observedAttributes() property to make the component provide a range of observable properties, as follows:

static get observedAttributes() {
return [’name’];
}

An attributeChangedCallback() is then added in, as this is needed whenever we define or change an attribute in the HTML code.

attributeChangedCallback(property, oldValue, newValue) {
if (oldValue === newValue) return;
this[ property ] = newValue;
}

Last but not least, we can use the connectedCallback() to finalize the component code.

connectedCallback() {
this.textContent = `Hello ${ this.name }!`;
}

With this code, it’s possible to provide a custom greeting message to quite literally say “Hello” to your e-commerce store visitors.

Essential Technologies of Web Components

There are three key technologies that make up web components and the ways in which we use them:

Custom Elements

Custom elements are HTML elements with their own custom templates, created with the aid of JavaScript APIs. When building a new web component, it’s always important to define a brand new HTML tag like <my-component> and then use JavaScript to define the element’s purpose and role.

Shadow DOM

The Shadow DOM is essentially a separated DOM that you can attach to your web components, using the following line of code:

const shadow = this.attachShadow({ mode: ’closed’ });

The Shadow DOM may be either open or closed. When it’s open, it means that JavaScript in the surrounding outer page is able to access the Shadow DOM. If it’s closed, that means that the Shadow DOM is only accessible within the web component itself.

Using the Shadow DOM is a good way to encapsulate web components to preserve the component’s exact structure and style, preventing any interference with the rest of the page.

HTML Templates

Given that it’s quite tricky to define all of the HTML inside individual web components, developers often prefer to use HTML templates instead. These are chunks of HTML code that web components can use.

How to Build Custom JavaScript Web Components for Your E-Commerce Store 1

Incorporating HTML templates allows you to make tweaks and changes to the code without having to rewrite lots of JavaScript code. They’re also easier to define within HTML itself using simple “<template>” tags.

Slots

Slots let users slot content into custom elements by simply adding it into the opening and closing tags of the element in question. They’re useful for when you want to customize your HTML templates for use in web components.

ES6 Modules

ES6 modules are used when importing web components to different sites and systems. They allow you to reuse the same component across multiple sites and platforms.

Use Web Components to Elevate Your E-Commerce Brand

If you don’t have a lot of development experience, using web components may seem a little too technical for you. But, if you know the basics of JavaScript and understand how to code, these components can be very useful, adding new features and functions to your e-commerce store.

There are countless ways to customize and incorporate web components to your store, delivering a more personalized and engaging experience for all your users. So, if you’re looking for a way to foster customer loyalty and make your store even better, invest in web components today.

You may also like

Person avatar
Person avatar
Person avatar

We're Ready When You Are

Our expert team is on standby - day or night - to talk timelines, budgets, and bring your idea from concept to launch - seamlessly. No stress, no delays.

Let's Figure This Out Together

Let’s Talk & Build Something Great.

Whether it’s a scalable SaaS platform, an innovative marketplace, a cutting-edge eCommerce solution, or another bold new tech idea, we bring the expertise to make it real - seamlessly and stress-free.No drama, no fluff - just damn good digital solutions.

Interactivated solutions contact person

Roy Van Eijsselsteijn

CEO | Head of Business Development

Write a message

By submitting the form, I agree with the rules for processing my personal data as described in the Privacy Policy.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.