euv-ui 0.35.93

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

/// A slideshow.
///
/// Renders a `c_euv_carousel` box holding a `c_euv_carousel_track` whose
/// slides are laid out side by side and shifted with a transform, plus a
/// row of `c_euv_carousel_dot` markers and previous/next controls.
///
/// The track is shifted with an inline transform rather than by
/// swapping slides: every slide stays mounted, so a slide that is
/// halfway out of view is still in the DOM and the transition is a
/// single animated property instead of a tree rebuild.
///
/// # Arguments
///
/// - `VirtualNode<EuvCarouselProps>` - The props node containing the
///   carousel configuration.
///
/// # Returns
///
/// - `VirtualNode` - The carousel virtual DOM tree.
#[component]
pub fn euv_carousel(node: VirtualNode<EuvCarouselProps>) -> VirtualNode {
    let EuvCarouselProps {
        slides,
        active,
        on_change,
    }: EuvCarouselProps = node.try_get_props().unwrap_or_default();
    let slide_count: u32 = slides.len() as u32;
    let index: u32 = if slide_count == 0 {
        0
    } else {
        active.get().min(slide_count - 1)
    };
    let track_style: String = format!("transform: translateX(-{}%);", index as i64 * 100);
    let track_ref: &str = track_style.as_str();
    let pane_nodes: Vec<VirtualNode> = slides
        .into_iter()
        .enumerate()
        .map(|(slide_index, slide): (usize, EuvCarouselSlide)| {
            let is_active: bool = slide_index as u32 == index;
            html! {
                div {
                    role: "group"
                    "aria-roledescription": CAROUSEL_SLIDE_ROLE_DESCRIPTION
                    "aria-label": format!("{CAROUSEL_SLIDE_LABEL} {} {CAROUSEL_OF_LABEL} {}", slide_index + 1, slide_count)
                    "aria-hidden": if is_active {
                        CAROUSEL_ATTR_FALSE
                    } else {
                        CAROUSEL_ATTR_TRUE
                    }
                    "data-slot": "carousel-item"
                    class: c_euv_carousel_slide()
                    key: slide_index.to_string()
                    div {
                        class: c_euv_text_title()
                        slide.title
                    }
                    if !slide.desc.is_empty() {
                        div {
                            class: c_euv_text_secondary()
                            slide.desc
                        }
                    }
                }
            }
        })
        .collect();
    let dot_count: u32 = pane_nodes.len() as u32;
    let dots: Vec<VirtualNode> = (0..dot_count)
        .map(|dot: u32| {
            let is_active: bool = dot == index;
            html! {
                button {
                    type: "button"
                    "data-slot": "carousel-dot"
                    "aria-label": format!("{CAROUSEL_DOT_LABEL_PREFIX} {} {CAROUSEL_OF_LABEL} {dot_count}", dot + 1)
                    "aria-pressed": if is_active {
                        CAROUSEL_ATTR_TRUE
                    } else {
                        CAROUSEL_ATTR_FALSE
                    }
                    class: if is_active {
                        c_euv_carousel_dot_active()
                    } else {
                        c_euv_carousel_dot()
                    }
                    key: dot.to_string()
                    onclick: on_carousel_dot(active, on_change.clone(), dot)
                }
            }
        })
        .collect();
    let previous_disabled: bool = index == 0;
    let next_disabled: bool = index + 1 >= dot_count;
    let previous_disabled_signal: Signal<bool> = Signal::create(previous_disabled);
    let next_disabled_signal: Signal<bool> = Signal::create(next_disabled);
    let previous_on_press: Rc<dyn Fn()> =
        on_carousel_step(active, on_change.clone(), -1, dot_count);
    let next_on_press: Rc<dyn Fn()> = on_carousel_step(active, on_change.clone(), 1, dot_count);
    let previous_on_click: Option<Rc<dyn Fn(Event)>> = Some(Rc::new(move |_: Event| {
        previous_on_press();
    }));
    let next_on_click: Option<Rc<dyn Fn(Event)>> = Some(Rc::new(move |_: Event| {
        next_on_press();
    }));
    let touch_start_x: Rc<Cell<Option<i32>>> = Rc::new(Cell::new(None));
    let on_touch_start: Option<Rc<dyn Fn(Event)>> = carousel_touch_start(touch_start_x.clone());
    let on_touch_end: Option<Rc<dyn Fn(Event)>> =
        carousel_touch_end(touch_start_x.clone(), active, on_change.clone(), dot_count);
    let on_touch_cancel: Option<Rc<dyn Fn(Event)>> = carousel_touch_cancel(touch_start_x);
    html! {
        div {
            role: "region"
            "aria-roledescription": CAROUSEL_ROLE_DESCRIPTION
            "aria-label": CAROUSEL_ARIA_LABEL
            "data-slot": "carousel"
            class: c_euv_carousel()
            onkeydown: on_carousel_keydown(active, on_change.clone(), dot_count)
            ontouchstart: on_touch_start
            ontouchend: on_touch_end
            ontouchcancel: on_touch_cancel
            div {
                "data-slot": "carousel-track"
                "aria-live": CAROUSEL_LIVE_POLITE
                "aria-atomic": CAROUSEL_ATTR_TRUE
                class: c_euv_carousel_track()
                style: track_ref
                pane_nodes
            }
            if dot_count > 1 {
                div {
                    role: "group"
                    "aria-label": CAROUSEL_DOT_GROUP_LABEL
                    "data-slot": "carousel-dots"
                    class: c_euv_carousel_dots()
                    dots
                }
                button {
                    key: CAROUSEL_PREVIOUS_LABEL.to_string()
                    type: "button"
                    "data-slot": "carousel-previous"
                    "aria-label": CAROUSEL_PREVIOUS_LABEL
                    disabled: previous_disabled_signal
                    class: c_euv_carousel_previous()
                    onclick: previous_on_click
                    span {
                        "aria-hidden": CAROUSEL_ATTR_TRUE
                        CAROUSEL_PREVIOUS_GLYPH
                    }
                }
                button {
                    key: CAROUSEL_NEXT_LABEL.to_string()
                    type: "button"
                    "data-slot": "carousel-next"
                    "aria-label": CAROUSEL_NEXT_LABEL
                    disabled: next_disabled_signal
                    class: c_euv_carousel_next()
                    onclick: next_on_click
                    span {
                        "aria-hidden": CAROUSEL_ATTR_TRUE
                        CAROUSEL_NEXT_GLYPH
                    }
                }
            }
        }
    }
}

