euv-ui 0.35.90

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

/// Renders one column heading of the calendar header row.
///
/// # Arguments
///
/// - `EuvCalendarWeekday` - The weekday this column stands for.
///
/// # Returns
///
/// - `VirtualNode` - The weekday heading cell.
fn calendar_weekday(weekday: EuvCalendarWeekday) -> VirtualNode {
    let label: &'static str = match weekday {
        EuvCalendarWeekday::Mon => "Mo",
        EuvCalendarWeekday::Tue => "Tu",
        EuvCalendarWeekday::Wed => "We",
        EuvCalendarWeekday::Thu => "Th",
        EuvCalendarWeekday::Fri => "Fr",
        EuvCalendarWeekday::Sat => "Sa",
        EuvCalendarWeekday::Sun => "Su",
    };
    html! {
        div {
            role: "columnheader"
            class: c_euv_calendar_weekday()
            key: label
            {
                label
            }
        }
    }
}

/// Renders one day cell of the calendar grid.
///
/// Every cell stays mounted; the cell picks exactly one class out of the
/// registry — `_muted`, `_selected`, `_today` or the plain day class — so the
/// class list of a cell never repeats a token and never leaves two
/// competing rules to be settled by stylesheet order. The registry entries
/// are complete cell styles rather than additive modifiers, so one class per
/// cell is what the cascade can actually honour.
///
/// A spill-over cell belongs to the neighbouring month, so it never carries
/// the selected marker: the day number it shows belongs to another month and
/// would mark the wrong cell.
///
/// # Arguments
///
/// - `EuvCalendarDay` - The cell to render.
/// - `Signal<u32>` - The selected-day signal driving the `_selected` modifier.
/// - `usize` - The position of the cell in the grid, used as its key.
/// - `usize` - The total number of day cells in the grid.
/// - `bool` - Whether this cell is the grid's roving tab stop.
/// - `Option<Rc<dyn Fn(u32, bool)>>` - The callback a click on the cell runs.
/// - `&str` - The month title used to form the accessible name.
///
/// # Returns
///
/// - `VirtualNode` - The grid cell containing the day button.
fn calendar_day(
    day: EuvCalendarDay,
    selected: Signal<u32>,
    index: usize,
    day_count: usize,
    is_roving_tab_stop: bool,
    on_day_click: Option<Rc<dyn Fn(u32, bool)>>,
    title: &str,
) -> VirtualNode {
    let day_number: u32 = day.day;
    let day_text: String = day_number.to_string();
    let is_selected: bool = !day.muted && selected.get() == day_number;
    let tabindex: &str = if is_roving_tab_stop { "0" } else { "-1" };
    let aria_selected: &str = if is_selected {
        CALENDAR_ATTR_TRUE
    } else {
        CALENDAR_ATTR_FALSE
    };
    let aria_current: &str = if day.today {
        CALENDAR_CURRENT_DATE
    } else {
        CALENDAR_ATTR_FALSE
    };
    // The cell does not resolve the click on its own: the caller owns the
    // date arithmetic, so the cell hands it the day number and whether the
    // cell belongs to a neighbouring month. Without a callback the cell
    // writes its day number into the selected signal, which is what a
    // calendar used on its own needs.
    let handler: Option<Rc<dyn Fn(Event)>> = match on_day_click {
        Some(callback) => {
            let callback: Rc<dyn Fn(u32, bool)> = Rc::clone(&callback);
            let muted: bool = day.muted;
            Some(Rc::new(move |_event: Event| {
                callback(day_number, muted);
            }))
        }
        None => on_calendar_day_click(selected, day_number),
    };
    let on_keydown: Option<Rc<dyn Fn(Event)>> = calendar_day_keydown(index, day_count);
    html! {
        div {
            role: "gridcell"
            "aria-selected": aria_selected
            "aria-current": aria_current
            class: c_euv_calendar_grid_cell()
            key: index.to_string()
            button {
                type: "button"
                "data-slot": "calendar-day-button"
                "data-day-index": index.to_string()
                "data-selected": if is_selected {
                    CALENDAR_ATTR_TRUE
                } else {
                    CALENDAR_ATTR_FALSE
                }
                "data-today": if day.today {
                    CALENDAR_ATTR_TRUE
                } else {
                    CALENDAR_ATTR_FALSE
                }
                "aria-label": format!("{} {}", title, day_number)
                tabindex: tabindex
                class: c_euv_calendar_grid_cell_button()
                class: if day.muted {
                    c_euv_calendar_day_muted()
                } else if is_selected {
                    c_euv_calendar_day_selected()
                } else if day.today {
                    c_euv_calendar_day_today()
                } else {
                    c_euv_calendar_day()
                }
                // The key has to be unique among the siblings. The day number
                // is not: the spill-over cells repeat the day numbers of the
                // month on display, and a duplicated key makes the keyed diff
                // hand a cell the DOM node of another one.
                onclick: handler
                onkeydown: on_keydown
                day_text
            }
        }
    }
}

