1
1
# element-lite
2
- [ ![ Build Status] ( https://travis-ci.org/tjmonsi/element-lite.svg?branch=master )] ( https://travis-ci.org/tjmonsi/element-lite )
2
+ Master: [ ![ Build Status] ( https://travis-ci.org/tjmonsi/element-lite.svg?branch=master )] ( https://travis-ci.org/tjmonsi/element-lite )
3
+ Develop: [ ![ Build Status] ( https://travis-ci.org/tjmonsi/element-lite.svg?branch=develop )] ( https://travis-ci.org/tjmonsi/element-lite )
3
4
4
5
A take on using lit-html and using methods coming from Polymer.
5
6
6
7
This is based on https://github.com/PolymerLabs/lit-element and added my own take on creating my own
7
- simple library.
8
+ simple library. You can freely use it on your own projects.
8
9
9
- This is a work in progress but I will use it on my own projects. You can freely use it on your own projects .
10
+ Most of the methods and its internals came from Polymer's properties-mixin, properties-changed, property-accessors mixin, and property-effects mixin ( You can see each file on where parts are copied/modified from) .
10
11
11
- Most of the methods and its internals came from Polymer's properties-mixin, properties-changed, property-accessors mixin, and property-effects mixin.
12
+ The reason for copying rather than depending on files is the added overhead of extending classes and
13
+ the number of method overrides, making some inherited methods not used in the final part of
14
+ upgrading the element. This is to squeeze out unused parts from the mixins.
12
15
13
16
14
17
## How to install:
15
18
16
- ### using npm
19
+ ### Using npm
17
20
18
21
This is the recommended way. To install, just do this:
19
22
```
20
23
npm i --save @littleq/element-lite
21
24
```
22
25
23
26
24
-
25
-
26
27
## Out of the box, what can I do?
27
28
29
+ ### On evergreen browsers that support ES6 and import (Latest Firefox and Chrome)
30
+
28
31
You can add this to your JS
29
32
30
33
``` js
@@ -43,21 +46,71 @@ customElement.define('web-component', Component);
43
46
and add this to your HTML
44
47
45
48
``` html
46
- <script src =" main.js" type =" module" >
49
+ <script src =" main.js" type =" module" ></ script >
47
50
```
48
51
49
- You can also add these additional scripts for polyfill
52
+ ### Usage on bundlers like Webpack
53
+
54
+ Same as above.
55
+
56
+ ### For non-Chrome evergreen browsers (Latest Firefox, Safari, and Edge)
57
+
58
+ You need to add this additional script for polyfill
50
59
51
60
``` html
52
61
<script src =" node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js" >
53
62
` ` `
54
63
55
- And if you are using Webpack and you have bundled it in ES5 for older browsers, you also need:
64
+ And then wrap around the files after the ` WebComponentsReady` event has been fired
65
+
66
+ ` ` ` html
67
+ < script>
68
+ window .addEventListener (' WebComponentsReady' , function () {
69
+ var component = document .createElement (' script' );
70
+ component .src = ' main.js' ;
71
+ component .type = ' module' ;
72
+ document .head .appendChild (component);
73
+ });
74
+ </script >
75
+ ```
76
+
77
+ ### If you compiled and bundled the elements to ES5
78
+
79
+ if you are using Webpack and you have bundled it in ES5 for older browsers, you also need:
56
80
57
81
``` html
58
82
<script src =" node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js" >
59
83
` ` `
60
84
85
+ ### If you don't need a bundler but is capable for use in older browsers
86
+
87
+ ` ` ` html
88
+ < script src= " /node_modules/@littleq/element-lite/dist/element-lite.umd.es5.min.js" >
89
+ < script>
90
+ window .addEventListener (' WebComponentsReady' , function () {
91
+ var component = document .createElement (' script' );
92
+ component .src = ' main.js' ;
93
+ document .head .appendChild (component);
94
+ });
95
+ </script >
96
+ ```
97
+
98
+ ``` js
99
+ // main.js
100
+ // wrap in anonymous function
101
+ // Bundle this in ES5 either using Rollup or Webpack
102
+ (function (){
103
+ // get ElementLite from window.ElementLite;
104
+ var ElementLite = window .ElementLite .ElementLite ;
105
+ class Component extends ElementLite (HTMLElement ) {
106
+ render () {
107
+ return html ` Whatever you want to put in the ShadowDom` ;
108
+ }
109
+ }
110
+ })();
111
+ ```
112
+
113
+
61
114
## What else can it do?
62
115
63
116
You can put a default value on a property.
@@ -206,6 +259,7 @@ el.splice('prop1', 1, 0, '1', '2') // el.prop1 = ['a', '1', '2', 'b', 'c']
206
259
el .splice (' prop1' , 1 , 1 ) // el.prop1 = ['a', '2', 'b', 'c']
207
260
```
208
261
262
+
209
263
## Example usage
210
264
211
265
index.html
@@ -225,6 +279,7 @@ index.html
225
279
226
280
test-element.js
227
281
``` js
282
+ // you can get lit-html's `html` from the bundle as well
228
283
import { ElementLite , html } from ' ./node_modules/@littleq/element-lite/element-lite.js' ;
229
284
const { customElements } = window ;
230
285
@@ -319,6 +374,127 @@ class Component extends ElementLiteStaticShadow(HTMLElement) {
319
374
customElement .define (' web-component-static' , Component);
320
375
```
321
376
377
+ ## Element-lite-lit-only
378
+
379
+ If you don't need the power of element-lite-base but need to the power of lit-html,
380
+ use ` element-lite-lit-only `
381
+
382
+ ``` js
383
+ // main-static.js
384
+ import { ElementLiteLitOnly } from ' node_modules/@littleq/element-lite/element-lite-lit-only.js'
385
+
386
+ class Component extends ElementLiteLitOnly (HTMLElement ) {
387
+ render () {
388
+ return ` Hello World!`
389
+ }
390
+ }
391
+
392
+ customElement .define (' web-component-static' , Component);
393
+ ```
394
+
395
+
396
+ ## What files to import and how?
397
+
398
+ If you are going to use it on Evergreen Browsers that allows ` <script type="module"> ` ,
399
+ then you can just do this on your ` js ` files
400
+
401
+ ``` js
402
+ import { ElementLite } from ' ./node_modules/@littleq/element-lite/element-lite.js' ;
403
+ ```
404
+
405
+ If you are going to use it on Webpack or Rollup, you can do any of these
406
+
407
+ ``` js
408
+ // provided that node_modules is resolved in your configurations
409
+ import { ElementLite } from ' element-lite/@littleq/element-lite.js' ;
410
+ ```
411
+
412
+ or
413
+
414
+ ``` js
415
+ import { ElementLite } from ' ./node_modules/@littleq/element-lite' ;
416
+ ```
417
+
418
+ or
419
+
420
+ ``` js
421
+ import { ElementLite } from ' ./node_modules/@littleq/element-lite/dist/element-lite.esm.js' ;
422
+ ```
423
+
424
+ If you are going to use ` require ` and not ` import ` you can do any of these
425
+
426
+ ``` js
427
+ // provided that node_modules is resolved in your configurations
428
+ var ElementLite = require (' @littleq/element-lite' ).ElementLite ;
429
+ ```
430
+
431
+ or
432
+
433
+ ``` js
434
+ // provided that node_modules is resolved in your configurations
435
+ var ElementLite = require (' @littleq/element-lite/dist/element-lite.cjs.js' ).ElementLite ;
436
+ ```
437
+
438
+ If you are going to load it via the ` <script> ` tag, you need to do these
439
+
440
+ For ES6
441
+
442
+ ``` html
443
+ <script src =" /node_modules/@littleq/element-lite/dist/element-lite.umd.es6.js" >
444
+ <!-- < script src= " /node_modules/@littleq/element-lite/dist/element-lite.umd.es6.min.js" >
445
+ if you need the minified file -->
446
+ ` ` `
447
+
448
+ For ES5
449
+
450
+ ` ` ` html
451
+ < script src= " /node_modules/@littleq/element-lite/dist/element-lite.umd.es5.js" >
452
+ <!-- < script src= " /node_modules/@littleq/element-lite/dist/element-lite.umd.es5.min.js" >
453
+ if you need the minified file -->
454
+ ` ` `
455
+
456
+ and then can access it here
457
+
458
+ ` ` ` js
459
+ var ElementLite = window .ElementLite .ElementLite
460
+ ` ` `
461
+
462
+ You can also load ElementLiteBase, ElementLiteStaticShadow and ElementLiteLitOnly from ElementLite as well
463
+
464
+ ` ` ` js
465
+ // right from the box
466
+ import { ElementLiteBase , ElementLiteStaticShadow , ElementLiteLitOnly } from ' ./node_modules/element-lite/element-lite.js' ;
467
+ ` ` `
468
+
469
+ using ` require`
470
+
471
+ ` ` ` js
472
+ // provided that node_modules is resolved in your configurations
473
+ var ElementLiteBase = require (' @littleq/element-lite/dist/element-lite.cjs.js' ).ElementLiteBase ;
474
+ var ElementLiteStaticShadow = require (' @littleq/element-lite/dist/element-lite.cjs.js' ).ElementLiteStaticShadow ;
475
+ var ElementLiteLitOnly = require (' @littleq/element-lite/dist/element-lite.cjs.js' ).ElementLiteLitOnly ;
476
+ ` ` `
477
+
478
+ using ` < script> `
479
+
480
+ ` ` ` js
481
+ var ElementLiteBase = window .ElementLite .ElementLiteBase
482
+ var ElementLiteStaticShadow = window .ElementLite .ElementLiteStaticShadow
483
+ var ElementLiteLitOnly = window .ElementLite .ElementLiteLitOnly
484
+ ` ` `
485
+
486
+
487
+ ## And does it work on?
488
+
489
+ It works on all major evergreen Browsers (Edge, Safari, Chrome, Firefox) as long as you have the Polyfills
490
+ set (make sure to add ` webcomponents- lite` or ` webcomponents- loader` and create the components after the
491
+ ` WebComponentsReady` event has been fired)
492
+
493
+ It also works on IE 11, Safari 11, Safari 10.1, Safari 9, and Safari 8.
494
+
495
+ Still checking on IE 10, 9, 8 and Safari 7, 6. (Need polyfills for ` Map ` and ` WeakMap ` ).
496
+
497
+
322
498
## Size
323
499
324
500
Based on size-limit
@@ -342,7 +518,7 @@ npm run size
342
518
Size limit: 3 KB
343
519
344
520
element- lite .js
345
- Package size: 5.23 KB
521
+ Package size: 5.33 KB
346
522
Size limit: 5.5 KB
347
523
348
524
With all dependencies, minified and gzipped
@@ -352,4 +528,4 @@ npm run size
352
528
353
529
1. Not yet tested for Production #2
354
530
2. Not yet tested using Webpack on older browsers #2
355
- 4 . Haven't tested a proper loop and if-then-else in #2
531
+ 3 . Haven't tested a proper loop and if-then-else in #2
0 commit comments