use crate::icon::{Icon, IconKind};
use dioxus::prelude::*;
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum CloseButtonSize {
Sm,
#[default]
Md,
Lg,
}
impl CloseButtonSize {
#[must_use]
pub fn class_name(&self) -> &'static str {
match self {
Self::Sm => "sd-close-btn--sm",
Self::Md => "sd-close-btn--md",
Self::Lg => "sd-close-btn--lg",
}
}
#[must_use]
pub fn icon_size(&self) -> u32 {
match self {
Self::Sm => 14,
Self::Md => 16,
Self::Lg => 20,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum CloseButtonVariant {
#[default]
Subtle,
Filled,
Transparent,
}
impl CloseButtonVariant {
#[must_use]
pub fn class_name(&self) -> &'static str {
match self {
Self::Subtle => "sd-close-btn--subtle",
Self::Filled => "sd-close-btn--filled",
Self::Transparent => "sd-close-btn--transparent",
}
}
}
#[component]
pub fn CloseButton(
#[props(default)]
size: CloseButtonSize,
#[props(default)]
variant: CloseButtonVariant,
#[props(default, into)]
aria_label: Option<String>,
#[props(default = false)]
disabled: bool,
#[props(default)]
onclick: Option<EventHandler<MouseEvent>>,
#[props(default)]
class: String,
) -> Element {
let locale = crate::locale::use_locale();
let effective_aria_label = aria_label.unwrap_or(locale.close);
let size_class = size.class_name();
let variant_class = variant.class_name();
let icon_sz = size.icon_size();
let handle_click = move |e: MouseEvent| {
if disabled {
return;
}
if let Some(h) = onclick {
h.call(e);
}
};
rsx! {
button {
r#type: "button",
class: "sd-close-btn {size_class} {variant_class} {class}",
title: "{effective_aria_label}",
"aria-label": "{effective_aria_label}",
disabled: disabled,
onclick: handle_click,
Icon { kind: IconKind::X, size: icon_sz }
}
}
}