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
|
# Refs and Click Handling Reference
## Overview
Refs give you direct access to rendered elements from your event handlers. Attach a ref to an element in `.gsx`, and after the render pass completes, the ref holds a pointer to that element. From there you can read layout bounds, control scroll position, or wire up click handlers.
go-tui provides three ref types to cover different use cases:
- **`Ref`** -- a single element reference
- **`RefList`** -- an ordered collection built from `for` loops
- **`RefMap[K]`** -- a keyed collection built from `for` loops with a `key` attribute
All three are thread-safe.
## Ref
A reference to one element. Declare it as a struct field, create it in the constructor, attach it in `.gsx` with `ref={...}`, and read it in handlers.
### NewRef
```go
func NewRef() *Ref
```
Creates a new empty ref. The framework populates it during each render pass.
```go
type myApp struct {
header *tui.Ref
}
func MyApp() *myApp {
return &myApp{
header: tui.NewRef(),
}
}
```
### Set
```go
func (r *Ref) Set(v *Element)
```
Stores an element in the ref. You rarely call this yourself since the generated code calls it during rendering. Each render pass overwrites the previous value, so the ref always points to the latest element.
### El
```go
func (r *Ref) El() *Element
```
Returns the stored element, or `nil` if the ref hasn't been set yet. Always check for `nil` before using the result, since the ref may be empty during the first render or if the element is conditionally hidden.
```go
el := s.header.El()
if el != nil {
_, maxY := el.MaxScroll()
// use maxY
}
```
### IsSet
```go
func (r *Ref) IsSet() bool
```
Returns `true` if the ref holds a non-nil element.
```go
if s.header.IsSet() {
s.header.El().ScrollToBottom()
}
```
### Usage in .gsx
Attach a ref to any element with the `ref` attribute:
```gsx
templ (s *myApp) Render() {
<div ref={s.header} class="border-rounded p-1">
<span>Header content</span>
</div>
}
```
The generated code calls `s.header.Set(element)` after creating the element, making it available in `HandleMouse`, `KeyMap`, and other handler methods.
## RefList
An ordered collection of element references, populated automatically when you use `ref=` inside a `for` loop without a `key` attribute.
### NewRefList
```go
func NewRefList() *RefList
```
Creates an empty ref list.
```go
type listApp struct {
items *tui.State[[]string]
itemRefs *tui.RefList
}
func ListApp() *listApp {
return &listApp{
items: tui.NewState([]string{"alpha", "beta", "gamma"}),
itemRefs: tui.NewRefList(),
}
}
```
### Append
```go
func (r *RefList) Append(el *Element)
```
Adds an element to the list. Called by the generated code for each iteration of a `for` loop where the element has a `ref` attribute.
### All
```go
func (r *RefList) All() []*Element
```
Returns a copy of all stored elements. The returned slice is safe to modify without affecting the ref list.
```go
for _, el := range s.itemRefs.All() {
fmt.Println(el.Text())
}
```
### At
```go
func (r *RefList) At(i int) *Element
```
Returns the element at the given index, or `nil` if the index is out of bounds.
```go
el := s.itemRefs.At(2)
if el != nil {
fmt.Println(el.Text())
}
```
### Len
```go
func (r *RefList) Len() int
```
Returns the number of elements in the list.
### Usage in .gsx
Use `ref=` inside a `for` loop **without** a `key` attribute. The analyzer detects the loop context and generates `Append` calls instead of `Set`:
```gsx
templ (s *listApp) Render() {
<div class="flex-col">
for _, item := range s.items.Get() {
<span ref={s.itemRefs} class="p-1">{item}</span>
}
</div>
}
```
The same `ref` variable is used on every iteration. The generated code calls `s.itemRefs.Append(el)` each time through the loop.
## RefMap[K]
A keyed collection of element references, populated when you use `ref=` inside a `for` loop **with** a `key` attribute. The `key` tells the generator to use `Put(key, el)` instead of `Append(el)`, so you can look up a specific element by its key rather than by position.
### NewRefMap
```go
func NewRefMap[K comparable]() *RefMap[K]
```
Creates an empty ref map. The type parameter `K` must be comparable (strings, ints, and most value types work).
```go
type tabApp struct {
tabs *tui.State[[]string]
tabRefs *tui.RefMap[string]
}
func TabApp() *tabApp {
return &tabApp{
tabs: tui.NewState([]string{"home", "settings", "help"}),
tabRefs: tui.NewRefMap[string](),
}
}
```
### Put
```go
func (r *RefMap[K]) Put(key K, el *Element)
```
Stores an element under the given key. Called by the generated code for each iteration of a `for` loop where the element has both `ref` and `key` attributes.
### Get
```go
func (r *RefMap[K]) Get(key K) *Element
```
Returns the element stored under the key, or `nil` if the key doesn't exist.
```go
el := s.tabRefs.Get("settings")
if el != nil {
// do something with the settings tab element
}
```
### All
```go
func (r *RefMap[K]) All() map[K]*Element
```
Returns a copy of all keyed elements. Safe to iterate over without affecting the ref map.
### Len
```go
func (r *RefMap[K]) Len() int
```
Returns the number of entries in the map.
### Usage in .gsx
Use `ref=` together with `key=` inside a `for` loop. The `key` attribute provides the map key for each element:
```gsx
templ (s *tabApp) Render() {
<div class="flex gap-1">
for _, name := range s.tabs.Get() {
<button ref={s.tabRefs} key={name} class="px-1">{name}</button>
}
</div>
}
```
The generated code calls `s.tabRefs.Put(name, el)` each iteration. Without `key`, the same loop would generate `Append` calls and treat the ref as a `RefList` instead.
On component elements (`<textarea>`, `<input>`, `<modal>`, `<markdown>`), the same `key` also identifies the cached component instance, so the component's state follows its item across reorders. See the [GSX Syntax Reference](gsx-syntax.md) for details.
You can then look up specific elements in handlers:
```go
el := s.tabRefs.Get("settings")
if el != nil && el.ContainsPoint(me.X, me.Y) {
s.activateTab("settings")
}
```
Or iterate over all entries:
```go
for name, el := range s.tabRefs.All() {
if el != nil && el.ContainsPoint(me.X, me.Y) {
s.activateTab(name)
return true
}
}
```
## Click Handling
go-tui pairs refs with handler functions so you don't have to compare mouse coordinates against layout bounds yourself. You list which ref maps to which action, and the framework does the hit testing.
### ClickBinding
```go
type ClickBinding struct {
Ref *Ref
Fn func()
}
```
Pairs a ref with a handler function. Created by `Click` and consumed by `HandleClicks`.
### Click
```go
func Click(ref *Ref, fn func()) ClickBinding
```
Links a ref to a callback.
```go
tui.Click(s.saveBtn, s.save)
```
### HandleClicks
```go
func HandleClicks(me MouseEvent, bindings ...ClickBinding) bool
```
Tests a mouse event against a list of click bindings. Returns `true` if a binding matched and its handler was called.
How it works:
1. Ignores everything except left-button press events (`MouseLeft` + `MousePress`). Right clicks, releases, drags, and scroll wheel events return `false` immediately.
2. Iterates through bindings in order.
3. For each binding, checks that the ref is set (non-nil element) and that the click coordinates fall within the element's layout bounds via `ContainsPoint`.
4. Calls the handler of the first match and returns `true`.
5. If nothing matches, returns `false`.
Use it in your `HandleMouse` implementation:
```go
func (c *colorMixer) HandleMouse(me tui.MouseEvent) bool {
return tui.HandleClicks(me,
tui.Click(c.redUpBtn, func() { c.adjustColor(&c.red, 16) }),
tui.Click(c.redDnBtn, func() { c.adjustColor(&c.red, -16) }),
tui.Click(c.greenUpBtn, func() { c.adjustColor(&c.green, 16) }),
tui.Click(c.greenDnBtn, func() { c.adjustColor(&c.green, -16) }),
)
}
```
Binding order matters. If two elements overlap, the first matching binding wins. Place more specific bindings before general ones.
### Enabling Mouse Support
Mouse events only fire if you enable mouse support on the app:
```go
app, err := tui.NewApp(
tui.WithRootComponent(MyApp()),
tui.WithMouse(),
)
```
Without `WithMouse()`, your `HandleMouse` method never gets called.
## Practical Patterns
### Scroll control with refs
Use refs to read layout information for scroll clamping:
```go
type logViewer struct {
lines *tui.State[[]string]
scrollY *tui.State[int]
content *tui.Ref
}
func LogViewer() *logViewer {
return &logViewer{
lines: tui.NewState([]string{}),
scrollY: tui.NewState(0),
content: tui.NewRef(),
}
}
func (v *logViewer) scrollBy(delta int) {
el := v.content.El()
if el == nil {
return
}
_, maxY := el.MaxScroll()
newY := v.scrollY.Get() + delta
if newY < 0 {
newY = 0
} else if newY > maxY {
newY = maxY
}
v.scrollY.Set(newY)
}
```
```gsx
templ (v *logViewer) Render() {
<div
ref={v.content}
class="flex-col border-single p-1"
scrollable={tui.ScrollVertical}
scrollOffset={0, v.scrollY.Get()}
flexGrow={1.0}
>
for _, line := range v.lines.Get() {
<span>{line}</span>
}
</div>
}
```
### Clickable buttons with visual feedback
Combine refs, state, and click handling for interactive buttons:
```gsx
templ (s *myApp) Render() {
<div class="flex gap-2">
<button ref={s.incBtn} class="px-2 border-rounded text-green">{" + "}</button>
<span class="font-bold">{fmt.Sprintf("%d", s.count.Get())}</span>
<button ref={s.decBtn} class="px-2 border-rounded text-red">{" - "}</button>
</div>
}
```
```go
func (s *myApp) HandleMouse(me tui.MouseEvent) bool {
return tui.HandleClicks(me,
tui.Click(s.incBtn, func() {
s.count.Update(func(v int) int { return v + 1 })
}),
tui.Click(s.decBtn, func() {
s.count.Update(func(v int) int { return v - 1 })
}),
)
}
```
### Conditional refs
When an element is conditionally rendered, its ref may be nil on some frames. Always guard access:
```go
func (s *myApp) doSomething() {
if el := s.panel.El(); el != nil {
el.ScrollToTop()
}
}
```
## Thread Safety
All ref types use `sync.RWMutex` internally. `El()`, `IsSet()`, `All()`, `At()`, `Get()`, and `Len()` acquire a read lock. `Set()`, `Append()`, and `Put()` acquire a write lock.
In practice, the framework writes refs during the render pass (single-threaded) and you read them in handlers (also single-threaded on the event loop). The mutex is there for safety, not because concurrent access is common.
|