-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasecoat.cfc
More file actions
3148 lines (2873 loc) · 121 KB
/
Basecoat.cfc
File metadata and controls
3148 lines (2873 loc) · 121 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
/**
* wheels-basecoat Plugin
* Basecoat UI component helpers for Wheels.
* Generates shadcn/ui-quality HTML using Basecoat CSS classes.
* Works with or without wheels-hotwire.
*/
component output="false" {
function init() {
// Source of truth: package.json. Reading at init keeps init()'s
// version field and the manifest from drifting (a recurring bug
// pre-2.0 — see Wheels Tutorial Finding #N).
this.version = $readPackageVersion();
return this;
}
private string function $readPackageVersion() {
try {
var pkgPath = getDirectoryFromPath(getCurrentTemplatePath()) & "package.json";
if (fileExists(pkgPath)) {
var pkg = deserializeJSON(fileRead(pkgPath, "utf-8"));
if (structKeyExists(pkg, "version") && len(pkg.version)) return pkg.version;
}
} catch (any e) {}
return "0.0.0-unknown";
}
// ==============================================
// INCLUDES
// ==============================================
/**
* Renders the <link>/<script> tags for Basecoat CSS + JS in the layout <head>.
*
* Defaults assume the package's bundled assets have been published to the
* app's public/ directory at `/assets/basecoat/...` — the recommended
* install does `cp -r vendor/wheels-basecoat/assets/basecoat public/assets/basecoat`.
* Override `cssPath` / `jsPath` if you've published them elsewhere or wish
* to use a CDN.
*
* @cssPath URL to basecoat.min.css. Default points at the published bundled asset.
* @jsPath URL to basecoat all-in-one JS bundle. Default points at the published bundled asset.
* @basecoatJS Load basecoat-js (drives tabs, dropdown, popover, select, command, sidebar, toast). Default true.
* @alpine Optionally load Alpine.js (no longer required for any built-in helper). Default false.
* @alpineVersion Alpine major version to load when alpine=true.
* @turboAware Emit `<meta name="turbo-cache-control" content="no-preview">` so Turbo doesn't cache stale dialog/popover snapshots.
*/
public string function basecoatIncludes(
string cssPath = "/assets/basecoat/basecoat.min.css",
string extrasCssPath = "/assets/basecoat/wheels-basecoat-extras.min.css",
string jsPath = "/assets/basecoat/js/all.min.js",
string uiJsPath = "/assets/basecoat/js/wheels-basecoat-ui.min.js",
boolean basecoatJS = true,
boolean uiJS = true,
boolean extrasCSS = true,
boolean alpine = false,
string alpineVersion = "3",
boolean turboAware = true
) {
var local = {};
savecontent variable="local.html" {
writeOutput(
(arguments.turboAware ? '<meta name="turbo-cache-control" content="no-preview">' & chr(10) : '')
& '<link rel="stylesheet" href="#arguments.cssPath#">' & chr(10)
& (arguments.extrasCSS ? '<link rel="stylesheet" href="#arguments.extrasCssPath#">' & chr(10) : '')
& (arguments.basecoatJS ? '<script defer src="#arguments.jsPath#"></script>' & chr(10) : '')
& (arguments.uiJS ? '<script defer src="#arguments.uiJsPath#"></script>' & chr(10) : '')
& (arguments.alpine ? '<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@#arguments.alpineVersion#/dist/cdn.min.js"></script>' & chr(10) : '')
);
}
return trim(local.html);
}
// ==============================================
// ARG VALIDATION
// ==============================================
/**
* Validates that `value` appears in `allowed` and throws a clear error
* naming both the helper and the bad value otherwise. The default
* Wheels behavior would silently produce e.g. `class="btn-primay"`
* (typo) and leave the developer puzzling at the unstyled button.
*
* Declared `public` despite the `$`-prefix-internal naming because
* Wheels' `PackageLoader` only mixes the package's PUBLIC methods into
* the target scope — and the public helpers that call this function
* (uiButton, uiBadge, uiAlert, uiField, uiToast, uiButtonGroup,
* uiThemeToggle, turboStream) all invoke it from the mixed-in
* variables-scope context. Same pattern as `$uiLucideIcon` /
* `$uiBuildId`. The leading `$` keeps signalling "internal — don't call
* from app code" while keeping the method visible across the mixin
* boundary.
*/
public void function $validateEnum(
required string value,
required string allowed,
required string helper,
required string argument
) {
if (!ListFindNoCase(arguments.allowed, arguments.value)) {
throw(
type = "WheelsBasecoat.InvalidArgument",
message = "#arguments.helper#() received an unsupported #arguments.argument# value: '#arguments.value#'.",
detail = "Allowed values are: #arguments.allowed#."
);
}
}
// ==============================================
// BUTTONS
// ==============================================
/**
* Basecoat button. Variants: primary, secondary, destructive, outline, ghost, link. Sizes: sm, md, lg.
*/
public string function uiButton(
string text = "",
string variant = "primary",
string size = "md",
string icon = "",
string href = "",
string type = "button",
boolean disabled = false,
boolean loading = false,
boolean close = false,
string class = "",
string id = "",
string ariaLabel = "",
string turboConfirm = "",
string turboMethod = ""
) {
$validateEnum(arguments.variant, "primary,secondary,destructive,outline,ghost,link", "uiButton", "variant");
$validateEnum(arguments.size, "sm,md,lg", "uiButton", "size");
var local = {};
// Build Basecoat compound class: btn[-size][-icon]-variant
// Always emit the variant suffix so rendered HTML self-documents
// (e.g. `btn-primary` instead of bare `btn`). basecoat-css ships
// matching selectors for every size×variant combination.
local.isIconOnly = len(arguments.icon) && !len(arguments.text);
local.parts = [];
if (arguments.size != "md") arrayAppend(local.parts, arguments.size);
if (local.isIconOnly) arrayAppend(local.parts, "icon");
arrayAppend(local.parts, arguments.variant);
local.cls = "btn-" & arrayToList(local.parts, "-");
if (len(arguments.class)) local.cls &= " " & arguments.class;
// Inner content: icon + text
local.inner = "";
if (arguments.loading) {
local.inner = $uiLucideIcon("loader", 24, 2, "animate-spin");
} else if (len(arguments.icon)) {
local.inner = $uiLucideIcon(arguments.icon, 24);
}
if (len(arguments.text)) {
if (len(local.inner)) local.inner &= " ";
local.inner &= arguments.text;
}
// Attributes
local.attrs = 'class="#local.cls#"';
if (len(arguments.id)) local.attrs &= ' id="#arguments.id#"';
if (arguments.disabled || arguments.loading) local.attrs &= " disabled";
if (len(arguments.ariaLabel)) local.attrs &= ' aria-label="#arguments.ariaLabel#"';
if (arguments.close) local.attrs &= " onclick=""this.closest('dialog').close()""";
if (len(arguments.turboConfirm)) local.attrs &= ' data-turbo-confirm="#arguments.turboConfirm#"';
if (len(arguments.turboMethod)) local.attrs &= ' data-turbo-method="#arguments.turboMethod#"';
if (len(arguments.href))
return '<a href="#arguments.href#" #local.attrs#>#local.inner#</a>';
return '<button type="#arguments.type#" #local.attrs#>#local.inner#</button>';
}
// ==============================================
// BADGES
// ==============================================
/** Basecoat badge. Variants: default, secondary, destructive, outline. */
public string function uiBadge(required string text, string variant = "default", string class = "") {
$validateEnum(arguments.variant, "default,secondary,destructive,outline", "uiBadge", "variant");
var cls = (arguments.variant == "default") ? "badge" : "badge-#arguments.variant#";
if (len(arguments.class)) cls &= " " & arguments.class;
return '<span class="#cls#">#arguments.text#</span>';
}
// ==============================================
// ICONS
// ==============================================
/** Renders a Lucide SVG icon by name. */
public string function uiIcon(required string name, numeric size = 24, numeric strokeWidth = 2, string class = "") {
return $uiLucideIcon(arguments.name, arguments.size, arguments.strokeWidth, arguments.class);
}
// ==============================================
// SIMPLE COMPONENTS
// ==============================================
/** Basecoat loading spinner. */
public string function uiSpinner(string class = "") {
var cls = "spinner";
if (len(arguments.class)) cls &= " " & arguments.class;
return '<div class="#cls#"></div>';
}
/** Basecoat skeleton loading placeholder. Specify lines for multiple, or use height/width for custom. */
public string function uiSkeleton(numeric lines = 1, string height = "h-4", string width = "w-full", string class = "") {
var cls = "skeleton #arguments.height# #arguments.width#";
if (len(arguments.class)) cls &= " " & arguments.class;
if (arguments.lines == 1)
return '<div class="#cls#"></div>';
var html = "";
for (var i = 1; i <= arguments.lines; i++) {
html &= '<div class="#cls#"></div>';
if (i < arguments.lines) html &= chr(10);
}
return html;
}
/** Basecoat progress bar. */
public string function uiProgress(required numeric value, string class = "") {
var cls = "progress";
if (len(arguments.class)) cls &= " " & arguments.class;
return '<div class="#cls#"><div class="progress-indicator" style="width: #arguments.value#%"></div></div>';
}
/** Basecoat horizontal separator. */
public string function uiSeparator(string class = "") {
var cls = "separator";
if (len(arguments.class)) cls &= " " & arguments.class;
return '<hr class="#cls#" />';
}
/** Opens a Basecoat tooltip wrapper. Place trigger element inside, close with uiTooltipEnd(). */
public string function uiTooltip(required string tip, string class = "") {
var cls = "tooltip";
if (len(arguments.class)) cls &= " " & arguments.class;
return '<span class="#cls#" data-tip="#arguments.tip#">';
}
public string function uiTooltipEnd() {
return '</span>';
}
// ==============================================
// ALERTS
// ==============================================
/**
* Basecoat alert. Self-closing (returns complete element). Variants: default, destructive.
*/
public string function uiAlert(
string title = "",
string description = "",
string variant = "default",
string icon = "",
string class = ""
) {
$validateEnum(arguments.variant, "default,destructive", "uiAlert", "variant");
var local = {};
local.cls = "alert";
if (arguments.variant == "destructive") local.cls &= " alert-destructive";
if (len(arguments.class)) local.cls &= " " & arguments.class;
// Default icons by variant
local.iconName = len(arguments.icon) ? arguments.icon : (arguments.variant == "destructive" ? "alert-triangle" : "info");
savecontent variable="local.html" {
writeOutput(
'<div class="#local.cls#" role="alert">' & chr(10)
& $uiLucideIcon(local.iconName, 16) & chr(10)
& (len(arguments.title) ? '<h5>#arguments.title#</h5>' & chr(10) : '')
& (len(arguments.description) ? '<section><p>#arguments.description#</p></section>' & chr(10) : '')
& '</div>'
);
}
return trim(local.html);
}
// ==============================================
// CARDS
// ==============================================
//
// basecoat-css 0.3.x styles cards via the semantic-element selectors
// `.card > header`, `.card > section`, `.card > footer` (rather than the
// older `.card-header` / `.card-content` / `.card-footer` class hooks).
// These helpers emit the matching semantic markup.
/** Opens a Basecoat card. Close with uiCardEnd(). */
public string function uiCard(string class = "") {
var cls = "card";
if (len(arguments.class)) cls &= " " & arguments.class;
return '<div class="#cls#">';
}
/**
* Card header with optional title and description. Self-closing.
* Renders an <h2> for the title — basecoat-css 0.3.x targets `.card > header h2`
* for the title typography. The `description` renders as a `<p>` sibling.
*/
public string function uiCardHeader(string title = "", string description = "", string class = "") {
var local = {};
var classAttr = len(arguments.class) ? ' class="#arguments.class#"' : "";
savecontent variable="local.html" {
writeOutput(
'<header#classAttr#>' & chr(10)
& (len(arguments.title) ? '<h2>#arguments.title#</h2>' & chr(10) : '')
& (len(arguments.description) ? '<p>#arguments.description#</p>' & chr(10) : '')
& '</header>'
);
}
return trim(local.html);
}
/** Opens card content section. Close with uiCardContentEnd(). */
public string function uiCardContent(string class = "") {
var classAttr = len(arguments.class) ? ' class="#arguments.class#"' : "";
return '<section#classAttr#>';
}
public string function uiCardContentEnd() {
return '</section>';
}
/** Opens card footer section. Close with uiCardFooterEnd(). */
public string function uiCardFooter(string class = "") {
var classAttr = len(arguments.class) ? ' class="#arguments.class#"' : "";
return '<footer#classAttr#>';
}
public string function uiCardFooterEnd() {
return '</footer>';
}
public string function uiCardEnd() {
return '</div>';
}
// ==============================================
// LUCIDE ICON SVG HELPER (intentionally public — see note below)
// ==============================================
/**
* Returns SVG markup for a Lucide icon. Extend the icon map as needed.
*
* Declared `public` despite the `$`-prefix-internal naming because Wheels'
* `PackageLoader` only mixes the package's PUBLIC methods into the target
* scope (controllers in this case). Sibling helpers like `uiButton(icon=...)`,
* `uiAlert`, and `uiPagination` invoke `$uiLucideIcon` from within the
* mixed-in variables-scope context — if it stays `private`, those calls
* blow up with `No matching function [$UILUCIDEICON] found`. The leading
* `$` keeps signalling "internal — don't call from app code" while keeping
* the method visible across the package after PackageLoader integrates
* the methods. See wheels-basecoat#2 / Wheels Tutorial Finding #14.
*/
public string function $uiLucideIcon(required string name, numeric size = 24, numeric strokeWidth = 2, string class = "") {
var icons = {
"plus": '<line x1="12" x2="12" y1="5" y2="19"/><line x1="5" x2="19" y1="12" y2="12"/>',
"trash": '<path d="M3 6h18"/><path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"/><path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"/>',
"pencil": '<path d="M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z"/><path d="m15 5 4 4"/>',
"x": '<path d="M18 6 6 18"/><path d="m6 6 12 12"/>',
"check": '<path d="M20 6 9 17l-5-5"/>',
"chevron-right": '<path d="m9 18 6-6-6-6"/>',
"chevron-left": '<path d="m15 18-6-6 6-6"/>',
"search": '<circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/>',
"loader": '<path d="M12 2v4"/><path d="m16.2 7.8 2.9-2.9"/><path d="M18 12h4"/><path d="m16.2 16.2 2.9 2.9"/><path d="M12 18v4"/><path d="m4.9 19.1 2.9-2.9"/><path d="M2 12h4"/><path d="m4.9 4.9 2.9 2.9"/>',
"alert-triangle": '<path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/>',
"info": '<circle cx="12" cy="12" r="10"/><path d="M12 16v-4"/><path d="M12 8h.01"/>',
"check-circle": '<path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><path d="m9 11 3 3L22 4"/>',
"send": '<path d="M14.536 21.686a.5.5 0 0 0 .937-.024l6.5-19a.496.496 0 0 0-.635-.635l-19 6.5a.5.5 0 0 0-.024.937l7.93 3.18a2 2 0 0 1 1.112 1.11z"/><path d="m21.854 2.147-10.94 10.939"/>',
"ellipsis": '<circle cx="12" cy="12" r="1"/><circle cx="19" cy="12" r="1"/><circle cx="5" cy="12" r="1"/>',
"external-link": '<path d="M15 3h6v6"/><path d="M10 14 21 3"/><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/>',
"sun": '<circle cx="12" cy="12" r="4"/><path d="M12 2v2"/><path d="M12 20v2"/><path d="m4.93 4.93 1.41 1.41"/><path d="m17.66 17.66 1.41 1.41"/><path d="M2 12h2"/><path d="M20 12h2"/><path d="m6.34 17.66-1.41 1.41"/><path d="m19.07 4.93-1.41 1.41"/>',
"moon": '<path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"/>',
"log-out": '<path d="m16 17 5-5-5-5"/><path d="M21 12H9"/><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/>',
"log-in": '<path d="m10 17 5-5-5-5"/><path d="M15 12H3"/><path d="M9 3h10a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H9"/>',
"user": '<path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/>',
"settings": '<path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z"/><circle cx="12" cy="12" r="3"/>',
"menu": '<line x1="4" x2="20" y1="12" y2="12"/><line x1="4" x2="20" y1="6" y2="6"/><line x1="4" x2="20" y1="18" y2="18"/>',
"home": '<path d="m3 9 9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/><polyline points="9 22 9 12 15 12 15 22"/>',
"file-text": '<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" x2="8" y1="13" y2="13"/><line x1="16" x2="8" y1="17" y2="17"/><polyline points="10 9 9 9 8 9"/>',
"star": '<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/>',
"copy": '<rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/>',
"upload": '<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" x2="12" y1="3" y2="15"/>',
"calendar": '<rect width="18" height="18" x="3" y="4" rx="2" ry="2"/><line x1="16" x2="16" y1="2" y2="6"/><line x1="8" x2="8" y1="2" y2="6"/><line x1="3" x2="21" y1="10" y2="10"/>'
};
var paths = structKeyExists(icons, arguments.name) ? icons[arguments.name] : '<circle cx="12" cy="12" r="10"/>';
var classAttr = len(arguments.class) ? ' class="#arguments.class#"' : "";
return '<svg xmlns="http://www.w3.org/2000/svg" width="#arguments.size#" height="#arguments.size#" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="#arguments.strokeWidth#" stroke-linecap="round" stroke-linejoin="round"#classAttr#>#paths#</svg>';
}
// ==============================================
// ID GENERATION (intentionally public — see $uiLucideIcon note above)
// ==============================================
/**
* Generates a short unique ID if none provided.
*
* Declared `public` for the same reason as `$uiLucideIcon` — `uiField`,
* `uiInput`, `uiCheckbox`, `uiTextarea`, etc. all call `$uiBuildId` from
* the mixed-in scope, and Wheels' `PackageLoader` only carries public
* methods across to the target. The `$` prefix still signals "internal".
*/
public string function $uiBuildId(string providedId = "", string prefix = "ui") {
if (len(arguments.providedId))
return arguments.providedId;
return arguments.prefix & "-" & replace(left(createUUID(), 8), "-", "", "all");
}
// ==============================================
// DIALOGS
// ==============================================
/**
* Opens a Basecoat dialog (native <dialog> element). Close with uiDialogEnd().
* Optionally renders a trigger button.
*/
public string function uiDialog(
required string title,
string description = "",
string triggerText = "",
string triggerClass = "btn-outline",
string id = "",
string maxWidth = "sm:max-w-[425px]",
string class = ""
) {
var local = {};
local.id = $uiBuildId(arguments.id, "dlg");
local.cls = "dialog w-full #arguments.maxWidth# max-h-[612px]";
if (len(arguments.class)) local.cls &= " " & arguments.class;
savecontent variable="local.html" {
// CSP-friendly trigger / close — handlers are delegated by
// wheels-basecoat-ui.js (loaded by `basecoatIncludes()`) via
// `data-ui-dialog-open` / `data-ui-dialog-close` and a
// click-on-backdrop dispatcher. No inline `onclick` required.
writeOutput(
(len(arguments.triggerText) ? '<button type="button" data-ui-dialog-open="#local.id#" class="#arguments.triggerClass#">#arguments.triggerText#</button>' & chr(10) : '')
& '<dialog id="#local.id#" class="#local.cls#" aria-labelledby="#local.id#-title"'
& (len(arguments.description) ? ' aria-describedby="#local.id#-desc"' : '')
& '>' & chr(10)
& '<div>' & chr(10)
& '<header>' & chr(10)
& '<h2 id="#local.id#-title">#arguments.title#</h2>' & chr(10)
& (len(arguments.description) ? '<p id="#local.id#-desc">#arguments.description#</p>' & chr(10) : '')
& '</header>' & chr(10)
& '<section>'
);
}
return trim(local.html);
}
/** Closes the dialog content section and opens the footer section. */
public string function uiDialogFooter() {
return '</section><footer>';
}
/** Closes the dialog footer, adds the close (X) button, and closes the dialog element. */
public string function uiDialogEnd() {
var xIcon = $uiLucideIcon("x", 24, 2);
return '</footer><button type="button" aria-label="Close dialog" data-ui-dialog-close>#xIcon#</button></div></dialog>';
}
// ==============================================
// FORM FIELDS
// ==============================================
/**
* Basecoat form field. Handles text, email, password, number, tel, url, textarea, select, checkbox, switch.
*/
public string function uiField(
required string label,
required string name,
string type = "text",
string value = "",
string id = "",
string placeholder = "",
string description = "",
string errorMessage = "",
boolean required = false,
boolean disabled = false,
boolean checked = false,
string options = "",
numeric rows = 4,
string class = ""
) {
$validateEnum(arguments.type, "text,email,password,number,tel,url,date,datetime-local,time,search,textarea,select,checkbox,switch", "uiField", "type");
var local = {};
local.id = $uiBuildId(arguments.id, "fld");
local.hasError = len(arguments.errorMessage);
local.isToggle = (arguments.type == "checkbox" || arguments.type == "switch");
// Build input attrs common to all types
local.commonAttrs = 'id="#local.id#" name="#arguments.name#"';
if (arguments.required) local.commonAttrs &= " required";
if (arguments.disabled) local.commonAttrs &= " disabled";
savecontent variable="local.html" {
// Wrapper
if (local.isToggle) {
writeOutput('<div class="flex items-center gap-2">' & chr(10));
} else {
writeOutput('<div class="grid gap-2">' & chr(10));
}
// Label before input (non-toggle types)
if (!local.isToggle)
writeOutput('<label for="#local.id#">#arguments.label#</label>' & chr(10));
// Input element by type
if (arguments.type == "textarea") {
writeOutput('<textarea #local.commonAttrs# class="textarea#local.hasError ? " border-destructive" : ""#"');
if (len(arguments.placeholder)) writeOutput(' placeholder="#arguments.placeholder#"');
writeOutput(' rows="#arguments.rows#"');
if (local.hasError) writeOutput(' aria-invalid="true" aria-describedby="#local.id#-error"');
writeOutput('>#arguments.value#</textarea>' & chr(10));
} else if (arguments.type == "select") {
writeOutput('<select #local.commonAttrs# class="select#local.hasError ? " border-destructive" : ""#"');
if (local.hasError) writeOutput(' aria-invalid="true" aria-describedby="#local.id#-error"');
writeOutput('>' & chr(10));
if (len(arguments.options)) {
for (var opt in listToArray(arguments.options)) {
var optParts = listToArray(opt, ":");
var optVal = optParts[1];
var optLabel = (arrayLen(optParts) > 1) ? optParts[2] : optParts[1];
writeOutput('<option value="#optVal#"#arguments.value == optVal ? " selected" : ""#>#optLabel#</option>' & chr(10));
}
}
writeOutput('</select>' & chr(10));
} else if (arguments.type == "checkbox") {
writeOutput('<input type="checkbox" #local.commonAttrs# class="checkbox#len(arguments.class) ? " " & arguments.class : ""#"');
if (arguments.checked) writeOutput(' checked');
if (local.hasError) writeOutput(' aria-invalid="true" aria-describedby="#local.id#-error"');
writeOutput(' />' & chr(10));
} else if (arguments.type == "switch") {
writeOutput('<input type="checkbox" #local.commonAttrs# class="switch#len(arguments.class) ? " " & arguments.class : ""#" role="switch"');
if (arguments.checked) writeOutput(' checked');
if (local.hasError) writeOutput(' aria-invalid="true" aria-describedby="#local.id#-error"');
writeOutput(' />' & chr(10));
} else {
writeOutput('<input type="#arguments.type#" #local.commonAttrs# class="input#local.hasError ? " border-destructive" : ""#"');
if (len(arguments.value)) writeOutput(' value="#arguments.value#"');
if (len(arguments.placeholder)) writeOutput(' placeholder="#arguments.placeholder#"');
if (local.hasError) writeOutput(' aria-invalid="true" aria-describedby="#local.id#-error"');
if (len(arguments.class)) writeOutput(' #arguments.class#');
writeOutput(' />' & chr(10));
}
// Label after input (toggle types)
if (local.isToggle)
writeOutput('<label for="#local.id#">#arguments.label#</label>' & chr(10));
// Description (only when no error)
if (len(arguments.description) && !local.hasError)
writeOutput('<p class="text-sm text-muted-foreground">#arguments.description#</p>' & chr(10));
// Error message
if (local.hasError)
writeOutput('<p id="#local.id#-error" class="text-sm text-destructive">#arguments.errorMessage#</p>' & chr(10));
writeOutput('</div>');
}
return trim(local.html);
}
// ==============================================
// MODEL-BOUND FORM FIELD (Wheels integration)
// ==============================================
/**
* Bind a Basecoat field to a Wheels model object — auto-resolves the
* input value, the error message (when validation has failed), and the
* `name` attribute (`<objectName>[<property>]`) from the controller-scoped
* object.
*
* Mirrors the ergonomics of Wheels' built-in `textField(objectName=,property=)`,
* but renders Basecoat-styled markup (with proper error highlighting and
* description) and supports every type that `uiField` does.
*
* <pre>
* ##startFormTag(action="update", key=post.id)##
* ##uiBoundField(objectName="post", property="title", required=true)##
* ##uiBoundField(objectName="post", property="body", type="textarea", rows=12)##
* ##uiBoundField(objectName="post", property="status", type="select",
* options="draft:Draft,published:Published")##
* ##endFormTag()##
* </pre>
*
* On a failed save, Wheels surfaces validation errors on the model object;
* `uiBoundField` reads `obj.errorsOn(property)` automatically and applies
* the destructive border + error paragraph below the input. Description
* is hidden when an error is present (matching `uiField` behavior).
*
* @objectName Variable name of the model in the current controller scope (e.g. "post").
* @property Property to bind on the model (e.g. "title").
* @label Form label. Defaults to a humanized version of the property name.
* @type Input type (see uiField for full list). Defaults to "text".
* @id Optional explicit input id. Auto-generated if omitted.
* @placeholder Optional placeholder text.
* @description Optional help text below the input. Hidden when an error is shown.
* @required HTML required.
* @disabled HTML disabled.
* @options For select inputs — same format as uiField.
* @rows For textarea inputs — same as uiField.
* @class Extra classes appended to the input element.
*/
public string function uiBoundField(
required string objectName,
required string property,
string label = "",
string type = "text",
string id = "",
string placeholder = "",
string description = "",
boolean required = false,
boolean disabled = false,
string options = "",
numeric rows = 4,
string class = ""
) {
// Look the object up in the surrounding scope. The package methods
// are mixed into the controller via PackageLoader and the view
// renders inside the controller's variables scope, so the model
// object exposed by the action (`post = params.post` etc.) is
// reachable here.
if (!structKeyExists(variables, arguments.objectName)) {
throw(
type = "WheelsBasecoat.ObjectNotFound",
message = "uiBoundField: object '#arguments.objectName#' not found in the current scope.",
detail = "Make sure the controller action exposes the model object (e.g. `post = model('Post').findByKey(params.id)`) before the view renders."
);
}
var obj = variables[arguments.objectName];
// Default label = humanized property name ("publishedAt" → "Published at").
var resolvedLabel = len(arguments.label) ? arguments.label : $humanize(arguments.property);
// Resolve the bound value. Plain structs and Wheels objects both
// expose properties via bracket access; defaulting to empty string
// means "new" objects with no fields render the right empty inputs.
var resolvedValue = "";
try {
resolvedValue = obj[arguments.property] ?: "";
} catch (any e) {
resolvedValue = "";
}
// Datetime values arrive from Lucee/Wheels in formats that don't
// round-trip into HTML form inputs cleanly — strip the surrounding
// quotes some adapters add and reformat date types to the input
// element's expected ISO-ish format.
if (Len(resolvedValue) && (arguments.type == "date" || arguments.type == "datetime-local" || arguments.type == "time")) {
var cleaned = Replace(Trim(resolvedValue), "'", "", "all");
if (IsDate(cleaned)) {
if (arguments.type == "date") {
resolvedValue = DateFormat(cleaned, "yyyy-mm-dd");
} else if (arguments.type == "datetime-local") {
resolvedValue = DateTimeFormat(cleaned, "yyyy-mm-dd'T'HH:nn");
} else {
resolvedValue = TimeFormat(cleaned, "HH:nn");
}
} else {
resolvedValue = cleaned;
}
}
// For toggle types, derive the `checked` flag from the resolved value.
var resolvedChecked = false;
if (arguments.type == "checkbox" || arguments.type == "switch") {
resolvedChecked = (IsBoolean(resolvedValue) && resolvedValue) || (IsNumeric(resolvedValue) && resolvedValue != 0);
resolvedValue = ""; // checkbox/switch ignore value, only emit `checked`.
}
// Read the validation error (if any). Tolerate plain structs that
// don't expose `hasErrors` / `errorsOn` — those simply have none.
var resolvedError = "";
try {
if (IsObject(obj) && obj.hasErrors(arguments.property)) {
var errs = obj.errorsOn(arguments.property);
if (ArrayLen(errs) >= 1) resolvedError = errs[1].message;
}
} catch (any e) {}
return uiField(
label = resolvedLabel,
name = "#arguments.objectName#[#arguments.property#]",
type = arguments.type,
value = resolvedValue,
id = arguments.id,
placeholder = arguments.placeholder,
description = arguments.description,
errorMessage = resolvedError,
required = arguments.required,
disabled = arguments.disabled,
checked = resolvedChecked,
options = arguments.options,
rows = arguments.rows,
class = arguments.class
);
}
/**
* Convert a property name like "publishedAt" / "first_name" into a
* human-readable label like "Published at" / "First name".
*
* Public for the same reason as `$validateEnum` — called from the
* mixed-in scope by `uiBoundField`. (See PackageLoader notes above.)
*/
public string function $humanize(required string property) {
var s = arguments.property;
// camelCase → space-separated
s = REReplace(s, "([a-z])([A-Z])", "\1 \2", "all");
// snake_case → space-separated
s = Replace(s, "_", " ", "all");
// Capitalize first letter only (sentence case).
if (len(s)) s = uCase(left(s, 1)) & lCase(mid(s, 2, len(s) - 1));
return s;
}
// ==============================================
// TABLES
// ==============================================
/** Opens a Basecoat table (table-container + table). Close with uiTableEnd(). */
public string function uiTable(string class = "") {
var cls = "table";
if (len(arguments.class)) cls &= " " & arguments.class;
return '<div class="table-container"><table class="#cls#">';
}
/** Opens the table thead and a tr. Close with uiTableHeaderEnd(). */
public string function uiTableHeader() {
return '<thead><tr>';
}
public string function uiTableHeaderEnd() {
return '</tr></thead>';
}
/** Opens the table tbody. Close with uiTableBodyEnd(). */
public string function uiTableBody() {
return '<tbody>';
}
public string function uiTableBodyEnd() {
return '</tbody>';
}
/** Opens a table tr. Close with uiTableRowEnd(). */
public string function uiTableRow(string class = "") {
if (len(arguments.class))
return '<tr class="#arguments.class#">';
return '<tr>';
}
public string function uiTableRowEnd() {
return '</tr>';
}
/** Renders a th cell. */
public string function uiTableHead(string text = "", string class = "") {
if (len(arguments.class))
return '<th class="#arguments.class#">#arguments.text#</th>';
return '<th>#arguments.text#</th>';
}
/** Renders a td cell. */
public string function uiTableCell(string text = "", string class = "") {
if (len(arguments.class))
return '<td class="#arguments.class#">#arguments.text#</td>';
return '<td>#arguments.text#</td>';
}
public string function uiTableEnd() {
return '</table></div>';
}
// ==============================================
// TABS (driven by basecoat-js's tabs.js)
// ==============================================
//
// basecoat-css 0.3.x styles tabs via ARIA-role selectors:
// .tabs [role=tablist]
// .tabs [role=tablist] [role=tab]
// .tabs [role=tabpanel]
// The active tab carries `aria-selected="true"` + `tabindex="0"`; inactive
// tabs carry `aria-selected="false"` + `tabindex="-1"`. Each tab's
// `aria-controls` matches the panel's `id`. tabs.js handles click +
// arrow-key navigation and toggles the `hidden` attribute on panels to
// show/hide them.
//
// Use a `defaultTab` value on `uiTabs(...)`; trigger and content helpers
// auto-activate when their `value` matches it. The defaultTab is stashed
// in the request scope between `uiTabs()` and `uiTabsEnd()`.
/** Opens a tabs container. Close with uiTabsEnd(). */
public string function uiTabs(string defaultTab = "", string id = "", string class = "") {
var cls = "tabs";
if (len(arguments.class)) cls &= " " & arguments.class;
// Stash the active value + a per-instance ID prefix so that nested
// uiTabTrigger / uiTabContent helpers can auto-pair `aria-controls`.
var prefix = $uiBuildId(arguments.id, "tabs");
request.$basecoatTabs = {
defaultTab: arguments.defaultTab,
prefix: prefix
};
var idAttr = len(arguments.id) ? ' id="#arguments.id#"' : "";
return '<div class="#cls#"#idAttr#>';
}
/** Opens the tablist (the row of triggers). Close with uiTabListEnd(). */
public string function uiTabList(string ariaLabel = "Tabs", string class = "") {
var classAttr = len(arguments.class) ? ' class="#arguments.class#"' : "";
return '<div role="tablist" aria-label="#arguments.ariaLabel#"#classAttr#>';
}
public string function uiTabListEnd() {
return '</div>';
}
/**
* Renders a tab trigger button. Auto-activates when its `value` matches
* the parent `uiTabs(defaultTab=)`. The `aria-controls` attribute targets
* the matching `uiTabContent(value=)` panel id.
*/
public string function uiTabTrigger(required string value, required string text, string class = "") {
var ctx = StructKeyExists(request, "$basecoatTabs") ? request.$basecoatTabs : { defaultTab: "", prefix: "tabs" };
var isActive = len(ctx.defaultTab) && ctx.defaultTab == arguments.value;
var tabId = "#ctx.prefix#-tab-#arguments.value#";
var panelId = "#ctx.prefix#-panel-#arguments.value#";
var classAttr = len(arguments.class) ? ' class="#arguments.class#"' : "";
return '<button type="button"'
& ' id="#tabId#"'
& ' role="tab"'
& ' aria-controls="#panelId#"'
& ' aria-selected="#isActive ? 'true' : 'false'#"'
& ' tabindex="#isActive ? '0' : '-1'#"'
& classAttr
& '>#arguments.text#</button>';
}
/** Opens a tab content panel. Auto-hidden unless its `value` matches the parent default. Close with uiTabContentEnd(). */
public string function uiTabContent(required string value, string class = "") {
var ctx = StructKeyExists(request, "$basecoatTabs") ? request.$basecoatTabs : { defaultTab: "", prefix: "tabs" };
var isActive = len(ctx.defaultTab) && ctx.defaultTab == arguments.value;
var tabId = "#ctx.prefix#-tab-#arguments.value#";
var panelId = "#ctx.prefix#-panel-#arguments.value#";
var classAttr = len(arguments.class) ? ' class="#arguments.class#"' : "";
return '<div'
& ' id="#panelId#"'
& ' role="tabpanel"'
& ' aria-labelledby="#tabId#"'
& ' tabindex="0"'
& (isActive ? '' : ' hidden')
& classAttr
& '>';
}
public string function uiTabContentEnd() {
return '</div>';
}
public string function uiTabsEnd() {
StructDelete(request, "$basecoatTabs");
return '</div>';
}
// ==============================================
// DROPDOWNS (driven by basecoat-js's dropdown-menu.js)
// ==============================================
//
// basecoat-css 0.3.x dropdowns are built on the popover primitive:
// <div class="dropdown-menu">
// <button aria-expanded="false" aria-haspopup="menu">Trigger</button>
// <div data-popover aria-hidden="true">
// <div role="menu">
// <button role="menuitem">Item</button>
// <a role="menuitem" href="...">Linked item</a>
// <hr role="separator">
// </div>
// </div>
// </div>
// dropdown-menu.js handles click + keyboard navigation, arrow keys to
// move focus between items, Escape to dismiss, outside-click to close.
// Items emit role="menuitem" (the JS also recognizes menuitemcheckbox /
// menuitemradio if you need them, via direct uiDropdownCheckItem etc.
// — out of scope for this rewrite).
/** Opens a dropdown menu. Close with uiDropdownEnd(). */
public string function uiDropdown(required string text, string triggerClass = "btn-outline", string class = "") {
var cls = "dropdown-menu";
if (len(arguments.class)) cls &= " " & arguments.class;
return '<div class="#cls#">'
& '<button type="button" class="#arguments.triggerClass#" aria-expanded="false" aria-haspopup="menu">#arguments.text#</button>'
& '<div data-popover aria-hidden="true">'
& '<div role="menu">';
}
/**
* Renders a dropdown menu item. With `href`, renders a navigation link
* (`<a>`); without, a `<button>`. Both carry `role="menuitem"` so
* dropdown-menu.js's keyboard navigation includes them.
*/
public string function uiDropdownItem(required string text, string href = "", string class = "", boolean disabled = false) {
var idAttr = ' id="dditem-' & replace(left(createUUID(), 8), "-", "", "all") & '"';
var classAttr = len(arguments.class) ? ' class="#arguments.class#"' : "";
var disAttr = arguments.disabled ? ' aria-disabled="true"' : "";
if (len(arguments.href)) {
return '<a role="menuitem"#idAttr# href="#arguments.href#"#classAttr##disAttr#>#arguments.text#</a>';
}
return '<button type="button" role="menuitem"#idAttr##classAttr##disAttr#>#arguments.text#</button>';
}
/** Renders a separator line inside a dropdown menu. */
public string function uiDropdownSeparator() {
return '<hr role="separator">';
}
public string function uiDropdownEnd() {
return '</div></div></div>';
}
// ==============================================
// PAGINATION
// ==============================================
/**
* Renders a pagination nav with prev/next, page window, and ellipsis.
*/
public string function uiPagination(
required numeric currentPage,
required numeric totalPages,
required string baseUrl,
string pageParam = "page",
numeric windowSize = 2,
string class = ""
) {
var local = {};
local.cls = "pagination";
if (len(arguments.class)) local.cls &= " " & arguments.class;
// URL builder helper
local.sep = (find("?", arguments.baseUrl) > 0) ? "&" : "?";
local.prevIcon = $uiLucideIcon("chevron-left", 16, 2);
local.nextIcon = $uiLucideIcon("chevron-right", 16, 2);
savecontent variable="local.html" {
writeOutput('<nav class="#local.cls#" aria-label="Pagination">' & chr(10));
// Previous
if (arguments.currentPage <= 1) {
writeOutput('<span class="pagination-item opacity-50" aria-disabled="true">#local.prevIcon#</span>' & chr(10));
} else {
writeOutput('<a href="#arguments.baseUrl##local.sep##arguments.pageParam#=#arguments.currentPage - 1#" class="pagination-item" aria-label="Previous page">#local.prevIcon#</a>' & chr(10));
}
// Page window
local.windowStart = max(1, arguments.currentPage - arguments.windowSize);
local.windowEnd = min(arguments.totalPages, arguments.currentPage + arguments.windowSize);
// First page + ellipsis
if (local.windowStart > 1) {
writeOutput('<a href="#arguments.baseUrl##local.sep##arguments.pageParam#=1" class="pagination-item">1</a>');
if (local.windowStart > 2)
writeOutput('<span class="pagination-item" aria-hidden="true">…</span>' & chr(10));
}
// Page numbers
for (var p = local.windowStart; p <= local.windowEnd; p++) {
if (p == arguments.currentPage) {
writeOutput('<span class="pagination-item pagination-item-active" aria-current="page">#p#</span>' & chr(10));
} else {
writeOutput('<a href="#arguments.baseUrl##local.sep##arguments.pageParam#=#p#" class="pagination-item">#p#</a>' & chr(10));
}
}
// Last page + ellipsis
if (local.windowEnd < arguments.totalPages) {
if (local.windowEnd < arguments.totalPages - 1)
writeOutput('<span class="pagination-item" aria-hidden="true">…</span>' & chr(10));
writeOutput('<a href="#arguments.baseUrl##local.sep##arguments.pageParam#=#arguments.totalPages#" class="pagination-item">#arguments.totalPages#</a>' & chr(10));
}
// Next
if (arguments.currentPage >= arguments.totalPages) {
writeOutput('<span class="pagination-item opacity-50" aria-disabled="true">#local.nextIcon#</span>' & chr(10));
} else {
writeOutput('<a href="#arguments.baseUrl##local.sep##arguments.pageParam#=#arguments.currentPage + 1#" class="pagination-item" aria-label="Next page">#local.nextIcon#</a>' & chr(10));
}
writeOutput('</nav>');
}
return trim(local.html);
}