yew_bootstrap/component/
alert.rs

1use yew::prelude::*;
2
3use crate::util::Color;
4
5/// # Alert component
6/// Used alongside [crate::util::Color] to create Alert components 
7/// 
8/// See [AlertProps] for a listing of properties
9/// 
10/// ## Example
11/// ```rust
12/// use yew::prelude::*;
13/// use yew_bootstrap::component::Alert;
14/// use yew_bootstrap::util::Color;
15/// fn test() -> Html {
16///     html!{
17///         <Alert style={Color::Primary}>
18///             {"This is a primary alert!"}
19///         </Alert>
20///     }
21/// }
22/// ```
23pub struct Alert {}
24
25/// # Properties of [Alert]
26#[derive(Properties, Clone, PartialEq)]
27pub struct AlertProps {
28    /// CSS class
29    #[prop_or_default]
30    pub class: String,
31
32    /// Inner components
33    #[prop_or_default]
34    pub children: Children,
35
36    /// Color style, default [Color::Primary]
37    #[prop_or(Color::Primary)]
38    pub style: Color,
39
40    /// Optional text placed before the children
41    #[prop_or_default]
42    pub text: String,
43}
44
45impl Component for Alert {
46    type Message = ();
47    type Properties = AlertProps;
48
49    fn create(_ctx: &Context<Self>) -> Self {
50        Self {}
51    }
52
53    fn view(&self, ctx: &Context<Self>) -> Html {
54        let props = ctx.props();
55        let mut classes = Classes::new();
56        classes.push("alert");
57        classes.push(format!("alert-{}", props.style));
58        classes.push(props.class.clone());
59
60        html! {
61            <div
62                class={classes}
63                role="alert"
64            >
65                { &props.text }
66                { for props.children.iter() }
67            </div>
68        }
69    }
70}