thaw/src/button/mod.rs

98 lines
3.4 KiB
Rust
Raw Normal View History

2023-03-31 13:02:44 +08:00
mod theme;
2023-06-30 22:25:41 +08:00
use crate::{components::*, icon::*, theme::*, utils::mount_style::mount_style};
2023-03-28 12:37:24 +08:00
use leptos::*;
2023-03-30 17:12:58 +08:00
use stylers::style_sheet_str;
2023-03-31 13:02:44 +08:00
pub use theme::ButtonTheme;
2023-04-12 17:48:11 +08:00
#[derive(Default, PartialEq, Clone, Copy)]
2023-03-31 13:02:44 +08:00
pub enum ButtonType {
#[default]
PRIMARY,
SOLID,
2023-04-04 15:55:14 +08:00
TEXT,
2023-04-12 17:48:11 +08:00
LINK,
2023-03-31 13:02:44 +08:00
}
2023-04-04 15:55:14 +08:00
#[derive(Default, Clone)]
2023-03-31 13:02:44 +08:00
pub enum ButtonColor {
#[default]
PRIMARY,
2023-04-04 15:55:14 +08:00
SUCCESS,
2023-03-31 13:02:44 +08:00
WARNING,
2023-04-12 12:47:31 +08:00
ERROR,
2023-03-31 13:02:44 +08:00
}
2023-03-28 12:37:24 +08:00
2023-04-04 15:55:14 +08:00
impl ButtonColor {
pub fn theme_color(&self, theme: &Theme) -> String {
match self {
ButtonColor::PRIMARY => theme.common.color_primary.clone(),
ButtonColor::SUCCESS => theme.common.color_success.clone(),
ButtonColor::WARNING => theme.common.color_warning.clone(),
2023-04-12 12:47:31 +08:00
ButtonColor::ERROR => theme.common.color_error.clone(),
2023-04-04 15:55:14 +08:00
}
}
2023-09-17 23:10:33 +08:00
pub fn theme_color_hover(&self, theme: &Theme) -> String {
match self {
ButtonColor::PRIMARY => theme.common.color_primary_hover.clone(),
ButtonColor::SUCCESS => theme.common.color_success_hover.clone(),
ButtonColor::WARNING => theme.common.color_warning_hover.clone(),
ButtonColor::ERROR => theme.common.color_error_hover.clone(),
}
}
2023-04-04 15:55:14 +08:00
}
2023-03-28 12:37:24 +08:00
#[component]
2023-03-31 13:02:44 +08:00
pub fn Button(
2023-06-07 23:42:04 +08:00
#[prop(optional, into)] style: MaybeSignal<String>,
2023-03-31 13:02:44 +08:00
#[prop(optional, into)] type_: MaybeSignal<ButtonType>,
#[prop(optional, into)] color: MaybeSignal<ButtonColor>,
2023-05-08 23:53:54 +08:00
#[prop(optional, into)] round: MaybeSignal<bool>,
2023-06-09 22:24:39 +08:00
#[prop(optional, into)] icon: Option<Icon>,
#[prop(optional)] children: Option<ChildrenFn>,
2023-03-31 13:02:44 +08:00
) -> impl IntoView {
2023-08-29 09:11:22 +08:00
let theme = use_theme(Theme::light);
let css_vars = create_memo(move |_| {
2023-04-12 17:48:11 +08:00
let mut css_vars = String::new();
let theme = theme.get();
let bg_color = color.get().theme_color(&theme);
2023-09-17 23:10:33 +08:00
let bg_color_hover = color.get().theme_color_hover(&theme);
2023-04-12 17:48:11 +08:00
if type_.get() == ButtonType::PRIMARY {
css_vars.push_str(&format!("--background-color: {bg_color};"));
2023-09-17 23:10:33 +08:00
css_vars.push_str(&format!("--background-color-hover: {bg_color_hover};"));
2023-04-12 17:48:11 +08:00
css_vars.push_str(&format!("--font-color: #fff;"));
css_vars.push_str(&format!("--border-color: {bg_color};"));
css_vars.push_str(&format!("--border-color-hover: {bg_color};"));
} else {
css_vars.push_str(&format!("--font-color-hover: {bg_color};"));
css_vars.push_str(&format!("--border-color: #555a;"));
css_vars.push_str(&format!("--border-color-hover: #555;"));
}
2023-04-12 12:47:31 +08:00
2023-04-12 17:48:11 +08:00
css_vars
});
2023-03-30 17:12:58 +08:00
let class_name = mount_style("button", || style_sheet_str!("./src/button/button.css"));
2023-04-04 15:55:14 +08:00
2023-05-15 12:52:03 +08:00
let icon_style = if children.is_some() {
2023-05-08 23:53:54 +08:00
"margin-right: 6px"
} else {
""
};
2023-08-29 09:11:22 +08:00
view! { class=class_name,
2023-04-12 12:47:31 +08:00
<button
class:melt-button=true
class=("melt-button--text", move || type_.get() == ButtonType::TEXT)
2023-04-12 17:48:11 +08:00
class=("melt-button--link", move || type_.get() == ButtonType::LINK)
2023-05-08 23:53:54 +08:00
class=("melt-button--round", move || round.get())
2023-06-07 23:42:04 +08:00
style=move || format!("{}{}", css_vars.get(), style.get())
2023-04-04 15:55:14 +08:00
>
2023-06-09 22:24:39 +08:00
<OptionComp value=icon bind:icon>
<Icon icon=icon style=icon_style/>
</OptionComp>
<OptionComp value=children bind:children>
2023-08-29 09:11:22 +08:00
{ children() }
2023-06-09 22:24:39 +08:00
</OptionComp>
2023-03-28 12:37:24 +08:00
</button>
}
}