leptos_bootstrap/v5/
badge.rs

1use leptos::prelude::*;
2use std::fmt;
3
4pub enum BadgeKind {
5    Primary,
6    Secondary,
7    Success,
8    Danger,
9    Warning,
10    Info,
11    Light,
12    Dark,
13    Link,
14}
15
16impl fmt::Display for BadgeKind {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        let s = match self {
19            Self::Primary => "text-bg-primary",
20            Self::Secondary => "text-bg-secondary",
21            Self::Success => "text-bg-success",
22            Self::Danger => "text-bg-danger",
23            Self::Warning => "text-bg-warning",
24            Self::Info => "text-bg-info",
25            Self::Light => "text-bg-light",
26            Self::Dark => "text-bg-dark",
27            Self::Link => "text-bg-link",
28        };
29        write!(f, "{}", s)
30    }
31}
32
33#[component]
34pub fn Badge<'a>(
35    #[prop(default = BadgeKind::Primary)] kind: BadgeKind,
36    #[prop(default = false)] pill: bool,
37    #[prop(optional, into)] class: &'a str,
38    children: Children,
39) -> impl IntoView {
40    let mut class = format!("badge {} {}", kind, class);
41    if pill {
42        class.push_str(" rounded-pill");
43    }
44    view! { <span class=class>{children()}</span> }
45}