gpui-rsx 0.4.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
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
# Best Practices

Guidelines and patterns for effective GPUI-RSX development.

## Code Organization

### Component Extraction

Extract reusable UI into separate functions:

```rust
impl MyView {
    fn render_header(&self) -> impl IntoElement {
        rsx! {
            <header class="flex justify-between p-4 bg-gray-100">
                <h1 styled>{self.title.clone()}</h1>
                {self.render_nav()}
            </header>
        }
    }

    fn render_nav(&self) -> impl IntoElement {
        rsx! {
            <nav class="flex gap-2">
                {for item in &self.nav_items {
                    <a class="cursor-pointer">
                        {item.label.clone()}
                    </a>
                }}
            </nav>
        }
    }

    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        rsx! {
            <div>
                {self.render_header()}
                <main>{self.render_content()}</main>
            </div>
        }
    }
}
```

### Avoid Deep Nesting

Break down complex UIs into smaller, focused components:

**Don't:**

```rust
rsx! {
    <div>
        <div>
            <div>
                <div>
                    <div>
                        // Too deep!
                    </div>
                </div>
            </div>
        </div>
    </div>
}
```

**Do:**

```rust
// Split into multiple methods
fn render_card(&self) -> impl IntoElement {
    rsx! { <div>{self.render_card_content()}</div> }
}

fn render_card_content(&self) -> impl IntoElement {
    rsx! { <div>{ /* content */ }</div> }
}
```

## Performance

### Minimize Cloning

Avoid unnecessary clones in loops:

**Less efficient:**

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

**More efficient:**

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

### Use References When Possible

```rust
// Good: pass by reference
fn render_list(&self, items: &[Item]) -> impl IntoElement {
    rsx! {
        <ul>
            {for item in items {
                <li>{item.display()}</li>
            }}
        </ul>
    }
}
```

### Batch Updates

Group related state changes:

```rust
onClick={cx.listener(|view, _, _window, cx| {
    // Batch multiple updates
    view.count += 1;
    view.last_update = now();
    view.update_history();
    // Single notify for all changes
    cx.notify();
})}
```

## Styling

### Use Class Attribute for Static Styles

Prefer `class` attribute for unchanging styles:

**Preferred:**

```rust
<div class="flex flex-col gap-4 p-4 bg-blue-500" />
```

**Verbose:**

```rust
<div
    flex
    flex_col
    gap={px(16.0)}
    p={px(16.0)}
    bg={rgb(0x3b82f6)}
/>
```

### Use Individual Attributes for Dynamic Styles

For dynamic values, use individual attributes:

```rust
<div
    class="flex flex-col"
    bg={self.get_background_color()}
    h={px(self.height)}
/>
```

### Consistent Color Usage

Define color constants:

```rust
const PRIMARY: u32 = 0x3b82f6;
const DANGER: u32 = 0xef4444;
const SUCCESS: u32 = 0x10b981;

rsx! {
    <button class="px-4 py-2" bg={rgb(PRIMARY)}>
        "Primary Action"
    </button>
}
```

Or use Tailwind classes:

```rust
<button class="px-4 py-2 bg-blue-500">"Primary Action"</button>
```

## Event Handling

### Extract Event Handlers

For complex logic, extract handlers:

```rust
impl MyView {
    fn handle_submit(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
        if self.validate() {
            self.submit_data(cx);
            cx.notify();
        }
    }
}

// In render:
rsx! {
    <button onClick={cx.listener(Self::handle_submit)}>
        "Submit"
    </button>
}
```

### Use Descriptive Handler Names

```rust
impl MyView {
    fn handle_increment_click(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
        self.count += 1;
        cx.notify();
    }

    fn handle_reset_click(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
        self.count = 0;
        cx.notify();
    }
}
```

### Avoid Inline Complex Logic

**Don't:**

```rust
<button onClick={cx.listener(|view, _, _window, cx| {
    // 20 lines of complex logic
    view.data.iter_mut().for_each(|item| {
        // more complex operations
    });
    // ...
})}>
    "Submit"
</button>
```

**Do:**

```rust
<button onClick={cx.listener(Self::handle_complex_submit)}>
    "Submit"
</button>
```

## Conditional Rendering

### Use `when` Attribute for Simple Cases

```rust
<div
    class="px-4 py-2"
    when={(self.is_active, |el| el.bg(rgb(0x00ff00)))}
>
    "Status"
</div>
```

### Use Rust `if` for Complex Conditions

```rust
{if self.loading {
    rsx! { <div>"Loading..."</div> }
} else if let Some(data) = &self.data {
    rsx! { <div>{data.render()}</div> }
} else {
    rsx! { <div>"No data"</div> }
}}
```

### Pattern Matching

```rust
{match &self.state {
    State::Loading => rsx! { <div>"Loading..."</div> },
    State::Success(data) => rsx! { <div>{data.display()}</div> },
    State::Error(err) => rsx! { <div class="text-red-600">{err.to_string()}</div> },
}}
```

## Lists and Iteration

### Use References in Loops

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

### Add Keys for Dynamic Lists

When items can be reordered or removed, use unique IDs:

```rust
{for item in &self.items {
    <div id={&item.id}>
        {item.name.as_str()}
    </div>
}}
```

### Handle Empty Lists

```rust
{if self.items.is_empty() {
    rsx! { <div class="text-gray-500">"No items"</div> }
} else {
    rsx! {
        <ul>
            {for item in &self.items {
                <li>{item.name.as_str()}</li>
            }}
        </ul>
    }
}}
```

## Type Safety

### Leverage Type Inference

GPUI-RSX generates type-safe code. Let Rust's type system help:

