2023-05-27 23:55:27 +08:00
|
|
|
mod tabbar_item;
|
2023-11-01 14:04:12 +08:00
|
|
|
mod theme;
|
2023-05-27 23:55:27 +08:00
|
|
|
|
2023-11-09 14:43:30 +08:00
|
|
|
use crate::{use_theme, utils::mount_style::mount_style, Theme};
|
2023-05-27 23:55:27 +08:00
|
|
|
use leptos::*;
|
|
|
|
pub use tabbar_item::*;
|
2023-11-01 14:04:12 +08:00
|
|
|
pub use theme::TabbarTheme;
|
2023-05-27 23:55:27 +08:00
|
|
|
|
|
|
|
#[component]
|
2023-10-11 21:25:11 +08:00
|
|
|
pub fn Tabbar(
|
2023-11-09 14:43:30 +08:00
|
|
|
#[prop(optional, into)] value: RwSignal<String>,
|
2023-10-11 21:25:11 +08:00
|
|
|
children: Children,
|
|
|
|
) -> impl IntoView {
|
2023-10-07 21:41:03 +08:00
|
|
|
mount_style("tabbar", include_str!("./tabbar.css"));
|
2023-11-01 14:04:12 +08:00
|
|
|
let theme = use_theme(Theme::light);
|
|
|
|
let css_vars = create_memo(move |_| {
|
|
|
|
theme.with(|theme| {
|
|
|
|
format!(
|
2023-11-05 16:03:58 +08:00
|
|
|
"--thaw-background-color: {};",
|
2023-11-01 14:04:12 +08:00
|
|
|
theme.tabbar.background_color
|
|
|
|
)
|
|
|
|
})
|
|
|
|
});
|
2023-05-27 23:55:27 +08:00
|
|
|
|
2023-10-11 21:25:11 +08:00
|
|
|
let tabbar_injection_key = create_rw_signal(TabbarInjectionKey::new(value.get()));
|
2023-08-29 09:11:22 +08:00
|
|
|
create_effect(move |_| {
|
2023-10-11 21:25:11 +08:00
|
|
|
let selected_key = value.get();
|
2023-05-27 23:55:27 +08:00
|
|
|
let key = tabbar_injection_key.get_untracked();
|
|
|
|
if selected_key != key.value {
|
|
|
|
tabbar_injection_key.set(TabbarInjectionKey::new(selected_key));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-08-29 09:11:22 +08:00
|
|
|
create_effect(move |_| {
|
2023-10-11 21:25:11 +08:00
|
|
|
let selected_key = value.get_untracked();
|
2023-05-27 23:55:27 +08:00
|
|
|
let key = tabbar_injection_key.get();
|
|
|
|
if selected_key != key.value {
|
2023-10-11 21:25:11 +08:00
|
|
|
value.set(key.value);
|
2023-05-27 23:55:27 +08:00
|
|
|
}
|
|
|
|
});
|
2023-08-29 09:11:22 +08:00
|
|
|
provide_context(tabbar_injection_key);
|
2023-11-02 22:01:48 +08:00
|
|
|
view! {
|
2023-11-05 16:03:58 +08:00
|
|
|
<div class="thaw-tabbar" style=move || css_vars.get()>
|
2023-11-02 22:01:48 +08:00
|
|
|
{children()}
|
|
|
|
</div>
|
|
|
|
}
|
2023-05-27 23:55:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct TabbarInjectionKey {
|
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TabbarInjectionKey {
|
|
|
|
pub fn new(value: String) -> Self {
|
|
|
|
Self { value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 09:11:22 +08:00
|
|
|
pub fn use_tabbar() -> RwSignal<TabbarInjectionKey> {
|
|
|
|
use_context::<RwSignal<TabbarInjectionKey>>().expect("TabbarInjectionKey not exist")
|
2023-05-27 23:55:27 +08:00
|
|
|
}
|