thaw/thaw_mobile/tabbar/tabbar_item.rs

47 lines
1.5 KiB
Rust
Raw Normal View History

2023-11-09 16:46:14 +08:00
use super::use_tabbar;
use crate::{icon::Icon, theme::use_theme, Theme};
2023-05-27 23:55:27 +08:00
use leptos::*;
use thaw_components::*;
use thaw_utils::{mount_style, StoredMaybeSignal};
2023-05-27 23:55:27 +08:00
#[component]
pub fn TabbarItem(
2023-10-17 17:25:20 +08:00
#[prop(into)] key: MaybeSignal<String>,
2024-02-01 21:38:01 +08:00
#[prop(optional, into)] icon: Option<icondata_core::Icon>,
2023-05-27 23:55:27 +08:00
children: Children,
) -> impl IntoView {
2023-10-07 21:41:03 +08:00
mount_style("tabbar-item", include_str!("./tabbar-item.css"));
2023-08-29 09:11:22 +08:00
let theme = use_theme(Theme::light);
let tabbar = use_tabbar();
2023-10-17 17:25:20 +08:00
let key: StoredMaybeSignal<_> = key.into();
let on_click = move |_| {
2023-11-09 16:46:14 +08:00
let click_key = key.get();
if tabbar.0.with(|key| key != &click_key) {
tabbar.0.set(click_key);
}
2023-05-27 23:55:27 +08:00
};
2023-08-29 09:11:22 +08:00
let css_vars = create_memo(move |_| {
2023-05-27 23:55:27 +08:00
let mut css_vars = String::new();
2023-11-02 22:01:48 +08:00
theme.with(|theme| {
let font_color = theme.common.color_primary.clone();
2023-11-05 16:03:58 +08:00
css_vars.push_str(&format!("--thaw-font-color-selected: {font_color};"));
2023-11-02 22:01:48 +08:00
});
2023-05-27 23:55:27 +08:00
css_vars
});
2023-10-07 21:41:03 +08:00
view! {
2023-10-08 09:28:13 +08:00
<div
2023-11-05 16:03:58 +08:00
class="thaw-tabbar-item"
2023-11-09 16:46:14 +08:00
class=("thaw-tabbar-item--selected", move || tabbar.0.get() == key.get())
2023-10-17 17:25:20 +08:00
on:click=on_click
2023-10-08 09:28:13 +08:00
style=move || css_vars.get()
>
2023-10-02 17:04:03 +08:00
<OptionComp value=icon let:icon>
2023-11-05 16:03:58 +08:00
<Icon icon=icon width="22px" height="22px" class="thaw-tabbar-item__icon"/>
2023-06-09 22:24:39 +08:00
</OptionComp>
2023-11-05 16:03:58 +08:00
<div class="thaw-tabbar-item__content">{children()}</div>
2023-05-27 23:55:27 +08:00
</div>
}
}