euv-ui 0.37.5

Reusable UI component library for the euv framework, providing buttons, cards, modals, inputs, and more.
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
use super::*;

/// A modal dialog component with overlay, title, and close handler.
///
/// # Arguments
///
/// - `VirtualNode<EuvModalProps>` - The props node containing title and onclick.
///
/// # Returns
///
/// - `VirtualNode` - A modal overlay element.
#[component]
pub fn euv_modal(node: VirtualNode<EuvModalProps>) -> VirtualNode {
    let EuvModalProps { title, onclick }: EuvModalProps = node.try_get_props().unwrap_or_default();
    let children: VirtualNode = node.get_children().into();
    let on_modal_content_click: Box<dyn FnMut(Event)> = Box::new(move |_: Event| {});
    html! {
        div {
            class: c_modal_overlay()
            onclick: onclick.clone()
            div {
                role: "dialog"
                aria-modal: "true"
                class: c_modal_content()
                onclick: on_modal_content_click
                div {
                    class: c_modal_header()
                    h3 {
                        class: c_modal_title()
                        title
                    }
                    button {
                        type: "button"
                        "data-slot": "dialog-close"
                        "aria-label": DIALOG_CLOSE_ARIA_LABEL
                        class: c_modal_close_button()
                        onclick: onclick
                        "×"
                    }
                }
                div {
                    class: c_modal_body()
                    children
                }
            }
        }
    }
}

/// The panel of a dialog.
///
/// The panel is a dialog: it carries the role and the modal flag, so the
/// page behind it is announced as unreachable. The title and the
/// description are drawn by the panel itself, which is what the
/// reference does with its header.
///
/// # Arguments
///
/// - `VirtualNode<EuvDialogProps>` - The props node carrying the component settings.
///
/// # Returns
///
/// - `VirtualNode` - The euv dialog virtual DOM tree.
#[component]
pub fn euv_dialog(node: VirtualNode<EuvDialogProps>) -> VirtualNode {
    let EuvDialogProps {
        open,
        title,
        description,
    }: EuvDialogProps = node.try_get_props().unwrap_or_default();
    let children: VirtualNode = node.get_children().into();
    let dialog_ref: NodeRef<HtmlElement> = App::use_node_ref();
    let previous_focus: Rc<RefCell<Option<HtmlElement>>> =
        HookContext::use_hook(|| Rc::new(RefCell::new(None)));
    let _open_subscription: usize = HookContext::use_hook({
        let watched_open: Signal<bool> = open;
        let watched_dialog_ref: NodeRef<HtmlElement> = dialog_ref.clone();
        let watched_previous_focus: Rc<RefCell<Option<HtmlElement>>> = previous_focus.clone();
        move || {
            let callback_open: Signal<bool> = watched_open;
            let callback_dialog_ref: NodeRef<HtmlElement> = watched_dialog_ref;
            let callback_previous_focus: Rc<RefCell<Option<HtmlElement>>> = watched_previous_focus;
            let subscription_id: usize = callback_open.subscribe(move || {
                if callback_open.get() {
                    if let Some(active_element) = window()
                        .and_then(|window: Window| window.document())
                        .and_then(|document: Document| document.active_element())
                        .and_then(|element: Element| element.dyn_into::<HtmlElement>().ok())
                    {
                        *callback_previous_focus.borrow_mut() = Some(active_element);
                    }
                    let focus_ref: NodeRef<HtmlElement> = callback_dialog_ref.clone();
                    let focus_callback: Function = Closure::once_into_js(move || {
                        if let Some(dialog) = focus_ref.get_cloned() {
                            let _: Result<(), JsValue> = dialog.focus();
                        }
                    })
                    .unchecked_into();
                    if let Some(window) = window() {
                        let _: Result<i32, JsValue> =
                            window.request_animation_frame(&focus_callback);
                    }
                } else if let Some(previous_element) = callback_previous_focus.borrow_mut().take() {
                    let _: Result<(), JsValue> = previous_element.focus();
                }
            });
            let cleanup_open: Signal<bool> = callback_open;
            App::use_cleanup(move || cleanup_open.unsubscribe(subscription_id));
            subscription_id
        }
    });
    let close_handler: Rc<dyn Fn(Event)> = Rc::new(move |_: Event| {
        open.set(false);
    });
    let dialog_keydown_ref: NodeRef<HtmlElement> = dialog_ref.clone();
    let escape_handler: Option<Rc<dyn Fn(Event)>> = Some(Rc::new(move |event: Event| {
        let Ok(keyboard_event) = event.dyn_into::<KeyboardEvent>() else {
            return;
        };
        match keyboard_event.key().as_str() {
            "Escape" => {
                keyboard_event.prevent_default();
                open.set(false);
            }
            "Tab" => {
                let Some(dialog) = dialog_keydown_ref.get_cloned() else {
                    return;
                };
                let Ok(focusable_nodes) = dialog.query_selector_all(DIALOG_FOCUSABLE_SELECTOR)
                else {
                    return;
                };
                let Some(first) = focusable_nodes
                    .item(0)
                    .and_then(|node: Node| node.dyn_into::<HtmlElement>().ok())
                else {
                    keyboard_event.prevent_default();
                    let _: Result<(), JsValue> = dialog.focus();
                    return;
                };
                let Some(last) = focusable_nodes
                    .item(focusable_nodes.length() - 1)
                    .and_then(|node: Node| node.dyn_into::<HtmlElement>().ok())
                else {
                    return;
                };
                let Some(active) = window()
                    .and_then(|window: Window| window.document())
                    .and_then(|document: Document| document.active_element())
                    .and_then(|element: Element| element.dyn_into::<HtmlElement>().ok())
                else {
                    return;
                };
                let active_node: Node = active.into();
                let dialog_node: Node = dialog.clone().into();
                let first_node: Node = first.clone().into();
                let last_node: Node = last.clone().into();
                let active_is_dialog: bool = active_node.is_same_node(Some(&dialog_node));
                let active_is_first: bool = active_node.is_same_node(Some(&first_node));
                let active_is_last: bool = active_node.is_same_node(Some(&last_node));
                if keyboard_event.shift_key() && (active_is_first || active_is_dialog) {
                    keyboard_event.prevent_default();
                    let _: Result<(), JsValue> = last.focus();
                } else if !keyboard_event.shift_key() && (active_is_last || active_is_dialog) {
                    keyboard_event.prevent_default();
                    let _: Result<(), JsValue> = first.focus();
                }
            }
            _ => {}
        }
    }));
    html! {
        div {
            "data-slot": "dialog-overlay"
            class: c_euv_dialog_overlay()
            onclick: Some(Rc::clone(&close_handler))
            class: if { open.get() } {
                c_euv_dialog_overlay_open()
            } else {
                c_euv_dialog_overlay_closed()
            }
        }
        div {
            role: "dialog"
            "data-slot": "dialog"
            "aria-modal": "true"
            "aria-label": title
            tabindex: "-1"
            ref: dialog_ref
            onkeydown: escape_handler
            class: c_euv_dialog()
            class: if { open.get() } {
                c_euv_dialog_open()
            } else {
                c_euv_dialog_closed()
            }
            euv_dialog_header {
                label: title
                euv_dialog_title {
                    text: title
                }
                if !description.is_empty() {
                    euv_dialog_description {
                        text: description
                    }
                }
            }
            children
            euv_dialog_close {
                label: DIALOG_CLOSE_ARIA_LABEL
                onclick: Some(Rc::clone(&close_handler))
            }
        }
    }
}