/// The viewport the slides travel inside.
///
/// The viewport clips the track, so a slide that sits beside the visible
/// one is hidden rather than spilling out of the carousel.
///
/// # Arguments
///
/// - `VirtualNode<EuvCarouselContentProps>` - The props node containing the
///   carousel configuration.
///
/// # Returns
///
/// - `VirtualNode` - The carousel virtual DOM tree.
#[component]
pub fn euv_carousel_content(node: VirtualNode<EuvCarouselContentProps>) -> VirtualNode {
    let EuvCarouselContentProps { orientation }: EuvCarouselContentProps =
        node.try_get_props().unwrap_or_default();
    let children: VirtualNode = node.get_children().into();
    html! {
        div {
            "data-slot": "carousel-content"
            class: c_euv_carousel_content()
            div {
                class: if orientation == EuvCarouselOrientation::Vertical {
                    c_euv_carousel_content_track_vertical()
                } else {
                    c_euv_carousel_content_track()
                }
                children
            }
        }
    }
}

/// One slide of the carousel.
///
/// # Arguments
///
/// - `VirtualNode<EuvCarouselItemProps>` - The props node containing the
///   carousel configuration.
///
/// # Returns
///
/// - `VirtualNode` - The carousel virtual DOM tree.
#[component]
pub fn euv_carousel_item(node: VirtualNode<EuvCarouselItemProps>) -> VirtualNode {
    let EuvCarouselItemProps { orientation }: EuvCarouselItemProps =
        node.try_get_props().unwrap_or_default();
    let children: VirtualNode = node.get_children().into();
    html! {
        div {
            role: "group"
            "aria-roledescription": "slide"
            "data-slot": "carousel-item"
            class: if orientation == EuvCarouselOrientation::Vertical {
                c_euv_carousel_item_vertical()
            } else {
                c_euv_carousel_item()
            }
            children
        }
    }
}

/// The button that steps the carousel forward.
///
/// # Arguments
///
/// - `VirtualNode<EuvCarouselNextProps>` - The props node containing the
///   carousel configuration.
///
/// # Returns
///
/// - `VirtualNode` - The carousel virtual DOM tree.
#[component]
pub fn euv_carousel_next(node: VirtualNode<EuvCarouselNextProps>) -> VirtualNode {
    let EuvCarouselNextProps {
        orientation,
        label,
        disabled,
        on_press,
    }: EuvCarouselNextProps = node.try_get_props().unwrap_or_default();
    let accessible_label: &str = if label.is_empty() {
        CAROUSEL_NEXT_LABEL
    } else {
        label
    };
    let is_disabled: bool = disabled || on_press.is_none();
    let disabled_signal: Signal<bool> = Signal::create(is_disabled);
    let on_click: Option<Rc<dyn Fn(Event)>> = if is_disabled {
        None
    } else {
        Some(Rc::new(move |_: Event| {
            if let Some(callback) = &on_press {
                callback();
            }
        }))
    };
    html! {
        button {
            type: "button"
            "data-slot": "carousel-next"
            "aria-label": accessible_label
            disabled: disabled_signal
            class: if orientation == EuvCarouselOrientation::Vertical {
                c_euv_carousel_next_vertical()
            } else {
                c_euv_carousel_next()
            }
            onclick: on_click
            span {
                "aria-hidden": CAROUSEL_ATTR_TRUE
                CAROUSEL_NEXT_GLYPH
            }
        }
    }
}

