-
Notifications
You must be signed in to change notification settings - Fork 10
/
example.html
231 lines (221 loc) · 5.44 KB
/
example.html
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<!doctype html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
<style>
:root {
font-family: Roboto;
margin-top: 16px;
}
body {
padding: 8px;
margin: 8px auto;
max-width: 800px;
}
h1,h2,h3,h4,h5,h6 {
margin: 0;
}
.flex {
display: flex;
}
.items-center {
align-items: center;
}
.items-end {
align-items: flex-end;
}
.muted {
opacity: 0.5;
}
.btn {
background: gainsboro;
border: none;
border-bottom: 1px solid #1221;
border-radius: 4px;
cursor: pointer;
font-family: Roboto;
font-weight: 500;
padding: 0.5em 1em;
transition: filter 0.2s;
}
.btn:hover {
filter: brightness(85%) contrast(133%);
}
.btn.primary {
background: royalblue;
color: white;
}
.badge {
border-radius: 10px;
font-size: 11px;
font-weight: 500;
padding: 0.2em 0.8em;
background: #ccc;
}
.ml-4 {
margin-left: 4px;
}
.mr-4 {
margin-right: 4px;
}
</style>
</head>
<body>
<h1>Todo list</h1>
<todo-items></todo-items>
</body>
<script type="module">
import { El } from './el.js'
const store = El.observable({
items: [
{ id: key(), title: "buy milk" },
{ id: key(), title: "buy bread" },
{ id: key(), title: "buy eggs" },
],
})
class TodoItem extends El {
editItem() {
const title = window.prompt("Enter title", this.item.title)
if (title !== null) this.item.title = title
}
toggleDone() {
this.item.done = !this.item.done
}
deleteItem() {
const index = store.items.findIndex(i => i.id == this.item.id)
store.items.splice(index, 1)
}
render(html) {
return html`
<span class="status-column">
${this.item.done
? html`<span class="badge success">Done!</span>`
: html`<span class="badge">New</span>`
}
</span>
<span>
<input
type="checkbox"
${this.item.done && 'checked'}
onclick=${this.toggleDone}>
</span>
<span><p class="muted">${this.item.id}</p></span>
<span class="title-column ${this.item.done && 'title--completed'}">
<h3>${this.item.title}</h3>
</span>
<span class="tools-column">
<button class="btn" onclick=${this.editItem}>Edit</button>
<button class="btn delete muted" title="Delete item" onclick=${this.deleteItem}>×</button>
</span>
`
}
styles(css) {
return css`
:host {
display: table-row;
&(:hover) {
background-color: #8ff4;
}
> span {
display: table-cell;
padding: 0.5em;
border-bottom: 1px solid #2232;
}
}
.btn.delete {
background: transparent;
box-shadow: 0 0 0 1px #1234;
margin-left: 2px;
}
.btn.delete:hover {
background: crimson;
color: white;
box-shadow: none;
}
.badge.success {
background: mediumseagreen;
color: white;
}
.title-column {
width: 100%;
}
.status-column {
min-width: 3em;
}
.title--completed {
opacity: 0.5;
text-decoration: line-through;
}
.tools-column {
white-space: nowrap;
}
input[type=checkbox] {
position: relative;
top: 2px;
}
`
}
}
class TodoItems extends El {
addItem() {
const title = window.prompt("Enter title")
if (title === null) return
store.items.push({
id: key(),
title,
})
}
toggleHideCompleted(e) {
store.hideCompleted = e.target.checked
}
filteredItems() {
return store.items.filter(i => store.hideCompleted ? !i.done : true)
}
render(html) {
const items = this.filteredItems()
return html`
<div class="items-container">
<header class="flex items-center">
<h4 class="flex items-center">
Items
<span class="badge ml-4 mr-4">${items.length}</span>
</h4>
<label class="ml-4 flex items-end">
<input type="checkbox" onclick=${this.toggleHideCompleted}>
<small class="muted">Hide completed</small>
</label>
<button class="btn primary" onclick=${this.addItem}>Add item</button>
</header>
<div class="items">
${items.map(item => html`
<todo-item key="${item.id}" item=${item}></todo-item>
`)}
</div>
</div>
`
}
styles(css) {
return css`
.items-container {
display: table;
margin-top: 16px;
max-width: 600px;
width: 100%;
}
header {
background: whitesmoke;
box-shadow: 0 1px 1px #1232;
padding: 16px 8px;
> *:last-child {
margin-left: auto;
}
}
`
}
}
customElements.define('todo-item', TodoItem)
customElements.define('todo-items', TodoItems)
function key() {
return Math.random().toString(36).slice(2,8)
}
</script>