/// The head of the dialog: its title and its description.
///
/// # Arguments
///
/// - `VirtualNode<EuvDialogHeaderProps>` - The props node carrying the component settings.
///
/// # Returns
///
/// - `VirtualNode` - The euv dialog header virtual DOM tree.
#[component]
pub fn euv_dialog_header(node: VirtualNode<EuvDialogHeaderProps>) -> VirtualNode {
    let EuvDialogHeaderProps { label }: EuvDialogHeaderProps =
        node.try_get_props().unwrap_or_default();
    let children: VirtualNode = node.get_children().into();
    html! {
        div {
            "aria-label": label
            "data-slot": "dialog-header"
            class: c_euv_dialog_header()
            children
        }
    }
}

/// The title of the dialog.
///
/// # Arguments
///
/// - `VirtualNode<EuvDialogTitleProps>` - The props node carrying the component settings.
///
/// # Returns
///
/// - `VirtualNode` - The euv dialog title virtual DOM tree.
#[component]
pub fn euv_dialog_title(node: VirtualNode<EuvDialogTitleProps>) -> VirtualNode {
    let EuvDialogTitleProps { text }: EuvDialogTitleProps =
        node.try_get_props().unwrap_or_default();
    html! {
        h2 {
            "data-slot": "dialog-title"
            class: c_euv_dialog_title()
            {
                text
            }
        }
    }
}

/// The description of the dialog.
///
/// # Arguments
///
/// - `VirtualNode<EuvDialogDescriptionProps>` - The props node carrying the component settings.
///
/// # Returns
///
/// - `VirtualNode` - The euv dialog description virtual DOM tree.
#[component]
pub fn euv_dialog_description(node: VirtualNode<EuvDialogDescriptionProps>) -> VirtualNode {
    let EuvDialogDescriptionProps { text }: EuvDialogDescriptionProps =
        node.try_get_props().unwrap_or_default();
    html! {
        p {
            "data-slot": "dialog-description"
            class: c_euv_dialog_description()
            {
                text
            }
        }
    }
}

