detaxine-ui 0.8.30

A UI Library for Leptos
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
use leptos::prelude::*;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[allow(dead_code)]
pub enum ProgressComponentSize {
    Sm,
    Md,
    Lg,
}

/// A horizontal progress bar supporting determinate and indeterminate states.
///
/// # Props
///
/// - `progress` – `RwSignal<f64>` where `0.0` is 0% and `100.0` is 100%. Clamped automatically. Defaults to `0.0`.
/// - `color` – Tailwind background class for the fill. Defaults to `"bg-primary"`.
/// - `size` – `ProgressComponentSize::Sm`, `Md`, or `Lg` controlling bar height. Defaults to `Md`.
/// - `show_percentage` – When `true`, renders a percentage label below the bar. Ignored when `indeterminate=true`. Defaults to `false`.
/// - `indeterminate` – When `true`, renders an animated sliding bar and ignores `progress`. Defaults to `false`.
///
/// # Example
///
/// ```
/// use leptos::prelude::*;
/// use detaxine_ui::components::feedback::progress::ProgressBar;
///
/// #[component]
/// fn Example() -> impl IntoView {
///     let progress = RwSignal::new(65.0);
///
///     view! {
///         <ProgressBar progress=progress show_percentage=true />
///     }
/// }
/// ```
#[component]
pub fn ProgressBar(
    #[prop(into, default = RwSignal::new(0.0))] progress: RwSignal<f64>,
    #[prop(into, optional, default = "bg-primary".to_string())] color: String,
    #[prop(into, optional, default = ProgressComponentSize::Md)] size: ProgressComponentSize,
    #[prop(default = false)] show_percentage: bool,
    #[prop(default = false)] indeterminate: bool,
) -> impl IntoView {
    // Determine height based on size prop
    let container_class = move || {
        let height = match size {
            ProgressComponentSize::Sm => "h-1",
            ProgressComponentSize::Md => "h-2",
            ProgressComponentSize::Lg => "h-4",
        };
        if indeterminate {
            format!(
                "w-full bg-light-gray rounded-full {} relative overflow-hidden",
                height
            )
        } else {
            format!("w-full bg-light-gray rounded-full {}", height)
        }
    };

    let fill_class = move || {
        if indeterminate {
            format!(
                "{} h-full rounded-full animate-progress-indeterminate absolute",
                color
            )
        } else {
            format!("{} h-full rounded-full transition-all duration-300", color)
        }
    };

    let fill_style = move || {
        if indeterminate {
            "".to_string() // Width is handled in CSS class
        } else {
            format!("width: {}%;", progress.get().min(100.0).max(0.0))
        }
    };

    let aria_valuenow = move || {
        if indeterminate {
            "-1".to_string() // Indicates indeterminate
        } else {
            (progress.get() as i32).to_string()
        }
    };

    view! {
        <div class={container_class}>
            <div
                class={fill_class}
                style=fill_style
                role="progressbar"
                aria-valuemin="0"
                aria-valuemax="100"
                aria-valuenow=aria_valuenow
            ></div>
        </div>
        {if show_percentage && !indeterminate {
            Some(view! {
                <div class="text-xs text-center mt-1">
                    {move || format!("{:.0}%", (progress.get()).min(100.0).max(0.0))}
                </div>
            })
        } else { None }}
    }
}

