euv-example 0.3.11

An example application demonstrating the euv UI framework with reactive signals, custom components, and WebAssembly.
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
use crate::*;

/// A child display component that receives strongly-typed props from parent.
///
/// Demonstrates one-way data flow: parent passes data down via props,
/// child communicates back up via callback.
///
/// # Arguments
///
/// - `VirtualNode` - The props node containing message and on_respond callback.
///
/// # Returns
///
/// - `VirtualNode` - A styled child display element.
pub fn child_display(props: VirtualNode) -> VirtualNode {
    let ChildDisplayProps {
        message,
        on_respond,
    }: ChildDisplayProps = props.into();
    html! {
        div {
            class: c_binding_child_box()
            p {
                class: c_binding_child_label()
                "Child received:"
            }
            p {
                class: c_binding_child_message()
                message
            }
            primary_button {
                label: "Respond"
                onclick: on_respond
                "Respond to Parent"
            }
        }
    }
}

/// A limited counter component that receives strongly-typed (non-String) props.
///
/// Demonstrates passing `bool` and `i32` props through `html!` macro
/// and extracting them with `try_get_typed_prop` via `From<&VirtualNode>`.
///
/// # Arguments
///
/// - `VirtualNode` - The props node containing disabled (bool) and max_count (i32).
///
/// # Returns
///
/// - `VirtualNode` - A styled limited counter element.
pub fn limited_counter(props: VirtualNode) -> VirtualNode {
    let LimitedCounterProps {
        disabled,
        max_count,
        on_increment,
        on_reset,
    }: LimitedCounterProps = props.into();
    html! {
        div {
            class: c_binding_child_box()
            p {
                class: c_binding_child_label()
                "Limited Counter"
            }
            p {
                class: c_demo_text()
                "Props received: disabled="
                span {
                    class: c_binding_typed_prop_value()
                    { disabled.to_string() }
                }
                ", max_count="
                span {
                    class: c_binding_typed_prop_value()
                    { max_count.to_string() }
                }
            }
            div {
                class: c_counter_row()
                primary_button {
                    label: "+1"
                    onclick: on_increment
                    disabled: disabled
                    "+1"
                }
                primary_button {
                    label: "Reset"
                    onclick: on_reset
                    "Reset"
                }
            }
            if { disabled } {
                p {
                    class: c_binding_typed_warning()
                    "Counter is disabled!"
                }
            } else {
                ""
            }
        }
    }
}

/// A callback input component that receives custom callback functions as props.
///
/// Demonstrates passing `on_change` and `on_submit` callbacks through
/// `html!` macro and extracting them with `try_get_callback` via `From<&VirtualNode>`.
///
/// # Arguments
///
/// - `VirtualNode` - The props node containing on_change and on_submit callbacks.
///
/// # Returns
///
/// - `VirtualNode` - A styled input with callback element.
pub fn callback_input(props: VirtualNode) -> VirtualNode {
    let CallbackInputProps {
        on_change,
        on_submit,
        on_reset,
    }: CallbackInputProps = props.into();
    html! {
        div {
            class: c_binding_child_box()
            p {
                class: c_binding_child_label()
                "Callback Input"
            }
            p {
                class: c_demo_text()
                "Custom callbacks: on_change, on_submit, on_reset"
            }
            div {
                class: c_binding_callback_form()
                input {
                    r#type: "text"
                    placeholder: "Type something..."
                    class: c_form_input_no_transition()
                    oninput: on_change
                }
                div {
                    class: c_counter_row()
                    primary_button {
                        label: "Submit"
                        onclick: on_submit
                        "Submit"
                    }
                    primary_button {
                        label: "Reset"
                        onclick: on_reset
                        "Reset"
                    }
                }
            }
        }
    }
}

/// A child input component that shares a Signal with parent for two-way binding.
///
/// Demonstrates two-way binding: the same Signal is used by both parent
/// and child components, so changes in either are reflected everywhere.
///
/// # Arguments
///
/// - `Signal<String>` - The shared text signal.
/// - `Signal<i32>` - The shared count signal.
///
/// # Returns
///
/// - `VirtualNode` - A styled child input element.
pub fn child_input(text_signal: Signal<String>, count_signal: Signal<i32>) -> VirtualNode {
    let text_value: String = text_signal.get();
    let count_value: i32 = count_signal.get();
    html! {
        div {
            class: c_binding_child_box()
            p {
                class: c_binding_child_label()
                "Child Component"
            }
            div {
                class: c_form_input_wrapper()
                label {
                    class: c_form_label()
                    "Edit shared text:"
                }
                input {
                    r#type: "text"
                    value: text_value
                    class: c_form_input_no_transition()
                    oninput: on_input_value(text_signal)
                }
            }
            div {
                class: c_counter_row()
                span {
                    class: c_demo_text()
                    "Shared count: "
                    span {
                        class: c_counter_value()
                        count_value
                    }
                }
                primary_button {
                    label: "Decrement"
                    onclick: two_way_on_decrement(count_signal)
                    "-"
                }
            }
        }
    }
}

