DEV Community

Faisal Rahman
Faisal Rahman

Posted on

Building Reusable Components with JavaScript Web Components and LIT

lit logo

In today’s fast-paced web development environment, building reusable and maintainable components is key. JavaScript Web Components offer a native way to create self-contained, modular elements that work across frameworks. However, creating these components manually can be tedious and complex. That’s where LIT comes in! LIT simplifies the process of building Web Components, making it easier to manage state, reactivity, and rendering, while staying lightweight.

Overview

JavaScript Web Components allow us to create encapsulated, reusable HTML elements, which include their structure, styles, and behavior. The core of Web Components relies on four technologies:

  • Custom Elements: Define your own HTML tags.
  • Shadow DOM: Ensures component encapsulation, so styles and functionality don’t bleed into other parts of the page.
  • HTML Templates: Provides a way to define HTML markup that can be reused.
  • Lifecycle Methods: Allows you to optimize how components are rendered, boosting performance and responsiveness. You can create interactive components by responding to user actions at specific lifecycle stages.

These technologies allow developers to create custom HTML elements that behave just like native elements.

What is LIT?

While JavaScript Web Components are powerful, writing them from scratch can sometimes feel verbose and complex. This is where LIT comes in. LIT is a modern, minimalistic framework for building fast Web Components. It adds superpowers like:

  • Reactive properties: Automatically updates your UI when data changes.
  • Lightweight: LIT's small size (~5kb) ensures that your components stay fast.
  • Declarative templates: LIT uses tagged template literals to simplify defining your component’s HTML structure.
  • Interoperable & future-ready: Web components work anywhere you use HTML, with any framework or none at all. This makes Lit ideal for building shareable components, design systems, or maintainable, future-ready sites and apps.

How to start with LIT?

Let’s build a simple counter component using LIT to see how easy it is to create interactive Web Components.

  1. Setup: First, install LIT by running the following:
    npm install lit

  2. Creating the Component: Create a new JavaScript file for your
    component, and start by importing LitElement and html from LIT:

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

class MyCounter extends LitElement {
  static properties = {
    count: { type: Number }
  };

  constructor() {
    super();
    this.count = 0;
  }

  increment() {
    this.count += 1;
  }

  render() {
    return html`
      <div>
        <p>Count: ${this.count}</p>
        <button @click=${this.increment}>Increment</button>
      </div>
    `;
  }
}

customElements.define('my-counter', MyCounter);
Enter fullscreen mode Exit fullscreen mode

3.
Using Your Component: You can now use in your HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My Counter</title>
  <script type="module" src="./path/to/your/my-counter.js"></script>
</head>
<body>
  <my-counter></my-counter>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

That’s it! You’ve just created an interactive, reusable Web Component with LIT.

Conclusion

Web Components are a powerful tool for creating reusable, modular elements that work seamlessly across frameworks. With LIT, building these components becomes much more straightforward and efficient. I hope this post has given you a good starting point to dive into the world of Web Components and LIT. Give it a try in your next project and experience the benefits firsthand!

What do you think about Web Components and LIT? Have you tried them yet? This is my first dev.to post so feel free to share your thoughts or ask any questions in the comments below. Let’s discuss!

Top comments (1)

Collapse
 
dannyengelman profile image
Danny Engelman • Edited

And the 60 alternatives for Lit

webcomponents.dev/blog/all-the-way...