Skip to content

Commit dc981fd

Browse files
committed
adding lit-demo
1 parent c50c8bd commit dc981fd

File tree

6 files changed

+265
-51
lines changed

6 files changed

+265
-51
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ citizens. The main features are:
1212

1313
- It's tiny, only about 5 kB minified (and 1.9 kB gzipped).
1414
- It uses Proxy under the hood for a little :sparkler:
15-
- It is meant for custom-elements, so you can use it with vanilla JS or any framework like [lit-element](https://www.npmjs.com/package/lit-element), [Brick](https://www.npmjs.com/package/brick-element).
15+
- It is meant for custom-elements, so you can use it with Vanilla-JS or any framework like [lit-element](https://www.npmjs.com/package/lit-element), [Brick](https://www.npmjs.com/package/brick-element).
1616
- Implements the usual flow: ACTION->REDUCER->STORE but with A LOOOOT less painfull sintax.
1717
- You can break the STORE in parts as small as you like.
1818
- Works with async out of the box.
19-
- It is tested.
19+
- Saves the state to ``localStorage`` automatically.
2020

2121
# Demo
2222

23-
Have a look at a [simple todo App demo](https://webcomponenthelpers.github.io/ImperaJS/demo/) made with ImperaJs.
23+
Have a look at a [simple todo App demo](https://webcomponenthelpers.github.io/ImperaJS/demo/litDemo.html) made with ImperaJs and Lit-Element.
24+
While [here](https://webcomponenthelpers.github.io/ImperaJS/demo/) the same demo with Brick-Element.
25+
2426

2527
# Getting Started
2628

demo/Store.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
import {StateVariable} from '../build/impera.js'
22

3+
/**
4+
* List of todos, initialized empty
5+
*/
36
export var todos = new StateVariable("todos",[]);
47

8+
/**
9+
* Transition (pure function) that modify the list by adding a todo
10+
*/
511
todos.addTransition("addTodo",(text)=>{
612
if(typeof text === "string"){
713
let todo = {txt : text, isComplete : false}
814
todos.value.push(todo)
915
}
1016
})
1117

18+
/**
19+
* Transition (pure function) that modify the list by removing a todo
20+
*/
1221
todos.addTransition("removeTodo", (index)=>{
1322
let _int = parseInt(index)
1423
todos.value.splice(_int,1)
1524
})
1625

26+
/**
27+
* Transition (pure function) that modify a todo by togling its completion state
28+
*/
1729
todos.addTransition("toggleTodo",(index)=>{
1830
let todo = todos.value[index]
1931
todo.isComplete = todo.isComplete ? false : true

demo/index.html

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
3+
4+
<head>
45
<meta charset="utf-8">
56
<meta name="viewport" content="width=device-width, initial-scale=1">
67
<title>Todo app test</title>
78
<script type="module" src="./webComponents.js"></script>
8-
</head>
9-
<body>
10-
<my-container>
11-
<my-input slot="head"></my-input>
12-
</my-container>
13-
</body>
9+
10+
<style>
11+
.flex {
12+
display: flex;
13+
height: 98vh;
14+
flex-direction: column;
15+
justify-content: space-between;
16+
max-width: 30rem;
17+
min-width: 15rem;
18+
margin: 0 auto;
19+
box-sizing: border-box;
20+
}
21+
22+
.vacuum {
23+
height: 1rem;
24+
}
25+
26+
.footer {
27+
align-self: flex-end;
28+
}
29+
</style>
30+
31+
</head>
32+
33+
<body>
34+
<div class="flex">
35+
<div class="vacuum"></div>
36+
37+
<my-container>
38+
<my-input slot="head"></my-input>
39+
</my-container>
40+
41+
<div class="footer">
42+
<span>
43+
Powered by <a href="https://github.com/WebComponentHelpers/Brick">Brick-Element</a> and <a href="https://bulma.io/">Bulma</a>.
44+
</span>
45+
</div>
46+
</div>
47+
</body>
48+
1449
</html>

demo/litDemo.html

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>Todo app test</title>
8+
<script type="module" src="./litWebComponents.js"></script>
9+
10+
<style>
11+
.flex {
12+
display: flex;
13+
height: 98vh;
14+
flex-direction: column;
15+
justify-content: space-between;
16+
max-width: 30rem;
17+
min-width: 15rem;
18+
margin: 0 auto;
19+
box-sizing: border-box;
20+
}
21+
22+
.vacuum {
23+
height: 1rem;
24+
}
25+
26+
.footer {
27+
align-self: flex-end;
28+
}
29+
</style>
30+
31+
</head>
32+
33+
<body>
34+
<div class="flex">
35+
<div class="vacuum"></div>
36+
37+
<my-container>
38+
<my-input slot="head"></my-input>
39+
</my-container>
40+
41+
<div class="footer">
42+
<span>
43+
Powered by <a href="https://lit-element.polymer-project.org/">LitElement</a> and <a href="https://bulma.io/">Bulma</a>.
44+
</span>
45+
</div>
46+
</div>
47+
</body>
48+
49+
</html>

demo/litWebComponents.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import {todos} from './Store.js'
2+
import {LitElement, css, html, unsafeCSS} from 'https://unpkg.com/lit-element?module';
3+
import {litStatesMixin} from '../build/impera.js'
4+
5+
6+
async function with_bulma(){
7+
// fetching bulma - is a bit dirty, but hey is just a demo ;)
8+
let response = await fetch('https://cdn.jsdelivr.net/npm/bulma@0.8.0/css/bulma.min.css');
9+
let txt = await response.text();
10+
const bulma_lit = css`${unsafeCSS(txt)}`
11+
12+
/**
13+
* Input field, note the mixin applied to class LitElement
14+
*/
15+
class myInput extends litStatesMixin([todos],LitElement){
16+
static get styles() { return bulma_lit; }
17+
18+
render(){
19+
return html`
20+
<div class="columns is-centered">
21+
<div class="field has-addons ">
22+
<div class="control">
23+
<input id="inpt" class="input" type="text" placeholder="Todo Text">
24+
</div>
25+
<div class="control">
26+
<a class="button is-info" @click="${this.add_todos}"> Add Todo </a>
27+
</div>
28+
</div>
29+
</div>
30+
`;
31+
}
32+
33+
constructor(){
34+
super();
35+
this.onkeydown = (function (e){ if(e.which === 13) this.add_todos()}).bind(this)
36+
}
37+
38+
add_todos(){
39+
let inpt = this.shadowRoot.querySelector("#inpt")
40+
if(inpt.value !== ""){
41+
this.applyTransition("addTodo",inpt.value);
42+
inpt.value = "";
43+
}
44+
}
45+
}
46+
47+
customElements.define("my-input",myInput);
48+
49+
/**
50+
* Todo element, this is a simple tag to change state of each single todo.
51+
* To change state we use ApplyTransition.
52+
*/
53+
class myTodo extends litStatesMixin([todos],LitElement){
54+
static get styles() {
55+
return [bulma_lit, css`#status {cursor:pointer;}`];
56+
}
57+
static get properties() {
58+
return {
59+
index: Number,
60+
iscomplete : Boolean,
61+
txt : String
62+
};
63+
}
64+
render(){
65+
return html`
66+
<div class="tags has-addons is-centered" style="margin-bottom:0rem">
67+
<span id="status" class="tag ${this.iscomplete === "true"? 'is-success' : 'is-warning'}" @click="${this.toggle}">
68+
${this.iscomplete === "true" ? "Done" : "Pending"}
69+
</span>
70+
<span class="tag is-info">${this.txt}</span>
71+
<a class="tag is-delete is-light" @click="${this.remove}"></a>
72+
</div>
73+
`;
74+
}
75+
toggle(){
76+
this.applyTransition("toggleTodo",this.index);
77+
}
78+
remove(){
79+
this.applyTransition('removeTodo',this.index)
80+
}
81+
}
82+
83+
customElements.define("my-todo",myTodo);
84+
85+
/**
86+
* Container class that gets updated each time the todos State variable changes or any transition is fired.
87+
* It shows a list of todos.
88+
*/
89+
class myContainer extends litStatesMixin([todos],LitElement){
90+
static get styles() {
91+
return [bulma_lit, css`.withmargin{margin-top:2rem;}`];
92+
}
93+
render(){
94+
return html`
95+
<div class="container">
96+
<span class="subtitle is-4 has-text-info"> ImperaJs</span> <span class="subtitle is-4">Todo App</span>
97+
<div class ="box" style="padding:3rem">
98+
<slot name="head"></slot>
99+
<div class="withmargin" >
100+
<div> ${this.todos.map((todo,index)=> html`<my-todo index="${index}" txt="${todo.txt}" iscomplete="${todo.isComplete}"></my-todo>`)}</div>
101+
</div>
102+
</div>
103+
</div>
104+
`;
105+
}
106+
}
107+
customElements.define("my-container",myContainer);
108+
109+
};
110+
111+
with_bulma();

demo/webComponents.js

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@ async function with_bulma(){
1212
// dummy class with state mixin
1313
class StateElement extends statesMixin([todos],HTMLElement){}
1414

15+
1516
let mxn_input = html`
16-
${bulma}
17+
${bulma}
1718
18-
<div class="columns is-centered">
19+
<div class="columns is-centered">
1920
<div class="field has-addons ">
20-
<div class="control">
21-
<input ${"#-inpt"}class="input" type="text" placeholder="Todo Text">
22-
</div>
23-
<div class="control">
24-
<a ${"#-btn"} class="button is-info">
25-
Add Todo
26-
</a>
27-
</div>
28-
</div>
21+
<div class="control">
22+
<input ${"#-inpt"}class="input" type="text" placeholder="Todo Text">
23+
</div>
24+
<div class="control">
25+
<a ${"#-btn"} class="button is-info"> Add Todo </a>
26+
</div>
2927
</div>
28+
</div>
3029
`;
31-
30+
/**
31+
* Input field, note we inherith from StateElement classs
32+
*/
3233
dfn("my-input", class extends mxn_input(StateElement){
3334
constructor(){
3435
super();
@@ -47,21 +48,25 @@ dfn("my-input", class extends mxn_input(StateElement){
4748

4849

4950
let mxn_todo = html`
50-
${bulma}
51-
<style>
52-
#status {
53-
cursor:pointer;
54-
}
55-
</style>
51+
${bulma}
52+
<style>
53+
#status {
54+
cursor:pointer;
55+
}
56+
</style>
5657
57-
<div class="tags has-addons is-centered" style="margin-bottom:0rem">
58-
<span ${"#-status"} class="tag is-warning" onclick="this.root.toggle()">Pending</span>
59-
<span ${"#-text"} class="tag is-info">Todo text</span>
60-
<a class="tag is-delete is-light" onclick="this.root.applyTransition('removeTodo',this.root.index)"></a>
61-
</div>
62-
${"|*index|iscomplete|txt*|"}
58+
<div class="tags has-addons is-centered" style="margin-bottom:0rem">
59+
<span ${"#-status"} class="tag is-warning" onclick="this.root.toggle()">Pending</span>
60+
<span ${"#-text"} class="tag is-info">Todo text</span>
61+
<a class="tag is-delete is-light" onclick="this.root.applyTransition('removeTodo',this.root.index)"></a>
62+
</div>
63+
64+
${"|*index|iscomplete|txt*|"}
6365
`;
64-
66+
/**
67+
* Todo element, this is a simple tag to change state of each single todo.
68+
* To change state we use ApplyTransition.
69+
*/
6570
dfn("my-todo", class extends mxn_todo(StateElement){
6671
update_txt(val){
6772
this.ids.text.innerText = val;
@@ -85,26 +90,26 @@ dfn("my-todo", class extends mxn_todo(StateElement){
8590
});
8691

8792
let mxn_container = html`
88-
${bulma}
89-
<style>
90-
div.container{
91-
margin:20% auto;
92-
max-width: 30rem;
93-
}
94-
.withmargin{
95-
margin-top:2rem;
96-
}
97-
</style>
98-
<div class="container">
93+
${bulma}
94+
<style>
95+
.withmargin{
96+
margin-top:2rem;
97+
}
98+
</style>
99+
<div class="container">
99100
<span class="subtitle is-4 has-text-info"> ImperaJs</span> <span class="subtitle is-4">Todo App</span>
100101
<div class ="box" style="padding:3rem">
101-
<slot name="head"></slot>
102-
<div class="withmargin" >
103-
<div ${"#-content"}></div>
104-
</div>
105-
</div>
102+
<slot name="head"></slot>
103+
<div class="withmargin" >
104+
<div ${"#-content"}></div>
105+
</div>
106106
</div>
107+
</div>
107108
`;
109+
/**
110+
* Container class that gets updated each time the todos State variable changes, this includes stateTransitions.
111+
* It shows a list of todos.
112+
*/
108113
dfn("my-container", class extends mxn_container(StateElement){
109114
on_todos_update(val){
110115
let todos_content = this.ids.content

0 commit comments

Comments
 (0)