-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestPageUsingArrowJsReactivity_GooberFeatures.html
More file actions
416 lines (358 loc) · 14.1 KB
/
TestPageUsingArrowJsReactivity_GooberFeatures.html
File metadata and controls
416 lines (358 loc) · 14.1 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>SSE client using Arrow.js Reactivity and goober</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css" />
</head>
<body>
<main class="container">
<div class="grid">
<div id="app"></div>
<div>
<table>
<thead>
<tr>
<th scope="col">Date</th>
<th scope="col">Weather</th>
<th scope="col">°C</th>
<th scope="col">°F</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
</div>
</div>
</main>
<script type="module">
import { reactive, html } from "https://esm.sh/@arrow-js/core";
import { css, keyframes, glob } from "https://esm.sh/goober";
import { createGlobalStyles } from "https://esm.sh/goober/global";
/*
goober feature: createGlobalStyles
----------------------------------
The official goober API exposes createGlobalStyles from "goober/global".
It is mainly designed for component-tree renderers such as React/Preact,
where you would render <GlobalStyles /> near the top of the tree.
This page is deliberately plain JavaScript + Arrow.js, not React/Preact,
so the live global CSS below uses glob(...), which is the direct vanilla
equivalent for injecting global rules.
*/
const GlobalStyles = createGlobalStyles`
body {
margin: 0;
}
`;
// goober feature: global styles for plain JavaScript pages.
// Theme switching is handled using CSS custom properties on body[data-theme-mode].
glob`
:root {
color-scheme: light;
--page-bg: #f6f8fb;
--panel-bg: #ffffff;
--panel-border: #d7dee8;
--text-colour: #18202a;
--muted-colour: #5f6c7b;
--shadow-colour: rgba(15, 23, 42, 0.12);
--table-row-hover: #eef4ff;
--cold-bg: #e3f2fd;
--cold-border: #2196f3;
--mild-bg: #e8f5e9;
--mild-border: #4caf50;
--warm-bg: #fbe9e7;
--warm-border: #ff5722;
--button-bg: #263f73;
--button-text: #ffffff;
}
body[data-theme-mode="dark"] {
color-scheme: dark;
--page-bg: #0f172a;
--panel-bg: #111827;
--panel-border: #334155;
--text-colour: #e5e7eb;
--muted-colour: #a6adbb;
--shadow-colour: rgba(0, 0, 0, 0.45);
--table-row-hover: #1e293b;
--cold-bg: #102a43;
--cold-border: #60a5fa;
--mild-bg: #123524;
--mild-border: #4ade80;
--warm-bg: #431b12;
--warm-border: #fb923c;
--button-bg: #7dd3fc;
--button-text: #082f49;
}
html, body {
min-height: 100%;
}
body {
background: var(--page-bg);
color: var(--text-colour);
transition: background 220ms ease, color 220ms ease;
}
main.container {
padding-top: 2rem;
}
h2 {
display: flex;
gap: 0.65rem;
align-items: center;
margin-bottom: 0.35rem;
}
ul {
list-style: none;
padding-left: 0;
}
table {
background: var(--panel-bg);
border: 1px solid var(--panel-border);
border-radius: 14px;
overflow: hidden;
box-shadow: 0 8px 22px var(--shadow-colour);
}
th, td {
color: var(--text-colour);
}
`;
// goober feature: reusable keyframe animations.
const pulseGlow = keyframes`
0%, 100% {
transform: scale(1);
filter: brightness(1);
}
50% {
transform: scale(1.12);
filter: brightness(1.25);
}
`;
const slideIn = keyframes`
from {
opacity: 0;
transform: translateY(8px);
}
to {
opacity: 1;
transform: translateY(0);
}
`;
const weatherIconBounce = keyframes`
0%, 100% {
transform: translateY(0);
}
50% {
transform: translateY(-3px);
}
`;
const state = reactive({
connected: false,
theme: localStorage.getItem("weather-demo-theme") || "light",
forecasts: []
});
document.body.dataset.themeMode = state.theme;
function toggleTheme() {
state.theme = state.theme === "light" ? "dark" : "light";
document.body.dataset.themeMode = state.theme;
localStorage.setItem("weather-demo-theme", state.theme);
}
function getWeatherIcon(forecast) {
const summary = (forecast.Summary || "").toLowerCase();
const temp = forecast.TemperatureC;
if (summary.includes("freezing") || temp <= 0) return "❄️";
if (summary.includes("chilly") || summary.includes("cool") || temp < 10) return "🧥";
if (summary.includes("mild")) return "🌤️";
if (summary.includes("warm") || summary.includes("balmy")) return "☀️";
if (summary.includes("hot") || summary.includes("sweltering") || summary.includes("scorching") || temp >= 28) return "🔥";
return "🌡️";
}
function getTemperatureBand(forecast) {
if (forecast.TemperatureC < 10) return "cold";
if (forecast.TemperatureC < 20) return "mild";
return "warm";
}
const classCache = new Map();
function cachedClass(key, factory) {
if (!classCache.has(key)) {
classCache.set(key, factory());
}
return classCache.get(key);
}
const panelClass = css`
background: var(--panel-bg);
border: 1px solid var(--panel-border);
border-radius: 14px;
padding: 1rem;
box-shadow: 0 8px 22px var(--shadow-colour);
`;
const toolbarClass = css`
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.75rem;
margin-bottom: 0.75rem;
`;
const themeButtonClass = css`
border: 0;
border-radius: 999px;
background: var(--button-bg);
color: var(--button-text);
padding: 0.45rem 0.75rem;
cursor: pointer;
font-size: 0.9rem;
width: auto;
&:hover {
transform: translateY(-1px);
box-shadow: 0 6px 14px var(--shadow-colour);
}
`;
const mutedTextClass = css`
color: var(--muted-colour);
margin-top: 0;
`;
const emptyStateClass = css`
color: var(--muted-colour);
border: 1px dashed var(--panel-border);
border-radius: 12px;
padding: 0.85rem;
margin-top: 1rem;
`;
const iconClass = css`
display: inline-block;
font-size: 1.5rem;
animation: ${weatherIconBounce} 900ms ease-in-out infinite;
`;
function ledClass(connected) {
return cachedClass(`led-${connected}`, () => css`
display: inline-block;
width: 18px;
height: 18px;
border-radius: 50%;
position: relative;
border: 1px solid rgba(0,0,0,0.4);
background: ${connected
? "radial-gradient(circle at 30% 30%, #aaffaa 0%, #55ff55 20%, #11bb11 60%, #006600 100%)"
: "radial-gradient(circle at 30% 30%, #ffaaaa 0%, #ff5555 20%, #cc1111 60%, #770000 100%)"};
box-shadow: ${connected
? "inset 0 -2px 4px rgba(0,0,0,0.35), inset 0 2px 4px rgba(255,255,255,0.2), 0 0 10px #00ff00, 0 0 20px rgba(0,255,0,0.6)"
: "inset 0 -2px 4px rgba(0,0,0,0.35), inset 0 2px 4px rgba(255,255,255,0.2), 0 0 10px #ff0000, 0 0 20px rgba(255,0,0,0.6)"};
animation: ${connected ? `${pulseGlow} 1.4s ease-in-out infinite` : "none"};
&::before {
content: '';
position: absolute;
top: 2px;
left: 3px;
width: 8px;
height: 5px;
border-radius: 50%;
background: rgba(255,255,255,0.75);
transform: rotate(-20deg);
}
`);
}
function forecastClass(forecast) {
const band = getTemperatureBand(forecast);
return cachedClass(`forecast-${band}`, () => css`
padding: 10px;
margin-bottom: 8px;
border-left: 6px solid var(--${band}-border);
background: var(--${band}-bg);
border-radius: 10px;
animation: ${slideIn} 240ms ease-out both;
transition: transform 180ms ease, box-shadow 180ms ease, background 220ms ease;
&:hover {
transform: translateX(8px);
box-shadow: 0 6px 16px var(--shadow-colour);
}
`);
}
function tableRowClass(forecast) {
const band = getTemperatureBand(forecast);
return cachedClass(`table-row-${band}`, () => css`
border-left: 5px solid var(--${band}-border);
animation: ${slideIn} 240ms ease-out both;
&:hover {
background: var(--table-row-hover);
}
`);
}
const alignCentreClass = css`
text-align: center;
`;
const alignRightClass = css`
text-align: right;
`;
function addSampleForecast() {
const samples = [
{ Date: "2026-05-23", TemperatureC: 6, TemperatureF: 43, Summary: "Chilly" },
{ Date: "2026-05-24", TemperatureC: 15, TemperatureF: 59, Summary: "Mild" },
{ Date: "2026-05-25", TemperatureC: 24, TemperatureF: 75, Summary: "Warm" },
{ Date: "2026-05-26", TemperatureC: 30, TemperatureF: 86, Summary: "Hot" }
];
state.forecasts.push(samples[state.forecasts.length % samples.length]);
}
const view = html`
<div class="${panelClass}">
<div class="${toolbarClass}">
<div>
<h2>
Weather stream
<span class="${() => ledClass(state.connected)}"
title="${() => state.connected ? "Connected" : "Disconnected"}"></span>
</h2>
<p class="${mutedTextClass}">
${() => state.connected ? "Connected to SSE" : "Disconnected from SSE"}
</p>
</div>
<button type="button" class="${themeButtonClass}" @click="${toggleTheme}">
${() => state.theme === "light" ? "Dark mode" : "Light mode"}
</button>
</div>
<button type="button" class="${themeButtonClass}" @click="${addSampleForecast}">
Add sample forecast
</button>
${() => state.forecasts.length === 0
? html`<p class="${emptyStateClass}">Waiting for weather events...</p>`
: html`
<ul>
${() => state.forecasts.map(forecast => html`
<li class="${forecastClass(forecast)}">
<span class="${iconClass}">
${getWeatherIcon(forecast)}
</span>
<strong>${forecast.Date}</strong>
${forecast.TemperatureC}°C
/
${forecast.TemperatureF}°F
${forecast.Summary ? `- ${forecast.Summary}` : ""}
</li>
`)}
</ul>
`}
</div>
`;
view(document.getElementById("app"));
const tableBody = html`
${() => state.forecasts.map(forecast => html`
<tr class="${tableRowClass(forecast)}">
<td>${forecast.Date}</td>
<td class="${alignCentreClass}">${getWeatherIcon(forecast)}</td>
<td class="${alignRightClass}">${forecast.TemperatureC}°C</td>
<td class="${alignRightClass}">${forecast.TemperatureF}°F</td>
</tr>
`)}
`;
tableBody(document.getElementById("tbody"));
const source = new EventSource("https://localhost:7017/weatherforecastevents");
source.onopen = () => {
state.connected = true;
};
source.onerror = () => {
state.connected = false;
};
source.addEventListener("weather", event => {
const forecast = JSON.parse(event.data);
state.forecasts.push(forecast);
});
</script>
</body>
</html>