Debian Codegraph @

test/manual/hx-live/index.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>hx-live playground</title>
6 <script src="../../../src/htmx.js"></script>
7 <script src="../../../src/ext/hx-live.js"></script>
8 <style>
9 :root { color-scheme: light dark; font-family: system-ui, sans-serif; }
10 body { max-width: 900px; margin: 2rem auto; padding: 0 1rem; line-height: 1.5; }
11 section { border: 1px solid #ddd; border-radius: 6px; padding: 1rem 1.25rem; margin: 1rem 0; }
12 h2 { margin-top: 0; font-size: 1.1rem; }
13 h3 { font-size: 0.95rem; color: #555; margin-bottom: 0.4rem; }
14 code, pre { background: #f4f4f4; padding: 1px 4px; border-radius: 3px; font-size: 0.85em; }
15 pre { padding: 8px; overflow-x: auto; }
16 label { display: inline-block; margin-right: 0.5rem; }
17 input, select, textarea, button { font: inherit; padding: 3px 6px; }
18 .pill { display: inline-block; padding: 2px 8px; border-radius: 999px; background: #eee; font-size: 0.85em; }
19 .pill.big { background: #fdd; color: #900; font-weight: 600; }
20 .pill.expensive { background: #fec; color: #850; }
21 .badge { display: inline-block; padding: 1px 6px; border-radius: 3px; background: gold; font-size: 0.8em; }
22 [role=tab] { padding: 4px 12px; background: #eee; border: 1px solid #ccc; cursor: pointer; }
23 [role=tab][aria-selected=true] { background: #fff; border-bottom-color: #fff; font-weight: 600; }
24 [role=tablist] { display: flex; gap: 0; border-bottom: 1px solid #ccc; }
25 [role=tabpanel] { padding: 0.75rem 0; }
26 [role=tabpanel][hidden] { display: none; }
27 .focused { box-shadow: 0 0 0 3px #fc6; }
28 fieldset[disabled] { opacity: 0.5; }
29 progress { width: 100%; }
30 .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; }
31 .small { font-size: 0.85em; color: #666; }
32 .external { outline: 1px dashed #999; padding: 2px; }
33 .transition { transition: opacity 0.3s, background 0.3s; }
34 .visible { background: #cfc; }
35 .hidden.faded { opacity: 0.4; background: #fcc; }
36 [aria-pressed=true] { background: #fcd; }
37 [role=tab].active { background: #fff; border-bottom-color: #fff; font-weight: 600; }
38 </style>
39 </head>
40 <body hx-ext="hx-live">
41
42 <h1>hx-live playground</h1>
43 <p class="small">Open DevTools. Type, click, change inputs. Verify state updates without server round-trips.</p>
44
45 <!-- =================================================================== -->
46 <section>
47 <h2>1. Simple form: <code>:attr="expr"</code> — boolean attributes</h2>
48 <p>
49 <label><input type="checkbox" id="lock"> Lock everything below</label>
50 </p>
51 <fieldset :disabled="q('#lock').checked">
52 <legend>Fieldset disables on lock</legend>
53 <input placeholder="email">
54 <button>Save</button>
55 </fieldset>
56 <p>
57 <button :hidden="!q('#lock').checked">I only appear when locked</button>
58 </p>
59 <pre>:disabled="q('#lock').checked"
60 :hidden="!q('#lock').checked"</pre>
61 </section>
62
63 <!-- =================================================================== -->
64 <section>
65 <h2>2. <code>:text</code> — computed text content</h2>
66 <p>
67 <label>Price: <input id="price" type="number" value="10"></label>
68 <label>Qty: <input id="qty" type="number" value="3"></label>
69 </p>
70 <p>
71 Subtotal: $<output :text="(q('#price').valueAsNumber * q('#qty').valueAsNumber).toFixed(2)"></output>
72 </p>
73 <p>
74 With 10% tax: $<output :text="(q('#price').valueAsNumber * q('#qty').valueAsNumber * 1.1).toFixed(2)"></output>
75 </p>
76 </section>
77
78 <!-- =================================================================== -->
79 <section>
80 <h2>3. <code>:html</code> — innerHTML binding</h2>
81 <p>
82 <label>Markdown-ish input: <input id="md" value="hello world"></label>
83 </p>
84 <div :html="`<em>${q('#md').value || 'type something…'}</em>`"></div>
85 <p class="small">Note: <code>:html</code> is XSS-unsafe if value comes from untrusted source. Prefer <code>:text</code>.</p>
86 </section>
87
88 <!-- =================================================================== -->
89 <section>
90 <h2>4. <code>:class</code> — string and object forms</h2>
91 <h3>String form (class-set swap, leaves managed classes alone)</h3>
92 <p class="small">
93 Use <code>:class</code> only when CSS pseudo-classes can't express the rule (cross-element
94 comparison, arithmetic). For checkbox/focus/invalid state, use CSS (<code>:has()</code>,
95 <code>peer-checked:</code>). Example below: branch on value-vs-budget comparison — CSS can't see it.
96 </p>
97 <p>
98 <label>Cost: <input id="cost" type="number" value="40"></label>
99 <label>Budget: <input id="budget" type="number" value="100"></label>
100 </p>
101 <div class="external transition"
102 :class="q('#cost').valueAsNumber > q('#budget').valueAsNumber ? 'hidden faded' : 'visible'">
103 External class stays; managed classes swap. (Goes red+faded when cost &gt; budget.)
104 </div>
105 <pre>:class="q('#cost').valueAsNumber > q('#budget').valueAsNumber
106 ? 'hidden faded'
107 : 'visible'"</pre>
108
109 <h3>Object form (toggle each class independently)</h3>
110 <p>
111 <label>Price: <input id="price2" type="number" value="500"></label>
112 </p>
113 <span class="pill"
114 :class="{ big: q('#price2').valueAsNumber > 1000, expensive: q('#price2').valueAsNumber > 100 }">
115 $<span :text="q('#price2').valueAsNumber"></span>
116 </span>
117 <pre>:class="{ big: price > 1000, expensive: price > 100 }"</pre>
118 </section>
119
120 <!-- =================================================================== -->
121 <section>
122 <h2>5. <code>:style</code> — string and object forms</h2>
123 <h3>Object form</h3>
124 <p><label>Width: <input id="w" type="range" min="0" max="100" value="40">%</label></p>
125 <div :style="{ width: q('#w').value + '%', height: '20px', background: 'cornflowerblue' }"></div>
126
127 <h3>String form (CSS custom property)</h3>
128 <p><label>Progress: <input id="p" type="range" min="0" max="100" value="60">%</label></p>
129 <progress :value="q('#p').valueAsNumber" max="100"></progress>
130 <pre>:style="'--pct: ' + q('#p').valueAsNumber / 100"</pre>
131 </section>
132
133 <!-- =================================================================== -->
134 <section>
135 <h2>6. <code>:aria-*</code> — always "true"/"false" (never removed)</h2>
136 <p><label><input type="checkbox" id="exp"> Expand</label></p>
137 <button :aria-expanded="q('#exp').checked">Toggleable section</button>
138 <div :hidden="!q('#exp').checked">
139 <p>Hidden until expanded.</p>
140 </div>
141 </section>
142
143 <!-- =================================================================== -->
144 <section>
145 <h2>7. <code>matches()</code> — bare alias for <code>this.matches()</code></h2>
146 <form>
147 <label>Required field: <input required placeholder="required"></label>
148 <fieldset :disabled="matches(':has(input:invalid)')">
149 <legend>Disabled while form has any :invalid input</legend>
150 <button>Submit</button>
151 </fieldset>
152 </form>
153 <pre>:disabled="matches(':has(input:invalid)')"</pre>
154 </section>
155
156 <!-- =================================================================== -->
157 <section>
158 <h2>8. <code>:checked</code> and <code>:value</code> — property + attribute sync</h2>
159 <p>
160 <label><input type="checkbox" id="src-chk"> Source checkbox</label>
161 </p>
162 <p>
163 Mirror: <input type="checkbox" :checked="q('#src-chk').checked">
164 (DOM attribute AND .checked property stay in sync)
165 </p>
166 <p>
167 Source text: <input id="src-text" value="hello">
168 </p>
169 <p>
170 Uppercased mirror: <input :value="q('#src-text').value.toUpperCase()">
171 </p>
172 </section>
173
174 <!-- =================================================================== -->
175 <section>
176 <h2>9. ARIA-as-state: tabs via <code>take()</code></h2>
177 <div role="tablist">
178 <button role="tab" id="t1" aria-controls="p1" aria-selected="true"
179 hx-on:click="take('aria-selected', '[role=tab]')">Home</button>
180 <button role="tab" id="t2" aria-controls="p2" aria-selected="false"
181 hx-on:click="take('aria-selected', '[role=tab]')">Profile</button>
182 <button role="tab" id="t3" aria-controls="p3" aria-selected="false"
183 hx-on:click="take('aria-selected', '[role=tab]')">Settings</button>
184 </div>
185 <div role="tabpanel" id="p1" :hidden="q('#t1').matches('[aria-selected=false]')">Home content.</div>
186 <div role="tabpanel" id="p2" :hidden="q('#t2').matches('[aria-selected=false]')" hidden>Profile content.</div>
187 <div role="tabpanel" id="p3" :hidden="q('#t3').matches('[aria-selected=false]')" hidden>Settings content.</div>
188 <pre>hx-on:click="take('aria-selected', '[role=tab]')"
189 :hidden="q('#tab-id').matches('[aria-selected=false]')"</pre>
190 </section>
191
192 <!-- =================================================================== -->
193 <section>
194 <h2>10. <code>toggle()</code> — binary flip + N-value cycling</h2>
195 <p>
196 <button hx-on:click="toggle('.active'); toggle('aria-pressed')" aria-pressed="false">Toggle me</button>
197 <span class="small">(class.active + aria-pressed)</span>
198 </p>
199 <p>
200 <button hx-on:click="toggle('data-mode', 'light|dark|auto')" data-mode="light">
201 Mode: <span :text="this.parentElement.querySelector('button').dataset.mode || 'unset'"></span>
202 </button>
203 <span class="small">(cycles through light → dark → auto)</span>
204 </p>
205 </section>
206
207 <!-- =================================================================== -->
208 <section>
209 <h2>11. <code>&lt;output&gt;</code> as computed variable</h2>
210 <p>
211 <label>Rate: <input id="rate" type="number" value="100"></label>
212 <label>Nights: <input id="nights" type="number" value="3"></label>
213 <label><input type="checkbox" id="promo"> 10% promo</label>
214 </p>
215 <output id="subtotal" style="display:none" :value="q('#rate').valueAsNumber * q('#nights').valueAsNumber"></output>
216 <p>
217 Subtotal: $<span :text="(+q('#subtotal').value).toFixed(2)"></span>
218 </p>
219 <p>
220 Total: $<output :text="(+q('#subtotal').value * (q('#promo').checked ? 0.9 : 1)).toFixed(2)"></output>
221 </p>
222 <p class="small">
223 <code>&lt;output :value="..."&gt;</code> acts as a named computed value. <code>output.value</code> is an alias for
224 <code>textContent</code>, so the result is readable via <code>q('#subtotal').value</code> elsewhere.
225 Add your own <code>display:none</code> (or a global rule) if you want it invisible.
226 Use <code>:text</code> on <code>&lt;output&gt;</code> when you want it visible.
227 </p>
228 <pre>&lt;output id="subtotal" style="display:none" :value="..."&gt;&lt;/output&gt;
229 q('#subtotal').value // read elsewhere</pre>
230 </section>
231
232 <!-- =================================================================== -->
233 <section>
234 <h2>12. <code>.focused</code> via <code>:class</code> + <code>:focus-within</code></h2>
235 <div :class="{ focused: matches(':focus-within') }" style="padding: 8px; border: 1px solid #ccc;">
236 <input placeholder="focus me">
237 <input placeholder="or me">
238 </div>
239 <pre>:class="{ focused: matches(':focus-within') }"</pre>
240 </section>
241
242 <!-- =================================================================== -->
243 <section>
244 <h2>13. Extended form: debounced live search</h2>
245 <p><label>Search: <input id="q" placeholder="type fast…"></label></p>
246 <output id="search-result" hx-live="
247 let term = q('#q').value;
248 if (!term) { this.textContent = '(empty)'; return; }
249 await debounce(300);
250 this.textContent = 'searching for &quot;' + term + '&quot;… (debounced 300ms)';
251 ">(empty)</output>
252 <pre>hx-live="
253 let term = q('#q').value;
254 if (!term) { this.textContent = '(empty)'; return; }
255 await debounce(300);
256 this.textContent = 'searching for \"' + term + '\"…';
257 "</pre>
258 </section>
259
260 <!-- =================================================================== -->
261 <section>
262 <h2>14. Extended form: cross-element writes</h2>
263 <p>
264 <label>Width: <input id="cross-w" type="range" min="50" max="500" value="200"></label>
265 <span :text="q('#cross-w').value + 'px'"></span>
266 </p>
267 <div hx-live="
268 let w = q('#cross-w').valueAsNumber;
269 q('#bar1').style.width = w + 'px';
270 q('#bar2').style.width = (w / 2) + 'px';
271 q('#bar3').style.width = (w * 1.5) + 'px';
272 "></div>
273 <div id="bar1" style="height: 10px; background: tomato; margin: 4px 0;"></div>
274 <div id="bar2" style="height: 10px; background: gold; margin: 4px 0;"></div>
275 <div id="bar3" style="height: 10px; background: limegreen; margin: 4px 0;"></div>
276 </section>
277
278 <!-- =================================================================== -->
279 <section>
280 <h2>15. Extended form: imperative DOM ops (filter)</h2>
281 <p><label>Filter: <input id="filter" placeholder="type to filter"></label></p>
282 <ul>
283 <li>apple</li>
284 <li>apricot</li>
285 <li>banana</li>
286 <li>blueberry</li>
287 <li>cherry</li>
288 <li>date</li>
289 <li>elderberry</li>
290 </ul>
291 <div hx-live="
292 let f = q('#filter').value.toLowerCase();
293 for (let li of q('ul li'))
294 li.hidden = f && !li.textContent.toLowerCase().includes(f);
295 "></div>
296 <pre>hx-live="
297 let f = q('#filter').value.toLowerCase();
298 for (let li of q('ul li'))
299 li.hidden = f && !li.textContent.toLowerCase().includes(f);
300 "</pre>
301 </section>
302
303 <!-- =================================================================== -->
304 <section>
305 <h2>16. <code>attr()</code> scope helper — unified attribute access</h2>
306 <p>
307 <button hx-on:click="attr('aria-pressed', !attr('aria-pressed'))" aria-pressed="false">
308 Toggle aria-pressed via attr()
309 </button>
310 </p>
311 <p>
312 <input id="attr-target" placeholder="will be disabled below">
313 <button hx-on:click="q('#attr-target').attr('disabled', true)">Disable target</button>
314 <button hx-on:click="q('#attr-target').attr('disabled', false)">Enable target</button>
315 </p>
316 </section>
317
318 <!-- =================================================================== -->
319 <section>
320 <h2>17. <code>q()</code> directional selectors</h2>
321 <p>
322 <input value="seed value">
323 <button hx-on:click="q('previous input').value = q('previous input').value + ' (clicked)'">
324 Append to previous input
325 </button>
326 </p>
327 <p>
328 <button hx-on:click="alert(q('closest section').querySelector('h2').textContent)">
329 Show closest section h2
330 </button>
331 </p>
332 </section>
333
334 <!-- =================================================================== -->
335 <section>
336 <h2>18. <code>htmx.live.refresh()</code> for non-DOM state</h2>
337 <output id="storage-out" :text="localStorage.getItem('hx-live-demo-key') || '(none)'"></output>
338 <p>
339 <button hx-on:click="localStorage.setItem('hx-live-demo-key', Date.now()); htmx.live.refresh()">
340 Set localStorage + refresh()
341 </button>
342 <button hx-on:click="localStorage.removeItem('hx-live-demo-key'); htmx.live.refresh()">
343 Clear + refresh()
344 </button>
345 </p>
346 <pre>localStorage.setItem(...)
347 htmx.live.refresh() // expressions reading localStorage recompute</pre>
348 </section>
349
350 <!-- =================================================================== -->
351 <section>
352 <h2>19. Cascading <code>data</code> proxy (Alpine <code>x-data</code> style)</h2>
353 <p class="small">
354 State lives on a container as <code>data-*</code> attribute. Descendants inherit reads and
355 writes via the <code>data</code> scope helper (Proxy). <code>data.foo</code> resolves to the
356 nearest ancestor with <code>data-foo</code>. Writes target the same ancestor.
357 </p>
358 <div data-count="0" style="padding: 0.5rem; border: 1px dashed #ccc;">
359 <p>
360 <button hx-on:click="data.count++">+</button>
361 <button hx-on:click="data.count--">−</button>
362 <button hx-on:click="data.count = 0">Reset</button>
363 Count (read at top level): <strong :text="data.count"></strong>
364 </p>
365 <div style="padding-left: 1rem; border-left: 2px solid #aaa;">
366 <p>Nested child reads ancestor: <strong :text="data.count"></strong></p>
367 <p>
368 <button hx-on:click="data.count++">Increment from nested (writes to ancestor)</button>
369 </p>
370 <div style="padding-left: 1rem; border-left: 2px solid #ccc;">
371 <p>Deeper still: <strong :text="data.count"></strong></p>
372 </div>
373 </div>
374 </div>
375 <pre>&lt;div data-count="0"&gt;
376 &lt;button hx-on:click="data.count++"&gt;+&lt;/button&gt;
377 &lt;span :text="data.count"&gt;&lt;/span&gt; &lt;!-- reactive --&gt;
378 &lt;/div&gt;</pre>
379 </section>
380
381 <!-- =================================================================== -->
382 <section>
383 <h2>20. <code>with (data) { ... }</code> — multi-write handlers</h2>
384 <p class="small">
385 JavaScript <code>with</code> statement scopes property lookups to <code>data</code>, letting
386 you mutate multiple keys without prefixing. Use sparingly — works only in non-strict mode
387 (hx-on / hx-live expressions, not modules).
388 </p>
389 <div data-likes="0" data-shares="0" data-views="0" style="padding: 0.5rem; border: 1px dashed #ccc;">
390 <p>
391 <button hx-on:click="with (data) { likes++; views++ }">Like</button>
392 <button hx-on:click="with (data) { shares++; likes++; views++ }">Share + Like</button>
393 <button hx-on:click="with (data) { likes = 0; shares = 0; views = 0 }">Reset</button>
394 </p>
395 <p>
396 ❤️ <span :text="data.likes"></span> ·
397 🔁 <span :text="data.shares"></span> ·
398 👁 <span :text="data.views"></span>
399 </p>
400 </div>
401 <pre>&lt;div data-likes="0" data-shares="0"&gt;
402 &lt;button hx-on:click="with (data) { likes++; shares++ }"&gt;Boost&lt;/button&gt;
403 &lt;/div&gt;</pre>
404 </section>
405
406 <!-- =================================================================== -->
407 <section>
408 <h2>21. Cascading data + <code>:class</code> for branching UI</h2>
409 <p class="small">
410 Tab state via cascading <code>data-tab</code> instead of <code>aria-selected</code>. One state attribute,
411 descendants read it. No <code>take()</code> needed since the state lives in one place.
412 </p>
413 <div data-tab="home">
414 <div role="tablist">
415 <button role="tab" hx-on:click="data.tab = 'home'"
416 :class="{ active: data.tab === 'home' }">Home</button>
417 <button role="tab" hx-on:click="data.tab = 'profile'"
418 :class="{ active: data.tab === 'profile' }">Profile</button>
419 <button role="tab" hx-on:click="data.tab = 'settings'"
420 :class="{ active: data.tab === 'settings' }">Settings</button>
421 </div>
422 <div :hidden="data.tab !== 'home'">Home panel content.</div>
423 <div :hidden="data.tab !== 'profile'">Profile panel content.</div>
424 <div :hidden="data.tab !== 'settings'">Settings panel content.</div>
425 </div>
426 <pre>&lt;div data-tab="home"&gt;
427 &lt;button hx-on:click="data.tab = 'home'" :class="{ active: data.tab === 'home' }"&gt;Home&lt;/button&gt;
428 &lt;div :hidden="data.tab !== 'home'"&gt;...&lt;/div&gt;
429 &lt;/div&gt;</pre>
430 </section>
431
432 <footer class="small">
433 <p>Source: <code>src/ext/hx-live.js</code> · Run via local server (e.g. <code>npx http-server -c-1 .</code>) then open this file.</p>
434 </footer>
435
436 </body>
437 </html>