/// A circular progress ring with optional percentage label in the center.
///
/// # Props
///
/// - `progress_percentage` – `RwSignal<f64>` from `0.0` to `100.0`. Clamped automatically. Defaults to `0.0`.
/// - `size` – `ProgressComponentSize::Sm`, `Md`, or `Lg` controlling the SVG dimensions and stroke width. Defaults to `Md`.
/// - `show_percentage` – When `true`, renders the percentage value in the center of the ring. Defaults to `true`.
///
/// # Example
///
/// ```
/// use leptos::prelude::*;
/// use detaxine_ui::components::feedback::progress::{CircularProgress, ProgressComponentSize};
///
/// #[component]
/// fn Example() -> impl IntoView {
///     let progress = RwSignal::new(42.0);
///     view! {
///         <CircularProgress progress_percentage=progress size=ProgressComponentSize::Lg show_percentage=true />
///     }
/// }
/// ```
#[component]
pub fn CircularProgress(
    #[prop(into, default = RwSignal::new(0.0))] progress_percentage: RwSignal<f64>,
    #[prop(into, optional, default = ProgressComponentSize::Md)] size: ProgressComponentSize,
    #[prop(default = true)] show_percentage: bool,
    /// Tailwind color class for the progress arc. Defaults to `"text-primary"`.
    #[prop(into, optional, default = "text-primary".to_string())]
    color: String,
    /// Tailwind color class for the track (background arc). Defaults to `"text-light-gray"`.
    #[prop(into, optional, default = "text-light-gray".to_string())]
    track_color: String,
) -> impl IntoView {
    let progress_val = move || progress_percentage.get().min(100.0).max(0.0);
    let svg_size = match size {
        ProgressComponentSize::Sm => 60,
        ProgressComponentSize::Md => 80, // md
        ProgressComponentSize::Lg => 120,
    };
    let stroke_width = match size {
        // ProgressComponentSize::Sm => 6,
        // ProgressComponentSize::Md => 8,
        // ProgressComponentSize::Lg => 12,
        _ => 6,
    };
    let r = 40.0 - (stroke_width as f64) / 2.0;
    let circ = 2.0 * std::f64::consts::PI * r;
    let font_size = match size {
        ProgressComponentSize::Sm => 12,
        ProgressComponentSize::Md => 14,
        ProgressComponentSize::Lg => 16,
    };

    view! {
        <div class="flex justify-center items-center">
            <svg
                width={svg_size}
                height={svg_size}
                viewBox="0 0 80 80"
                class=format!("transform -rotate-90 {}", track_color)
            >
                // Track arc
                <circle
                    cx="40"
                    cy="40"
                    r={r}
                    stroke="currentColor"
                    stroke-width={stroke_width}
                    fill="none"
                />
                // Progress arc
                <circle
                    cx="40"
                    cy="40"
                    r={r}
                    stroke="currentColor"
                    stroke-width={stroke_width}
                    fill="none"
                    stroke-dasharray={circ}
                    stroke-dashoffset=move || circ - (progress_val() / 100.0 * circ)
                    stroke-linecap="round"
                    class=format!("transition-all duration-300 {}", color)
                />
                {if show_percentage {
                    Some(view! {
                        <text
                            x="40"
                            y="40"
                            text-anchor="middle"
                            dominant-baseline="central"
                            font-size={font_size}
                            fill="currentColor"
                            class=format!("font-bold {}", color)
                            transform="rotate(90, 40, 40)"
                        >
                            {move || format!("{:.0}%", progress_val())}
                        </text>
                    })
                } else { None }}
            </svg>
        </div>
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::f64::consts::PI;

    // ProgressComponentSize

    #[test]
    fn size_eq() {
        assert_eq!(ProgressComponentSize::Sm, ProgressComponentSize::Sm);
        assert_ne!(ProgressComponentSize::Sm, ProgressComponentSize::Lg);
    }

    #[test]
    fn size_clone() {
        assert_eq!(ProgressComponentSize::Md.clone(), ProgressComponentSize::Md);
    }

    // fill_style / progress clamping

    fn fill_style(progress: f64, indeterminate: bool) -> String {
        if indeterminate {
            "".to_string()
        } else {
            format!("width: {}%;", progress.min(100.0).max(0.0))
        }
    }

    #[test]
    fn fill_style_normal_progress() {
        assert_eq!(fill_style(65.0, false), "width: 65%;");
    }

    #[test]
    fn fill_style_clamps_above_100() {
        assert_eq!(fill_style(150.0, false), "width: 100%;");
    }

    #[test]
    fn fill_style_clamps_below_0() {
        assert_eq!(fill_style(-10.0, false), "width: 0%;");
    }

    #[test]
    fn fill_style_empty_when_indeterminate() {
        assert_eq!(fill_style(50.0, true), "");
    }

    // aria_valuenow

    fn aria_valuenow(progress: f64, indeterminate: bool) -> String {
        if indeterminate {
            "-1".to_string()
        } else {
            (progress as i32).to_string()
        }
    }

    #[test]
    fn aria_valuenow_determinate() {
        assert_eq!(aria_valuenow(75.0, false), "75");
    }

    #[test]
    fn aria_valuenow_indeterminate() {
        assert_eq!(aria_valuenow(50.0, true), "-1");
    }

    // show_percentage visibility

    fn shows_percentage(show_percentage: bool, indeterminate: bool) -> bool {
        show_percentage && !indeterminate
    }

    #[test]
    fn percentage_shown_when_enabled_and_determinate() {
        assert!(shows_percentage(true, false));
    }

    #[test]
    fn percentage_hidden_when_indeterminate() {
        assert!(!shows_percentage(true, true));
    }

    #[test]
    fn percentage_hidden_when_disabled() {
        assert!(!shows_percentage(false, false));
    }

    // container_class / height mapping

    fn height_class(size: &ProgressComponentSize) -> &'static str {
        match size {
            ProgressComponentSize::Sm => "h-1",
            ProgressComponentSize::Md => "h-2",
            ProgressComponentSize::Lg => "h-4",
        }
    }

    #[test]
    fn sm_height() {
        assert_eq!(height_class(&ProgressComponentSize::Sm), "h-1");
    }

    #[test]
    fn md_height() {
        assert_eq!(height_class(&ProgressComponentSize::Md), "h-2");
    }

    #[test]
    fn lg_height() {
        assert_eq!(height_class(&ProgressComponentSize::Lg), "h-4");
    }

    // CircularProgress svg dimensions

    fn svg_size(size: &ProgressComponentSize) -> i32 {
        match size {
            ProgressComponentSize::Sm => 60,
            ProgressComponentSize::Md => 80,
            ProgressComponentSize::Lg => 120,
        }
    }

    fn stroke_width(size: &ProgressComponentSize) -> i32 {
        match size {
            ProgressComponentSize::Sm => 6,
            ProgressComponentSize::Md => 8,
            ProgressComponentSize::Lg => 12,
        }
    }

    #[test]
    fn svg_sizes_correct() {
        assert_eq!(svg_size(&ProgressComponentSize::Sm), 60);
        assert_eq!(svg_size(&ProgressComponentSize::Md), 80);
        assert_eq!(svg_size(&ProgressComponentSize::Lg), 120);
    }

    #[test]
    fn stroke_widths_correct() {
        assert_eq!(stroke_width(&ProgressComponentSize::Sm), 6);
        assert_eq!(stroke_width(&ProgressComponentSize::Md), 8);
        assert_eq!(stroke_width(&ProgressComponentSize::Lg), 12);
    }

    // CircularProgress stroke-dashoffset

    fn stroke_dashoffset(progress: f64, r: f64) -> f64 {
        let circ = 2.0 * PI * r;
        circ - (progress.min(100.0).max(0.0) / 100.0 * circ)
    }

    #[test]
    fn dashoffset_at_zero_equals_full_circumference() {
        let r = 40.0 - 8.0 / 2.0; // Md
        let circ = 2.0 * PI * r;
        assert!((stroke_dashoffset(0.0, r) - circ).abs() < 1e-9);
    }

    #[test]
    fn dashoffset_at_100_equals_zero() {
        let r = 40.0 - 8.0 / 2.0;
        assert!(stroke_dashoffset(100.0, r).abs() < 1e-9);
    }

    #[test]
    fn dashoffset_at_50_is_half_circumference() {
        let r = 40.0 - 8.0 / 2.0;
        let circ = 2.0 * PI * r;
        assert!((stroke_dashoffset(50.0, r) - circ / 2.0).abs() < 1e-9);
    }

    #[test]
    fn dashoffset_clamps_above_100() {
        let r = 40.0 - 8.0 / 2.0;
        assert!(stroke_dashoffset(150.0, r).abs() < 1e-9);
    }

    #[test]
    fn dashoffset_clamps_below_0() {
        let r = 40.0 - 8.0 / 2.0;
        let circ = 2.0 * PI * r;
        assert!((stroke_dashoffset(-10.0, r) - circ).abs() < 1e-9);
    }

    // reactive progress signal

    #[test]
    fn progress_signal_updates() {
        let owner = Owner::new();
        owner.with(|| {
            let progress = RwSignal::new(0.0f64);
            assert_eq!(progress.get(), 0.0);
            progress.set(75.0);
            assert_eq!(progress.get(), 75.0);
        });
    }
}