/// Builds the roving-focus handler for a calendar day.
///
/// Arrow keys move by one day or one week; Home and End stay within the
/// current week. Clicking the focused cell reuses the caller's date logic.
///
/// # Arguments
///
/// - `usize` - The current cell index.
/// - `usize` - The number of cells in the calendar grid.
///
/// # Returns
///
/// - `Option<Rc<dyn Fn(Event)>>` - The day-navigation handler.
fn calendar_day_keydown(index: usize, day_count: usize) -> Option<Rc<dyn Fn(Event)>> {
    Some(Rc::new(move |event: Event| {
        let Ok(keyboard_event) = event.dyn_into::<KeyboardEvent>() else {
            return;
        };
        let key: String = keyboard_event.key();
        let week_start: usize = index / 7 * 7;
        let next_index: usize = match key.as_str() {
            CALENDAR_KEY_ARROW_LEFT if index > 0 => index - 1,
            CALENDAR_KEY_ARROW_RIGHT if index + 1 < day_count => index + 1,
            CALENDAR_KEY_ARROW_UP if index >= 7 => index - 7,
            CALENDAR_KEY_ARROW_DOWN if index + 7 < day_count => index + 7,
            CALENDAR_KEY_HOME => week_start,
            CALENDAR_KEY_END => (week_start + 6).min(day_count.saturating_sub(1)),
            _ => return,
        };
        let Some(target) = keyboard_event
            .target()
            .and_then(|target: EventTarget| target.dyn_into::<Element>().ok())
        else {
            return;
        };
        let Ok(Some(root)) = target.closest(CALENDAR_GRID_SELECTOR) else {
            return;
        };
        let Ok(buttons) = root.query_selector_all(CALENDAR_DAY_BUTTON_SELECTOR) else {
            return;
        };
        let Some(next) = buttons
            .item(next_index as u32)
            .and_then(|button: Node| button.dyn_into::<HtmlElement>().ok())
        else {
            return;
        };
        keyboard_event.prevent_default();
        let _: Result<(), JsValue> = next.focus();
        next.click();
    }))
}

