1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use dioxus::prelude::*;
use crate::types::Color;
/// Bootstrap Badge component.
///
/// # Bootstrap HTML → Dioxus
///
/// | HTML | Dioxus |
/// |---|---|
/// | `<span class="badge text-bg-primary">New</span>` | `Badge { color: Color::Primary, "New" }` |
/// | `<span class="badge rounded-pill text-bg-danger">99+</span>` | `Badge { color: Color::Danger, pill: true, "99+" }` |
///
/// ```rust,no_run
/// rsx! {
/// Badge { color: Color::Primary, "New" }
/// Badge { color: Color::Danger, pill: true, "99+" }
/// // Inside a heading
/// h1 { "Messages " Badge { color: Color::Info, "4" } }
/// }
/// ```
#[derive(Clone, PartialEq, Props)]
pub struct BadgeProps {
/// Badge color variant.
#[props(default = Color::Primary)]
pub color: Color,
/// Use pill (rounded) style.
#[props(default)]
pub pill: bool,
/// Additional CSS classes.
#[props(default)]
pub class: String,
/// Any additional HTML attributes.
#[props(extends = GlobalAttributes)]
attributes: Vec<Attribute>,
/// Child elements.
pub children: Element,
}
#[component]
pub fn Badge(props: BadgeProps) -> Element {
let pill = if props.pill { " rounded-pill" } else { "" };
let full_class = if props.class.is_empty() {
format!("badge text-bg-{}{pill}", props.color)
} else {
format!("badge text-bg-{}{pill} {}", props.color, props.class)
};
rsx! {
span { class: "{full_class}", ..props.attributes, {props.children} }
}
}