gpui-rsx 0.3.2

A JSX-like macro for GPUI - simplify UI development with HTML-like syntax
Documentation
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
# Troubleshooting

Common issues and solutions when using GPUI-RSX.

## Compilation Errors

### Error: "closing tag does not match opening tag"

**Error message:**
```
Closing tag `</span>` does not match opening tag `<div>`. Tags must be properly nested.
```

**Solution:**
Ensure all tags are properly matched:

```rust
// ✗ Wrong
<div>
    <span>"Text"</div>
</span>

// ✓ Correct
<div>
    <span>"Text"</span>
</div>
```

### Error: "unclosed tag"

**Error message:**
```
Unclosed tag `<div>`. Expected closing tag before end of input.
```

**Solution:**
Add the missing closing tag:

```rust
// ✗ Wrong
rsx! {
    <div>"Content"
}

// ✓ Correct
rsx! {
    <div>"Content"</div>
}
```

### Error: "unexpected token"

**Error message:**
```
Unexpected token in `<div>`. Expected one of: {expr}, "text", <child>, or </div>
```

**Solution:**
Wrap bare identifiers in braces:

```rust
// ✗ Wrong
<div>count</div>

// ✓ Correct
<div>{count}</div>
```

### Error: "class attribute only supports string literals"

**Error message:**
```
class attribute only supports string literals; use individual attributes for dynamic styling
```

**Solution:**
Use individual attributes for dynamic values:

```rust
// ✗ Wrong
<div class={format!("flex {}", self.extra_classes)} />

// ✓ Correct
<div
    flex
    bg={self.get_background()}
/>
```

### Error: "expected '{' after for-in expression"

**Error message:**
```
Expected '{' after for-in expression to start the loop body.
```

**Solution:**
Add braces around the for-loop body:

```rust
// ✗ Wrong
{for item in items
    <div>{item}</div>
}

// ✓ Correct
{for item in items {
    <div>{item}</div>
}}
```

## Type Errors

### Error: "trait `IntoElement` is not implemented"

**Error:**
```
the trait `IntoElement` is not implemented for `Option<String>`
```

**Solution:**
Unwrap or handle the Option:

```rust
// ✗ Wrong
<div>{self.optional_text}</div>

// ✓ Correct - unwrap with default
<div>{self.optional_text.as_deref().unwrap_or("")}</div>

// ✓ Correct - conditional rendering
{if let Some(text) = &self.optional_text {
    rsx! { <div>{text.as_str()}</div> }
}}
```

### Error: "mismatched types" in conditional

**Error:**
```
expected `Div`, found `Span`
```

**Solution:**
Both branches must return the same type. Use a common container:

```rust
// ✗ Wrong
fn render_content(&self) -> impl IntoElement {
    if self.use_div {
        rsx! { <div>"Content"</div> }
    } else {
        rsx! { <span>"Content"</span> }
    }
}

// ✓ Correct - wrap in common type
fn render_content(&self) -> impl IntoElement {
    rsx! {
        <div>
            {if self.use_bold {
                rsx! { <strong>"Content"</strong> }
            } else {
                rsx! { <span>"Content"</span> }
            }}
        </div>
    }
}
```

### Error: "cannot move out of `self`"

**Error:**
```
cannot move out of `self.items` which is behind a shared reference
```

**Solution:**
Use references in loops:

```rust
// ✗ Wrong
{for item in self.items {
    <div>{item.name}</div>
}}

// ✓ Correct
{for item in &self.items {
    <div>{item.name.as_str()}</div>
}}
```

## Runtime Issues

### Elements not appearing

**Symptoms:**
- Elements don't show up in the UI
- Layout is empty

**Possible causes and solutions:**

1. **Forgot to return from render:**
   ```rust
   // ✗ Wrong
   fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
       rsx! { <div>"Content"</div> };  // Note the semicolon
   }

   // ✓ Correct
   fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
       rsx! { <div>"Content"</div> }  // No semicolon
   }
   ```

2. **Missing `cx.notify()` after state changes:**
   ```rust
   // ✗ Wrong
   fn handle_click(&mut self, cx: &mut ViewContext<Self>) {
       self.count += 1;
       // Missing cx.notify()
   }

   // ✓ Correct
   fn handle_click(&mut self, cx: &mut ViewContext<Self>) {
       self.count += 1;
       cx.notify();  // Trigger re-render
   }
   ```

3. **Size constraints:**
   ```rust
   // Element might be zero-sized
   <div />

   // Add explicit size
   <div class="w-full h-full" />
   ```

### Events not firing

**Symptoms:**
- Clicks or other events don't trigger handlers

**Possible causes and solutions:**

1. **Element needs an ID for interactive events:**

   RSX automatically adds IDs for elements with event handlers, but if you're getting issues:

   ```rust
   // Add explicit ID if needed
   <button id="my-button" onClick={handler}>
       "Click"
   </button>
   ```

2. **Element is covered by another element:**

   Check z-index and layering:
   ```rust
   <button class="relative z-10" onClick={handler}>
       "Click"
   </button>
   ```

3. **Handler signature is incorrect:**
   ```rust
   // ✓ Correct
   onClick={cx.listener(|view, _event, cx| {
       view.handle_click(cx);
   })}
   ```

### Styling not applied

