use crate::icon::{Icon, IconKind};
use dioxus::prelude::*;
#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub enum AlertVariant {
#[default]
Info,
Success,
Warning,
Danger,
}
impl AlertVariant {
pub fn scss_class(&self) -> &'static str {
match self {
AlertVariant::Info => "sd-alert--info",
AlertVariant::Success => "sd-alert--success",
AlertVariant::Warning => "sd-alert--warning",
AlertVariant::Danger => "sd-alert--danger",
}
}
pub fn glow_class(&self) -> &'static str {
match self {
AlertVariant::Info => "sd-alert--glow-info",
AlertVariant::Success => "sd-alert--glow-success",
AlertVariant::Warning => "sd-alert--glow-warning",
AlertVariant::Danger => "sd-alert--glow-danger",
}
}
pub fn title_class(&self) -> &'static str {
match self {
AlertVariant::Info => "sd-alert__title--info",
AlertVariant::Success => "sd-alert__title--success",
AlertVariant::Warning => "sd-alert__title--warning",
AlertVariant::Danger => "sd-alert__title--danger",
}
}
pub fn icon_kind(&self) -> IconKind {
match self {
AlertVariant::Info => IconKind::Info,
AlertVariant::Success => IconKind::CheckCircle,
AlertVariant::Warning => IconKind::AlertTriangle,
AlertVariant::Danger => IconKind::XCircle,
}
}
pub fn icon_color_class(&self) -> &'static str {
match self {
AlertVariant::Info => "sd-alert__icon--info",
AlertVariant::Success => "sd-alert__icon--success",
AlertVariant::Warning => "sd-alert__icon--warning",
AlertVariant::Danger => "sd-alert__icon--danger",
}
}
}
#[component]
pub fn Alert(
#[props(default)] title: Option<String>,
#[props(default)] variant: AlertVariant,
#[props(default)] class: String,
#[props(default)] glow: Option<bool>,
on_close: Option<EventHandler<MouseEvent>>,
children: Element,
) -> Element {
let variant_class = variant.scss_class();
let title_variant_class = variant.title_class();
let icon_color_class = variant.icon_color_class();
#[cfg(feature = "theme")]
let theme = crate::theme::use_theme();
#[cfg(feature = "theme")]
let radius_class = theme().radius_scale.md();
#[cfg(feature = "theme")]
let glow_class = if theme().is_component_glow_enabled("alert", glow) {
variant.glow_class()
} else {
""
};
#[cfg(not(feature = "theme"))]
let radius_class = "";
#[cfg(not(feature = "theme"))]
let glow_class = if glow.unwrap_or(false) {
variant.glow_class()
} else {
""
};
let icon_kind = variant.icon_kind();
rsx! {
div {
class: "sd-alert {variant_class} {glow_class} {radius_class} {class}",
span { class: "sd-alert__icon {icon_color_class}",
Icon { kind: icon_kind, size: 18 }
}
div { class: "sd-alert__content",
if let Some(t) = title {
h4 { class: "sd-alert__title {title_variant_class}", "{t}" }
}
div { class: "sd-alert__body", {children} }
}
if let Some(close_handler) = on_close {
button {
r#type: "button",
class: "sd-alert__close",
onclick: move |e| close_handler.call(e),
Icon { kind: IconKind::X, size: 14 }
}
}
}
}
}