thaw/src/mobile/tabbar/mod.rs

67 lines
1.7 KiB
Rust
Raw Normal View History

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-01 14:04:12 +08:00
use crate::{
use_theme,
utils::{maybe_rw_signal::MaybeRwSignal, 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(
#[prop(optional, into)] value: MaybeRwSignal<String>,
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!(
"--melt-background-color: {};",
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! {
<div class="melt-tabbar" style=move || css_vars.get()>
{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
}