**Symptoms:**
- Colors, sizes, or other styles don't appear

**Possible causes and solutions:**

1. **Typo in class name:**
   ```rust
   // ✗ Wrong
   <div class="flex-collumn" />  // Typo

   // ✓ Correct
   <div class="flex-col" />
   ```

2. **Invalid color name:**
   ```rust
   // ✗ Wrong - invalid shade
   <div class="bg-blue-550" />

   // ✓ Correct - valid shade
   <div class="bg-blue-500" />
   ```

3. **Style override order:**
   ```rust
   // Later attributes override earlier ones
   <div
       bg={rgb(0xff0000)}  // Red
       class="bg-blue-500"  // Blue overrides red
   />
   ```

### Performance issues

**Symptoms:**
- Slow rendering
- High CPU usage
- Laggy interactions

**Solutions:**

1. **Minimize cloning in loops:**
   ```rust
   // ✗ Slow
   {for item in &self.items {
       <div>{item.description.clone()}</div>
   }}

   // ✓ Faster
   {for item in &self.items {
       <div>{item.description.as_str()}</div>
   }}
   ```

2. **Extract static elements:**
   ```rust
   // ✗ Re-creates header on every render
   rsx! {
       <div>
           <header class="flex p-4">"Static Header"</header>
           {self.dynamic_content()}
       </div>
   }

   // ✓ Extract to separate method
   fn render_static_header() -> impl IntoElement {
       rsx! { <header class="flex p-4">"Static Header"</header> }
   }
   ```

3. **Batch state updates:**
   ```rust
   // ✗ Multiple re-renders
   self.value1 = new_value1;
   cx.notify();
   self.value2 = new_value2;
   cx.notify();

   // ✓ Single re-render
   self.value1 = new_value1;
   self.value2 = new_value2;
   cx.notify();
   ```

## IDE Issues

### No syntax highlighting

**Solution:**
RSX syntax is valid Rust syntax, so standard Rust syntax highlighting should work. If you're experiencing issues:

1. **Restart your IDE/language server**
2. **Ensure rust-analyzer is up to date:**
   ```bash
   rustup update
   ```

### No autocomplete in RSX

**Solution:**
Autocomplete inside `rsx!` macros is limited. For better IDE support:

1. **Extract complex logic to methods**
2. **Use explicit types:**
   ```rust
   let items: &[Item] = &self.items;
   rsx! {
       <div>
           {for item in items {
               <div>{item.name.as_str()}</div>
           }}
       </div>
   }
   ```

### Macro expansion errors

**Solution:**
View expanded macro output:

```bash
cargo expand --lib
```

Or for a specific module:

```bash
cargo expand --lib module_name
```

## Build Issues

### Long compile times

**Symptoms:**
- Slow incremental builds
- High CPU usage during compilation

**Solutions:**

1. **Use release mode for final builds only:**
   ```bash
   cargo build  # Fast debug builds for development
   ```

2. **Split large RSX blocks:**
   ```rust
   // ✗ One huge RSX block
   rsx! {
       <div>
           // 500 lines of RSX
       </div>
   }

   // ✓ Split into methods
   fn render(&self) -> impl IntoElement {
       rsx! {
           <div>
               {self.render_header()}
               {self.render_body()}
               {self.render_footer()}
           </div>
       }
   }
   ```

3. **Update dependencies:**
   ```bash
   cargo update
   ```

### Dependency conflicts

**Error:**
```
failed to select a version for `syn`
```

**Solution:**
Ensure compatible versions:

```toml
[dependencies]
gpui-rsx = "0.1"
syn = { version = "2.0", features = ["full"] }
```

## Getting Help

If you're still stuck:

1. **Enable debug logging:**
   ```rust
   env_logger::init();
   log::debug!("State: {:?}", self);
   ```

3. **Simplify to minimal reproduction:**
   ```rust
   // Remove features until it works
   // Then add them back one by one
   ```

4. **Search existing issues:**
   - [GPUI-RSX Issues]https://github.com/wsafight/gpui-rsx/issues
   - [GPUI Issues]https://github.com/zed-industries/gpui/issues

5. **Create a new issue:**
   Include:
   - Minimal code example
   - Error messages
   - Expected vs actual behavior
   - Rust version (`rustc --version`)
   - GPUI version
   - GPUI-RSX version

## Common Pitfalls

### Semicolon after rsx! macro

```rust
// ✗ Returns ()
rsx! { <div>"Content"</div> };

// ✓ Returns impl IntoElement
rsx! { <div>"Content"</div> }
```

### Forgetting braces around expressions

```rust
// ✗ Syntax error
<div>self.count</div>

// ✓ Correct
<div>{self.count}</div>
```

### Using `clone()` excessively

```rust
// ✗ Expensive
{for item in &self.items {
    <div>{item.name.clone()}</div>
}}

// ✓ Cheaper
{for item in &self.items {
    <div>{item.name.as_str()}</div>
}}
```

### Missing return type bounds

```rust
// ✗ May cause inference issues
fn render_item(&self, item: &Item) {
    rsx! { <div>{item.name.as_str()}</div> }
}

// ✓ Explicit return type
fn render_item(&self, item: &Item) -> impl IntoElement {
    rsx! { <div>{item.name.as_str()}</div> }
}
```