impulse_thaw/loading_bar/
loading_bar_provider.rs

1use super::{LoadingBar, LoadingBarRef};
2use leptos::{context::Provider, prelude::*};
3use thaw_components::Teleport;
4use thaw_utils::ComponentRef;
5
6#[component]
7pub fn LoadingBarProvider(children: Children) -> impl IntoView {
8    let loading_bar_ref = ComponentRef::<LoadingBarRef>::default();
9
10    view! {
11        <Provider value=LoadingBarInjection {
12            loading_bar_ref,
13        }>
14            {children()} <Teleport>
15                <LoadingBar comp_ref=loading_bar_ref />
16            </Teleport>
17        </Provider>
18    }
19}
20
21#[derive(Clone)]
22pub struct LoadingBarInjection {
23    loading_bar_ref: ComponentRef<LoadingBarRef>,
24}
25
26impl Copy for LoadingBarInjection {}
27
28impl LoadingBarInjection {
29    #[deprecated = "`expect_use()` is deprecated, please use `LoadingBarInjection::expect_context()`."]
30    pub fn expect_use() -> Self {
31        expect_context::<Self>()
32    }
33
34    pub fn expect_context() -> Self {
35        expect_context()
36    }
37
38    /// Callback function for loading bar to start loading.
39    pub fn start(&self) {
40        if let Some(loading_bar_ref) = self.loading_bar_ref.get_untracked() {
41            loading_bar_ref.start();
42        }
43    }
44
45    /// The callback function when the loading bar finishes loading.
46    pub fn finish(&self) {
47        if let Some(loading_bar_ref) = self.loading_bar_ref.get_untracked() {
48            loading_bar_ref.finish();
49        }
50    }
51
52    /// Callback function for loading bar error.
53    pub fn error(&self) {
54        if let Some(loading_bar_ref) = self.loading_bar_ref.get_untracked() {
55            loading_bar_ref.error();
56        }
57    }
58}