-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmx_arrow_single_file_simple_demo.html
More file actions
556 lines (484 loc) · 16.6 KB
/
htmx_arrow_single_file_simple_demo.html
File metadata and controls
556 lines (484 loc) · 16.6 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>HTMX + Arrow-JS Interactive Islands Demo</title>
<!-- htmx -->
<script src="https://unpkg.com/htmx.org@2.0.7"></script>
<style>
:root {
--border: #d7d7db;
--muted: #666;
--bg: #f7f7f9;
--panel: #fff;
--accent: #1f5eff;
--accent-soft: #eef3ff;
}
* { box-sizing: border-box; }
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
background: var(--bg);
color: #222;
line-height: 1.45;
}
.wrap {
max-width: 1100px;
margin: 0 auto;
padding: 24px;
}
h1, h2, h3 { margin-top: 0; }
.intro, .panel, .info {
background: var(--panel);
border: 1px solid var(--border);
border-radius: 14px;
padding: 18px;
box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.intro { margin-bottom: 16px; }
.toolbar {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin: 16px 0;
}
button {
border: 1px solid var(--border);
background: #fff;
border-radius: 10px;
padding: 10px 14px;
cursor: pointer;
font-size: 14px;
}
button.primary {
background: var(--accent);
border-color: var(--accent);
color: #fff;
}
button.soft {
background: var(--accent-soft);
border-color: #cfdcff;
}
.layout {
display: grid;
grid-template-columns: 2fr 1fr;
gap: 16px;
align-items: start;
}
.muted { color: var(--muted); }
.small { font-size: 13px; }
.badge {
display: inline-block;
padding: 4px 10px;
border-radius: 999px;
border: 1px solid var(--border);
background: #fafafa;
font-size: 12px;
margin-left: 8px;
}
.row {
display: flex;
gap: 10px;
flex-wrap: wrap;
align-items: center;
margin: 12px 0;
}
input[type="text"], select {
padding: 10px 12px;
border-radius: 10px;
border: 1px solid var(--border);
min-width: 220px;
font-size: 14px;
}
.results-table {
width: 100%;
border-collapse: collapse;
margin-top: 12px;
}
.results-table th,
.results-table td {
border: 1px solid var(--border);
padding: 10px;
text-align: left;
vertical-align: top;
}
.results-table th { background: #fafafa; }
.list-grid {
display: grid;
grid-template-columns: 1fr auto 1fr;
gap: 14px;
margin-top: 16px;
align-items: start;
}
.list-box {
border: 1px solid var(--border);
border-radius: 12px;
padding: 12px;
background: #fff;
min-height: 280px;
}
.list-box ul {
list-style: none;
padding: 0;
margin: 10px 0 0;
}
.list-box li {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
border: 1px solid #ececf0;
border-radius: 10px;
padding: 8px 10px;
margin-bottom: 8px;
background: #fcfcfd;
}
.move-buttons {
display: flex;
flex-direction: column;
gap: 10px;
justify-content: center;
align-items: center;
padding-top: 60px;
}
details {
border: 1px solid var(--border);
border-radius: 10px;
padding: 10px 12px;
background: #fff;
margin-top: 10px;
}
details summary { cursor: pointer; font-weight: 600; }
.code {
font-family: Consolas, monospace;
background: #f4f4f6;
border: 1px solid var(--border);
border-radius: 8px;
padding: 2px 6px;
}
@media (max-width: 850px) {
.layout { grid-template-columns: 1fr; }
.list-grid { grid-template-columns: 1fr; }
.move-buttons {
flex-direction: row;
padding-top: 0;
justify-content: flex-start;
}
}
</style>
</head>
<body>
<div class="wrap">
<div class="intro">
<h1>HTMX + Arrow-JS interactive islands demo</h1>
<p>
This single file demonstrates the idea we discussed:
<strong>server-rendered pages or fragments</strong> plus small pockets of
<strong>client-side reactivity</strong>.
</p>
<p class="muted small">
In a real ASP.NET Core Razor Pages app, the fragment HTML would normally come from
Razor partials or page handlers. In this demo, htmx loads fragment markup from
inline templates so you only need one file.
</p>
<div class="toolbar">
<button class="primary" data-demo-load="search">Load search island</button>
<button class="soft" data-demo-load="results">Load results island</button>
<button class="soft" data-demo-load="picker">Load dual-list island</button>
</div>
</div>
<div class="layout">
<div id="content" class="panel">
<p>Click a button above to load a fragment into this panel.</p>
</div>
<div class="info">
<h3>What this is showing</h3>
<ul>
<li><strong>htmx-like fragment loading</strong> into a target panel</li>
<li><strong>Arrow-JS local state</strong> inside each loaded fragment</li>
<li>Search filtering, results toggling, and a dual-list picker</li>
<li>The “interactive islands” approach for Razor Pages</li>
</ul>
<details>
<summary>How this maps to Razor Pages</summary>
<div class="small muted" style="margin-top:8px;">
<p>
<span class="code">index.html</span> here represents the Razor Page shell.
</p>
<p>
Each fragment would become a Razor partial like
<span class="code">_Search.cshtml</span> or
<span class="code">_Results.cshtml</span>.
</p>
<p>
htmx would call a page handler or controller action that returns the partial,
then Arrow-JS would enhance the swapped fragment.
</p>
</div>
</details>
<details>
<summary>Why one file here?</summary>
<div class="small muted" style="margin-top:8px;">
<p>
Pure htmx normally makes HTTP requests for HTML. That means a local server is
usually required for separate files. This single-file version avoids that by
taking the fragment HTML from inline templates, which makes it very easy to demo.
</p>
</div>
</details>
</div>
</div>
</div>
<!-- Fragment templates -->
<template id="tpl-search">
<div data-arrow-search></div>
</template>
<template id="tpl-results">
<div data-arrow-results></div>
</template>
<template id="tpl-picker">
<div data-arrow-picker></div>
</template>
<script type="module">
import { reactive, html } from 'https://esm.sh/@arrow-js/core';
const content = document.getElementById('content');
// ---------------------------------------------------------------------
// Tiny helper that mimics the fragment-loading idea for a one-file demo.
// In a real htmx version this content would arrive via hx-get.
// ---------------------------------------------------------------------
function loadTemplate(name) {
const tpl = document.getElementById(`tpl-${name}`);
if (!tpl) return;
content.innerHTML = tpl.innerHTML;
initArrowIslands(content);
}
document.querySelectorAll('[data-demo-load]').forEach(btn => {
btn.addEventListener('click', () => loadTemplate(btn.dataset.demoLoad));
});
// ---------------------------------------------------------------------
// Search island
// ---------------------------------------------------------------------
function initSearchIsland(root) {
const host = root.querySelector('[data-arrow-search]');
if (!host || host.dataset.arrowBound === 'true') return;
host.dataset.arrowBound = 'true';
const state = reactive({
term: '',
role: 'all',
people: [
{ name: 'Alice Brown', role: 'Developer', location: 'London' },
{ name: 'Ben Smith', role: 'Tester', location: 'Birmingham' },
{ name: 'Chloe Jones', role: 'Analyst', location: 'Bristol' },
{ name: 'Daniel Reed', role: 'Developer', location: 'Manchester' },
{ name: 'Ella Green', role: 'Product Owner', location: 'Leeds' }
]
});
function filtered() {
const term = state.term.trim().toLowerCase();
return state.people.filter(p => {
const matchesTerm = !term || `${p.name} ${p.role} ${p.location}`.toLowerCase().includes(term);
const matchesRole = state.role === 'all' || p.role === state.role;
return matchesTerm && matchesRole;
});
}
const view = html`
<div>
<h2>Search island <span class="badge">Arrow-JS</span></h2>
<p class="muted">
This simulates a search panel loaded into a Razor Page area. Arrow-JS handles
the local filter state and instant updates.
</p>
<div class="row">
<input
type="text"
placeholder="Type to filter name, role or location"
@input="${e => state.term = e.target.value}"
/>
<select @change="${e => state.role = e.target.value}">
<option value="all">All roles</option>
<option value="Developer">Developer</option>
<option value="Tester">Tester</option>
<option value="Analyst">Analyst</option>
<option value="Product Owner">Product Owner</option>
</select>
<span class="badge">${() => filtered().length} matches</span>
</div>
<ul>
${() => filtered().map(p => html`
<li>${p.name} — ${p.role} — ${p.location}</li>
`)}
</ul>
<div class="row" style="margin-top:14px;">
<button class="soft" data-demo-load="results">Swap to results island</button>
<button class="soft" data-demo-load="picker">Swap to dual-list island</button>
</div>
</div>
`;
view(host);
wireSwapButtons(host);
}
// ---------------------------------------------------------------------
// Results island
// ---------------------------------------------------------------------
function initResultsIsland(root) {
const host = root.querySelector('[data-arrow-results]');
if (!host || host.dataset.arrowBound === 'true') return;
host.dataset.arrowBound = 'true';
const state = reactive({
showDetails: false,
compact: true,
results: [
{ name: 'Alice Brown', score: 97, notes: 'Strong API and Azure background' },
{ name: 'Daniel Reed', score: 91, notes: 'Good front-end and UX skills' },
{ name: 'Chloe Jones', score: 88, notes: 'Solid analysis and stakeholder work' }
]
});
const view = html`
<div>
<h2>Results island <span class="badge">htmx swap target + Arrow state</span></h2>
<p class="muted">
In a real app, htmx might fetch this fragment from the server after a form submit.
Arrow-JS then manages purely local behaviour like expanding rows or switching display modes.
</p>
<div class="row">
<button class="soft" @click="${() => state.showDetails = !state.showDetails}">
${() => state.showDetails ? 'Hide details' : 'Show details'}
</button>
<button class="soft" @click="${() => state.compact = !state.compact}">
${() => state.compact ? 'Switch to full view' : 'Switch to compact view'}
</button>
<span class="badge">${() => state.results.length} results</span>
</div>
<table class="results-table">
<thead>
<tr>
<th>Name</th>
<th>Score</th>
<th>${() => state.compact ? 'Summary' : 'Notes'}</th>
</tr>
</thead>
<tbody>
${() => state.results.map(r => html`
<tr>
<td>${r.name}</td>
<td>${r.score}</td>
<td>
${() => state.showDetails
? r.notes
: 'Hidden until details are shown'}
</td>
</tr>
`)}
</tbody>
</table>
<div class="row" style="margin-top:14px;">
<button class="soft" data-demo-load="search">Back to search island</button>
<button class="soft" data-demo-load="picker">Load dual-list island</button>
</div>
</div>
`;
view(host);
wireSwapButtons(host);
}
// ---------------------------------------------------------------------
// Dual-list picker island
// ---------------------------------------------------------------------
function initPickerIsland(root) {
const host = root.querySelector('[data-arrow-picker]');
if (!host || host.dataset.arrowBound === 'true') return;
host.dataset.arrowBound = 'true';
const state = reactive({
available: [
'English',
'Maths',
'Science',
'History',
'Geography',
'Computing'
],
selected: ['Art'],
filterAvailable: '',
filterSelected: ''
});
function filteredAvailable() {
const t = state.filterAvailable.trim().toLowerCase();
return state.available.filter(x => !t || x.toLowerCase().includes(t));
}
function filteredSelected() {
const t = state.filterSelected.trim().toLowerCase();
return state.selected.filter(x => !t || x.toLowerCase().includes(t));
}
function moveToSelected(item) {
state.available = state.available.filter(x => x !== item);
state.selected = [...state.selected, item].sort();
}
function moveToAvailable(item) {
state.selected = state.selected.filter(x => x !== item);
state.available = [...state.available, item].sort();
}
const view = html`
<div>
<h2>Dual-list picker island <span class="badge">Good interactive-island example</span></h2>
<p class="muted">
This is the kind of self-contained UI that is often awkward with plain jQuery but does not justify a full SPA.
Razor could still own the page, form post and persistence.
</p>
<div class="list-grid">
<div class="list-box">
<h3>Available subjects <span class="badge">${() => filteredAvailable().length}</span></h3>
<input
type="text"
placeholder="Filter available"
@input="${e => state.filterAvailable = e.target.value}"
/>
<ul>
${() => filteredAvailable().map(item => html`
<li>
<span>${item}</span>
<button class="soft" @click="${() => moveToSelected(item)}">Add →</button>
</li>
`)}
</ul>
</div>
<div class="move-buttons">
<button class="soft" data-demo-load="search">Search</button>
<button class="soft" data-demo-load="results">Results</button>
</div>
<div class="list-box">
<h3>Selected subjects <span class="badge">${() => filteredSelected().length}</span></h3>
<input
type="text"
placeholder="Filter selected"
@input="${e => state.filterSelected = e.target.value}"
/>
<ul>
${() => filteredSelected().map(item => html`
<li>
<span>${item}</span>
<button class="soft" @click="${() => moveToAvailable(item)}">← Remove</button>
</li>
`)}
</ul>
</div>
</div>
</div>
`;
view(host);
wireSwapButtons(host);
}
function wireSwapButtons(root) {
root.querySelectorAll('[data-demo-load]').forEach(btn => {
btn.addEventListener('click', () => loadTemplate(btn.dataset.demoLoad));
});
}
function initArrowIslands(root = document) {
initSearchIsland(root);
initResultsIsland(root);
initPickerIsland(root);
}
// Load an initial fragment
loadTemplate('search');
</script>
</body>
</html>