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
|
package schema
type ElementDef struct {
Tag string
Description string
Attributes []AttributeDef
SelfClosing bool
Category string
}
type AttributeDef struct {
Name string
Type string
Description string
Category string
}
type EventHandlerDef struct {
Name string
Description string
Signature string
}
var Elements = map[string]*ElementDef{
"div": {
Tag: "div",
Description: "A block container with flexbox layout. The primary building block for layouts.",
Attributes: containerAttrs(),
Category: "container",
},
"span": {
Tag: "span",
Description: "An inline text container for styling text content.",
Attributes: textElementAttrs(),
Category: "text",
},
"p": {
Tag: "p",
Description: "A paragraph element for text blocks.",
Attributes: textElementAttrs(),
Category: "text",
},
"ul": {
Tag: "ul",
Description: "An unordered list container. Use with `<li>` children.",
Attributes: containerAttrs(),
Category: "container",
},
"li": {
Tag: "li",
Description: "A list item. Should be a child of `<ul>`.",
Attributes: containerAttrs(),
Category: "container",
},
"button": {
Tag: "button",
Description: "A clickable button element that can receive focus and handle events.",
Attributes: buttonAttrs(),
Category: "input",
},
"input": {
Tag: "input",
Description: "A single-line text input with cursor management, placeholder support, and submit handling.",
Attributes: inputAttrs(),
SelfClosing: true,
Category: "input",
},
"textarea": {
Tag: "textarea",
Description: "A multi-line text input with word wrapping, cursor management, and submit handling.",
Attributes: textareaAttrs(),
SelfClosing: true,
Category: "input",
},
"table": {
Tag: "table",
Description: "A table container for tabular data.",
Attributes: containerAttrs(),
Category: "display",
},
"tr": {
Tag: "tr",
Description: "A table row. Must be a child of `<table>`. Contains `<td>` or `<th>` children.",
Attributes: containerAttrs(),
Category: "display",
},
"td": {
Tag: "td",
Description: "A table data cell. Must be a child of `<tr>`.",
Attributes: tableCellAttrs(),
Category: "display",
},
"th": {
Tag: "th",
Description: "A table header cell. Renders bold by default. Must be a child of `<tr>`.",
Attributes: tableCellAttrs(),
Category: "display",
},
"progress": {
Tag: "progress",
Description: "A progress bar element showing completion status.",
Attributes: progressAttrs(),
SelfClosing: true,
Category: "display",
},
"hr": {
Tag: "hr",
Description: "A horizontal dividing line.",
SelfClosing: true,
Category: "display",
Attributes: []AttributeDef{
{Name: "id", Type: "string", Description: "Unique identifier for the element", Category: "generic"},
{Name: "class", Type: "string", Description: "Tailwind-style CSS classes", Category: "generic"},
},
},
"br": {
Tag: "br",
Description: "An empty line break.",
SelfClosing: true,
Category: "display",
Attributes: []AttributeDef{
{Name: "id", Type: "string", Description: "Unique identifier for the element", Category: "generic"},
{Name: "class", Type: "string", Description: "Tailwind-style CSS classes", Category: "generic"},
},
},
"modal": {
Tag: "modal",
Description: "A modal overlay that renders on top of all other content. Supports backdrop dimming, focus trapping, and close-on-escape.",
Attributes: modalAttrs(),
SelfClosing: false,
Category: "container",
},
"markdown": {
Tag: "markdown",
Description: "Renders a markdown string into the widget tree (headings, bold/italic, inline code, fenced code blocks, tables, lists, blockquotes, links). A pure content renderer: wrap it in a scrollable container to scroll long documents.",
Attributes: markdownAttrs(),
SelfClosing: true,
Category: "display",
},
}
var EventHandlers = map[string]*EventHandlerDef{
"onFocus": {
Name: "onFocus",
Description: "Called when the element gains focus.",
Signature: "func()",
},
"onBlur": {
Name: "onBlur",
Description: "Called when the element loses focus.",
Signature: "func()",
},
}
func VoidElements() map[string]bool {
void := make(map[string]bool)
for tag, elem := range Elements {
if elem.SelfClosing {
void[tag] = true
}
}
return void
}
func IsVoidElement(tag string) bool {
elem := Elements[tag]
return elem != nil && elem.SelfClosing
}
func GetElement(tag string) *ElementDef {
return Elements[tag]
}
func GetAttribute(tag, attr string) *AttributeDef {
elem := Elements[tag]
if elem == nil {
return nil
}
for i := range elem.Attributes {
if elem.Attributes[i].Name == attr {
return &elem.Attributes[i]
}
}
return nil
}
func GetEventHandler(name string) *EventHandlerDef {
return EventHandlers[name]
}
func IsElementTag(tag string) bool {
_, ok := Elements[tag]
return ok
}
func IsEventHandler(name string) bool {
_, ok := EventHandlers[name]
return ok
}
func AllElementTags() []string {
tags := make([]string, 0, len(Elements))
for tag := range Elements {
tags = append(tags, tag)
}
return tags
}
func AllEventHandlerNames() []string {
names := make([]string, 0, len(EventHandlers))
for name := range EventHandlers {
names = append(names, name)
}
return names
}
func genericAttrs() []AttributeDef {
return []AttributeDef{
{Name: "id", Type: "string", Description: "Unique identifier for the element", Category: "generic"},
{Name: "class", Type: "string", Description: "Tailwind-style CSS classes", Category: "generic"},
{Name: "disabled", Type: "bool", Description: "Whether the element is disabled", Category: "generic"},
{Name: "deps", Type: "expression", Description: "Explicit state dependencies for reactive bindings", Category: "generic"},
{Name: "ref", Type: "expression", Description: "Bind this element to a ref variable (tui.NewRef/NewRefList/NewRefMap)", Category: "ref"},
}
}
func layoutAttrs() []AttributeDef {
return []AttributeDef{
{Name: "width", Type: "int", Description: "Fixed width in characters", Category: "layout"},
{Name: "widthPercent", Type: "int", Description: "Width as percentage of parent", Category: "layout"},
{Name: "height", Type: "int", Description: "Fixed height in rows", Category: "layout"},
{Name: "heightPercent", Type: "int", Description: "Height as percentage of parent", Category: "layout"},
{Name: "minWidth", Type: "int", Description: "Minimum width", Category: "layout"},
{Name: "minHeight", Type: "int", Description: "Minimum height", Category: "layout"},
{Name: "maxWidth", Type: "int", Description: "Maximum width", Category: "layout"},
{Name: "maxHeight", Type: "int", Description: "Maximum height", Category: "layout"},
}
}
func flexAttrs() []AttributeDef {
return []AttributeDef{
{Name: "display", Type: "string", Description: "Display mode (block, flex)", Category: "flex"},
{Name: "direction", Type: "direction", Description: "Flex direction (row, column)", Category: "flex"},
{Name: "justify", Type: "justify", Description: "Justify content (start, center, end, between, around)", Category: "flex"},
{Name: "align", Type: "align", Description: "Align items (start, center, end, stretch)", Category: "flex"},
{Name: "gap", Type: "int", Description: "Gap between children", Category: "flex"},
{Name: "flexGrow", Type: "float", Description: "Flex grow factor", Category: "flex"},
{Name: "flexShrink", Type: "float", Description: "Flex shrink factor", Category: "flex"},
{Name: "alignSelf", Type: "align", Description: "Override parent's align for this item", Category: "flex"},
}
}
func spacingAttrs() []AttributeDef {
return []AttributeDef{
{Name: "padding", Type: "int", Description: "Padding on all sides", Category: "spacing"},
{Name: "margin", Type: "int", Description: "Margin on all sides", Category: "spacing"},
}
}
func visualAttrs() []AttributeDef {
return []AttributeDef{
{Name: "border", Type: "border", Description: "Border style (none, single, double, rounded, thick)", Category: "visual"},
{Name: "borderStyle", Type: "string", Description: "Border style name", Category: "visual"},
{Name: "background", Type: "color", Description: "Background color", Category: "visual"},
{Name: "borderTitle", Type: "string", Description: "Title text drawn in the top border", Category: "visual"},
}
}
func textAttrs() []AttributeDef {
return []AttributeDef{
{Name: "text", Type: "string", Description: "Text content", Category: "text"},
{Name: "textStyle", Type: "style", Description: "Text styling", Category: "text"},
{Name: "textAlign", Type: "string", Description: "Text alignment (left, center, right)", Category: "text"},
{Name: "wrap", Type: "bool", Description: "Enable or disable text wrapping (enabled by default)", Category: "text"},
}
}
func eventAttrs() []AttributeDef {
return []AttributeDef{
{Name: "onFocus", Type: "func", Description: "Focus gained handler", Category: "event"},
{Name: "onBlur", Type: "func", Description: "Focus lost handler", Category: "event"},
{Name: "onActivate", Type: "func", Description: "Called when Enter is pressed while focused: func()", Category: "event"},
{Name: "focusable", Type: "bool", Description: "Whether the element can receive focus", Category: "event"},
{Name: "autoFocus", Type: "bool", Description: "Automatically focus this element on startup", Category: "event"},
}
}
func scrollAttrs() []AttributeDef {
return []AttributeDef{
{Name: "scrollable", Type: "bool", Description: "Enable scrolling for overflow content", Category: "scroll"},
{Name: "scrollbarStyle", Type: "style", Description: "Style for the scrollbar track", Category: "scroll"},
{Name: "scrollbarThumbStyle", Type: "style", Description: "Style for the scrollbar thumb", Category: "scroll"},
{Name: "hideScrollbar", Type: "bool", Description: "Hide the scrollbar and reclaim its gutter width", Category: "scroll"},
}
}
func containerAttrs() []AttributeDef {
var attrs []AttributeDef
attrs = append(attrs, genericAttrs()...)
attrs = append(attrs, layoutAttrs()...)
attrs = append(attrs, flexAttrs()...)
attrs = append(attrs, spacingAttrs()...)
attrs = append(attrs, visualAttrs()...)
attrs = append(attrs, eventAttrs()...)
attrs = append(attrs, scrollAttrs()...)
return attrs
}
func tableCellAttrs() []AttributeDef {
var attrs []AttributeDef
attrs = append(attrs, genericAttrs()...)
attrs = append(attrs, textAttrs()...)
attrs = append(attrs, layoutAttrs()...)
attrs = append(attrs, spacingAttrs()...)
attrs = append(attrs, visualAttrs()...)
attrs = append(attrs, eventAttrs()...)
return attrs
}
func textElementAttrs() []AttributeDef {
var attrs []AttributeDef
attrs = append(attrs, genericAttrs()...)
attrs = append(attrs, textAttrs()...)
attrs = append(attrs, layoutAttrs()...)
attrs = append(attrs, spacingAttrs()...)
attrs = append(attrs, visualAttrs()...)
attrs = append(attrs, eventAttrs()...)
return attrs
}
func buttonAttrs() []AttributeDef {
var attrs []AttributeDef
attrs = append(attrs, genericAttrs()...)
attrs = append(attrs, textAttrs()...)
attrs = append(attrs, layoutAttrs()...)
attrs = append(attrs, spacingAttrs()...)
attrs = append(attrs, visualAttrs()...)
attrs = append(attrs, eventAttrs()...)
return attrs
}
func inputAttrs() []AttributeDef {
return []AttributeDef{
{Name: "id", Type: "string", Description: "Unique identifier for the element", Category: "generic"},
{Name: "class", Type: "string", Description: "Tailwind-style CSS classes", Category: "generic"},
{Name: "value", Type: "expression", Description: "Bind to a *State[string] for reactive two-way text binding", Category: "generic"},
{Name: "placeholder", Type: "string", Description: "Placeholder text shown when empty and unfocused", Category: "generic"},
{Name: "placeholderStyle", Type: "style", Description: "Placeholder text styling (default: dim)", Category: "visual"},
{Name: "width", Type: "int", Description: "Input width in characters (default 20)", Category: "layout"},
{Name: "border", Type: "border", Description: "Border style", Category: "visual"},
{Name: "textStyle", Type: "style", Description: "Text styling", Category: "visual"},
{Name: "cursor", Type: "expression", Description: "Cursor rune (default '▌')", Category: "visual"},
{Name: "focusColor", Type: "expression", Description: "Border color when focused (default tui.Cyan)", Category: "visual"},
{Name: "borderGradient", Type: "expression", Description: "Border gradient when unfocused (tui.Gradient)", Category: "visual"},
{Name: "focusGradient", Type: "expression", Description: "Border gradient when focused (tui.Gradient)", Category: "visual"},
{Name: "onSubmit", Type: "func", Description: "Callback when Enter is pressed: func(string)", Category: "event"},
{Name: "onChange", Type: "func", Description: "Callback when text changes: func(string)", Category: "event"},
{Name: "autoFocus", Type: "bool", Description: "Automatically focus this input on startup", Category: "event"},
{Name: "ref", Type: "expression", Description: "Bind this element to a ref variable", Category: "ref"},
{Name: "deps", Type: "expression", Description: "Explicit state dependencies for reactive bindings", Category: "generic"},
}
}
func textareaAttrs() []AttributeDef {
return []AttributeDef{
{Name: "id", Type: "string", Description: "Unique identifier for the element", Category: "generic"},
{Name: "class", Type: "string", Description: "Tailwind-style CSS classes", Category: "generic"},
{Name: "value", Type: "expression", Description: "Bind to a *State[string] for reactive two-way text binding", Category: "generic"},
{Name: "placeholder", Type: "string", Description: "Placeholder text shown when empty and unfocused", Category: "generic"},
{Name: "width", Type: "int", Description: "Text area width in characters (default 40)", Category: "layout"},
{Name: "maxHeight", Type: "int", Description: "Maximum height in rows (0 = unlimited)", Category: "layout"},
{Name: "border", Type: "border", Description: "Border style (none, single, double, rounded, thick)", Category: "visual"},
{Name: "textStyle", Type: "style", Description: "Text styling", Category: "visual"},
{Name: "placeholderStyle", Type: "style", Description: "Placeholder text styling (default: dim)", Category: "visual"},
{Name: "cursor", Type: "expression", Description: "Cursor rune (default '▌')", Category: "visual"},
{Name: "focusColor", Type: "expression", Description: "Border color when focused (default tui.Cyan)", Category: "visual"},
{Name: "borderGradient", Type: "expression", Description: "Border gradient when unfocused (tui.Gradient)", Category: "visual"},
{Name: "focusGradient", Type: "expression", Description: "Border gradient when focused (tui.Gradient)", Category: "visual"},
{Name: "submitKey", Type: "expression", Description: "Key that triggers submit (default KeyEnter)", Category: "event"},
{Name: "onSubmit", Type: "func", Description: "Callback when submit key is pressed: func(string)", Category: "event"},
{Name: "autoFocus", Type: "bool", Description: "Automatically focus this text area on startup", Category: "event"},
{Name: "ref", Type: "expression", Description: "Bind this element to a ref variable", Category: "ref"},
{Name: "deps", Type: "expression", Description: "Explicit state dependencies for reactive bindings", Category: "generic"},
}
}
func modalAttrs() []AttributeDef {
return []AttributeDef{
{Name: "id", Type: "string", Description: "Unique identifier for the element", Category: "generic"},
{Name: "class", Type: "string", Description: "Tailwind-style CSS classes for positioning and styling", Category: "generic"},
{Name: "open", Type: "expression", Description: "Bind to a *State[bool] to control modal visibility (required)", Category: "generic"},
{Name: "backdrop", Type: "string", Description: "Backdrop style: \"dim\" (default), \"blank\", or \"none\"", Category: "visual"},
{Name: "closeOnEscape", Type: "bool", Description: "Escape key closes the modal (default true)", Category: "event"},
{Name: "closeOnBackdropClick", Type: "bool", Description: "Clicking backdrop closes the modal (default true)", Category: "event"},
{Name: "trapFocus", Type: "bool", Description: "Tab/Shift+Tab restricted to modal children; when true, also blocks unhandled keys from reaching parent components (default true)", Category: "event"},
{Name: "keyMap", Type: "expression", Description: "Custom KeyMap bindings for the modal; fire before the catch-all when trapFocus is true", Category: "event"},
{Name: "ref", Type: "expression", Description: "Bind this element to a ref variable", Category: "ref"},
{Name: "deps", Type: "expression", Description: "Explicit state dependencies for reactive bindings", Category: "generic"},
}
}
func markdownAttrs() []AttributeDef {
return []AttributeDef{
{Name: "id", Type: "string", Description: "Unique identifier for the element", Category: "generic"},
{Name: "class", Type: "string", Description: "Tailwind-style CSS classes", Category: "generic"},
{Name: "source", Type: "expression", Description: "Static markdown content (string expression)", Category: "generic"},
{Name: "state", Type: "expression", Description: "Reactive *State[string] markdown source; re-renders on change", Category: "generic"},
{Name: "width", Type: "int", Description: "Fixed render width in characters (0 = fill available width)", Category: "layout"},
{Name: "theme", Type: "expression", Description: "tui.MarkdownTheme overriding the default styling", Category: "visual"},
{Name: "ref", Type: "expression", Description: "Bind this element to a ref variable", Category: "ref"},
{Name: "deps", Type: "expression", Description: "Explicit state dependencies for reactive bindings", Category: "generic"},
}
}
func progressAttrs() []AttributeDef {
return []AttributeDef{
{Name: "id", Type: "string", Description: "Unique identifier for the element", Category: "generic"},
{Name: "class", Type: "string", Description: "Tailwind-style CSS classes", Category: "generic"},
{Name: "value", Type: "int", Description: "Current progress value (0 to max)", Category: "generic"},
{Name: "max", Type: "int", Description: "Maximum progress value", Category: "generic"},
{Name: "width", Type: "int", Description: "Progress bar width in characters", Category: "layout"},
}
}
|