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
use dioxus::prelude::*;
use freya_elements::elements as dioxus_elements;
use freya_hooks::{use_get_theme, ProgressBarTheme};

/// [`ProgressBar`] component properties.
#[derive(Props, PartialEq)]
pub struct ProgressBarProps {
    /// Show a label with the current progress. Default to false.
    #[props(default = false)]
    show_progress: bool,

    /// Width of the progress bar. Default to 100%.
    #[props(default = "100%".to_string(), into)]
    width: String,

    /// Height of the progress bar. Default to 20px.
    #[props(default = "20".to_string(), into)]
    height: String,
    /// Percentage of the progress bar.
    pub progress: f32,
}

/// `ProgressBar` component.
///
/// # Props
/// See [`ProgressBarProps`].
///
/// # Styling
/// Inherits the [`ProgressBarTheme`](freya_hooks::ProgressBarTheme) theme.
///
/// # Example
///
/// ```no_run
/// # use freya::prelude::*;
/// fn app(cx: Scope) -> Element {
///     render!(
///         ProgressBar {
///             progress: 75.0
///         }
///     )
/// }
/// ```
///
#[allow(non_snake_case)]
pub fn ProgressBar(cx: Scope<ProgressBarProps>) -> Element {
    let theme = use_get_theme(cx);

    let ProgressBarTheme {
        color,
        background,
        progress_background,
    } = theme.progress_bar;
    let width = &cx.props.width;
    let height = &cx.props.height;
    let show_progress = cx.props.show_progress;
    let progress = cx.props.progress;

    render!(
        rect {
            width: "{width}",
            height: "{height}",
            padding: "2",
            rect {
                corner_radius: "999",
                width: "100%",
                height: "100%",
                shadow: "0 2 10 1 rgb(0, 0, 0, 45)",
                background: "{background}",
                font_size: "13",
                direction: "horizontal",
                rect {
                    corner_radius: "999",
                    width: "{progress}%",
                    height: "100%",
                    background: "{progress_background}",
                    display: "center",
                    overflow: "clip",
                    if show_progress {
                        rsx!(
                            label {
                                align: "center",
                                width: "100%",
                                color: "{color}",
                                max_lines: "1",
                                "{progress.floor()}%"
                            }
                        )
                    }
                }
            }
        }
    )
}