```rust
// Compiler will catch type mismatches
<div gap={px(16.0)}>  // ✓ Correct type
    {self.render_child()}
</div>

<div gap={"16px"}>    // ✗ Compiler error - wrong type
    {self.render_child()}
</div>
```

### Return Consistent Types

Ensure all branches return compatible types:

```rust
fn render_content(&self) -> impl IntoElement {
    if self.show_text {
        rsx! { <div>"Text"</div> }
    } else {
        // Both branches return impl IntoElement
        rsx! { <span>"Other"</span> }
    }
}
```

## Error Handling

### Provide Meaningful Error Context

```rust
impl MyView {
    fn load_data(&mut self, cx: &mut Context<Self>) {
        match self.try_load() {
            Ok(data) => {
                self.data = Some(data);
                self.error = None;
            }
            Err(e) => {
                self.error = Some(format!("Failed to load: {}", e));
                log::error!("Data load error: {:?}", e);
            }
        }
        cx.notify();
    }
}

// In render:
{if let Some(error) = &self.error {
    rsx! {
        <div class="p-4 bg-red-100 text-red-800 rounded">
            {error.clone()}
        </div>
    }
}}
```

## Testing

### Test Component Logic Separately

```rust
impl MyView {
    // Testable logic in methods
    pub fn increment(&mut self) -> usize {
        self.count += 1;
        self.count
    }

    pub fn reset(&mut self) {
        self.count = 0;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_increment() {
        let mut view = MyView { count: 0 };
        assert_eq!(view.increment(), 1);
        assert_eq!(view.increment(), 2);
    }

    #[test]
    fn test_reset() {
        let mut view = MyView { count: 5 };
        view.reset();
        assert_eq!(view.count, 0);
    }
}
```

## Documentation

### Document Complex Components

```rust
/// A reusable card component with header and body.
///
/// # Example
/// ```ignore
/// self.render_card("Title", rsx! { <div>"Content"</div> })
/// ```
fn render_card(&self, title: &str, content: impl IntoElement) -> impl IntoElement {
    rsx! {
        <div class="border rounded-lg p-4">
            <h3 class="font-bold mb-2">{title}</h3>
            <div>{content}</div>
        </div>
    }
}
```

### Add Examples to Complex Patterns

```rust
/// Renders a list with alternating background colors.
///
/// # Example
/// ```ignore
/// let items = vec!["A", "B", "C"];
/// self.render_striped_list(&items)
/// ```
fn render_striped_list(&self, items: &[String]) -> impl IntoElement {
    rsx! {
        <ul>
            {for (i, item) in items.iter().enumerate() {
                <li
                    class="p-2"
                    when={(i % 2 == 0, |el| el.bg(rgb(0xf3f4f6)))}
                >
                    {item.as_str()}
                </li>
            }}
        </ul>
    }
}
```

## Common Patterns

### Loading States

```rust
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
    match &self.state {
        LoadState::Initial => rsx! {
            <button onClick={cx.listener(Self::start_load)}>
                "Load Data"
            </button>
        },
        LoadState::Loading => rsx! {
            <div class="flex items-center gap-2">
                <div class="spinner" />
                "Loading..."
            </div>
        },
        LoadState::Loaded(data) => rsx! {
            <div>{self.render_data(data)}</div>
        },
        LoadState::Error(err) => rsx! {
            <div class="text-red-600">
                "Error: "{err.as_str()}
            </div>
        },
    }
}
```

### Modal Dialogs

```rust
fn render_with_modal(&self) -> impl IntoElement {
    rsx! {
        <div>
            {self.render_main_content()}

            {if self.show_modal {
                rsx! {
                    <div class="fixed inset-0 flex items-center justify-center">
                        <div class="absolute inset-0 bg-black opacity-50" />
                        <div class="relative bg-white rounded-lg p-8">
                            {self.render_modal_content()}
                        </div>
                    </div>
                }
            }}
        </div>
    }
}
```

### Tooltips

```rust
<div
    class="relative"
    onHover={cx.listener(|view, _, _window, cx| {
        view.show_tooltip = true;
        cx.notify();
    })}
>
    "Hover me"

    {if self.show_tooltip {
        rsx! {
            <div class="absolute top-full mt-2 p-2 bg-gray-800 text-white rounded">
                "Tooltip text"
            </div>
        }
    }}
</div>
```

## Migration from Manual GPUI

### Before (Manual GPUI)

```rust
div()
    .flex()
    .flex_col()
    .gap(px(16.0))
    .child("Hello")
    .child(
        button()
            .bg(rgb(0x3b82f6))
            .on_click(handler)
            .child("Click")
    )
```

### After (GPUI-RSX)

```rust
rsx! {
    <div class="flex flex-col gap-4">
        "Hello"
        <button
            class="bg-blue-500"
            onClick={handler}
        >
            "Click"
        </button>
    </div>
}
```

## Anti-Patterns

### Don't Use String Concatenation for Classes

**Don't:**

```rust
// This won't work!
<div class={format!("flex {}", if active { "bg-blue-500" } else { "" })} />
```

**Do:**

```rust
<div
    class="flex"
    when={(active, |el| el.bg(rgb(0x3b82f6)))}
/>
```

### Don't Mix Styles and Logic

Keep business logic separate from rendering:

**Don't:**

```rust
{for item in &items {
    {
        let processed = complex_transformation(item);
        rsx! { <div>{processed}</div> }
    }
}}
```

**Do:**

```rust
{for item in &self.get_processed_items() {
    <div>{item.display()}</div>
}}
```

### Don't Overuse `clone()`

Minimize cloning by using references and `as_str()`:

**Don't:**

```rust
<div>{self.text.clone()}</div>
```

**Do:**

```rust
<div>{self.text.as_str()}</div>
```