-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo2d.js
79 lines (68 loc) · 1.44 KB
/
demo2d.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const { patch, h } = window.snabbdom;
function mount(id, component) {
let el = document.getElementById(id);
const render = component(update);
function update() {
el = patch(el, render());
}
update();
}
function counter(update) {
let num = 0;
function incr(d) {
num += d;
update();
}
return () =>
h('span', { class:{ red: num < 0 }}, [
button('-', [incr, -1]),
" ",
button('+', [incr, 1]),
` Count: ${num} `,
])
}
function test(update) {
return () => h('span', 'Test!')
}
function button(title, op) {
return h('button', { on:{ click:op }}, title)
}
function genericList(create) {
return update => {
const components = [];
function addItem() {
components.push(create(update));
update();
}
function delItem(i) {
components.splice(i, 1);
update();
}
return () =>
h('div', [
button('Add', addItem),
h('ul', components.map((c, i) =>
h('li', {
key: c,
style: {
transition: 'opacity 0.5s, transform 0.5s, max-height 0.5s',
opacity: 0, transform: 'translateX(200px)', maxHeight: '0',
delayed: {
opacity: 1, transform: 'translateX(0px)', maxHeight: '50px',
},
remove: {
opacity: 0, transform: 'translateX(-200px)', maxHeight:0,
},
}
}, [
button('Del', [delItem, i]),
' ',
c(),
])
)
),
])
}
}
mount('cnt1', genericList(counter));
mount('cnt2', genericList(test));