/// A temperature converter component that reactively syncs Celsius and Fahrenheit.
///
/// Demonstrates cross-component reactive binding using `watch!`:
/// changing either field automatically updates the other.
///
/// # Arguments
///
/// - `Signal<f64>` - The celsius signal.
/// - `Signal<f64>` - The fahrenheit signal.
///
/// # Returns
///
/// - `VirtualNode` - A temperature converter element.
pub fn temperature_converter(
    celsius_signal: Signal<f64>,
    fahrenheit_signal: Signal<f64>,
) -> VirtualNode {
    let celsius_value: f64 = celsius_signal.get();
    let fahrenheit_value: f64 = fahrenheit_signal.get();
    html! {
        div {
            class: c_binding_temp_converter()
            div {
                class: c_binding_temp_field()
                label {
                    class: c_form_label()
                    "Celsius"
                }
                input {
                    r#type: "number"
                    value: format!("{:.1}", celsius_value)
                    class: c_form_input_no_transition()
                    oninput: cross_on_input_celsius(celsius_signal)
                }
            }
            span {
                class: c_binding_temp_arrow()
                "="
            }
            div {
                class: c_binding_temp_field()
                label {
                    class: c_form_label()
                    "Fahrenheit"
                }
                input {
                    r#type: "number"
                    value: format!("{:.1}", fahrenheit_value)
                    class: c_form_input_no_transition()
                    oninput: cross_on_input_fahrenheit(fahrenheit_signal)
                }
            }
        }
    }
}

/// A color mixer component that reactively syncs RGB sliders with a hex color display.
///
/// Demonstrates cross-component reactive binding: changing any RGB slider
/// automatically updates the hex color string and the color preview.
///
/// # Arguments
///
/// - `Signal<i32>` - The red channel signal.
/// - `Signal<i32>` - The green channel signal.
/// - `Signal<i32>` - The blue channel signal.
/// - `Signal<String>` - The hex color signal.
///
/// # Returns
///
/// - `VirtualNode` - A color mixer element.
pub fn color_mixer(
    red_signal: Signal<i32>,
    green_signal: Signal<i32>,
    blue_signal: Signal<i32>,
    hex_color_signal: Signal<String>,
) -> VirtualNode {
    let red_value: i32 = red_signal.get();
    let green_value: i32 = green_signal.get();
    let blue_value: i32 = blue_signal.get();
    let hex_value: String = hex_color_signal.get();
    html! {
        div {
            class: c_binding_color_mixer()
            div {
                class: c_binding_color_preview()
                style: { background: { &hex_value }; }
                span {
                    class: c_binding_color_hex()
                    hex_value
                }
            }
            div {
                class: c_binding_slider_row()
                label {
                    class: c_binding_slider_label()
                    style: { color: "#ef4444"; }
                    "R"
                }
                input {
                    r#type: "range"
                    min: "0"
                    max: "255"
                    value: red_value.to_string()
                    class: c_binding_slider()
                    oninput: cross_on_input_i32(red_signal)
                }
                span {
                    class: c_binding_slider_value()
                    red_value
                }
            }
            div {
                class: c_binding_slider_row()
                label {
                    class: c_binding_slider_label()
                    style: { color: "#22c55e"; }
                    "G"
                }
                input {
                    r#type: "range"
                    min: "0"
                    max: "255"
                    value: green_value.to_string()
                    class: c_binding_slider()
                    oninput: cross_on_input_i32(green_signal)
                }
                span {
                    class: c_binding_slider_value()
                    green_value
                }
            }
            div {
                class: c_binding_slider_row()
                label {
                    class: c_binding_slider_label()
                    style: { color: "#3b82f6"; }
                    "B"
                }
                input {
                    r#type: "range"
                    min: "0"
                    max: "255"
                    value: blue_value.to_string()
                    class: c_binding_slider()
                    oninput: cross_on_input_i32(blue_signal)
                }
                span {
                    class: c_binding_slider_value()
                    blue_value
                }
            }
        }
    }
}

