thaw/src/button/mod.rs

62 lines
1.6 KiB
Rust
Raw Normal View History

2023-03-31 13:02:44 +08:00
mod theme;
2023-04-04 15:55:14 +08:00
use crate::{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;
#[derive(Default, PartialEq, Clone)]
pub enum ButtonType {
#[default]
PRIMARY,
SOLID,
2023-04-04 15:55:14 +08:00
TEXT,
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,
Error,
}
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(),
ButtonColor::Error => theme.common.color_error.clone(),
}
}
}
2023-03-28 12:37:24 +08:00
#[component]
2023-03-31 13:02:44 +08:00
pub fn Button(
cx: Scope,
#[prop(optional, into)] type_: MaybeSignal<ButtonType>,
#[prop(optional, into)] color: MaybeSignal<ButtonColor>,
children: Children,
) -> impl IntoView {
2023-04-04 15:55:14 +08:00
let theme = use_theme(cx, Theme::light);
let css_vars = create_memo(cx, move |_| {
let mut css_vars = String::new();
let theme = theme.get();
let bg_color = color.get().theme_color(&theme);
css_vars.push_str(&format!("--background-color: {bg_color}"));
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-03-28 12:37:24 +08:00
view! {cx, class=class_name,
2023-04-04 15:55:14 +08:00
<button
class:melt-button=true
class=("melt-button--text", move || type_.get() == ButtonType::TEXT)
style=move || css_vars.get()
>
2023-03-28 12:37:24 +08:00
{children(cx)}
</button>
}
}