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
use crate::*;

/// Renders a navigation item link with active state styling.
///
/// # Arguments
///
/// - `Signal<String>` - The reactive signal holding the current route.
/// - `&str` - The display label for the navigation item.
/// - `&str` - The target route path.
///
/// # Returns
///
/// - `VirtualNode` - The navigation item virtual DOM tree.
pub fn nav_item(route_signal: Signal<String>, label: &str, target: &str) -> VirtualNode {
    let target_string: String = target.to_string();
    let current_route_value: String = route_signal.get();
    let is_active: bool = current_route_value == target;
    html! {
        a {
            href: format!("#{}", target_string)
            class: if { is_active } { c_nav_item_active() } else { c_nav_item_inactive() }
            onclick: link_handler(target_string)
            label
        }
    }
}

/// Renders a mobile navigation item link that closes the drawer on navigation.
///
/// # Arguments
///
/// - `Signal<String>` - The reactive signal holding the current route.
/// - `Signal<bool>` - The reactive signal controlling the mobile nav drawer visibility.
/// - `&str` - The display label for the navigation item.
/// - `&str` - The target route path.
///
/// # Returns
///
/// - `VirtualNode` - The mobile navigation item virtual DOM tree.
fn mobile_nav_item(
    route_signal: Signal<String>,
    drawer_open: Signal<bool>,
    label: &str,
    target: &str,
) -> VirtualNode {
    let target_string: String = target.to_string();
    let current_route_value: String = route_signal.get();
    let is_active: bool = current_route_value == target;
    let nav_target: String = target_string.clone();
    html! {
        a {
            href: format!("#{}", target_string)
            class: if { is_active } { c_nav_item_active() } else { c_nav_item_inactive() }
            onclick: move |_event: NativeEvent| {
                navigate(&nav_target);
                drawer_open.set(false);
            }
            label
        }
    }
}

/// Builds the navigation items list for the desktop sidebar.
///
/// # Arguments
///
/// - `Signal<String>` - The reactive signal holding the current route.
///
/// # Returns
///
/// - `VirtualNode` - The scrollable nav items container virtual DOM tree.
fn build_desktop_nav_items(route_signal: Signal<String>) -> VirtualNode {
    html! {
        div {
            class: c_nav_items_scroll()
            { nav_item(route_signal, "Signals", "/signals") }
            { nav_item(route_signal, "Event", "/event") }
            { nav_item(route_signal, "List", "/list") }
            { nav_item(route_signal, "Conditional", "/conditional") }
            { nav_item(route_signal, "Modal", "/modal") }
            { nav_item(route_signal, "Select", "/select") }
            { nav_item(route_signal, "Async", "/async") }
            { nav_item(route_signal, "Form", "/form") }
            { nav_item(route_signal, "File Upload", "/file-upload") }
            { nav_item(route_signal, "Timer", "/timer") }
            { nav_item(route_signal, "Animation", "/animation") }
            { nav_item(route_signal, "Browser", "/browser") }
            { nav_item(route_signal, "Lifecycle", "/lifecycle") }
            { nav_item(route_signal, "Keep-Alive", "/keep-alive") }
            { nav_item(route_signal, "Component Binding", "/component-binding") }
            { nav_item(route_signal, "Custom Attrs", "/custom-attrs") }
        }
    }
}

/// Builds the navigation items list for the mobile drawer.
///
/// Navigation items close the drawer on click.
///
/// # Arguments
///
/// - `Signal<String>` - The reactive signal holding the current route.
/// - `Signal<bool>` - The reactive signal controlling the mobile nav drawer visibility.
///
/// # Returns
///
/// - `VirtualNode` - The scrollable nav items container virtual DOM tree.
fn build_mobile_nav_items(route_signal: Signal<String>, drawer_open: Signal<bool>) -> VirtualNode {
    html! {
        div {
            class: c_nav_items_scroll()
            { mobile_nav_item(route_signal, drawer_open, "Signals", "/signals") }
            { mobile_nav_item(route_signal, drawer_open, "Event", "/event") }
            { mobile_nav_item(route_signal, drawer_open, "List", "/list") }
            { mobile_nav_item(route_signal, drawer_open, "Conditional", "/conditional") }
            { mobile_nav_item(route_signal, drawer_open, "Modal", "/modal") }
            { mobile_nav_item(route_signal, drawer_open, "Select", "/select") }
            { mobile_nav_item(route_signal, drawer_open, "Async", "/async") }
            { mobile_nav_item(route_signal, drawer_open, "Form", "/form") }
            { mobile_nav_item(route_signal, drawer_open, "File Upload", "/file-upload") }
            { mobile_nav_item(route_signal, drawer_open, "Timer", "/timer") }
            { mobile_nav_item(route_signal, drawer_open, "Animation", "/animation") }
            { mobile_nav_item(route_signal, drawer_open, "Browser", "/browser") }
            { mobile_nav_item(route_signal, drawer_open, "Lifecycle", "/lifecycle") }
            { mobile_nav_item(route_signal, drawer_open, "Keep-Alive", "/keep-alive") }
            { mobile_nav_item(route_signal, drawer_open, "Component Binding", "/component-binding") }
            { mobile_nav_item(route_signal, drawer_open, "Custom Attrs", "/custom-attrs") }
        }
    }
}

