Introduction to LitElement
Tags: litelement, typescript, webdev, webcomponents
Published: 2021-02-10 20:18:19

The use of powerful frameworks and libraries is very widespread nowadays. We can name Angular, React.js, Vue, Svelte, among others.

It's hard to imagine building a web application without the use of any of these component-based frameworks. For these options, the components are reusable and configurable _widgets_. They are able to provide a custom behavior and styling and they're used as building blocks for the application.

Can we share a component between these frameworks? The short answer is no. Every framework/library has a custom API definition to build components and they are not interoperable with each other. 

## What is LitElement?
According to the official LitElement [website](https://lit-element.polymer-project.org/guide):

> LitElement is a simple base class for creating fast, lightweight web components that work in any web page with any framework.

That means we can use the OOP (Object-Oriented Programming) paradigm using JavaScript or even better: TypeScript. 

## JavaScript vs TypeScript

To create your first custom Web Component using **JavaScript**, you'll need to define a class that implements the appearance and functionality of it as follows:

```js
import { LitElement, html } from 'lit-element';

class HelloComponent extends LitElement {
  static get properties() {
    return { name: { type: String } };
  }

  constructor() {
    super();
    this.name = 'Luis'; // Set a default value here
  }

  // Defines a template to be "rendered" as part of the component.
  render() { 
    return html`Hello ${this.name}. Welcome to LitElement!`;
  }
}

// Register as a custom element named <hello-component>
customElements.define('hello-component', HelloComponent);
```

Now let's see how you can write your first Web Component using the power of **TypeScript**:

```ts
import { LitElement, html, property, customElement } from 'lit-element';

//Decorator that register as a custom element named <hello-component>
@customElement('hello-component') 
export class HelloComponent extends LitElement {
  // You can assign the default value here.
  @property({type: String}) name = 'Luis'; 

  // Defines a template to be "rendered" as part of the component.
  render() { 
    return html`Hello, ${this.name}. Welcome to LitElement!</p>`;
  }
}
```

As you may note, the use of [TypeScript decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) will provide the ability to annotate the class declarations and members.

In the next section, you can find more articles to learn and get started with LitElement and TypeScript.

## More about LitElement

In the past months, I've been publishing several articles about using **LitElement with TypeScript**. Also, I've been using these technologies actively to build Single-page applications, and I'm happy to share these resources with you:

* [Getting started with LitElement and TypeScript](https://labs.thisdot.co/blog/getting-started-with-litelement-and-typescript)
* [Routing Management with LitElement and TypeScript](https://labs.thisdot.co/blog/routing-management-with-litelement)
* [Navigation Lifecycle using Vaadin Router, LitElement and TypeScript](https://labs.thisdot.co/blog/navigation-lifecycle-using-vaadin-router-litelement-and-typescript)
* [Web Components Integration using LitElement and TypeScript](https://labs.thisdot.co/blog/web-components-integration-using-litelement-and-typescript)
* [Testing Web Components with Cypress and TypeScript](https://labs.thisdot.co/blog/testing-web-components-with-cypress-and-typescript)
* [Testing Web Components with Karma and TypeScript](https://labs.thisdot.co/blog/testing-web-components-with-karma-and-typescript)
* [Using Route Guards, Actions and Web Components](https://labs.thisdot.co/blog/using-route-guards-actions-and-web-components)
* [How to Serve a Single Page Application (SPA) using Rollup.js and Web Dev Server](https://labs.thisdot.co/blog/how-to-serve-a-single-page-application-spa-using-rollup-js-and-web-dev)
* [How to Setup a TypeScript project using Rollup.js](https://labs.thisdot.co/blog/how-to-setup-a-typescript-project-using-rollup-js)
* [How to Build a LitElement Application with Rollup.js and TypeScript](https://labs.thisdot.co/blog/how-to-build-a-litelement-application-with-rollup-js-and-typescript)

More articles are coming soon! Do not miss any of them and visit the [This Dot Blog](https://labs.thisdot.co/blog/) to be up-to-date.

Feel free to reach out on [Twitter](https://twitter.com/luixaviles) if you have any questions. Follow me on [GitHub](https://github.com/luixaviles) to see more about my work.

---

*This Dot Labs is a modern web consultancy focused on helping companies realize their digital transformation efforts. For expert architectural guidance, training, or consulting in React, Angular, Vue, Web Components, GraphQL, Node, Bazel, or Polymer, visit [thisdotlabs.com](https://www.thisdotlabs.com).*

*This Dot Media is focused on creating an inclusive and educational web for all.  We keep you up to date with advancements in the modern web through events, podcasts, and free content. To learn, visit [thisdot.co](https://www.thisdot.co).*