thaw/src/checkbox/mod.rs

54 lines
1.5 KiB
Rust
Raw Normal View History

2023-10-03 15:57:35 +08:00
mod checkbox_group;
mod checkbox_item;
2023-10-07 15:43:39 +08:00
use crate::{
components::*,
icon::*,
theme::use_theme,
utils::{maybe_rw_signal::MaybeRwSignal, mount_style::mount_style},
Theme,
};
2023-10-03 15:57:35 +08:00
pub use checkbox_group::CheckboxGroup;
pub use checkbox_item::CheckboxItem;
2023-06-30 22:25:41 +08:00
use icondata::AiIcon;
2023-04-14 17:28:32 +08:00
use leptos::*;
#[component]
2023-10-07 15:43:39 +08:00
pub fn Checkbox(
2023-10-10 17:00:08 +08:00
#[prop(optional, into)] value: MaybeRwSignal<bool>,
2023-10-07 15:43:39 +08:00
children: Children,
) -> impl IntoView {
2023-08-29 09:11:22 +08:00
let theme = use_theme(Theme::light);
2023-10-07 21:41:03 +08:00
mount_style("checkbox", include_str!("./checkbox.css"));
2023-04-14 17:28:32 +08:00
2023-08-29 09:11:22 +08:00
let css_vars = create_memo(move |_| {
2023-04-14 17:28:32 +08:00
let mut css_vars = String::new();
2023-11-02 22:01:48 +08:00
theme.with(|theme| {
css_vars.push_str(&format!(
2023-11-05 16:03:58 +08:00
"--thaw-background-color-checked: {};",
2023-11-02 22:01:48 +08:00
theme.common.color_primary
));
});
2023-04-14 17:28:32 +08:00
css_vars
});
2023-06-10 12:40:52 +08:00
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-checkbox=true
class=("thaw-checkbox--checked", move || value.get())
2023-10-08 09:28:13 +08:00
style=move || css_vars.get()
2023-10-10 17:00:08 +08:00
on:click=move |_| value.set(!value.get_untracked())
2023-10-08 09:28:13 +08:00
>
2023-11-05 16:03:58 +08:00
<input class="thaw-checkbox__input" type="checkbox"/>
<div class="thaw-checkbox__dot">
2023-10-10 17:00:08 +08:00
<If cond=value.clone_into()>
2023-06-10 12:40:52 +08:00
<Then slot>
2023-06-30 22:25:41 +08:00
<Icon icon=Icon::from(AiIcon::AiCheckOutlined) style="color: white"/>
2023-06-10 12:40:52 +08:00
</Then>
</If>
2023-04-14 17:28:32 +08:00
</div>
2023-11-05 16:03:58 +08:00
<div class="thaw-checkbox__label">{children()}</div>
2023-04-14 17:28:32 +08:00
</div>
}
}