/// Renders the desktop layout with a persistent left sidebar navigation.
///
/// # Arguments
///
/// - `Signal<String>` - The reactive signal holding the current route.
/// - `Signal<String>` - The reactive signal holding the current theme.
/// - `Signal<String>` - The reactive signal holding the root class name.
/// - `Signal<bool>` - The reactive signal controlling vconsole panel visibility.
///
/// # Returns
///
/// - `VirtualNode` - The desktop application shell virtual DOM tree.
fn desktop_layout(
    route_signal: Signal<String>,
    theme_signal: Signal<String>,
    root_class_signal: Signal<String>,
    panel_open: Signal<bool>,
) -> VirtualNode {
    html! {
        div {
            class: root_class_signal
            nav {
                class: c_app_nav()
                a {
                    href: "https://github.com/euv-dev/euv"
                    target: "_blank"
                    class: c_nav_header()
                    span {
                        class: c_nav_logo()
                        "E"
                    }
                    span {
                        class: c_inline()
                        "Euv"
                    }
                }
                p {
                    class: c_nav_section_label()
                    "Pages"
                }
                { build_desktop_nav_items(route_signal) }
                div {
                    class: c_nav_theme_toggle()
                    button {
                        class: c_nav_theme_button()
                        onclick: toggle_theme(theme_signal)
                        if { theme_signal.get() == "dark" } {
                            ""
                        } else {
                            "🌙"
                        }
                    }
                }
                a {
                    href: "https://github.com/euv-dev/euv"
                    target: "_blank"
                    class: c_nav_footer()
                    "Built with Euv & WASM"
                }
            }
            main {
                class: c_app_main()
                match { route_signal.get().as_str() } {
                    "/" | "/signals" => { { page_signals() } }
                    "/event" => { { page_event() } }
                    "/list" => { { page_list() } }
                    "/conditional" => { { page_conditional() } }
                    "/modal" => { { page_modal() } }
                    "/select" => { { page_select() } }
                    "/async" => { { page_async_demo() } }
                    "/form" => { { page_form() } }
                    "/file-upload" => { { page_file_upload() } }
                    "/timer" => { { page_timer() } }
                    "/animation" => { { page_animation() } }
                    "/browser" => { { page_browser() } }
                    "/lifecycle" => { { page_lifecycle() } }
                    "/keep-alive" => { { page_keep_alive() } }
                    "/component-binding" => { { page_component_binding() } }
                    "/custom-attrs" => { { page_custom_attrs() } }
                    _ => { { page_not_found() } }
                }
            }
            { vconsole_panel(panel_open) }
        }
    }
}

