UNPKG

3.4 kBMarkdownView Raw
1# lit-element
2
3[![Greenkeeper badge](https://badges.greenkeeper.io/DiiLord/lit-element.svg)](https://greenkeeper.io/)
4Implements [lit-html](https://github.com/PolymerLabs/lit-html) via a LitElement class. Made for custom Elements.
5
6[![Build Status](https://travis-ci.org/DiiLord/lit-element.svg?branch=master)](https://travis-ci.org/DiiLord/lit-element)
7
8## New in 0.4.0
9- We now allow you to switch out the standard lit-html `render` and `html` functions
10- You can now use `lit-html-extended` via `lit-element-extended.js`
11- Added `notify` option for properties, which will fire an event, if the value changes
12- A lot of bug fixes
13
14## New in 0.3.0
15- You can now set any property of your element to a promise and LitElement will set the property to the resolved value of the promise. (credit: [depeele](https://github.com/depeele))
16- Attributes of properties with `reflectToAttribute: true` are now transformed to kebab-case. (credit: [depeele](https://github.com/depeele))
17- Codebase moved to TypeScript.
18
19## Installation
20
21You can get it through npm or yarn
22
23```
24npm install lit-element
25```
26```
27yarn add lit-element
28```
29
30## Default Usage
31
32```javascript
33// import lit-element
34import {LitElement, html} from '../node_modules/lit-element/lit-element.js'
35
36// define Custom Element
37class MyElement extends LitElement(HTMLElement) {
38
39 // define properties similiar to Polymer 2/3
40 static get properties() {
41 return {
42 title: String,
43 body: {
44 type: String,
45 value: 'That is a cool LitElement',
46 observer: '_bodyChanged',
47 reflectToAttribute: true,
48 notify: true
49 }
50 }
51 }
52
53 // define your template in render
54 render() {
55 this.title = 'This is lit';
56 return html`
57 <h1 id="title">${this.title}</h1>
58 <p>${this.body}</h1>
59 `;
60 }
61
62 // observer callback
63 _bodyChanged(newValue) {
64 console.log(`Body updated to ${newValue}`);
65 }
66
67 // If you want work done after the first render, like accessing elements with ids, do it here
68 afterRender(isFirstRender) {
69 if(isFirstRender) {
70 // access the element with id 'title'
71 this.$.title.classList.add('title--main');
72 this.addEventListener('body-changed', e => {
73 const body = e.detail;
74 ...
75 })
76 }
77 }
78}
79```
80
81## Declaring properties
82Properties of your element are set through a static getter of `properties`, as seen above.
83
84Properties can be set with the following options:
85- type: The type function of this property. Must be set!
86- reflectToAttribute: Keeps the property in sync with the attribute of the same name, konverted to kebab-case (myProp <-> my-prop)
87- value: The initial value of the property. If it should be an array or an object, set value to a function returning that object, to keep it unique for each instance of the element
88- observer: The name of the method that should be called whenever the property changes.
89- notify: Dispatch an event on property-change. The event name follows the pattern `my-prop-changed`. The new value is in `event.detail`.
90
91
92## Notes
93
94 - This Element does not use Polymer, just Polymer-like syntax for properties.
95 - Currently only `type`, `reflectToAttribute`, `observer`, `value` and `notify` are supported for properties.
\No newline at end of file