thaw/badge/
badge.rs

1use leptos::{either::Either, prelude::*};
2use thaw_utils::{class_list, mount_style};
3
4#[component]
5pub fn Badge(
6    #[prop(optional, into)] class: MaybeProp<String>,
7    /// A Badge can be filled, outline, ghost, inverted.
8    #[prop(optional, into)]
9    appearance: Signal<BadgeAppearance>,
10    /// A Badge can be on of several preset sizes.
11    #[prop(optional, into)]
12    size: Signal<BadgeSize>,
13    /// A Badge can be one of preset colors.
14    #[prop(optional, into)]
15    color: Signal<BadgeColor>,
16    #[prop(optional)] children: Option<Children>,
17) -> impl IntoView {
18    mount_style("badge", include_str!("./badge.css"));
19
20    view! {
21        <div class=class_list![
22            "thaw-badge",
23            move || format!("thaw-badge--{}", appearance.get().as_str()),
24            move || format!("thaw-badge--{}", size.get().as_str()),
25            move || format!("thaw-badge--{}", color.get().as_str()),
26            class
27        ]>
28            {if let Some(children) = children {
29                Either::Left(children())
30            } else {
31                Either::Right(())
32            }}
33        </div>
34    }
35}
36
37#[derive(Default, Clone)]
38pub enum BadgeAppearance {
39    #[default]
40    Filled,
41    Ghost,
42    Outline,
43    Tint,
44}
45
46impl BadgeAppearance {
47    pub fn as_str(&self) -> &'static str {
48        match self {
49            BadgeAppearance::Filled => "filled",
50            BadgeAppearance::Ghost => "ghost",
51            BadgeAppearance::Outline => "outline",
52            BadgeAppearance::Tint => "tint",
53        }
54    }
55}
56
57#[derive(Default, Clone)]
58pub enum BadgeSize {
59    Tiny,
60    ExtraSmall,
61    Small,
62    #[default]
63    Medium,
64    Large,
65    ExtraLarge,
66}
67
68impl BadgeSize {
69    pub fn as_str(&self) -> &'static str {
70        match self {
71            BadgeSize::Tiny => "tiny",
72            BadgeSize::ExtraSmall => "extra-small",
73            BadgeSize::Small => "small",
74            BadgeSize::Medium => "medium",
75            BadgeSize::Large => "large",
76            BadgeSize::ExtraLarge => "extra-large",
77        }
78    }
79}
80
81#[derive(Default, Clone)]
82pub enum BadgeColor {
83    #[default]
84    Brand,
85    Danger,
86    Important,
87    Informative,
88    Severe,
89    Subtle,
90    Success,
91    Warning,
92}
93
94impl BadgeColor {
95    pub fn as_str(&self) -> &'static str {
96        match self {
97            BadgeColor::Brand => "brand",
98            BadgeColor::Danger => "danger",
99            BadgeColor::Important => "important",
100            BadgeColor::Informative => "informative",
101            BadgeColor::Severe => "severe",
102            BadgeColor::Subtle => "subtle",
103            BadgeColor::Success => "success",
104            BadgeColor::Warning => "warning",
105        }
106    }
107}