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
use dioxus::prelude::*;
use freya_elements as dioxus_elements;
use freya_hooks::{
    use_applied_theme,
    ProgressBarTheme,
    ProgressBarThemeWith,
};
/// Properties for the [`ProgressBar`] component.
#[derive(Props, Clone, PartialEq)]
pub struct ProgressBarProps {
    /// Width of the progress bar. Default to `fill`.
    #[props(default = "fill".into())]
    pub width: String,
    /// Theme override.
    pub theme: Option<ProgressBarThemeWith>,
    /// Show a label with the current progress. Default to false.
    #[props(default = false)]
    pub show_progress: bool,
    /// Percentage of the progress bar.
    pub progress: f32,
}
/// Display the progress of something visually. For example: downloading files, fetching data, etc.
///
/// # Styling
/// Inherits the [`ProgressBarTheme`](freya_hooks::ProgressBarTheme) theme.
///
/// # Example
///
/// ```rust
/// # use freya::prelude::*;
/// fn app() -> Element {
///     rsx!(ProgressBar {
///         show_progress: true,
///         progress: 50.0
///     })
/// }
/// # use freya_testing::prelude::*;
/// # launch_doc(|| {
/// #   rsx!(
/// #       Preview {
/// #           ProgressBar {
/// #               width: "50%",
/// #               show_progress: true,
/// #               progress: 50.0
/// #           }
/// #       }
/// #   )
/// # }, (250., 250.).into(), "./images/gallery_progress_bar.png");
/// ```
///
/// # Preview
/// ![ProgressBar Preview][progress_bar]
#[cfg_attr(feature = "docs",
    doc = embed_doc_image::embed_image!("progress_bar", "images/gallery_progress_bar.png")
)]
#[allow(non_snake_case)]
pub fn ProgressBar(
    ProgressBarProps {
        width,
        theme,
        show_progress,
        progress,
    }: ProgressBarProps,
) -> Element {
    let ProgressBarTheme {
        color,
        background,
        progress_background,
        height,
    } = use_applied_theme!(&theme, progress_bar);
    let progress = progress.clamp(0., 100.);
    rsx!(
        rect {
            width: "{width}",
            height: "{height}",
            padding: "2",
            rect {
                corner_radius: "999",
                width: "100%",
                height: "100%",
                background: "{background}",
                font_size: "13",
                direction: "horizontal",
                border: "1 outer {background}",
                rect {
                    corner_radius: "999",
                    width: "{progress}%",
                    height: "100%",
                    background: "{progress_background}",
                    main_align: "center",
                    cross_align: "center",
                    overflow: "clip",
                    if show_progress {
                        label {
                            text_align: "center",
                            width: "100%",
                            color: "{color}",
                            max_lines: "1",
                            text_height: "disable-least-ascent",
                            "{progress.floor()}%"
                        }
                    }
                }
            }
        }
    )
}