UNPKG

3.64 kBMarkdownView Raw
1# LitElement
2A simple base class for creating fast, lightweight web components with [lit-html](https://lit-html.polymer-project.org/).
3
4[![Build Status](https://travis-ci.org/Polymer/lit-element.svg?branch=master)](https://travis-ci.org/Polymer/lit-element)
5[![Published on npm](https://img.shields.io/npm/v/lit-element.svg)](https://www.npmjs.com/package/lit-element)
6[![Published on webcomponents.org](https://img.shields.io/badge/webcomponents.org-published-blue.svg)](https://www.webcomponents.org/element/lit-element)
7[![Mentioned in Awesome lit-html](https://awesome.re/mentioned-badge.svg)](https://github.com/web-padawan/awesome-lit-html)
8
9## Documentation
10
11Full documentation is available at [lit-element.polymer-project.org](https://lit-element.polymer-project.org).
12
13## Overview
14
15LitElement uses [lit-html](https://lit-html.polymer-project.org/) to render into the
16element's [Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM)
17and adds API to help manage element properties and attributes. LitElement reacts to changes in properties
18and renders declaratively using `lit-html`. See the [lit-html guide](https://lit-html.polymer-project.org/guide)
19for additional information on how to create templates for lit-element.
20
21```ts
22 import {LitElement, html, css, customElement, property} from 'lit-element';
23
24 // This decorator defines the element.
25 @customElement('my-element')
26 export class MyElement extends LitElement {
27
28 // This decorator creates a property accessor that triggers rendering and
29 // an observed attribute.
30 @property()
31 mood = 'great';
32
33 static styles = css`
34 span {
35 color: green;
36 }`;
37
38 // Render element DOM by returning a `lit-html` template.
39 render() {
40 return html`Web Components are <span>${this.mood}</span>!`;
41 }
42
43 }
44```
45
46```html
47 <my-element mood="awesome"></my-element>
48```
49
50Note, this example uses decorators to create properties. Decorators are a proposed
51standard currently available in [TypeScript](https://www.typescriptlang.org/) or [Babel](https://babeljs.io/docs/en/babel-plugin-proposal-decorators). LitElement also supports a [vanilla JavaScript method](https://lit-element.polymer-project.org/guide/properties#declare) of declaring reactive properties.
52
53## Examples
54
55 * Runs in all [supported](#supported-browsers) browsers: [Glitch](https://glitch.com/edit/#!/hello-lit-element?path=index.html)
56
57 * Runs in browsers with [JavaScript Modules](https://caniuse.com/#search=modules): [Stackblitz](https://stackblitz.com/edit/lit-element-demo?file=src%2Fmy-element.js), [JSFiddle](https://jsfiddle.net/sorvell1/801f9cdu/), [JSBin](http://jsbin.com/vecuyan/edit?html,output),
58[CodePen](https://codepen.io/sorvell/pen/RYQyoe?editors=1000).
59
60 * You can also copy [this HTML file](https://gist.githubusercontent.com/sorvell/48f4b7be35c8748e8f6db5c66d36ee29/raw/67346e4e8bc4c81d5a7968d18f0a6a8bc00d792e/index.html) into a local file and run it in any browser that supports [JavaScript Modules]((https://caniuse.com/#search=modules)).
61
62## Installation
63
64From inside your project folder, run:
65
66```bash
67$ npm install lit-element
68```
69
70To install the web components polyfills needed for older browsers:
71
72```bash
73$ npm i -D @webcomponents/webcomponentsjs
74```
75
76## Supported Browsers
77
78The last 2 versions of all modern browsers are supported, including
79Chrome, Safari, Opera, Firefox, Edge. In addition, Internet Explorer 11 is also supported.
80
81Edge and Internet Explorer 11 require the web components polyfills.
82
83## Contributing
84
85Please see [CONTRIBUTING.md](./CONTRIBUTING.md).
\No newline at end of file