Skip to main content

shadow_design/
alert.rs

1use crate::icon::{Icon, IconKind};
2use dioxus::prelude::*;
3
4#[derive(Clone, Copy, PartialEq, Debug, Default)]
5pub enum AlertVariant {
6    #[default]
7    Info,
8    Success,
9    Warning,
10    Danger,
11}
12
13impl AlertVariant {
14    pub fn scss_class(&self) -> &'static str {
15        match self {
16            AlertVariant::Info => "sd-alert--info",
17            AlertVariant::Success => "sd-alert--success",
18            AlertVariant::Warning => "sd-alert--warning",
19            AlertVariant::Danger => "sd-alert--danger",
20        }
21    }
22
23    pub fn glow_class(&self) -> &'static str {
24        match self {
25            AlertVariant::Info => "sd-alert--glow-info",
26            AlertVariant::Success => "sd-alert--glow-success",
27            AlertVariant::Warning => "sd-alert--glow-warning",
28            AlertVariant::Danger => "sd-alert--glow-danger",
29        }
30    }
31
32    pub fn title_class(&self) -> &'static str {
33        match self {
34            AlertVariant::Info => "sd-alert__title--info",
35            AlertVariant::Success => "sd-alert__title--success",
36            AlertVariant::Warning => "sd-alert__title--warning",
37            AlertVariant::Danger => "sd-alert__title--danger",
38        }
39    }
40
41    pub fn icon_kind(&self) -> IconKind {
42        match self {
43            AlertVariant::Info => IconKind::Info,
44            AlertVariant::Success => IconKind::CheckCircle,
45            AlertVariant::Warning => IconKind::AlertTriangle,
46            AlertVariant::Danger => IconKind::XCircle,
47        }
48    }
49
50    pub fn icon_color_class(&self) -> &'static str {
51        match self {
52            AlertVariant::Info => "sd-alert__icon--info",
53            AlertVariant::Success => "sd-alert__icon--success",
54            AlertVariant::Warning => "sd-alert__icon--warning",
55            AlertVariant::Danger => "sd-alert__icon--danger",
56        }
57    }
58}
59
60#[component]
61pub fn Alert(
62    #[props(default)] title: Option<String>,
63    #[props(default)] variant: AlertVariant,
64    #[props(default)] class: String,
65    #[props(default)] glow: Option<bool>,
66    on_close: Option<EventHandler<MouseEvent>>,
67    children: Element,
68) -> Element {
69    let variant_class = variant.scss_class();
70    let title_variant_class = variant.title_class();
71    let icon_color_class = variant.icon_color_class();
72
73    #[cfg(feature = "theme")]
74    let theme = crate::theme::use_theme();
75    #[cfg(feature = "theme")]
76    let radius_class = theme().radius_scale.md();
77    #[cfg(feature = "theme")]
78    let glow_class = if theme().is_component_glow_enabled("alert", glow) {
79        variant.glow_class()
80    } else {
81        ""
82    };
83
84    #[cfg(not(feature = "theme"))]
85    let radius_class = "";
86    #[cfg(not(feature = "theme"))]
87    let glow_class = if glow.unwrap_or(false) {
88        variant.glow_class()
89    } else {
90        ""
91    };
92
93    let icon_kind = variant.icon_kind();
94
95    rsx! {
96        div {
97            class: "sd-alert {variant_class} {glow_class} {radius_class} {class}",
98            span { class: "sd-alert__icon {icon_color_class}",
99                Icon { kind: icon_kind, size: 18 }
100            }
101            div { class: "sd-alert__content",
102                if let Some(t) = title {
103                    h4 { class: "sd-alert__title {title_variant_class}", "{t}" }
104                }
105                div { class: "sd-alert__body", {children} }
106            }
107            if let Some(close_handler) = on_close {
108                button {
109                    r#type: "button",
110                    class: "sd-alert__close",
111                    onclick: move |e| close_handler.call(e),
112                    Icon { kind: IconKind::X, size: 14 }
113                }
114            }
115        }
116    }
117}