-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
162 lines (148 loc) · 3.94 KB
/
index.html
File metadata and controls
162 lines (148 loc) · 3.94 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!doctype html>
<html>
<head>
<title>WebComponents, Yo!</title>
<link rel="import" href="./com/hello-label.html">
<link rel="import" href="./com/seconds-elapsed.html">
<link rel="import" href="./com/todo-app.html">
<link rel="import" href="./com/markdown-editor.html">
</head>
<body>
<h1>WebComponents, Yo! (WCYO)</h1>
<p>This page contains my first attempts to use <a href="https://npm.im/yo-yo">Yo-Yo</a> with Web Components. My goal is to create something that's as easy-to-use as React, but that's much more minimal and native.</p>
<p><a href="https://github.com/pfrazee/webcomponents-yo">page repo</a></p>
<hr>
<h2><code>hello-label</code></h2>
<pre>
window.customElements.define('hello-label', class extends HTMLYoYo {
render() {
return yo`<div>Hello ${this.getAttribute('label')}</div>`
}
})
</pre>
<section>
<hello-label label="Jane"></hello-label>
</section>
<br>
<hr>
<h2><code>seconds-elapsed</code></h2>
<pre>
window.customElements.define('seconds-elapsed', class extends HTMLYoYo {
static get observedAttributes() {
return ['tick']
}
connectedCallback() {
this.state.tick = 0
this.interval = setInterval(() => {
this.state.tick++
}, 1000)
}
disconnectedCallback() {
clearInterval(this.interval)
}
render() {
return yo`<div>Seconds Elapsed: ${this.getAttribute('tick')}</div>`
}
})
</pre>
<section>
<seconds-elapsed></seconds-elapsed>
</section>
<br>
<hr>
<h2><code>todo-app</code></h2>
<pre>
window.customElements.define('todo-app', class extends HTMLYoYo {
constructor() {
super()
this.handleChange = this.handleChange.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
this.state.items = []
this.state.text = ''
}
static get observedAttributes() {
return ['items', 'text']
}
render() {
return yo`
<div>
<h3>TODO</h3>
<todo-list items=${this.getAttribute('items')}></todo-list>
<form onsubmit=${this.handleSubmit}>
<input onchange=${this.handleChange} value=${this.state.text} />
<button>${'Add #' + (this.state.items.length + 1)}</button>
</form>
</div>
`
}
handleChange(e) {
this.state.text = e.target.value
}
handleSubmit(e) {
e.preventDefault()
var newItem = {
text: this.state.text,
id: Date.now()
}
this.state.items = this.state.items.concat([newItem])
this.state.text = ''
}
})
window.customElements.define('todo-list', class extends ThirdBase {
constructor() {
super()
}
static get observedAttributes() {
return ['items']
}
render() {
const items = this.state.items || []
return yo`
<ul>
${items.map(item => yo`
<li key=${item.id}>${item.text}</li>
`)}
</ul>
`
}
})
</pre>
<section>
<todo-app></todo-app>
</section>
<hr>
<h2><code>markdown-editor</code></h2>
<pre>
window.customElements.define('markdown-editor', class extends HTMLYoYo {
constructor() {
super()
this.handleChange = this.handleChange.bind(this);
this.state.value = 'Type some *markdown* here!'
}
static get observedAttributes() {
return ['value']
}
handleChange(e) {
this.state.value = e.target.value
}
getRawMarkup() {
var md = new Remarkable()
return md.render(this.state.value)
}
render() {
var el = yo`
<div class="markdown-editor">
<h3>Input</h3>
<textarea onchange=${this.handleChange}>${this.state.value}</textarea>
<h3>Output</h3>
<div class="content"></textarea>
</div>
`
el.querySelector('.content').innerHTML = this.getRawMarkup()
return el
}
})
</pre>
<markdown-editor></markdown-editor>
</body>
</html>