LitElement
LEGACY REPO. This repository is for maintenance of the legacy LitElement li...
README
LitElement
LitElement is simple base class for creating fast, lightweight web components with lit-html.
Looking for Lit?
LitElement is now part of the Lit library monorepo. Lit 2 includes lit-html 2.x and LitElement 3.x.
This repo contains the code for LitElement 2.x.
Documentation
For LitElement 2.x documentation, see the legacy documentation site.
For Lit 2, the next version of LitElement and lit-html, see the Lit site.
Overview
LitElement uses lit-html to render into the
element's Shadow DOM
and adds API to help manage element properties and attributes. LitElement reacts to changes in properties
and renders declaratively using lit-html. See the lit-html guide
for additional information on how to create templates for lit-element.
- ```ts
- import {LitElement, html, css, customElement, property} from 'lit-element';
- // This decorator defines the element.
- @customElement('my-element')
- export class MyElement extends LitElement {
- // This decorator creates a property accessor that triggers rendering and
- // an observed attribute.
- @property()
- mood = 'great';
- static styles = css`
- span {
- color: green;
- }`;
- // Render element DOM by returning a `lit-html` template.
- render() {
- return html`Web Components are <span>${this.mood}</span>!`;
- }
- }
- ```
- ``` html
- <my-element mood="awesome"></my-element>
- ```
Note, this example uses decorators to create properties. Decorators are a proposed
standard currently available in TypeScript or Babel. LitElement also supports a vanilla JavaScript method of declaring reactive properties.
Examples
You can also copy this HTML file into a local file and run it in any browser that supports JavaScript Modules).
Installation
From inside your project folder, run:
- ``` sh
- $ npm install lit-element
- ```
To install the web components polyfills needed for older browsers:
- ``` sh
- $ npm i -D @webcomponents/webcomponentsjs
- ```
Supported Browsers
The last 2 versions of all modern browsers are supported, including
Chrome, Safari, Opera, Firefox, Edge. In addition, Internet Explorer 11 is also supported.
Edge and Internet Explorer 11 require the web components polyfills.
Forward Compatibility With Lit 2
Lit 2 (LitElement 3.0) has a few breaking changes and deprecations that have been back-ported to LitElement 2.5 in order to ease upgrading.
To prepare for Lit 2, update these APIs:
| LitElement | LitElement |
|---|---|
| |------------------------|-----------------------| | |
| Decorator | `import |
| `@internalProperty() | `@state() |
| Override | Override |
| Shadow | Add |
| `import | `import |
Contributing
Please see CONTRIBUTING.md.
探客时代
