Skip to main content

orbital_base_components/loading_bar/
provider.rs

1use leptos::prelude::*;
2
3use crate::overlay::ThemedPortal;
4use crate::ComponentRef;
5
6use super::bar::{BaseLoadingBar, LoadingBarRef};
7
8#[derive(Clone)]
9pub struct LoadingBarInjection {
10    loading_bar_ref: ComponentRef<LoadingBarRef>,
11}
12
13impl LoadingBarInjection {
14    pub fn expect_context() -> Self {
15        expect_context::<Self>()
16    }
17
18    /// Callback function for loading bar to start loading.
19    pub fn start(&self) {
20        if let Some(loading_bar_ref) = self.loading_bar_ref.get_untracked() {
21            loading_bar_ref.start();
22        }
23    }
24
25    /// The callback function when the loading bar finishes loading.
26    pub fn finish(&self) {
27        if let Some(loading_bar_ref) = self.loading_bar_ref.get_untracked() {
28            loading_bar_ref.finish();
29        }
30    }
31
32    /// Callback function for loading bar error.
33    pub fn error(&self) {
34        if let Some(loading_bar_ref) = self.loading_bar_ref.get_untracked() {
35            loading_bar_ref.error();
36        }
37    }
38}
39
40#[component]
41pub fn BaseLoadingBarProvider(children: Children) -> impl IntoView {
42    let loading_bar_ref = ComponentRef::<LoadingBarRef>::default();
43    provide_context(LoadingBarInjection {
44        loading_bar_ref: loading_bar_ref.clone(),
45    });
46
47    view! {
48        {children()}
49        <ThemedPortal immediate=true>
50            <BaseLoadingBar comp_ref=loading_bar_ref />
51        </ThemedPortal>
52    }
53}