/// The button that steps the carousel back.
///
/// # Arguments
///
/// - `VirtualNode<EuvCarouselPreviousProps>` - The props node containing the
///   carousel configuration.
///
/// # Returns
///
/// - `VirtualNode` - The carousel virtual DOM tree.
#[component]
pub fn euv_carousel_previous(node: VirtualNode<EuvCarouselPreviousProps>) -> VirtualNode {
    let EuvCarouselPreviousProps {
        orientation,
        label,
        disabled,
        on_press,
    }: EuvCarouselPreviousProps = node.try_get_props().unwrap_or_default();
    let accessible_label: &str = if label.is_empty() {
        CAROUSEL_PREVIOUS_LABEL
    } else {
        label
    };
    let is_disabled: bool = disabled || on_press.is_none();
    let disabled_signal: Signal<bool> = Signal::create(is_disabled);
    let on_click: Option<Rc<dyn Fn(Event)>> = if is_disabled {
        None
    } else {
        Some(Rc::new(move |_: Event| {
            if let Some(callback) = &on_press {
                callback();
            }
        }))
    };
    html! {
        button {
            type: "button"
            "data-slot": "carousel-previous"
            "aria-label": accessible_label
            disabled: disabled_signal
            class: if orientation == EuvCarouselOrientation::Vertical {
                c_euv_carousel_previous_vertical()
            } else {
                c_euv_carousel_previous()
            }
            onclick: on_click
            span {
                "aria-hidden": CAROUSEL_ATTR_TRUE
                CAROUSEL_PREVIOUS_GLYPH
            }
        }
    }
}

/// Builds a handler that moves one slide in the given direction.
///
/// # Arguments
///
/// - `Signal<u32>` - The active slide signal.
/// - `Option<Rc<dyn Fn(u32)>>` - The callback invoked after the active slide changes.
/// - `i8` - The direction to move (`-1` for previous, `1` for next).
/// - `u32` - The number of slides.
///
/// # Returns
///
/// - `Rc<dyn Fn()>` - The slide-change handler.
fn on_carousel_step(
    active: Signal<u32>,
    on_change: Option<Rc<dyn Fn(u32)>>,
    direction: i8,
    slide_count: u32,
) -> Rc<dyn Fn()> {
    Rc::new(move || {
        if slide_count == 0 {
            return;
        }
        let current_index: u32 = active.get().min(slide_count - 1);
        let target_index: Option<u32> = match direction {
            -1 if current_index > 0 => Some(current_index - 1),
            1 if current_index + 1 < slide_count => Some(current_index + 1),
            _ => None,
        };
        if let Some(target_index) = target_index {
            active.set(target_index);
            if let Some(callback) = &on_change {
                callback(target_index);
            }
        }
    })
}

/// Builds the keyboard handler for the carousel region.
///
/// Arrow keys move between adjacent slides; Home and End select the bounds.
///
/// # Arguments
///
/// - `Signal<u32>` - The active slide signal.
/// - `Option<Rc<dyn Fn(u32)>>` - The callback invoked after the active slide changes.
/// - `u32` - The number of slides.
///
/// # Returns
///
/// - `Option<Rc<dyn Fn(Event)>>` - The keyboard handler, absent for an empty carousel.
fn on_carousel_keydown(
    active: Signal<u32>,
    on_change: Option<Rc<dyn Fn(u32)>>,
    slide_count: u32,
) -> Option<Rc<dyn Fn(Event)>> {
    if slide_count == 0 {
        return None;
    }
    Some(Rc::new(move |event: Event| {
        let Ok(keyboard_event) = event.dyn_into::<KeyboardEvent>() else {
            return;
        };
        let current_index: u32 = active.get().min(slide_count - 1);
        let target_index: u32 = match keyboard_event.key().as_str() {
            CAROUSEL_KEY_ARROW_LEFT if current_index > 0 => current_index - 1,
            CAROUSEL_KEY_ARROW_RIGHT if current_index + 1 < slide_count => current_index + 1,
            CAROUSEL_KEY_HOME => 0,
            CAROUSEL_KEY_END => slide_count - 1,
            _ => return,
        };
        if target_index == current_index {
            return;
        }
        keyboard_event.prevent_default();
        active.set(target_index);
        if let Some(callback) = &on_change {
            callback(target_index);
        }
    }))
}