/// The foot of the dialog: the actions that end the interaction.
///
/// # Arguments
///
/// - `VirtualNode<EuvDialogFooterProps>` - The props node carrying the component settings.
///
/// # Returns
///
/// - `VirtualNode` - The euv dialog footer virtual DOM tree.
#[component]
pub fn euv_dialog_footer(node: VirtualNode<EuvDialogFooterProps>) -> VirtualNode {
    let EuvDialogFooterProps { label }: EuvDialogFooterProps =
        node.try_get_props().unwrap_or_default();
    let children: VirtualNode = node.get_children().into();
    html! {
        div {
            "aria-label": label
            "data-slot": "dialog-footer"
            class: c_euv_dialog_footer()
            children
        }
    }
}

/// The button that opens the dialog.
///
/// The trigger carries the open state the panel reads, so the two always
/// agree on whether the dialog is showing.
///
/// # Arguments
///
/// - `VirtualNode<EuvDialogTriggerProps>` - The props node carrying the
///   open state.
///
/// # Returns
///
/// - `VirtualNode` - The euv dialog trigger virtual DOM tree.
#[component]
pub fn euv_dialog_trigger(node: VirtualNode<EuvDialogTriggerProps>) -> VirtualNode {
    let EuvDialogTriggerProps { open }: EuvDialogTriggerProps =
        node.try_get_props().unwrap_or_default();
    let children: VirtualNode = node.get_children().into();
    html! {
        button {
            type: "button"
            "data-slot": "dialog-trigger"
            "aria-haspopup": "dialog"
            "aria-expanded": if { open.get() } {
                "true"
            } else {
                "false"
            }
            class: c_euv_alert_dialog_trigger()
            onclick: dialog_open(open)
            children
        }
    }
}

/// Builds the handler that opens the dialog.
///
/// # Arguments
///
/// - `Signal<bool>` - The open state of the dialog.
///
/// # Returns
///
/// - `Option<Rc<dyn Fn(Event)>>` - The open click handler.
fn dialog_open(open: Signal<bool>) -> Option<Rc<dyn Fn(Event)>> {
    Some(Rc::new(move |_: Event| {
        open.set(true);
    }))
}

/// The button that closes the dialog.
///
/// # Arguments
///
/// - `VirtualNode<EuvDialogCloseProps>` - The props node carrying the component settings.
///
/// # Returns
///
/// - `VirtualNode` - The euv dialog close virtual DOM tree.
#[component]
pub fn euv_dialog_close(node: VirtualNode<EuvDialogCloseProps>) -> VirtualNode {
    let EuvDialogCloseProps { label, onclick }: EuvDialogCloseProps =
        node.try_get_props().unwrap_or_default();
    html! {
        button {
            type: "button"
            "data-slot": "dialog-close"
            "aria-label": label
            class: c_euv_dialog_close()
            onclick: onclick
        }
    }
}

/// Dims the page behind a dialog.
///
/// The overlay covers the page and takes the click, so a click outside the
/// panel of the dialog does not reach the page behind it.
///
/// # Arguments
///
/// - `VirtualNode<EuvDialogOverlayProps>` - The props node carrying whether
///   the dialog is open.
///
/// # Returns
///
/// - `VirtualNode` - The overlay element.
#[component]
pub fn euv_dialog_overlay(node: VirtualNode<EuvDialogOverlayProps>) -> VirtualNode {
    let EuvDialogOverlayProps { open }: EuvDialogOverlayProps =
        node.try_get_props().unwrap_or_default();
    html! {
        div {
            "data-slot": "dialog-overlay"
            "data-state": if { open.get() } {
                "open"
            } else {
                "closed"
            }
            class: c_euv_dialog_overlay()
            class: if { open.get() } {
                c_euv_dialog_overlay_open()
            } else {
                c_euv_dialog_overlay_closed()
            }
        }
    }
}

/// Holds the panel of a dialog.
///
/// The panel is what the reader reads while the dialog is open. It tells
/// the screen reader that it interrupts the reader with something they have
/// to answer.
///
/// # Arguments
///
/// - `VirtualNode<EuvDialogContentProps>` - The props node carrying whether
///   the dialog is open.
///
/// # Returns
///
/// - `VirtualNode` - The panel element.
#[component]
pub fn euv_dialog_content(node: VirtualNode<EuvDialogContentProps>) -> VirtualNode {
    let EuvDialogContentProps { open }: EuvDialogContentProps =
        node.try_get_props().unwrap_or_default();
    let children: VirtualNode = node.get_children().into();
    html! {
        div {
            "data-slot": "dialog-content"
            "data-state": if { open.get() } {
                "open"
            } else {
                "closed"
            }
            role: "dialog"
            "aria-modal": "true"
            class: c_euv_dialog_content()
            class: if { open.get() } {
                c_euv_dialog_content_open()
            } else {
                c_euv_dialog_content_closed()
            }
            children
        }
    }
}