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
mod theme;

use crate::{use_theme, utils::mount_style, Theme};
use leptos::*;
pub use theme::ProgressTheme;

#[derive(Default, Clone, PartialEq)]
pub enum ProgressIndicatorPlacement {
    #[default]
    Outside,
    Inside,
}

impl Copy for ProgressIndicatorPlacement {}

impl ProgressIndicatorPlacement {
    pub fn as_str(&self) -> &'static str {
        match self {
            ProgressIndicatorPlacement::Outside => "outside",
            ProgressIndicatorPlacement::Inside => "inside",
        }
    }
}

#[derive(Default, Clone)]
pub enum ProgressColor {
    #[default]
    Primary,
    Success,
    Warning,
    Error,
}

impl ProgressColor {
    fn theme_background_color(&self, theme: &Theme) -> String {
        match self {
            Self::Primary => theme.common.color_primary.clone(),
            Self::Success => theme.common.color_success.clone(),
            Self::Warning => theme.common.color_warning.clone(),
            Self::Error => theme.common.color_error.clone(),
        }
    }
}

#[component]
pub fn Progress(
    #[prop(into, optional)] percentage: MaybeSignal<f32>,
    #[prop(into, optional)] color: MaybeSignal<ProgressColor>,
    #[prop(into, default = MaybeSignal::Static(true))] show_indicator: MaybeSignal<bool>,
    #[prop(into, optional)] indicator_placement: MaybeSignal<ProgressIndicatorPlacement>,
) -> impl IntoView {
    mount_style("progress", include_str!("./progress.css"));
    let theme = use_theme(Theme::light);
    let css_vars = create_memo(move |_| {
        let mut css_vars = String::new();
        theme.with(|theme| {
            css_vars.push_str(&format!(
                "--thaw-background-color: {};",
                theme.progress.background_color
            ));
            css_vars.push_str(&format!(
                "--thaw-inner-background-color: {};",
                color.get().theme_background_color(theme)
            ));
        });
        css_vars
    });
    let style = move || {
        let percentage = percentage.get();
        let percentage = if percentage < 0.0 {
            0.0
        } else if percentage > 100.0 {
            100.0
        } else {
            percentage
        };
        format!("width: {}%;", percentage)
    };

    let class = move || {
        let mut class = String::from("thaw-progress__progress");

        class.push_str(&format!(
            " thaw-progress__progress--indicator-{}",
            indicator_placement.get().as_str()
        ));

        class
    };

    view! {
        <div class="thaw-progress" style=move || css_vars.get()>
            <div class=class>
                <div class="thaw-progress__progress-inner" style=style>
                    <Show when=move || {
                        show_indicator.get()
                            && indicator_placement.get() == ProgressIndicatorPlacement::Inside
                    }>
                        <div class="thaw-progress__indicator--inside">

                            {move || { format!("{}%", percentage.get()) }}

                        </div>
                    </Show>
                </div>
            </div>
            <Show when=move || {
                show_indicator.get()
                    && indicator_placement.get() == ProgressIndicatorPlacement::Outside
            }>
                <div class="thaw-progress__indicator--outside">

                    {move || { format!("{}%", percentage.get()) }}

                </div>
            </Show>
        </div>
    }
}