/// Captures the starting position of a single-touch carousel gesture.
///
/// Touches that begin on a button are left to the button's native activation.
///
/// # Arguments
///
/// - `Rc<Cell<Option<i32>>>` - Storage shared with the touch-end handler.
///
/// # Returns
///
/// - `Option<Rc<dyn Fn(Event)>>` - The touch-start handler.
fn carousel_touch_start(start_x: Rc<Cell<Option<i32>>>) -> Option<Rc<dyn Fn(Event)>> {
    Some(Rc::new(move |event: Event| {
        start_x.set(None);
        let Some(target) = event
            .target()
            .and_then(|target: EventTarget| target.dyn_into::<Element>().ok())
        else {
            return;
        };
        if target
            .closest(CAROUSEL_BUTTON_SELECTOR)
            .ok()
            .flatten()
            .is_some()
        {
            return;
        }
        let touch_event: &TouchEvent = event.unchecked_ref::<TouchEvent>();
        let touches: TouchList = touch_event.touches();
        if touches.length() == 1
            && let Some(touch) = touches.get(0)
        {
            start_x.set(Some(touch.client_x()));
        }
    }))
}

/// Advances the carousel after a horizontal swipe.
///
/// # Arguments
///
/// - `Rc<Cell<Option<i32>>>` - The starting position captured on touch-start.
/// - `Signal<u32>` - The active slide signal.
/// - `Option<Rc<dyn Fn(u32)>>` - The callback invoked after the active slide changes.
/// - `u32` - The number of slides.
///
/// # Returns
///
/// - `Option<Rc<dyn Fn(Event)>>` - The touch-end handler, absent for fewer than two slides.
fn carousel_touch_end(
    start_x: Rc<Cell<Option<i32>>>,
    active: Signal<u32>,
    on_change: Option<Rc<dyn Fn(u32)>>,
    slide_count: u32,
) -> Option<Rc<dyn Fn(Event)>> {
    if slide_count < 2 {
        return None;
    }
    Some(Rc::new(move |event: Event| {
        let Some(start_x) = start_x.take() else {
            return;
        };
        let touch_event: &TouchEvent = event.unchecked_ref::<TouchEvent>();
        let touches: TouchList = touch_event.changed_touches();
        let Some(touch) = touches.get(0) else {
            return;
        };
        let distance: i32 = touch.client_x() - start_x;
        if distance.abs() < CAROUSEL_SWIPE_THRESHOLD {
            return;
        }
        let current_index: u32 = active.get().min(slide_count - 1);
        let target_index: Option<u32> = if distance < 0 && current_index + 1 < slide_count {
            Some(current_index + 1)
        } else if distance > 0 && current_index > 0 {
            Some(current_index - 1)
        } else {
            None
        };
        if let Some(target_index) = target_index {
            event.prevent_default();
            active.set(target_index);
            if let Some(callback) = &on_change {
                callback(target_index);
            }
        }
    }))
}

/// Clears the starting position when a touch gesture is cancelled.
///
/// # Arguments
///
/// - `Rc<Cell<Option<i32>>>` - Storage shared with the touch-start handler.
///
/// # Returns
///
/// - `Option<Rc<dyn Fn(Event)>>` - The touch-cancel handler.
fn carousel_touch_cancel(start_x: Rc<Cell<Option<i32>>>) -> Option<Rc<dyn Fn(Event)>> {
    Some(Rc::new(move |_: Event| {
        start_x.set(None);
    }))
}

/// Builds the handler that moves the carousel to a slide.
///
/// # Arguments
///
/// - `Signal<u32>` - The index signal updated by the handler.
/// - `Option<Rc<dyn Fn(u32)>>` - The change callback.
/// - `u32` - The index of the slide that owns the handler.
///
/// # Returns
///
/// - `Option<Rc<dyn Fn(Event)>>` - The click handler.
fn on_carousel_dot(
    active: Signal<u32>,
    on_change: Option<Rc<dyn Fn(u32)>>,
    index: u32,
) -> Option<Rc<dyn Fn(Event)>> {
    Some(Rc::new(move |_event: Event| {
        active.set(index);
        if let Some(callback) = &on_change {
            callback(index);
        }
    }))
}