Skip to main content

dioxus_bootstrap/
alert.rs

1use dioxus::prelude::*;
2
3#[derive(Clone, Copy, PartialEq)]
4pub enum AlertVariant {
5    Primary,
6    Secondary,
7    Success,
8    Danger,
9    Warning,
10    Info,
11    Light,
12    Dark,
13}
14
15impl Into<&'static str> for AlertVariant {
16    fn into(self) -> &'static str {
17        match self {
18            AlertVariant::Primary => "alert-primary",
19            AlertVariant::Secondary => "alert-secondary",
20            AlertVariant::Success => "alert-success",
21            AlertVariant::Danger => "alert-danger",
22            AlertVariant::Warning => "alert-warning",
23            AlertVariant::Info => "alert-info",
24            AlertVariant::Light => "alert-light",
25            AlertVariant::Dark => "alert-dark",
26        }
27    }
28}
29
30#[derive(Clone, Props, PartialEq)]
31pub struct AlertProps {
32    #[props(optional)]
33    id: String,
34    #[props(optional, default = "".to_string())]
35    class: String,
36    #[props(optional, default = AlertVariant::Primary)]
37    variant: AlertVariant,
38    #[props(optional, default = false)]
39    dismissible: bool,
40    #[props(optional, default = false)]
41    fade: bool,
42    #[props(optional, default = false)]
43    show: bool,
44    children: Element,
45}
46
47#[component]
48pub fn Alert(props: AlertProps) -> Element {
49    let mut class_list = vec!["alert".to_string()];
50    
51    let variant_class: &str = props.variant.into();
52    class_list.push(variant_class.to_string());
53    
54    if props.dismissible {
55        class_list.push("alert-dismissible".to_string());
56    }
57    
58    if props.fade {
59        class_list.push("fade".to_string());
60    }
61    
62    if props.show {
63        class_list.push("show".to_string());
64    }
65    
66    if !props.class.is_empty() {
67        class_list.push(props.class.clone());
68    }
69    
70    let class_list = class_list.join(" ");
71    
72    rsx! {
73        div {
74            id: props.id,
75            class: class_list,
76            role: "alert",
77            {props.children}
78            if props.dismissible {
79                button {
80                    r#type: "button",
81                    class: "btn-close",
82                    "data-bs-dismiss": "alert",
83                    "aria-label": "Close",
84                }
85            }
86        }
87    }
88}
89
90#[derive(Clone, Props, PartialEq)]
91pub struct AlertHeadingProps {
92    #[props(optional)]
93    id: String,
94    #[props(optional, default = "".to_string())]
95    class: String,
96    #[props(optional, default = "h4".to_string())]
97    tag: String,
98    children: Element,
99}
100
101#[component]
102pub fn AlertHeading(props: AlertHeadingProps) -> Element {
103    let mut class_list = vec!["alert-heading".to_string()];
104    
105    if !props.class.is_empty() {
106        class_list.push(props.class.clone());
107    }
108    
109    let class_list = class_list.join(" ");
110    
111    match props.tag.as_str() {
112        "h1" => rsx! { h1 { id: props.id, class: class_list, {props.children} } },
113        "h2" => rsx! { h2 { id: props.id, class: class_list, {props.children} } },
114        "h3" => rsx! { h3 { id: props.id, class: class_list, {props.children} } },
115        "h5" => rsx! { h5 { id: props.id, class: class_list, {props.children} } },
116        "h6" => rsx! { h6 { id: props.id, class: class_list, {props.children} } },
117        _ => rsx! { h4 { id: props.id, class: class_list, {props.children} } },
118    }
119}
120
121#[derive(Clone, Props, PartialEq)]
122pub struct AlertLinkProps {
123    #[props(optional)]
124    id: String,
125    #[props(optional, default = "".to_string())]
126    class: String,
127    #[props(optional, default = "".to_string())]
128    href: String,
129    children: Element,
130}
131
132#[component]
133pub fn AlertLink(props: AlertLinkProps) -> Element {
134    let mut class_list = vec!["alert-link".to_string()];
135    
136    if !props.class.is_empty() {
137        class_list.push(props.class.clone());
138    }
139    
140    let class_list = class_list.join(" ");
141    
142    rsx! {
143        a {
144            id: props.id,
145            class: class_list,
146            href: props.href,
147            {props.children}
148        }
149    }
150}