/// A pure-presentation month grid for date selection.
///
/// This component deliberately computes no dates: the caller resolves the
/// month, builds the `weekdays` header and the `days` cells (including
/// leading / trailing spill-over cells) and hands them in. Presentation-only
/// means the grid is reproducible from its props alone, so a test can assert
/// the rendered classes without touching the clock.
///
/// Day cells are always mounted; selection is a reactive class swap driven by
/// the `selected` signal, and clicking a cell writes the day number back
/// through [`on_calendar_day_click`]. The chevrons are inert — paging between
/// months belongs to the caller, which owns the date state this component
/// deliberately does not.
///
/// # Arguments
///
/// - `VirtualNode<EuvCalendarProps>` - The props node containing the month
///   caption, the weekday headings, the day cells and the selected day.
///
/// # Returns
///
/// - `VirtualNode` - The calendar virtual DOM tree.
#[component]
pub fn euv_calendar(node: VirtualNode<EuvCalendarProps>) -> VirtualNode {
    let EuvCalendarProps {
        title,
        weekdays,
        days,
        selected,
        on_day_click,
    }: EuvCalendarProps = node.try_get_props().unwrap_or_default();
    let selected_day: u32 = selected.get();
    let roving_index: usize = days
        .iter()
        .position(|day: &EuvCalendarDay| !day.muted && day.day == selected_day)
        .or_else(|| {
            days.iter()
                .position(|day: &EuvCalendarDay| !day.muted && day.today)
        })
        .or_else(|| days.iter().position(|day: &EuvCalendarDay| !day.muted))
        .unwrap_or(0);
    let weekday_nodes: Vec<VirtualNode> = weekdays
        .iter()
        .map(|weekday: &EuvCalendarWeekday| calendar_weekday(*weekday))
        .collect();
    let day_nodes: Vec<VirtualNode> = days
        .iter()
        .enumerate()
        .map(
            |(index, day): (usize, &calendar::view::r#struct::EuvCalendarDay)| {
                calendar_day(
                    *day,
                    selected,
                    index,
                    days.len(),
                    index == roving_index,
                    on_day_click.clone(),
                    title,
                )
            },
        )
        .collect();
    html! {
        div {
            "data-slot": "calendar"
            class: c_euv_calendar()
            // The header carries the month title and the two month
            // steps. A calendar that is embedded in a picker brings its
            // own header, so a calendar with no title renders no header
            // at all — a header with an empty title and a pair of month
            // steps that belong to nobody is worse than no header.
            if !title.is_empty() {
                div {
                    class: c_euv_calendar_header()
                    h3 {
                        class: c_euv_calendar_title()
                        "aria-live": "polite"
                        "aria-atomic": "true"
                        title
                    }
                    div {
                        class: c_euv_calendar_nav()
                        button {
                            type: "button"
                            "aria-label": CALENDAR_PREVIOUS_MONTH_LABEL
                            class: c_euv_calendar_day()
                            "‹"
                        }
                        button {
                            type: "button"
                            "aria-label": CALENDAR_NEXT_MONTH_LABEL
                            class: c_euv_calendar_day()
                            "›"
                        }
                    }
                }
            }
            div {
                "data-slot": "calendar-grid"
                role: "grid"
                "aria-label": if title.is_empty() {
                    CALENDAR_ARIA_LABEL
                } else {
                    title
                }
                div {
                    role: "row"
                    class: c_euv_calendar_weekdays()
                    weekday_nodes
                }
                div {
                    role: "rowgroup"
                    class: c_euv_calendar_grid()
                    for row in day_nodes.chunks(7) {
                        div {
                            role: "row"
                            class: c_euv_calendar_grid_row()
                            for day_node in row.iter() {
                                day_node.clone()
                            }
                        }
                    }
                }
            }
        }
    }
}

/// The clickable cell of a day.
///
/// The cell reports the chosen day and the refused one the way the
/// reference does: the chosen day carries `data-selected` and
/// `aria-selected`, the refused one carries `data-disabled` and
/// `aria-disabled`.
///
/// # Arguments
///
/// - `VirtualNode<EuvCalendarDayButtonProps>` - The props node containing the
///   day number and the state of the cell.
///
/// # Returns
///
/// - `VirtualNode` - The euv calendar day button virtual DOM tree.
#[component]
pub fn euv_calendar_day_button(node: VirtualNode<EuvCalendarDayButtonProps>) -> VirtualNode {
    let EuvCalendarDayButtonProps {
        day,
        selected,
        disabled,
        on_select,
    }: EuvCalendarDayButtonProps = node.try_get_props().unwrap_or_default();
    let disabled_signal: Signal<bool> = Signal::create(disabled);
    let day_number: u32 = day;
    let day_text: String = day_number.to_string();
    let on_click: Option<Rc<dyn Fn(Event)>> = if disabled {
        None
    } else {
        Some(Rc::new(move |_: Event| {
            if let Some(callback) = &on_select {
                callback(day_number);
            }
        }))
    };
    html! {
        button {
            type: "button"
            "data-slot": "calendar-day-button"
            "data-selected": if selected {
                "true"
            } else {
                "false"
            }
            "data-disabled": if disabled {
                "true"
            } else {
                "false"
            }
            "aria-selected": if selected {
                "true"
            } else {
                "false"
            }
            "aria-disabled": if disabled {
                "true"
            } else {
                "false"
            }
            tabindex: if disabled {
                "-1"
            } else {
                "0"
            }
            disabled: disabled_signal
            class: if disabled {
                c_euv_calendar_day_button_disabled()
            } else if selected {
                c_euv_calendar_day_button_selected()
            } else {
                c_euv_calendar_day_button()
            }
            onclick: on_click
            day_text
        }
    }
}

/// Builds the click handler that selects a calendar day.
///
/// # Arguments
///
/// - `Signal<u32>` - The selected-day signal to write.
/// - `u32` - The day number of the clicked cell.
///
/// # Returns
///
/// - `Option<Rc<dyn Fn(Event)>>` - The click handler.
pub fn on_calendar_day_click(selected: Signal<u32>, day: u32) -> Option<Rc<dyn Fn(Event)>> {
    Some(Rc::new(move |_: Event| {
        selected.set(day);
    }))
}