freya_components/
loader.rs

1use dioxus::prelude::*;
2use freya_elements as dioxus_elements;
3use freya_hooks::{
4    use_animation,
5    use_applied_theme,
6    AnimNum,
7    LoaderTheme,
8    LoaderThemeWith,
9    OnFinish,
10};
11
12/// Properties for the [`Loader`] component.
13#[derive(Props, Clone, PartialEq)]
14pub struct LoaderProps {
15    /// Theme override.
16    pub theme: Option<LoaderThemeWith>,
17}
18
19/// # Styling
20/// Inherits the [`LoaderTheme`](freya_hooks::LoaderTheme) theme.
21///
22/// Use cases: showing the progress of an external task (http calls for example), etc.
23///
24/// # Example
25///
26/// ```rust
27/// # use freya::prelude::*;
28/// fn app() -> Element {
29///     rsx!(Loader {})
30/// }
31/// # use freya_testing::prelude::*;
32/// # launch_doc(|| {
33/// #   rsx!(
34/// #       Preview {
35/// #           {app()}
36/// #       }
37/// #   )
38/// # }, (250., 250.).into(), "./images/gallery_loader.png");
39/// ```
40///
41/// # Preview
42/// ![Loader Preview][loader]
43#[cfg_attr(feature = "docs",
44    doc = embed_doc_image::embed_image!("loader", "images/gallery_loader.png")
45)]
46#[allow(non_snake_case)]
47pub fn Loader(props: LoaderProps) -> Element {
48    let theme = use_applied_theme!(&props.theme, loader);
49    let animation = use_animation(|conf| {
50        conf.auto_start(true);
51        conf.on_finish(OnFinish::Restart);
52        AnimNum::new(0.0, 360.0).time(650)
53    });
54
55    let LoaderTheme { primary_color } = theme;
56
57    let degrees = animation.get().read().read();
58
59    rsx!(svg {
60        rotate: "{degrees}deg",
61        width: "48",
62        height: "48",
63        svg_content: r#"
64            <svg viewBox="0 0 600 600" xmlns="http://www.w3.org/2000/svg">
65                <circle class="spin" cx="300" cy="300" fill="none"
66                r="250" stroke-width="64" stroke="{primary_color}"
67                stroke-dasharray="256 1400"
68                stroke-linecap="round" />
69            </svg>
70        "#
71    })
72}