/// Renders the mobile layout with a top header bar and a slide-out navigation drawer.
///
/// # Arguments
///
/// - `Signal<String>` - The reactive signal holding the current route.
/// - `Signal<String>` - The reactive signal holding the current theme.
/// - `Signal<String>` - The reactive signal holding the root class name.
/// - `Signal<bool>` - The reactive signal controlling vconsole panel visibility.
/// - `Signal<bool>` - The reactive signal controlling the mobile nav drawer visibility.
///
/// # Returns
///
/// - `VirtualNode` - The mobile application shell virtual DOM tree.
fn mobile_layout(
    route_signal: Signal<String>,
    theme_signal: Signal<String>,
    root_class_signal: Signal<String>,
    panel_open: Signal<bool>,
    drawer_open: Signal<bool>,
) -> VirtualNode {
    html! {
        div {
            class: root_class_signal
            header {
                class: c_mobile_header()
                div {
                    class: c_mobile_header_left()
                    button {
                        class: if { drawer_open.get() } { c_mobile_menu_button_active() } else { c_mobile_menu_button() }
                        onclick: use_toggle(drawer_open)
                        ""
                    }
                    span {
                        class: c_nav_logo()
                        "E"
                    }
                    span {
                        class: c_inline()
                        "euv"
                    }
                }
                button {
                    class: c_mobile_menu_button()
                    onclick: toggle_theme(theme_signal)
                    if { theme_signal.get() == "dark" } {
                        ""
                    } else {
                        "🌙"
                    }
                }
            }
            main {
                class: c_mobile_main()
                match { route_signal.get().as_str() } {
                    "/" | "/signals" => { { page_signals() } }
                    "/event" => { { page_event() } }
                    "/list" => { { page_list() } }
                    "/conditional" => { { page_conditional() } }
                    "/modal" => { { page_modal() } }
                    "/select" => { { page_select() } }
                    "/async" => { { page_async_demo() } }
                    "/form" => { { page_form() } }
                    "/file-upload" => { { page_file_upload() } }
                    "/timer" => { { page_timer() } }
                    "/animation" => { { page_animation() } }
                    "/browser" => { { page_browser() } }
                    "/lifecycle" => { { page_lifecycle() } }
                    "/keep-alive" => { { page_keep_alive() } }
                    "/component-binding" => { { page_component_binding() } }
                    "/custom-attrs" => { { page_custom_attrs() } }
                    _ => { { page_not_found() } }
                }
            }
            { vconsole_panel(panel_open) }
            if { drawer_open.get() } {
                div {
                    class: c_mobile_overlay()
                    onclick: move |_event: NativeEvent| {
                        drawer_open.set(false);
                    }
                }
                nav {
                    class: c_mobile_nav_drawer()
                    div {
                        class: c_mobile_nav_drawer_header()
                        a {
                            href: "https://github.com/euv-dev/euv"
                            target: "_blank"
                            class: c_nav_header()
                            span {
                                class: c_nav_logo()
                                "E"
                            }
                            span {
                                class: c_inline()
                                "Euv"
                            }
                        }
                        button {
                            class: c_mobile_nav_drawer_close()
                            onclick: move |_event: NativeEvent| {
                                drawer_open.set(false);
                            }
                            ""
                        }
                    }
                    p {
                        class: c_nav_section_label()
                        "Pages"
                    }
                    { build_mobile_nav_items(route_signal, drawer_open) }
                    div {
                        class: c_nav_theme_toggle()
                        button {
                            class: c_nav_theme_button()
                            onclick: toggle_theme(theme_signal)
                            if { theme_signal.get() == "dark" } {
                                ""
                            } else {
                                "🌙"
                            }
                        }
                    }
                    a {
                        href: "https://github.com/euv-dev/euv"
                        target: "_blank"
                        class: c_nav_footer()
                        "Built with Euv & WASM"
                    }
                }
            } else {
                ""
            }
        }
    }
}

/// Renders the application shell with navigation and route-based page content.
///
/// Detects viewport size and switches between desktop sidebar layout and
/// mobile header + drawer layout accordingly.
///
/// # Returns
///
/// - `VirtualNode` - The root application virtual DOM tree.
pub fn app() -> VirtualNode {
    init_console();
    let route_signal: Signal<String> = use_signal(current_route);
    let panel_open: Signal<bool> = use_signal(|| false);
    let drawer_open: Signal<bool> = use_signal(|| false);
    let mobile_signal: Signal<bool> = use_resize();
    let theme_state: ThemeState = use_theme();
    let theme_signal: Signal<String> = theme_state.get_theme();
    let root_class_signal: Signal<String> = theme_state.get_root_class();
    use_hash_change(route_signal);
    let is_mobile: bool = mobile_signal.get();
    let current_root_class: String = if is_mobile {
        format!(
            "{} {}",
            c_mobile_app_root().get_name(),
            theme_class_name(&theme_signal.get())
        )
    } else {
        root_class_signal.get()
    };
    let root_class: Signal<String> = use_signal(move || current_root_class);
    watch!(mobile_signal, theme_signal, |mobile, theme| {
        if mobile {
            root_class.set(format!(
                "{} {}",
                c_mobile_app_root().get_name(),
                theme_class_name(&theme)
            ));
        } else {
            root_class.set(format!(
                "{} {}",
                c_app_root().get_name(),
                theme_class_name(&theme)
            ));
        }
    });
    if is_mobile {
        mobile_layout(
            route_signal,
            theme_signal,
            root_class,
            panel_open,
            drawer_open,
        )
    } else {
        desktop_layout(route_signal, theme_signal, root_class, panel_open)
    }
}