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
|
# Built-in Components Reference
## Overview
go-tui ships with three built-in components: `Input` (single-line text), `TextArea` (multi-line text), and `Modal` (overlay dialog). Input and TextArea handle text entry, cursor management, and focus. Modal handles backdrop rendering, focus trapping, and preemptive key blocking.
All three implement `Component`, `KeyListener`, and `AppBinder`. Use them standalone or embed them in a larger UI.
## Input
A single-line text input with cursor management, horizontal scrolling, and placeholder support.
```go
import tui "github.com/grindlemire/go-tui"
inp := tui.NewInput(
tui.WithInputWidth(30),
tui.WithInputBorder(tui.BorderRounded),
tui.WithInputPlaceholder("Type here..."),
tui.WithInputOnSubmit(func(text string) {
// handle submitted text
}),
)
```
### NewInput
```go
func NewInput(opts ...InputOption) *Input
```
Creates a new `Input` with the given options. Default values:
| Setting | Default | Description |
|-------------|--------------|------------------------------------------|
| Width | 20 | Characters visible before scrolling |
| Border | `BorderNone` | No border |
| TextStyle | `Style{}` | Default terminal style |
| Placeholder | `""` | No placeholder text |
| PlaceholderStyle | `Style{}.Dim()` | Dim text for placeholder |
| Cursor | `'▌'` | Block cursor character |
| FocusColor | `Cyan` | Border color when focused |
### Reactive Value Binding
Bind the Input to a `*State[string]` for two-way binding. The Input shares the state directly, so typing updates the state and changing the state updates the display:
```go
name := tui.NewState("")
inp := tui.NewInput(
tui.WithInputValue(name),
tui.WithInputBorder(tui.BorderRounded),
)
// Later: name.Set("Alice") updates the input display
// Typing in the input updates name.Get()
```
In `.gsx`:
```gsx
<input value={s.name} placeholder="Type your name..." border={tui.BorderRounded} />
```
### GSX Attributes
All `<input>` attributes and their types:
| Attribute | Type | Description |
|-----------|------|-------------|
| `value` | `*State[string]` | Two-way text binding |
| `placeholder` | `string` | Text shown when empty and unfocused |
| `placeholderStyle` | `tui.Style` | Placeholder styling (default: dim) |
| `width` | `int` | Width in characters (default 20) |
| `border` | `tui.BorderStyle` | Border style |
| `textStyle` | `tui.Style` | Text styling |
| `cursor` | `rune` | Cursor character (default '▌') |
| `focusColor` | `tui.Color` | Border color when focused (default Cyan) |
| `borderGradient` | `tui.Gradient` | Border gradient when unfocused |
| `focusGradient` | `tui.Gradient` | Border gradient when focused |
| `onSubmit` | `func(string)` | Called when Enter is pressed |
| `onChange` | `func(string)` | Called when text changes |
| `autoFocus` | `bool` | Focus this input on startup |
### Focus Border Styling
Control how the border looks when focused and unfocused:
```go
// Solid color when focused (default: Cyan)
tui.WithInputFocusColor(tui.Magenta)
// Gradient border when unfocused
tui.WithInputBorderGradient(tui.NewGradient(tui.Blue, tui.Cyan))
// Gradient border when focused (overrides focusColor)
tui.WithInputFocusGradient(tui.NewGradient(tui.Cyan, tui.Magenta))
```
In `.gsx`:
```gsx
<input
value={s.query}
border={tui.BorderRounded}
focusColor={tui.Magenta}
borderGradient={tui.NewGradient(tui.Blue, tui.Cyan)}
focusGradient={tui.NewGradient(tui.Cyan, tui.Magenta)}
/>
```
### Keyboard Behavior
**Text input:**
| Key | Action |
|-------------|-----------------------------------|
| Any rune | Insert character at cursor |
| Backspace | Delete character before cursor |
| Delete | Delete character at cursor |
**Navigation:**
| Key | Action |
|-------------|-----------------------------------|
| Left | Move cursor left |
| Right | Move cursor right |
| Home | Move cursor to start |
| End | Move cursor to end |
**Submit:**
| Key | Action |
|-------------|-----------------------------------|
| Enter | Trigger `onSubmit` callback |
### InputOption Functions
| Function | Description |
|----------|-------------|
| `WithInputWidth(int)` | Width in characters (default 20) |
| `WithInputBorder(BorderStyle)` | Border style |
| `WithInputTextStyle(Style)` | Text style |
| `WithInputPlaceholder(string)` | Placeholder text |
| `WithInputPlaceholderStyle(Style)` | Placeholder style (default: dim) |
| `WithInputCursor(rune)` | Cursor character (default '▌') |
| `WithInputValue(*State[string])` | Reactive two-way text binding |
| `WithInputFocusColor(Color)` | Border color when focused (default Cyan) |
| `WithInputBorderGradient(Gradient)` | Border gradient when unfocused |
| `WithInputFocusGradient(Gradient)` | Border gradient when focused |
| `WithInputOnSubmit(func(string))` | Enter key callback |
| `WithInputOnChange(func(string))` | Text change callback |
## TextArea
A multi-line text input with word wrapping and a blinking cursor.
```go
import tui "github.com/grindlemire/go-tui"
ta := tui.NewTextArea(
tui.WithTextAreaWidth(60),
tui.WithTextAreaBorder(tui.BorderRounded),
tui.WithTextAreaPlaceholder("Type something..."),
tui.WithTextAreaOnSubmit(func(text string) {
// handle submitted text
}),
)
```
### NewTextArea
```go
func NewTextArea(opts ...TextAreaOption) *TextArea
```
Creates a new `TextArea` with the given options. Default values:
| Setting | Default | Description |
|-------------|--------------|------------------------------------------|
| Width | 40 | Characters per line before wrapping |
| MaxHeight | 0 (no limit) | Maximum rows of text visible |
| Border | `BorderNone` | No border |
| TextStyle | `Style{}` | Default terminal style |
| Placeholder | `""` | No placeholder text |
| PlaceholderStyle | `Style{}.Dim()` | Dim text for placeholder |
| Cursor | `'▌'` | Block cursor character |
| FocusColor | `Cyan` | Border color when focused |
| SubmitKey | `KeyEnter` | Enter submits, Ctrl+J inserts newline |
### GSX Attributes
All `<textarea>` attributes and their types:
| Attribute | Type | Description |
|-----------|------|-------------|
| `value` | `*State[string]` | Two-way text binding |
| `placeholder` | `string` | Text shown when empty and unfocused |
| `placeholderStyle` | `tui.Style` | Placeholder styling (default: dim) |
| `width` | `int` | Width in characters (default 40) |
| `maxHeight` | `int` | Maximum visible rows (0 = unlimited) |
| `border` | `tui.BorderStyle` | Border style |
| `textStyle` | `tui.Style` | Text styling |
| `cursor` | `rune` | Cursor character (default '▌') |
| `focusColor` | `tui.Color` | Border color when focused (default Cyan) |
| `borderGradient` | `tui.Gradient` | Border gradient when unfocused |
| `focusGradient` | `tui.Gradient` | Border gradient when focused |
| `submitKey` | `tui.Key` | Key that triggers submit (default KeyEnter) |
| `onSubmit` | `func(string)` | Called when submit key is pressed |
| `autoFocus` | `bool` | Focus this text area on startup |
### State Access Methods
#### Text
```go
func (t *TextArea) Text() string
```
Returns the current text content.
#### SetText
```go
func (t *TextArea) SetText(s string)
```
Replaces the text and moves the cursor to the end.
```go
ta.SetText("Hello, world!")
fmt.Println(ta.Text()) // "Hello, world!"
```
#### Clear
```go
func (t *TextArea) Clear()
```
Removes all text and resets the cursor to position 0.
#### Height
```go
func (t *TextArea) Height() int
```
Returns the total rendered height in rows, including border rows if a border is set. The height depends on the current text content and word wrapping. If `maxHeight` is set, the returned value is capped to that limit (plus border rows).
### Focus Methods
`TextArea` implements the `Focusable` interface. When focused, the cursor blinks and keystrokes are captured. When unfocused, placeholder text appears (if configured) and no keystrokes are processed.
#### IsFocusable
```go
func (t *TextArea) IsFocusable() bool
```
Always returns `true`.
#### Focus
```go
func (t *TextArea) Focus()
```
Activates the text area. The cursor becomes visible and starts blinking.
#### Blur
```go
func (t *TextArea) Blur()
```
Deactivates the text area. The cursor disappears and placeholder text shows if the input is empty.
### HandleEvent
```go
func (t *TextArea) HandleEvent(e Event) bool
```
Processes a keyboard event against the TextArea's key map. Returns `true` if the event was handled (and propagation should stop), `false` otherwise. This method is part of the `Focusable` interface and is called automatically by the focus manager when the TextArea has focus.
### BindApp
```go
func (t *TextArea) BindApp(app *App)
```
Binds the TextArea's internal reactive states to the given `App`, so that state changes trigger re-renders. Called automatically when the TextArea is used as a root component or mounted as a sub-component.
### Keyboard Behavior
`TextArea` returns a `KeyMap` with built-in bindings. All bindings use stop propagation so keystrokes don't bubble up to parent components.
**Text input:**
| Key | Action |
|-------------|-----------------------------------|
| Any rune | Insert character at cursor |
| Backspace | Delete character before cursor |
| Delete | Delete character at cursor |
**Navigation:**
| Key | Action |
|-------------|-----------------------------------|
| Left | Move cursor left |
| Right | Move cursor right |
| Up | Move cursor up one line |
| Down | Move cursor down one line |
| Home | Move cursor to start of line |
| End | Move cursor to end of line |
**Submit and newline (default submit key = Enter):**
| Key | Action |
|-------------|-----------------------------------|
| Enter | Trigger `onSubmit` callback |
| Ctrl+J | Insert newline character |
When `submitKey` is set to something other than `KeyEnter`, the behavior flips: Enter inserts a newline and the configured key triggers submit.
### Cursor Blink
`TextArea` implements `WatcherProvider` and returns a timer watcher that toggles the cursor visibility every 500ms while focused. The cursor resets to visible on every keystroke so the user always sees where they're typing.
### TextAreaOption Functions
Options follow the functional options pattern. Each returns a `TextAreaOption` (which is `func(*TextArea)`).
#### WithTextAreaWidth
```go
func WithTextAreaWidth(cells int) TextAreaOption
```
Sets the width in characters. Text wraps at this boundary. Default: 40.
```go
ta := tui.NewTextArea(tui.WithTextAreaWidth(80))
```
#### WithTextAreaMaxHeight
```go
func WithTextAreaMaxHeight(rows int) TextAreaOption
```
Caps the visible height to `rows` lines of text. Set to 0 (the default) for unlimited height. This does not include border rows. If a border is set, the actual element height is `maxHeight + 2`.
```go
ta := tui.NewTextArea(tui.WithTextAreaMaxHeight(10))
```
#### WithTextAreaBorder
```go
func WithTextAreaBorder(b BorderStyle) TextAreaOption
```
Sets the border style around the text area. Default: `BorderNone`.
```go
ta := tui.NewTextArea(tui.WithTextAreaBorder(tui.BorderRounded))
```
#### WithTextAreaTextStyle
```go
func WithTextAreaTextStyle(s Style) TextAreaOption
```
Sets the style for the text content. Default: zero-value `Style{}` (terminal default).
```go
ta := tui.NewTextArea(
tui.WithTextAreaTextStyle(tui.NewStyle().Foreground(tui.ANSIColor(tui.Cyan))),
)
```
#### WithTextAreaPlaceholder
```go
func WithTextAreaPlaceholder(text string) TextAreaOption
```
Sets placeholder text shown when the TextArea is empty and unfocused. Default: empty string (no placeholder).
```go
ta := tui.NewTextArea(tui.WithTextAreaPlaceholder("Enter your message..."))
```
#### WithTextAreaPlaceholderStyle
```go
func WithTextAreaPlaceholderStyle(s Style) TextAreaOption
```
Sets the style for placeholder text. Default: `Style{}.Dim()`.
```go
ta := tui.NewTextArea(
tui.WithTextAreaPlaceholderStyle(tui.NewStyle().Dim().Italic()),
)
```
#### WithTextAreaCursor
```go
func WithTextAreaCursor(r rune) TextAreaOption
```
Sets the cursor character. Default: `'▌'` (left half block).
```go
ta := tui.NewTextArea(tui.WithTextAreaCursor('█'))
```
#### WithTextAreaSubmitKey
```go
func WithTextAreaSubmitKey(k Key) TextAreaOption
```
Sets which key triggers the `onSubmit` callback. Default: `KeyEnter`.
When the submit key is `KeyEnter`, pressing Enter triggers submit and Ctrl+J inserts a newline. For any other submit key, Enter inserts a newline and the configured key triggers submit.
```go
// Ctrl+S submits, Enter inserts newlines (good for multi-line editing)
ta := tui.NewTextArea(tui.WithTextAreaSubmitKey(tui.KeyCtrlS))
```
#### WithTextAreaOnSubmit
```go
func WithTextAreaOnSubmit(fn func(string)) TextAreaOption
```
Sets the callback invoked when the submit key is pressed. The callback receives the current text content.
```go
ta := tui.NewTextArea(
tui.WithTextAreaOnSubmit(func(text string) {
fmt.Println("Submitted:", text)
}),
)
```
#### WithTextAreaValue
```go
func WithTextAreaValue(state *State[string]) TextAreaOption
```
Binds the TextArea to a `*State[string]` for two-way binding. The TextArea shares the state directly, so parent components can read or change the text at any time.
```go
note := tui.NewState("")
ta := tui.NewTextArea(tui.WithTextAreaValue(note))
// note.Get() reflects whatever the user types
// note.Set("preset text") updates the textarea display
```
In `.gsx`:
```gsx
<textarea value={s.note} placeholder="Write a note..." border={tui.BorderRounded} />
```
#### WithTextAreaFocusColor
```go
func WithTextAreaFocusColor(c Color) TextAreaOption
```
Sets the border color when focused. Default: `Cyan`. Only visible when a border is set.
```go
ta := tui.NewTextArea(
tui.WithTextAreaBorder(tui.BorderRounded),
tui.WithTextAreaFocusColor(tui.Magenta),
)
```
##### WithTextAreaBorderGradient
```go
func WithTextAreaBorderGradient(g Gradient) TextAreaOption
```
Sets a gradient for the border color when unfocused. Only visible when a border is set.
```go
ta := tui.NewTextArea(
tui.WithTextAreaBorder(tui.BorderRounded),
tui.WithTextAreaBorderGradient(tui.NewGradient(tui.Blue, tui.Cyan)),
)
```
#### WithTextAreaFocusGradient
```go
func WithTextAreaFocusGradient(g Gradient) TextAreaOption
```
Sets a gradient for the border color when focused. Takes priority over `focusColor` when set.
```go
ta := tui.NewTextArea(
tui.WithTextAreaBorder(tui.BorderRounded),
tui.WithTextAreaFocusGradient(tui.NewGradient(tui.Cyan, tui.Magenta)),
)
```
### Example
A note-taking input using `TextArea` with a rounded border and Ctrl+S to submit:
```go
package main
import (
"fmt"
"os"
tui "github.com/grindlemire/go-tui"
)
func main() {
ta := tui.NewTextArea(
tui.WithTextAreaWidth(60),
tui.WithTextAreaMaxHeight(10),
tui.WithTextAreaBorder(tui.BorderRounded),
tui.WithTextAreaPlaceholder("Write a note..."),
tui.WithTextAreaSubmitKey(tui.KeyCtrlS),
tui.WithTextAreaOnSubmit(func(text string) {
fmt.Printf("Saved: %s\n", text)
}),
)
app, err := tui.NewApp(
tui.WithRootComponent(ta),
)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
defer app.Close()
if err := app.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
```
For an inline chat pattern with `PrintAbove`, see the [Inline Mode Guide](../guides/12-inline-mode.md).
## Modal
Modal renders a full-screen overlay with a backdrop effect, focus trapping, and preemptive key handling.
### Constructor
```go
func NewModal(opts ...ModalOption) *Modal
```
### ModalOption Functions
| Function | Description |
|----------|-------------|
| `WithModalOpen(state *State[bool])` | Bind visibility to a boolean state (required) |
| `WithModalBackdrop(b string)` | Backdrop style: `"dim"` (default), `"blank"`, or `"none"` |
| `WithModalCloseOnEscape(v bool)` | Escape closes the modal (default `true`) |
| `WithModalCloseOnBackdropClick(v bool)` | Backdrop click closes the modal (default `true`) |
| `WithModalTrapFocus(v bool)` | Restrict Tab navigation to modal children and block unhandled keys from parent handlers (default `true`) |
| `WithModalKeyMap(km KeyMap)` | Custom key bindings for the modal; fire after built-in handlers but before the catch-all. Use `OnPreemptStop`; non-preemptive bindings (`On`, `OnStop`) are inert when `trapFocus` is true |
| `WithModalElementOptions(opts ...Option)` | Pass layout options to the overlay container (used by generated code for `class` attributes) |
### GSX Attributes
All `<modal>` attributes and their types:
| Attribute | Type | Description |
|-----------|------|-------------|
| `open` | `*State[bool]` | Controls visibility (required) |
| `backdrop` | `string` | `"dim"` (default), `"blank"`, or `"none"` |
| `closeOnEscape` | `bool` | Escape closes the modal (default true) |
| `closeOnBackdropClick` | `bool` | Backdrop click closes the modal (default true) |
| `trapFocus` | `bool` | Tab/Shift+Tab restricted to modal children; also blocks unhandled keys from parents (default true) |
| `keyMap` | `expression` | Custom `KeyMap` bindings for the modal |
| `class` | `string` | Tailwind classes for positioning (e.g. `"justify-center items-center"`) |
### Behavior
When open, the modal:
- Applies the backdrop effect (dim, blank, or none) to the buffer before rendering the overlay
- Handles Enter by calling `Activate()` on the focused element
- Closes on Escape (if `closeOnEscape` is true)
- Closes on backdrop click (if `closeOnBackdropClick` is true)
- Walks clicked elements up to find `onActivate` callbacks for mouse support
When `trapFocus` is true (the default):
- Tab/Shift+Tab only cycle through focusable children inside the modal
- A catch-all binding blocks unhandled keys from parent handlers
When `trapFocus` is false, Tab and unhandled keys propagate to parent components normally.
Custom key bindings provided via `WithModalKeyMap` (or the `keyMap` GSX attribute) fire after the built-in Escape/Tab/Enter handlers but before the catch-all. This lets you add hotkeys to a modal while still blocking everything else:
```go
tui.WithModalKeyMap(tui.KeyMap{
tui.OnPreemptStop(tui.Rune('n'), func(ke tui.KeyEvent) { startNewGame() }),
tui.OnPreemptStop(tui.Rune('q'), func(ke tui.KeyEvent) { quit() }),
})
```
When closed, it returns a hidden placeholder element with no key bindings.
### Interfaces Implemented
| Interface | Purpose |
|-----------|---------|
| `Component` | `Render(app *App) *Element` returns the overlay element |
| `KeyListener` | `KeyMap()` returns Escape, Tab, Enter, custom, and catch-all bindings (catch-all only when `trapFocus` is true) |
| `MouseListener` | `HandleMouse()` handles backdrop click and onActivate delegation |
| `AppBinder` | `BindApp()` wires the open state to the app |
### GSX Usage
```gsx
<modal open={s.showDialog} class="justify-center items-center" backdrop="dim">
<div class="border-rounded p-2 flex-col gap-1 w-40">
<span class="font-bold">Title</span>
<button class="px-2 border-rounded focusable" onActivate={s.onConfirm}>OK</button>
</div>
</modal>
```
The `class` attribute on `<modal>` controls how the dialog is positioned within the full-screen overlay. Use `justify-center items-center` for a centered dialog or `justify-end items-stretch` for a bottom sheet.
### Inline Mode
Modals are not supported in inline mode (`WithInlineHeight`). The overlay system requires a full-screen buffer for backdrop effects, centering, and mouse hit testing. Modal overlays registered while in inline mode are silently ignored.
To show a modal from an inline app, switch to the alternate screen first:
```go
app.EnterAlternateScreen()
s.showDialog.Set(true)
// ... user interacts with modal ...
// on close:
app.ExitAlternateScreen()
```
See the [Inline Mode Guide](../guides/15-inline-mode) for the full pattern.
## Markdown
Markdown renders a markdown string into the widget tree. It is a pure content renderer with no scroll state or key handling, so it is wrapped in a scrollable container for long documents. The parsed block tree is cached and re-parsed only when the resolved source string changes.
### Constructor
```go
func NewMarkdown(opts ...MarkdownOption) *Markdown
```
### MarkdownOption Functions
| Function | Description |
|----------|-------------|
| `WithMarkdownSource(s string)` | Static markdown content. Ignored when a state source is set |
| `WithMarkdownState(s *State[string])` | Reactive source; takes precedence over the static source and re-renders on change |
| `WithMarkdownWidth(w int)` | Fixed render width in characters. `0` (the default) fills the width the parent assigns |
| `WithMarkdownTheme(t MarkdownTheme)` | Override the default styling theme |
### GSX Attributes
All `<markdown>` attributes and their types:
| Attribute | Type | Description |
|-----------|------|-------------|
| `source` | `string` | Static markdown content (string expression) |
| `state` | `*State[string]` | Reactive source; re-renders on change. Takes precedence over `source` |
| `width` | `int` | Fixed render width in characters (`0` fills the parent width) |
| `theme` | `MarkdownTheme` | Override the default styling |
The tag is self-closing. Content comes from `source` or `state` rather than from children, because the generator cannot tell a literal markdown string from a Go expression that returns one.
### Supported Markdown
| Construct | Notes |
|-----------|-------|
| Headings | ATX (`#` through `######`) and single-line setext (`===`, `---`) |
| Emphasis | Bold, italic, and bold-italic, with `*`/`_` and `**`/`__` markers |
| Inline code | Backtick spans |
| Links | Rendered as OSC 8 hyperlinks on capable terminals |
| Code blocks | Fenced blocks, syntax-highlighted for Go, JSON, Bash, and JS/TS |
| Tables | Pipe tables drawn as a full grid with inline formatting kept in cells |
| Lists | Ordered, unordered (`-`, `*`, `+`), and nested |
| Blockquotes | Including nested quotes and quotes that contain a list |
An emphasis delimiter with no closer stays literal, so `see **docs` and `3 * 4` render as written.
### Width and Wrapping
With `width` at `0` (the default), the component fills the width its parent assigns. Paragraphs and headings wrap to that width, while list and blockquote content renders on one line and clips on overflow. Set an explicit `width` to wrap list and blockquote content as well.
### Theming
`MarkdownTheme` is a flat struct of `Style` fields plus a few extras. `DefaultMarkdownTheme()` returns a glow-inspired theme. Override individual fields and pass the result to `WithMarkdownTheme` or the `theme` attribute.
| Field | Type | Controls |
|-------|------|----------|
| `Heading` | `[6]Style` | Per-level heading styles, indexed `0` (h1) through `5` (h6) |
| `Paragraph` | `Style` | Body text |
| `Bold`, `Italic`, `CodeSpan`, `Link` | `Style` | Inline runs, layered over the surrounding text |
| `CodeBlockText` | `Style` | Fenced code block text |
| `CodeBlockBg` | `Color` | Code block fill (default: no fill) |
| `CodeBlockBorder` | `BorderStyle` | Box border around code blocks |
| `CodeHighlighter` | `CodeHighlighter` | Colorizes fenced code; `nil` disables highlighting |
| `TableHeader` | `Style` | Table header cells |
| `TableBorder` | `BorderStyle` | Table grid border |
| `BlockquoteBar` | `rune` | Left bar glyph |
| `BlockquoteBarStyle` | `Style` | Left bar style |
| `BlockquoteText` | `Style` | Quoted text |
| `BulletMarker` | `string` | Unordered-list marker, e.g. `"• "` |
### Syntax Highlighting
Fenced code blocks run through the theme's `CodeHighlighter`. The default is a built-in zero-dependency lexer covering Go, JSON, Bash, and JS/TS. An unrecognized language renders in `CodeBlockText`.
```go
type CodeHighlighter interface {
Highlight(lang, code string) [][]TextSpan
}
```
| Function | Description |
|----------|-------------|
| `NewHighlighter(p Palette)` | Built-in highlighter using palette `p` |
| `DefaultPalette()` | Default One Dark color scheme as a `Palette` (a `map[TokenKind]Color`) |
Set `theme.CodeHighlighter = nil` to render code uncolored, pass `NewHighlighter` a custom `Palette` to recolor the built-in lexer, or implement `CodeHighlighter` to plug in another engine such as chroma.
### Interfaces Implemented
| Interface | Purpose |
|-----------|---------|
| `Component` | `Render(app *App) *Element` returns the rendered block tree |
| `AppBinder` | `BindApp()` wires the reactive state source to the app (a no-op for a static source) |
| `PropsUpdater` | `UpdateProps()` receives fresh source, width, and theme when re-rendered from cache |
### GSX Usage
```gsx
<div class="overflow-y-scroll scrollbar-hidden grow" ref={s.content} scrollOffset={0, s.scrollY.Get()}>
<markdown source={s.doc} width={80} />
</div>
```
Markdown holds no scroll position, so the surrounding container provides the ref, the `scrollOffset` binding, and the key handling. See the [Markdown Guide](../guides/24-markdown) for the full scrollable viewer.
## Cross-References
- [Component Interfaces Reference](interfaces.md) — `Component`, `KeyListener`, `WatcherProvider`, `Focusable`, `AppBinder`
- [Events Reference](events.md) — `KeyEvent`, `Key` constants, `KeyMap`
- [State Reference](state.md) — `State[T]` used internally by TextArea
- [Styling Reference](styling.md) — `Style` and `BorderStyle` for visual configuration
- [Focus Guide](../guides/13-focus.md) — Focus management and Tab navigation
- [Inline Mode Guide](../guides/12-inline-mode.md) — Using TextArea in inline mode
|