-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
134 lines (114 loc) · 2.94 KB
/
index.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { text, fragment, watch } from '../core'
import { h1, a, p, code, h2, input, label, br, button } from '../helpers'
console.log('ts')
console.time('init')
document.body.appendChild(
h1([
'dom-proxy demo',
a({ textContent: 'git', href: 'https://github.com/beenotung/dom-proxy' }),
a({ textContent: 'npm', href: 'https://www.npmjs.com/package/dom-proxy' }),
]).node, // get the native element from .node property
// (only necessary when not wrapped by fragment helper function)
)
document.body.appendChild(
p([
"This interactive page is created entirely in Typescript using dom-proxy's creation helper functions and auto-tracking ",
code({ textContent: 'watch()' }),
' function.',
]).node,
)
let upTimeText = text(0)
let startTime = Date.now()
setInterval(() => {
upTimeText.textContent = ((Date.now() - startTime) / 1000).toFixed(0)
}, 500)
document.body.appendChild(
fragment([h2({ textContent: 'up time' }), upTimeText, ' seconds']),
)
let nameInput = input({
placeholder: 'guest',
id: 'visitor-name',
})
let nameText = text()
let greetDotsText = text()
// the read-dependencies are tracked automatically
watch(
() => {
nameText.textContent = nameInput.value || nameInput.placeholder
},
{ listen: 'change' },
)
watch(() => {
let n = +upTimeText.textContent! % 5
greetDotsText.textContent = '.'.repeat(n)
})
document.body.appendChild(
// use a DocumentFragment to contain the elements
fragment([
h2({ textContent: 'change event demo' }),
label({ textContent: 'name: ', htmlFor: nameInput.id }),
nameInput,
p(['hello, ', nameText, greetDotsText]),
]),
)
let aInput = input({ type: 'number', value: '0' })
let bInput = input({
type: 'number',
value: '0',
readOnly: true,
disabled: true,
})
let cInput = input({
type: 'number',
value: '0',
readOnly: true,
disabled: true,
})
watch(() => {
bInput.value = upTimeText.textContent!
})
watch(() => {
cInput.value = String(aInput.valueAsNumber + bInput.valueAsNumber)
})
let aText = text()
let bText = text()
let cText = text()
watch(() => (aText.textContent = String(aInput.valueAsNumber)))
watch(() => (bText.textContent = String(bInput.valueAsNumber)))
watch(() => (cText.textContent = String(cInput.valueAsNumber)))
let resetButton = button({ textContent: 'reset', onclick: reset })
watch(() => {
resetButton.disabled = aInput.valueAsNumber === 0
})
function reset() {
aInput.value = '0'
}
document.body.appendChild(
fragment([
h2({ textContent: 'input event demo' }),
aInput,
' + ',
bInput,
' = ',
cInput,
br(),
aText,
' + ',
bText,
' = ',
cText,
br(),
resetButton,
]),
)
document.body.appendChild(
fragment([
h2({ textContent: 'more demo' }),
a({ href: 'signup.html', textContent: 'signup.html' }),
', ',
a({ href: 'hybrid.html', textContent: 'hybrid.html' }),
', ',
a({ href: 'style.html', textContent: 'style.html' }),
]),
)
console.timeEnd('init')