-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
2043 lines (1879 loc) · 91.7 KB
/
index.html
File metadata and controls
2043 lines (1879 loc) · 91.7 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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Stimulus Reactive UI</title>
<!-- Tailwind CSS via Play CDN -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Import Maps for Stimulus -->
<script type="importmap">
{
"imports": {
"@hotwired/stimulus": "https://unpkg.com/@hotwired/stimulus/dist/stimulus.js"
}
}
</script>
</head>
<body class="bg-gray-100 min-h-screen pt-12 pb-24">
<div class="container mx-auto px-4">
<div class="max-w-2xl mx-auto space-y-6">
<h1 class="text-3xl font-bold text-gray-800 text-center mb-8">Stimulus Reactive UI</h1>
<!-- 1. Interactive Counter -->
<div class="bg-white rounded-lg shadow-md p-6 space-y-4">
<a href="#interactive-counter" id="interactive-counter" class="text-xl font-semibold text-gray-700 scroll-mt-12 hover:underline"
>1. Interactive Counter</a
>
<div data-controller="live">
<!-- State States -->
<input type="hidden" data-live-state="count" value="0" />
<input type="hidden" data-live-state="totalClicks" value="0" />
<input type="hidden" data-live-state="stepSize" value="5" />
<!-- Computed States -->
<script type="text/template" data-live-computed="absMax">
42
</script>
<script type="text/template" data-live-computed="isNegative">
state.count < 0
</script>
<script type="text/template" data-live-computed="isPositive">
state.count > 0
</script>
<script type="text/template" data-live-computed="isZero">
state.count === 0
</script>
<script type="text/template" data-live-computed="absCount">
Math.abs(state.count)
</script>
<script type="text/template" data-live-computed="countCategory">
state.count === 0 ? 'neutral' :
state.count > 0 && state.count <= 10 ? 'small-positive' :
state.count > 10 ? 'large-positive' :
state.count < 0 && state.count >= -10 ? 'small-negative' :
'large-negative'
</script>
<script type="text/template" data-live-computed="progressWidth">
Math.min(100, (computed.absCount / computed.absMax) * 100)
</script>
<script type="text/template" data-live-computed="canDecrement">
state.count > -computed.absMax
</script>
<script type="text/template" data-live-computed="canIncrement">
state.count < computed.absMax
</script>
<!-- Step Size Selector -->
<div class="mb-4">
<label class="block text-sm font-medium text-gray-700 mb-2">Step Size</label>
<div class="flex gap-2">
<button
data-action="click->live#update"
data-live-update-param="state.stepSize = 1"
class="px-3 py-1 text-sm rounded transition-colors"
live:class="{
'bg-blue-600 text-white': state.stepSize === 1,
'bg-gray-200 text-gray-700 hover:bg-gray-300': state.stepSize !== 1
}"
>
1x
</button>
<button
data-action="click->live#update"
data-live-update-param="state.stepSize = 5"
class="px-3 py-1 text-sm rounded transition-colors"
live:class="{
'bg-blue-600 text-white': state.stepSize === 5,
'bg-gray-200 text-gray-700 hover:bg-gray-300': state.stepSize !== 5
}"
>
5x
</button>
<button
data-action="click->live#update"
data-live-update-param="state.stepSize = 10"
class="px-3 py-1 text-sm rounded transition-colors"
live:class="{
'bg-blue-600 text-white': state.stepSize === 10,
'bg-gray-200 text-gray-700 hover:bg-gray-300': state.stepSize !== 10
}"
>
10x
</button>
</div>
</div>
<!-- Main Counter Display -->
<div class="text-center mb-6">
<div
class="inline-block text-6xl font-bold p-6 rounded-2xl transition-all duration-300 transform"
live:class="{
'text-gray-600 bg-gray-100 scale-100': computed.isZero,
'text-green-600 bg-green-100 scale-105': computed.countCategory === 'small-positive',
'text-green-700 bg-green-200 scale-110': computed.countCategory === 'large-positive',
'text-red-600 bg-red-100 scale-105': computed.countCategory === 'small-negative',
'text-red-700 bg-red-200 scale-110': computed.countCategory === 'large-negative'
}"
live:text="state.count"
>
0
</div>
<!-- Progress Bar -->
<div class="mt-4 max-w-md mx-auto">
<div class="flex justify-between text-xs text-gray-500 mb-1">
<span live:text="`-${computed.absMax}`"></span>
<span live:text="`Progress: ${computed.absCount}/${computed.absMax}`"></span>
<span live:text="`+${computed.absMax}`"></span>
</div>
<div class="w-full bg-gray-200 rounded-full h-2">
<div
class="h-2 rounded-full transition-all duration-300"
live:class="{
'bg-green-500': computed.isPositive,
'bg-red-500': computed.isNegative,
'bg-gray-400': computed.isZero
}"
live:style="{ width: computed.progressWidth + '%' }"
></div>
</div>
</div>
</div>
<!-- Counter Controls -->
<div class="flex justify-center gap-4 mb-6">
<button
data-action="click->live#update"
data-live-update-param="state.count = Math.max(-computed.absMax, state.count - state.stepSize), state.totalClicks++"
live:disabled="!computed.canDecrement"
class="flex items-center gap-2 px-6 py-3 text-lg font-semibold rounded-lg transform hover:scale-105 disabled:hover:scale-100"
live:class="{
'bg-red-500 hover:bg-red-600 text-white shadow-lg': computed.canDecrement,
'bg-gray-300 text-gray-500 cursor-not-allowed': !computed.canDecrement
}"
>
<span class="text-xl">−</span>
<span live:text="state.stepSize">1</span>
</button>
<button
data-action="click->live#update"
data-live-update-param="state.count = 0, state.totalClicks++"
class="px-6 py-3 text-lg font-semibold bg-gray-500 hover:bg-gray-600 text-white rounded-lg transform hover:scale-105"
>
Reset
</button>
<button
data-action="click->live#update"
data-live-update-param="state.count = Math.min(computed.absMax, state.count + state.stepSize), state.totalClicks++"
live:disabled="!computed.canIncrement"
class="flex items-center gap-2 px-6 py-3 text-lg font-semibold rounded-lg transform hover:scale-105 disabled:hover:scale-100"
live:class="{
'bg-green-500 hover:bg-green-600 text-white shadow-lg': computed.canIncrement,
'bg-gray-300 text-gray-500 cursor-not-allowed': !computed.canIncrement
}"
>
<span class="text-xl">+</span>
<span live:text="state.stepSize">1</span>
</button>
</div>
<!-- Analytics Panel -->
<div class="grid grid-cols-3 gap-4 p-4 bg-gray-50 rounded-lg">
<div class="text-center">
<div class="text-2xl font-bold text-blue-600" live:text="state.totalClicks">0</div>
<div class="text-sm text-gray-600">Total Clicks</div>
</div>
<div class="text-center">
<div class="text-2xl font-bold text-purple-600" live:text="computed.progressWidth.toFixed(0) + '%'">0</div>
<div class="text-sm text-gray-600">Progress</div>
</div>
<div class="text-center">
<div
class="text-2xl font-bold"
live:class="{
'text-green-600': computed.isPositive,
'text-red-600': computed.isNegative,
'text-gray-600': computed.isZero
}"
live:text="computed.isPositive ? 'Positive' : (computed.isNegative ? 'Negative' : 'Neutral')"
>
Neutral
</div>
<div class="text-sm text-gray-600">Current State</div>
</div>
</div>
<!-- Status Messages -->
<div class="mt-4 space-y-2">
<div
class="text-sm bg-yellow-100 text-yellow-800 border border-yellow-200 p-3 rounded-lg"
live:show="computed.absCount >= 25 && computed.absCount < 35"
style="display: none"
>
<span class="pr-1">⚡️</span> You're getting close to the limits, keep going!
</div>
<div
class="text-sm bg-orange-100 text-orange-800 border border-orange-200 p-3 rounded-lg"
live:show="computed.absCount >= 35 && computed.absCount < computed.absMax"
style="display: none"
>
<span class="pr-1">🔥</span> Close to the ultimate number, almost there!
</div>
<div
class="text-sm p-3 rounded-lg bg-green-100 text-green-800 border border-green-200"
live:show="state.count === computed.absMax"
style="display: none"
>
<span class="pr-1">🎉</span> The answer to the ultimate question of life, the universe, and everything!
</div>
<div
class="text-sm p-3 rounded-lg bg-blue-100 text-blue-800 border border-blue-200"
live:show="state.totalClicks > 0 && state.count === 0"
style="display: none"
>
<span class="pr-1">🎯</span> Back to the beginning! You've used up <span live:text="state.totalClicks">0</span> clicks
total.
</div>
</div>
</div>
</div>
<!-- 2. Progress Bar with Status -->
<div class="bg-white rounded-lg shadow-md p-6 space-y-4">
<a href="#progress-bar" id="progress-bar" class="text-xl font-semibold text-gray-700 scroll-mt-12 hover:underline"
>2. Progress Bar with Status</a
>
<div data-controller="live">
<input type="range" data-live-state="progress" min="0" max="100" value="25" class="hidden" />
<script type="text/template" data-live-computed="progressPercentageColor">
{
"text-red-600": state.progress < 30,
"text-yellow-600": state.progress >= 30 && state.progress < 70,
"text-green-600": state.progress >= 70,
}
</script>
<script type="text/template" data-live-computed="progressBarColor">
{
"bg-red-600": state.progress < 30,
"bg-yellow-600": state.progress >= 30 && state.progress < 70,
"bg-green-600": state.progress >= 70,
}
</script>
<script type="text/template" data-live-computed="canDecrease">
state.progress > 0
</script>
<script type="text/template" data-live-computed="canIncrease">
state.progress < 100
</script>
<div class="flex justify-between items-center mb-2">
<span class="text-sm font-medium text-gray-700">Progress:</span>
<span live:text="state.progress + '%'" live:class="computed.progressPercentageColor" class="text-sm font-bold"></span>
</div>
<div class="w-full bg-gray-200 rounded-full h-4 mb-4">
<div
live:class="computed.progressBarColor"
class="h-4 rounded-full transition-all duration-150"
live:style="{ width: state.progress + '%' }"
></div>
</div>
<div class="flex gap-2">
<button
data-action="click->live#update"
data-live-update-param="state.progress = Math.max(0, state.progress - 10)"
live:disabled="!computed.canDecrease"
class="bg-red-500 text-white hover:bg-red-600 disabled:bg-gray-300 disabled:text-gray-500 disabled:cursor-not-allowed px-3 py-1 rounded text-sm transition-colors"
>
-10%
</button>
<button
data-action="click->live#update"
data-live-update-param="state.progress = Math.min(100, state.progress + 10)"
live:disabled="!computed.canIncrease"
class="bg-green-500 text-white hover:bg-green-600 disabled:bg-gray-300 disabled:text-gray-500 disabled:cursor-not-allowed px-3 py-1 rounded text-sm transition-colors"
>
+10%
</button>
<button
data-action="click->live#update"
data-live-update-param="state.progress = 25"
class="bg-gray-500 text-white px-3 py-1 rounded text-sm hover:bg-gray-600"
>
Reset
</button>
</div>
</div>
</div>
<!-- 3. Shopping Cart Calculator -->
<div class="bg-white rounded-lg shadow-md p-6 space-y-4">
<a href="#shopping-cart" id="shopping-cart" class="text-xl font-semibold text-gray-700 scroll-mt-12 hover:underline"
>3. Shopping Cart Calculator</a
>
<div data-controller="live">
<script type="text/template" data-live-computed="subtotal">
state.quantity * state.price;
</script>
<script type="text/template" data-live-computed="discountAmount">
(computed.subtotal * state.discount) / 100;
</script>
<script type="text/template" data-live-computed="total">
computed.subtotal - computed.discountAmount;
</script>
<div class="grid grid-cols-2 gap-4 mb-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Quantity</label>
<input
type="number"
min="1"
value="1"
data-live-state="quantity"
data-action="input->live#update"
class="w-full p-2 border border-gray-300 rounded"
/>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Price per item</label>
<input
type="number"
step="0.01"
min="0"
value="29.99"
data-live-state="price"
data-action="input->live#update"
class="w-full p-2 border border-gray-300 rounded"
/>
</div>
</div>
<div class="mb-4">
<label class="block text-sm font-medium text-gray-700 mb-1">Discount (%)</label>
<input type="range" min="0" max="50" value="0" data-live-state="discount" data-action="input->live#update" class="w-full" />
<div class="text-center text-sm text-gray-600 mt-1" live:text="state.discount + '% off'">0% off</div>
</div>
<div class="bg-gray-50 p-4 rounded">
<div class="flex justify-between items-center text-sm text-gray-600 mb-1">
<span>Subtotal:</span>
<span live:text="'$' + computed.subtotal.toFixed(2)">$29.99</span>
</div>
<div class="flex justify-between items-center text-sm text-gray-600 mb-1">
<span>Discount:</span>
<span live:text="'-$' + computed.discountAmount.toFixed(2)">-$0.00</span>
</div>
<div class="flex justify-between items-center text-lg font-bold text-gray-800 border-t pt-2">
<span>Total:</span>
<span live:text="'$' + computed.total.toFixed(2)">$29.99</span>
</div>
</div>
</div>
</div>
<!-- 4. Theme Switcher -->
<div class="bg-white rounded-lg shadow-md p-6 space-y-4">
<a href="#theme-switcher" id="theme-switcher" class="text-xl font-semibold text-gray-700 scroll-mt-12 hover:underline"
>4. Theme Switcher</a
>
<div data-controller="live">
<script type="text/template" data-live-computed="previewClasses">
{
'p-4 rounded transition-all duration-150': true,
'text-sm': state.fontSize === 'small',
'text-base': state.fontSize === 'medium',
'text-lg': state.fontSize === 'large',
'bg-gray-50 text-gray-800': state.theme === 'light',
'bg-gray-800 text-gray-100': state.theme === 'dark',
'bg-gradient-to-r from-purple-400 via-pink-500 to-red-500 text-white': state.theme === 'colorful'
}
</script>
<script type="text/template" data-live-computed="themeTitle">
state.theme.charAt(0).toUpperCase() + state.theme.slice(1)
</script>
<script type="text/template" data-live-computed="previewHtml">
<h3 class="font-bold mb-2">${computed.themeTitle} theme</h3>
<p>This is what your content will look like with the selected theme and font size.</p>
</script>
<div class="grid grid-cols-2 gap-4 mb-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Theme</label>
<div class="space-y-2">
<label class="flex items-center">
<input
type="radio"
name="theme"
value="light"
data-live-state="theme"
data-action="change->live#update"
class="mr-2"
checked
/>
Light
</label>
<label class="flex items-center">
<input type="radio" name="theme" value="dark" data-live-state="theme" data-action="change->live#update" class="mr-2" />
Dark
</label>
<label class="flex items-center">
<input
type="radio"
name="theme"
value="colorful"
data-live-state="theme"
data-action="change->live#update"
class="mr-2"
/>
Colorful
</label>
</div>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Font Size</label>
<select data-live-state="fontSize" data-action="change->live#update" class="w-full p-2 border border-gray-300 rounded">
<option value="small">Small</option>
<option value="medium" selected>Medium</option>
<option value="large">Large</option>
</select>
</div>
</div>
<div live:class="computed.previewClasses" live:html="computed.previewHtml"></div>
</div>
</div>
<!-- 5. Stateful Accordion -->
<div class="bg-white rounded-lg shadow-md p-6 space-y-4">
<a href="#stateful-accordion" id="stateful-accordion" class="text-xl font-semibold text-gray-700 scroll-mt-12 hover:underline"
>5. Stateful Accordion</a
>
<div data-controller="live">
<!-- Hidden input to track which panel is open -->
<input type="hidden" data-live-state="openPanel" value="" data-action="input->live#update" />
<!-- Panel 1 -->
<div class="border border-gray-200 rounded-lg mb-2">
<button
class="w-full px-4 py-3 text-left font-medium text-gray-800 hover:bg-gray-50 rounded-t-lg flex justify-between items-center"
data-action="click->live#update"
data-live-update-param="state.openPanel = state.openPanel === 'panel1' ? '' : 'panel1'"
live:class="{ 'bg-blue-50 border-b border-gray-200': state.openPanel === 'panel1' }"
>
<span>What is Stimulus?</span>
<svg
class="w-5 h-5 transition-transform duration-150"
live:class="{ 'rotate-180': state.openPanel === 'panel1' }"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
</svg>
</button>
<div class="overflow-hidden transition-all duration-150" live:class="{ 'hidden': state.openPanel !== 'panel1' }">
<div class="p-4 text-gray-600">
Stimulus is a JavaScript framework with modest ambitions. It doesn't seek to take over your entire front-end—in fact, it's
not concerned with rendering HTML at all. Instead, it's designed to augment your HTML with just enough behavior to make it
shine.
</div>
</div>
</div>
<!-- Panel 2 -->
<div class="border border-gray-200 rounded-lg mb-2">
<button
class="w-full px-4 py-3 text-left font-medium text-gray-800 hover:bg-gray-50 rounded-t-lg flex justify-between items-center"
data-action="click->live#update"
data-live-update-param="state.openPanel = state.openPanel === 'panel2' ? '' : 'panel2'"
live:class="{ 'bg-blue-50 border-b border-gray-200': state.openPanel === 'panel2' }"
>
<span>How does data binding work?</span>
<svg
class="w-5 h-5 transition-transform duration-150"
live:class="{ 'rotate-180': state.openPanel === 'panel2' }"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
</svg>
</button>
<div class="overflow-hidden transition-all duration-150" live:class="{ 'hidden': state.openPanel !== 'panel2' }">
<div class="p-4 text-gray-600">
Data binding allows you to automatically sync your application state with the DOM. When state changes, the UI updates
automatically without manual DOM manipulation. This creates reactive interfaces with minimal code.
</div>
</div>
</div>
<!-- Panel 3 -->
<div class="border border-gray-200 rounded-lg mb-2">
<button
class="w-full px-4 py-3 text-left font-medium text-gray-800 hover:bg-gray-50 rounded-t-lg flex justify-between items-center"
data-action="click->live#update"
data-live-update-param="state.openPanel = state.openPanel === 'panel3' ? '' : 'panel3'"
live:class="{ 'bg-blue-50 border-b border-gray-200': state.openPanel === 'panel3' }"
>
<span>Why use this approach?</span>
<svg
class="w-5 h-5 transition-transform duration-150"
live:class="{ 'rotate-180': state.openPanel === 'panel3' }"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
</svg>
</button>
<div class="overflow-hidden transition-all duration-150" live:class="{ 'hidden': state.openPanel !== 'panel3' }">
<div class="p-4 text-gray-600">
This approach provides a clean separation between state and presentation. It's declarative, easy to understand, and
reduces the complexity of managing UI state. Plus, it works great with server-rendered HTML!
</div>
</div>
</div>
<!-- Status indicator -->
<div class="mt-4 text-sm text-gray-500 text-center">
Currently open:
<span live:text="state.openPanel || 'None'">None</span>
</div>
</div>
</div>
<!-- 6. Live Search Results -->
<div class="bg-white rounded-lg shadow-md p-6 space-y-4">
<a href="#live-search" id="live-search" class="text-xl font-semibold text-gray-700 scroll-mt-12 hover:underline"
>6. Live Search Results</a
>
<div data-controller="live">
<script type="text/template" data-live-computed="items">
['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry', 'Fig', 'Grape']
</script>
<script type="text/template" data-live-computed="hasQuery">
state.query !== ''
</script>
<script type="text/template" data-live-computed="filteredResults">
computed.items.filter(item => item.toLowerCase().includes(state.query.toLowerCase()))
</script>
<script type="text/template" data-live-computed="resultsText">
computed.hasQuery ? computed.filteredResults.length + ' results found' : computed.items.length + ' items available'
</script>
<script type="text/template" data-live-computed="hasNoResults">
computed.hasQuery && computed.filteredResults.length === 0
</script>
<script type="text/template" data-live-computed="itemsHtml">
computed.filteredResults.map(item => `
<div class="p-2 bg-gray-50 rounded border-l-4 ${computed.hasQuery ? 'border-blue-500' : 'border-gray-300'}">
${item}
</div>
`).join('')
</script>
<input
type="text"
placeholder="Search fruits..."
value=""
data-live-state="query"
data-action="input->live#update"
class="w-full p-3 border border-gray-300 rounded-lg mb-4"
/>
<!-- Results counter -->
<div class="text-sm text-gray-600 mb-2">
<span live:text="computed.resultsText">7 items available</span>
</div>
<!-- Results list -->
<div class="space-y-2" live:html="computed.itemsHtml"></div>
<!-- No results message -->
<div live:show="computed.hasNoResults">
<p class="text-center text-gray-500 mt-4">No results found for "<span live:text="state.query"></span>"</p>
</div>
</div>
</div>
<!-- 7. Character Counter & Text Analysis -->
<div class="bg-white rounded-lg shadow-md p-6 space-y-4">
<a href="#character-counter" id="character-counter" class="text-xl font-semibold text-gray-700 scroll-mt-12 hover:underline"
>7. Character Counter & Text Analysis</a
>
<div data-controller="live">
<!-- Computed States Templates -->
<script type="text/template" data-live-computed="lineCount">
state.text.split('\n').length
</script>
<script type="text/template" data-live-computed="wordCount">
state.text.trim().split(/\s+/).filter(word => word.length > 0).length
</script>
<script type="text/template" data-live-computed="readingTime">
Math.ceil(computed.wordCount / 200)
</script>
<script type="text/template" data-live-computed="usagePercentage">
Math.round((state.text.length / state.maxLength) * 100)
</script>
<script type="text/template" data-live-computed="isOverLimit">
state.text.length > state.maxLength
</script>
<script type="text/template" data-live-computed="isNearLimit">
state.text.length > state.maxLength * 0.8 && state.text.length <= state.maxLength
</script>
<script type="text/template" data-live-computed="isComfortable">
state.text.length > 0 && state.text.length <= state.maxLength * 0.8
</script>
<script type="text/template" data-live-computed="isEmpty">
state.text.length === 0
</script>
<script type="text/template" data-live-computed="isHalfway">
state.text.length > 0 && state.text.length <= state.maxLength * 0.5
</script>
<div class="mb-4">
<label class="block text-sm font-medium text-gray-700 mb-2">Max characters allowed</label>
<input
type="range"
min="50"
max="500"
step="10"
data-live-state="maxLength"
data-action="input->live#update"
class="w-full"
/>
<div class="text-center text-sm text-gray-600 mt-1" live:text="state.maxLength + ' characters max'">280 characters max</div>
</div>
<div class="relative">
<textarea
placeholder="Start typing your message..."
data-live-state="text"
data-action="input->live#update"
live:class="{
'border-red-500 bg-red-50': computed.isOverLimit,
'border-yellow-500 bg-yellow-50': computed.isNearLimit,
'border-green-500 bg-green-50': computed.isComfortable
}"
class="w-full p-3 border border-gray-300 rounded-lg resize-none h-32 transition-colors"
>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus laoreet elit efficitur, feugiat ex vitae, auctor ex. Sed mollis diam et mollis volutpat.</textarea
>
<!-- Character count overlay -->
<div
class="absolute bottom-3 right-3 px-2 py-1 rounded text-xs font-medium text-white"
live:class="{
'bg-red-600': computed.isOverLimit,
'bg-yellow-600': computed.isNearLimit,
'bg-green-600': computed.isComfortable,
'bg-gray-500': computed.isEmpty
}"
live:text="state.text.length + '/' + state.maxLength"
>
0/280
</div>
</div>
<!-- Text Statistics -->
<div class="grid grid-cols-2 md:grid-cols-4 gap-4 mt-4">
<div class="bg-gray-50 p-3 rounded text-center">
<div class="text-lg font-bold text-gray-800" live:text="state.text.length">0</div>
<div class="text-sm text-gray-600">Characters</div>
</div>
<div class="bg-gray-50 p-3 rounded text-center">
<div class="text-lg font-bold text-gray-800" live:text="computed.wordCount">0</div>
<div class="text-sm text-gray-600">Words</div>
</div>
<div class="bg-gray-50 p-3 rounded text-center">
<div class="text-lg font-bold text-gray-800" live:text="computed.lineCount">1</div>
<div class="text-sm text-gray-600">Lines</div>
</div>
<div class="bg-gray-50 p-3 rounded text-center">
<div class="text-lg font-bold text-gray-800" live:text="computed.readingTime">0</div>
<div class="text-sm text-gray-600">Est. Reading (min)</div>
</div>
</div>
<!-- Progress Bar -->
<div class="mt-4">
<div class="flex justify-between items-center mb-2">
<span class="text-sm font-medium text-gray-700">Usage:</span>
<span
class="text-sm font-bold"
live:class="{
'text-red-600': computed.isOverLimit,
'text-yellow-600': computed.isNearLimit,
'text-green-600': !computed.isOverLimit && !computed.isNearLimit
}"
live:text="computed.usagePercentage + '%'"
>
0%
</span>
</div>
<div class="w-full bg-gray-200 rounded-full h-3">
<div
class="h-3 rounded-full transition-all duration-150"
live:class="{
'bg-red-500': computed.isOverLimit,
'bg-yellow-500': computed.isNearLimit,
'bg-green-500': !computed.isOverLimit && !computed.isNearLimit
}"
live:style="{ width: Math.min(100, computed.usagePercentage) + '%' }"
></div>
</div>
</div>
<!-- Status Messages -->
<div class="mt-4 space-y-2">
<div
class="text-sm p-2 rounded"
live:class="{
'hidden': !computed.isOverLimit,
'bg-red-100 text-red-700 border border-red-200': computed.isOverLimit
}"
>
⚠️ You've exceeded the character limit by
<span live:text="Math.max(0, state.text.length - state.maxLength)">0</span> characters.
</div>
<div
class="text-sm p-2 rounded"
live:class="{
'hidden': !computed.isNearLimit,
'bg-yellow-100 text-yellow-700 border border-yellow-200': computed.isNearLimit
}"
>
⚡ Approaching character limit - <span live:text="state.maxLength - state.text.length">0</span> characters remaining.
</div>
<div
class="text-sm p-2 rounded"
live:class="{
'hidden': !computed.isHalfway,
'bg-green-100 text-green-700 border border-green-200': computed.isHalfway
}"
>
✅ Looking good! Plenty of room for more content.
</div>
</div>
</div>
</div>
<!-- 8. Image Slideshow -->
<div class="bg-white rounded-lg shadow-md p-6 space-y-4">
<a href="#image-slideshow" id="image-slideshow" class="text-xl font-semibold text-gray-700 scroll-mt-12 hover:underline">
8. Image Slideshow
</a>
<div data-controller="live">
<!-- State -->
<input type="hidden" data-live-state="currentSlide" value="0" />
<!-- Events -->
<div
data-action="keydown.left@document->live#update"
data-live-update-param="state.currentSlide = Math.max(0, state.currentSlide - 1)"
></div>
<div
data-action="keydown.right@document->live#update"
data-live-update-param="state.currentSlide = Math.min(computed.totalSlides - 1, state.currentSlide + 1)"
></div>
<!-- Computed States -->
<script type="text/template" data-live-computed="slides">
[
{
url: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&h=400&fit=crop',
title: 'Mountain Vista',
description: 'Breathtaking mountain landscape at sunrise'
},
{
url: 'https://images.unsplash.com/photo-1439066615861-d1af74d74000?w=800&h=400&fit=crop',
title: 'Lake Reflection',
description: 'Serene lake with perfect mountain reflections'
},
{
url: 'https://images.unsplash.com/photo-1501594907352-04cda38ebc29?w=800&h=400&fit=crop',
title: 'Forest Path',
description: 'Mystical forest trail in golden morning light'
},
{
url: 'https://images.unsplash.com/photo-1518837695005-2083093ee35b?w=800&h=400&fit=crop',
title: 'Ocean Sunset',
description: 'Dramatic sunset over calm ocean waters'
}
]
</script>
<script type="text/template" data-live-computed="totalSlides">
computed.slides.length
</script>
<script type="text/template" data-live-computed="currentSlideData">
computed.slides[state.currentSlide]
</script>
<script type="text/template" data-live-computed="canGoPrevious">
state.currentSlide > 0
</script>
<script type="text/template" data-live-computed="canGoNext">
state.currentSlide < computed.totalSlides - 1
</script>
<script type="text/template" data-live-computed="slideCounter">
`${state.currentSlide + 1} / ${computed.totalSlides}`
</script>
<!-- Main Slideshow Container -->
<div class="relative bg-gray-100 rounded-lg overflow-hidden" style="height: 400px">
<!-- Current Image -->
<img
live:attributes="{ src: computed.currentSlideData.url, alt: computed.currentSlideData.title }"
class="w-full h-full object-cover transition-opacity duration-300"
/>
<!-- Navigation Buttons -->
<button
data-action="click->live#update"
data-live-update-param="state.currentSlide = Math.max(0, state.currentSlide - 1)"
live:disabled="!computed.canGoPrevious"
class="absolute left-4 top-1/2 transform -translate-y-1/2 w-12 h-12 rounded-full flex items-center justify-center transition-all duration-200"
live:class="{
'bg-white/80 hover:bg-white/90 text-gray-800 shadow-lg': computed.canGoPrevious,
'bg-gray-300/50 text-gray-400 cursor-not-allowed': !computed.canGoPrevious
}"
>
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path>
</svg>
</button>
<button
data-action="click->live#update"
data-live-update-param="state.currentSlide = Math.min(computed.totalSlides - 1, state.currentSlide + 1)"
live:disabled="!computed.canGoNext"
class="absolute right-4 top-1/2 transform -translate-y-1/2 w-12 h-12 rounded-full flex items-center justify-center transition-all duration-200"
live:class="{
'bg-white/80 hover:bg-white/90 text-gray-800 shadow-lg': computed.canGoNext,
'bg-gray-300/50 text-gray-400 cursor-not-allowed': !computed.canGoNext
}"
>
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path>
</svg>
</button>
<!-- Slide Counter -->
<div class="absolute top-4 right-4 bg-black/50 text-white px-3 py-1 rounded-full text-sm font-medium">
<span live:text="computed.slideCounter">1 / 4</span>
</div>
</div>
<!-- Slide Information -->
<div class="mt-4 text-center">
<h3 class="text-xl font-semibold text-gray-800" live:text="computed.currentSlideData.title">Mountain Vista</h3>
<p class="text-gray-600 mt-1" live:text="computed.currentSlideData.description">Breathtaking mountain landscape at sunrise</p>
</div>
<!-- Dot Indicators -->
<div class="flex justify-center space-x-2 mt-4">
<button
data-action="click->live#update"
data-live-update-param="state.currentSlide = 0"
class="w-3 h-3 rounded-full transition-all duration-200"
live:class="{
'bg-blue-600': state.currentSlide === 0,
'bg-gray-300 hover:bg-gray-400': state.currentSlide !== 0
}"
title="Slide 1"
></button>
<button
data-action="click->live#update"
data-live-update-param="state.currentSlide = 1"
class="w-3 h-3 rounded-full transition-all duration-200"
live:class="{
'bg-blue-600': state.currentSlide === 1,
'bg-gray-300 hover:bg-gray-400': state.currentSlide !== 1
}"
title="Slide 2"
></button>
<button
data-action="click->live#update"
data-live-update-param="state.currentSlide = 2"
class="w-3 h-3 rounded-full transition-all duration-200"
live:class="{
'bg-blue-600': state.currentSlide === 2,
'bg-gray-300 hover:bg-gray-400': state.currentSlide !== 2
}"
title="Slide 3"
></button>
<button
data-action="click->live#update"
data-live-update-param="state.currentSlide = 3"
class="w-3 h-3 rounded-full transition-all duration-200"
live:class="{
'bg-blue-600': state.currentSlide === 3,
'bg-gray-300 hover:bg-gray-400': state.currentSlide !== 3
}"
title="Slide 4"
></button>
</div>
<!-- Slideshow Controls -->
<div class="mt-6 flex justify-center space-x-3">
<button
data-action="click->live#update"
data-live-update-param="state.currentSlide = 0"
class="px-4 py-2 text-sm font-medium bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg transition-colors"
>
First
</button>
<button
data-action="click->live#update"
data-live-update-param="state.currentSlide = Math.floor(Math.random() * computed.totalSlides)"
class="px-4 py-2 text-sm font-medium bg-purple-100 hover:bg-purple-200 text-purple-700 rounded-lg transition-colors"
>
Random
</button>
<button
data-action="click->live#update"
data-live-update-param="state.currentSlide = computed.totalSlides - 1"
class="px-4 py-2 text-sm font-medium bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg transition-colors"
>
Last
</button>
</div>
<!-- Help Text -->
<div class="mt-4 text-sm text-gray-500 text-center">
Use the arrow buttons, click the dots, or use the control buttons to navigate through the slideshow.
</div>
</div>
</div>
<!-- 9. Subdomain Input with Validation -->
<div class="bg-white rounded-lg shadow-md p-6 space-y-4">
<a href="#subdomain-validation" id="subdomain-validation" class="text-xl font-semibold text-gray-700 scroll-mt-12 hover:underline"
>9. Subdomain Input with Validation</a
>
<div data-controller="live">
<script type="text/template" data-live-computed="containsDomain">
state.subdomain.toLowerCase().includes('example.com')
</script>
<script type="text/template" data-live-computed="isEmpty">
state.subdomain.length === 0
</script>
<script type="text/template" data-live-computed="isValid">
state.subdomain.length > 0 && !computed.containsDomain && /^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$/.test(state.subdomain)
</script>
<div class="space-y-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Enter your subdomain</label>
<div class="relative">
<input
type="text"
placeholder="www"
value="www.example.com"
data-live-state="subdomain"
data-action="input->live#update"
live:class="{
'border-red-500 bg-red-50': !computed.isValid && !computed.isEmpty,
'border-green-500 bg-green-50': computed.isValid,
'border-gray-300': computed.isEmpty,
}"
class="w-full pl-3 pr-32 py-2 border rounded-lg transition-colors focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
/>
<div class="absolute inset-y-0 right-0 flex items-center pr-3 text-gray-500 text-sm font-medium">.example.com</div>
</div>
</div>
<!-- Preview of full domain -->
<div live:show="state.subdomain.length > 0" class="p-3 bg-gray-50 rounded-lg">
<div class="text-sm text-gray-600 mb-1">Your full domain will be:</div>
<div
class="font-mono text-lg font-medium"
live:class="{
'text-red-600': computed.containsDomain,
'text-green-600': computed.isValid,
'text-gray-800': !computed.containsDomain && !computed.isValid
}"
live:text="state.subdomain + '.example.com'"