/// A component binding demo page showcasing parent-child data passing,
/// two-way binding, cross-component reactive binding, typed props,
/// and custom callback functions.
///
/// # Returns
///
/// - `VirtualNode` - The component binding demo page virtual DOM tree.
pub fn page_component_binding() -> VirtualNode {
    let props_state: UsePropsDemo = use_props_demo();
    let two_way_state: UseTwoWayDemo = use_two_way_demo();
    let cross_state: UseCrossComponentDemo = use_cross_component_demo();
    let typed_state: UseTypedPropsDemo = use_typed_props_demo();
    let callback_state: UseCustomCallbackDemo = use_custom_callback_demo();
    let is_disabled: bool = typed_state.get_disabled().get();
    let current_count: i32 = typed_state.get_current_count().get();
    let max_count: i32 = typed_state.get_max_count().get();
    let callback_text: String = callback_state.get_text_value().get();
    let last_event: String = callback_state.get_last_event().get();
    html! {
        div {
            class: c_page_container()
            { page_header("Component Binding", "Parent-child data passing, two-way binding, and cross-component reactive binding.") }
            my_card {
                title: "Props Down, Callback Up"
                p {
                    class: c_demo_text()
                    "Parent passes data to child via props. Child communicates back via callbacks."
                }
                div {
                    class: c_form_input_wrapper()
                    label {
                        class: c_form_label()
                        "Parent message:"
                    }
                    input {
                        r#type: "text"
                        value: props_state.get_parent_message()
                        class: c_form_input_no_transition()
                        oninput: on_input_value(props_state.get_parent_message())
                    }
                }
                child_display {
                    message: props_state.get_parent_message()
                    onclick: props_on_child_respond(
                        props_state.get_child_response(),
                        format!("Child says: I got \"{}\"!", props_state.get_parent_message().get()),
                    )
                }
                if { !props_state.get_child_response().get().is_empty() } {
                    div {
                        class: c_success_box()
                        props_state.get_child_response()
                    }
                } else {
                    ""
                }
            }
            my_card {
                title: "Typed Props (bool, i32)"
                p {
                    class: c_demo_text()
                    "Pass non-String props (bool, i32) through html! and extract with try_get_typed_prop."
                }
                div {
                    class: c_binding_parent_box()
                    p {
                        class: c_binding_child_label()
                        "Parent Controls"
                    }
                    div {
                        class: c_counter_row()
                        primary_button {
                            label: "Toggle Disabled"
                            onclick: typed_props_on_toggle_disabled(typed_state.get_disabled())
                            { if is_disabled { "Enable" } else { "Disable" } }
                        }
                        div {
                            class: c_binding_typed_prop_group()
                            label {
                                class: c_form_label()
                                "Max: "
                                span {
                                    class: c_binding_typed_prop_value()
                                    { max_count.to_string() }
                                }
                            }
                        }
                    }
                    p {
                        class: c_demo_text()
                        "Count: "
                        span {
                            class: c_counter_value()
                            { current_count.to_string() }
                        }
                        " / "
                        span {
                            class: c_binding_typed_prop_value()
                            { max_count.to_string() }
                        }
                    }
                }
                limited_counter {
                    disabled: is_disabled
                    max_count: max_count
                    on_increment: typed_props_on_increment(typed_state.get_current_count(), typed_state.get_max_count())
                    on_reset: typed_props_on_reset_count(typed_state.get_current_count())
                }
            }
            my_card {
                title: "Custom Callbacks"
                p {
                    class: c_demo_text()
                    "Pass custom callback functions as component props (on_change, on_submit, on_reset)."
                }
                div {
                    class: c_binding_parent_box()
                    p {
                        class: c_binding_child_label()
                        "Parent State"
                    }
                    p {
                        class: c_demo_text()
                        "Text: "
                        span {
                            class: c_event_highlight()
                            { if callback_text.is_empty() { "(empty)".to_string() } else { callback_text.clone() } }
                        }
                    }
                    p {
                        class: c_demo_text()
                        "Last event: "
                        span {
                            class: c_binding_typed_prop_value()
                            last_event
                        }
                    }
                }
                callback_input {
                    on_change: custom_on_change(callback_state.get_text_value(), "on_change".to_string())
                    on_submit: custom_on_submit(callback_state.get_last_event(), "on_submit".to_string())
                    on_reset: custom_on_reset(callback_state.get_text_value(), callback_state.get_last_event(), "on_reset".to_string())
                }
            }
            my_card {
                title: "Two-Way Binding (Shared Signal)"
                p {
                    class: c_demo_text()
                    "Parent and child share the same Signal. Changes in either component are reflected everywhere."
                }
                div {
                    class: c_binding_parent_box()
                    p {
                        class: c_binding_child_label()
                        "Parent Component"
                    }
                    p {
                        class: c_demo_text()
                        "Text: "
                        span {
                            class: c_event_highlight()
                            two_way_state.get_shared_text()
                        }
                    }
                    p {
                        class: c_demo_text()
                        "Count: "
                        span {
                            class: c_counter_value()
                            two_way_state.get_shared_count()
                        }
                    }
                    primary_button {
                        label: "Increment"
                        onclick: two_way_on_increment(two_way_state.get_shared_count())
                        "+"
                    }
                }
                { child_input(two_way_state.get_shared_text(), two_way_state.get_shared_count()) }
            }
            my_card {
                title: "Cross-Component Reactive Binding (watch!)"
                p {
                    class: c_demo_text()
                    "Signals are linked across components using watch! Changing one automatically updates the other."
                }
                h4 {
                    class: c_binding_section_title()
                    "Temperature Converter"
                }
                p {
                    class: c_demo_text_muted()
                    "Edit either field — the other updates reactively via watch!"
                }
                { temperature_converter(cross_state.get_celsius(), cross_state.get_fahrenheit()) }
                h4 {
                    class: c_binding_section_title()
                    "Color Mixer"
                }
                p {
                    class: c_demo_text_muted()
                    "Adjust RGB sliders — the hex color updates reactively via watch!"
                }
                { color_mixer(cross_state.get_red(), cross_state.get_green(), cross_state.get_blue(), cross_state.get_hex_color()) }
            }
        }
    }
}