thaw/thaw_mobile/nav_bar/mod.rs

129 lines
4.1 KiB
Rust
Raw Permalink Normal View History

2023-11-01 14:04:12 +08:00
mod theme;
pub use theme::NavBarTheme;
2023-06-04 17:14:41 +08:00
use crate::{icon::*, use_theme, Theme};
use leptos::*;
use thaw_components::*;
use thaw_utils::{class_list, mount_style, OptionalProp};
#[slot]
pub struct NavBarLeft {
#[prop(optional, into)]
class: OptionalProp<MaybeSignal<String>>,
children: Children,
}
#[slot]
pub struct NavBarRight {
#[prop(optional, into)]
class: OptionalProp<MaybeSignal<String>>,
children: Children,
}
2023-06-04 17:14:41 +08:00
#[component]
pub fn NavBar(
2023-10-17 17:25:20 +08:00
#[prop(optional, into)] title: MaybeSignal<String>,
2023-06-04 17:14:41 +08:00
#[prop(optional, into)] left_arrow: MaybeSignal<bool>,
#[prop(optional, into)] left_text: OptionalProp<MaybeSignal<String>>,
#[prop(optional)] nav_bar_left: Option<NavBarLeft>,
2023-10-17 17:25:20 +08:00
#[prop(optional, into)] on_click_left: Option<Callback<ev::MouseEvent>>,
#[prop(optional, into)] right_text: OptionalProp<MaybeSignal<String>>,
#[prop(optional)] nav_bar_right: Option<NavBarRight>,
2023-10-17 17:25:20 +08:00
#[prop(optional, into)] on_click_right: Option<Callback<ev::MouseEvent>>,
#[prop(optional, into)] class: OptionalProp<MaybeSignal<String>>,
2023-06-04 17:14:41 +08:00
) -> impl IntoView {
2023-10-07 21:41:03 +08:00
mount_style("nav-bar", include_str!("./nav-bar.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.nav_bar.background_color
)
})
});
2023-06-04 17:14:41 +08:00
2023-10-11 09:28:44 +08:00
let on_click_left = move |ev| {
2023-10-17 17:25:20 +08:00
if let Some(click_left) = on_click_left.as_ref() {
2023-10-09 10:28:22 +08:00
click_left.call(ev);
2023-06-04 17:32:33 +08:00
}
2023-10-11 09:28:44 +08:00
};
2023-10-09 20:56:30 +08:00
2023-10-11 09:28:44 +08:00
let on_click_right = move |ev| {
2023-10-17 17:25:20 +08:00
if let Some(click_right) = on_click_right.as_ref() {
2023-10-09 10:28:22 +08:00
click_right.call(ev);
2023-06-04 17:32:33 +08:00
}
2023-10-11 09:28:44 +08:00
};
2023-06-04 17:32:33 +08:00
2023-10-07 21:41:03 +08:00
view! {
2024-02-01 15:24:09 +08:00
<div
class=class_list!["thaw-nav-bar", class.map(| c | move || c.get())]
style=move || css_vars.get()
>
{if let Some(NavBarLeft { class, children }) = nav_bar_left {
view! {
<div
class=class_list!["thaw-nav-bar__left", class.map(| c | move || c.get())]
on:click=on_click_left
>
{children()}
</div>
}
2024-02-01 15:24:09 +08:00
.into_view()
} else if let Some(left_text) = left_text.into_option() {
view! {
<div class="thaw-nav-bar__left" on:click=on_click_left>
<If cond=left_arrow>
<Then slot>
2024-02-01 21:38:01 +08:00
<Icon icon=icondata_ai::AiLeftOutlined/>
2024-02-01 15:24:09 +08:00
</Then>
</If>
{move || left_text.get()}
</div>
}
.into_view()
} else {
(move || {
if left_arrow.get() {
view! {
<div class="thaw-nav-bar__left" on:click=on_click_left>
2024-02-01 21:38:01 +08:00
<Icon icon=icondata_ai::AiLeftOutlined/>
2024-02-01 15:24:09 +08:00
</div>
}
.into()
} else {
None
}
})
.into_view()
}}
2023-11-05 16:03:58 +08:00
<div class="thaw-nav-bar__center">{move || title.get()}</div>
2024-02-01 15:24:09 +08:00
{if let Some(NavBarRight { class, children }) = nav_bar_right {
view! {
<div
class=class_list!["thaw-nav-bar__right", class.map(| c | move || c.get())]
on:click=on_click_right
>
{children()}
</div>
}
2024-02-01 15:24:09 +08:00
.into()
} else if let Some(right_text) = right_text.into_option() {
view! {
<div class="thaw-nav-bar__right" on:click=on_click_right>
{move || right_text.get()}
</div>
}
.into()
} else {
None
}}
2023-06-04 17:14:41 +08:00
</div>
}
2023-06-30 22:25:41 +08:00
}