You're viewing docs for an older version of Lit. Click here for the latest version.
Getting started
There are two main ways to use LitElement:
Creating reusable components to share with other developers. Individual components or sets of related components are usually published to npm. To create a reusable component project, see Create a LitElement component project.
Creating app-specific components. The source for these components may live as part of an application project. To add LitElement components to your app, see Add LitElement to an existing project.
None of these tools is required to work with LitElement. They represent one possible set of tools for a good developer experience.
Alternative starting point. As an alternative to the official LitElement starter projects, the open-wc project has a project generator for web components using LitElement. The open-wc script asks a series of questions and scaffolds out a project for you.
Want it on GitHub? If you're familiar with git you may want to create a GitHub repository for your starter project, instead of just downloading the zip file. You can use the GitHub template repository feature to create your own repository from the JavaScript starter project or the TypeScript starter project. Then clone your new repository and install dependencies, as above.
Edit your component definition. The file you edit depends on which language you're using:
JavaScript. Edit the my-element.js file in the project root.
TypeScript. Edit the my-element.ts file in the src directory.
A couple of things to look for in the code:
The code defines a class for the component (MyElement) and registers it with the browser as a custom element named <my-element>.
JavaScript
exportclassMyElementextendsLitElement { ... }
customElements.define('my-element', MyElement);
TypeScript
@customElement('my-element')
exportclassMyElementextendsLitElement { ... }
The component's render method defines a template that will be rendered as a part of the component. In this case, it includes some text, some data bindings, and a button. For more information, see Templates.
exportclassMyElementextendsLitElement {
...
render() {
returnhtml`
<h1>Hello, ${this.name}!</h1>
<button @click=${this._onClick}>
Click Count: ${this.count}
</button>
<slot></slot>
`;
}
}
The component defines some properties. The component responds to changes in these properties (for example, by re-rendering the template when necessary). For more information, see Properties.
You'll probably want to change the component name from "my-element" to something more appropriate. This is easiest to do using an IDE or other text editor that lets you do a global search and replace through an entire project.
If you're using the TypeScript version, remove generated files:
npm run clean
Search and replace "my-element" with your new component name in all files in your project (except in the node_modules folder).
Search and replace "MyElement" with your new class name in all files in your project (except in the node_modules folder).
Rename the source and test files to match the new component name:
JavaScript:
src/my-element.js
src/test/my-element_test.js
TypeScript:
src/my-element.ts
src/test/my-element_test.ts
If you're using the TypeScript version, rebuild the project:
npm run build
Test and make sure your component is still working:
You don't need a lot of tooling for LitElement projects. Chances are if you have an existing set of tools, you can integrate LitElement into it with very limited changes. These instructions assume you're using npm for package management, and using a tool like Rollup or webpack to bundle your code for production.
For details on building projects, including some sample Rollup configurations, see Build for production.
If you're starting from scratch, the open-wc project has a project generator that can scaffold out an application project using LitElement.
How you import the component may vary slightly depending on your build system and project structure. If you're writing in TypeScript, you'll use TypeScript files and use LitElement's decorators. (You can find a sample TypeScript element in the TypeScript starter project).
If you already have a dev server that works with your build system, it should work with LitElement. ES dev server is an alternative dev server that provides a simple, bundler-free workflow. It performs a small number of useful transforms, including transforming bare module specifiers, transpiling to ES5 when needed, and adding polyfills for older browsers.
To support older browsers that don't support ES6 and the web components specifications, you'll need to take a few extra steps to produce code that will run on the older browsers.