-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature-webcomponent.html
More file actions
200 lines (173 loc) · 5.88 KB
/
Copy pathfeature-webcomponent.html
File metadata and controls
200 lines (173 loc) · 5.88 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Select UI — Web Components</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="importmap">
{"imports": {"@./": "../src/js/select/" } }
</script>
<style>
body {
font-family: system-ui, sans-serif;
padding: 20px;
max-width: 840px;
margin: 0 auto;
}
.layout {
display: grid;
gap: 16px;
}
.panel {
border: 1px solid #ddd;
border-radius: 8px;
padding: 12px;
background: #fff;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 10px;
}
button {
padding: 6px 10px;
border: 1px solid #bbb;
border-radius: 4px;
background: #f8f8f8;
cursor: pointer;
}
button:hover {
background: #f0f0f0;
}
x-ui-counter,
x-pure-badge,
x-ui-stepper {
display: block;
margin-top: 12px;
}
.readout {
margin-top: 12px;
padding: 10px 12px;
border-radius: 8px;
background: #f6f8fb;
font-family: ui-monospace, monospace;
}
</style>
</head>
<body>
<h1>Web Components</h1>
<p>This example registers both a Select UI custom element and a pure render-function custom element using <code>webcomponent()</code>.</p>
<div class="layout">
<section class="panel">
<h2>Select UI component</h2>
<x-ui-counter id="counter" title="Web Counter" count="2"></x-ui-counter>
<div class="controls">
<button id="counter-dec">count -1 (attribute)</button>
<button id="counter-inc">count +1 (attribute)</button>
<button id="counter-title">toggle title</button>
</div>
</section>
<section class="panel">
<h2>Pure render function component</h2>
<x-pure-badge id="badge" label="Ready" tone="info"></x-pure-badge>
<div class="controls">
<button id="badge-tone">toggle tone</button>
<button id="badge-label">toggle label</button>
</div>
</section>
<section class="panel">
<h2>Parent Pub/Sub Rebinding</h2>
<p>A wrapped kebab-case custom element inside a Select template now inherits <code>ui-parent</code> automatically. Use explicit <code>ui-parent</code> only when mounting the element outside the Select tree.</p>
<div id="event-demo"></div>
</section>
</div>
<script type="module">
import ui, { webcomponent } from "@./ui.js"
const Counter = ui(`
<section style="display:grid;gap:8px;padding:10px;border:1px solid #ccd5ff;border-radius:8px;background:#f7f9ff;">
<strong out="title">Counter</strong>
<div>
<button on:click="dec">-</button>
<span out="count" style="padding:0 8px;">0</span>
<button on:click="inc">+</button>
</div>
</section>
`).does({
title: (_self, { title }) => title,
count: (_self, { count }) => count ?? 0,
dec: (self, { count }) => self.update({ count: (count ?? 0) - 1 }),
inc: (self, { count }) => self.update({ count: (count ?? 0) + 1 }),
})
webcomponent("x-ui-counter", Counter, { title: "Counter", count: 0 })
const Stepper = ui(`
<section style="display:grid;gap:8px;padding:10px;border:1px solid #cbe7d8;border-radius:8px;background:#f5fff8;">
<strong out="label">Stepper</strong>
<div>
<button on:click="decrement">-</button>
<span out="count" style="padding:0 8px;">0</span>
<button on:click="increment">+</button>
</div>
</section>
`).does({
label: (_self, { label }) => label ?? "Stepper",
count: (_self, { count }) => count ?? 0,
decrement: (self, { count }) => self.pub("Step", -Math.max(1, Number(count) || 0)),
increment: (self, { count }) => self.pub("Step", Math.max(1, Number(count) || 0)),
})
webcomponent("x-ui-stepper", Stepper, { label: "Nested Stepper", count: 2 })
const PureBadge = ({ label, tone }) => {
const root = document.createElement("div")
root.style.display = "inline-flex"
root.style.alignItems = "center"
root.style.gap = "8px"
root.style.padding = "8px 12px"
root.style.borderRadius = "999px"
root.style.border = "1px solid #d0d0d0"
root.style.background = tone === "warn" ? "#fff7e0" : "#e8f2ff"
const dot = document.createElement("span")
dot.style.width = "10px"
dot.style.height = "10px"
dot.style.borderRadius = "50%"
dot.style.background = tone === "warn" ? "#d97706" : "#2563eb"
const text = document.createElement("strong")
text.textContent = label ?? "Badge"
root.appendChild(dot)
root.appendChild(text)
return root
}
webcomponent("x-pure-badge", PureBadge, { label: "Ready", tone: "info" })
const EventDemo = ui(`
<section style="display:grid;gap:12px;">
<x-ui-stepper label="Nested Stepper" count="2"></x-ui-stepper>
<div class="readout">Parent total: <strong out="total">0</strong></div>
</section>
`).sub({
Step: (_self, { total }, event) => ({ total: (total ?? 0) + event.data }),
})
EventDemo.new().set({ total: 10 }).mount("#event-demo")
const counter = document.getElementById("counter")
document.getElementById("counter-dec").addEventListener("click", () => {
const count = Number(counter.getAttribute("count") || 0)
counter.setAttribute("count", `${count - 1}`)
})
document.getElementById("counter-inc").addEventListener("click", () => {
const count = Number(counter.getAttribute("count") || 0)
counter.setAttribute("count", `${count + 1}`)
})
document.getElementById("counter-title").addEventListener("click", () => {
const next = counter.getAttribute("title") === "Web Counter" ? "Counter via Attribute" : "Web Counter"
counter.setAttribute("title", next)
})
const badge = document.getElementById("badge")
document.getElementById("badge-tone").addEventListener("click", () => {
const next = badge.getAttribute("tone") === "warn" ? "info" : "warn"
badge.setAttribute("tone", next)
})
document.getElementById("badge-label").addEventListener("click", () => {
const next = badge.getAttribute("label") === "Ready" ? "Needs Attention" : "Ready"
badge.setAttribute("label", next)
})
</script>
</body>
</html>