2023-05-27 23:55:27 +08:00
|
|
|
mod tabbar_item;
|
|
|
|
|
|
|
|
use crate::utils::mount_style::mount_style;
|
|
|
|
use leptos::*;
|
2023-10-07 21:41:03 +08:00
|
|
|
|
2023-05-27 23:55:27 +08:00
|
|
|
pub use tabbar_item::*;
|
|
|
|
|
|
|
|
#[component]
|
2023-08-29 09:11:22 +08:00
|
|
|
pub fn Tabbar(#[prop(into)] selected: RwSignal<String>, children: Children) -> impl IntoView {
|
2023-10-07 21:41:03 +08:00
|
|
|
mount_style("tabbar", include_str!("./tabbar.css"));
|
2023-05-27 23:55:27 +08:00
|
|
|
|
2023-08-29 09:11:22 +08:00
|
|
|
let tabbar_injection_key = create_rw_signal(TabbarInjectionKey::new(selected.get()));
|
|
|
|
create_effect(move |_| {
|
2023-05-27 23:55:27 +08:00
|
|
|
let selected_key = selected.get();
|
|
|
|
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-05-27 23:55:27 +08:00
|
|
|
let selected_key = selected.get_untracked();
|
|
|
|
let key = tabbar_injection_key.get();
|
|
|
|
if selected_key != key.value {
|
|
|
|
selected.set(key.value);
|
|
|
|
}
|
|
|
|
});
|
2023-08-29 09:11:22 +08:00
|
|
|
provide_context(tabbar_injection_key);
|
2023-10-08 09:28:13 +08:00
|
|
|
view! { <div class="melt-tabbar">{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
|
|
|
}
|