| 1 | use topcoat::{ |
| 2 | Result, |
| 3 | view::{Attributes, View, class, component, view}, |
| 4 | }; |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 10 | #[allow(dead_code)] |
| 11 | pub enum BadgeVariant { |
| 12 | |
| 13 | #[default] |
| 14 | Primary, |
| 15 | |
| 16 | Secondary, |
| 17 | |
| 18 | Outline, |
| 19 | |
| 20 | Destructive, |
| 21 | } |
| 22 | |
| 23 | impl BadgeVariant { |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | fn classes(self) -> &'static str { |
| 31 | match self { |
| 32 | Self::Primary => "border-transparent bg-primary text-primary-foreground", |
| 33 | Self::Secondary => "border-transparent bg-foreground/5 text-foreground", |
| 34 | Self::Outline => "border-border text-foreground", |
| 35 | Self::Destructive => "border-transparent bg-destructive text-destructive-foreground", |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | const BASE: &str = "inline-flex w-fit shrink-0 items-center justify-center gap-1 rounded-md \ |
| 45 | border px-2 py-0.5 text-xs font-medium whitespace-nowrap"; |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | #[must_use] |
| 57 | pub fn badge_variants(variant: BadgeVariant) -> String { |
| 58 | format!("{BASE} {}", variant.classes()) |
| 59 | } |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | #[component] |
| 76 | pub async fn badge( |
| 77 | #[default] variant: BadgeVariant, |
| 78 | #[default] mut attrs: Attributes, |
| 79 | #[default] child: View, |
| 80 | ) -> Result { |
| 81 | view! { |
| 82 | <span class=(class!(BASE, variant.classes(), attrs.remove("class"))) (attrs)> |
| 83 | (child) |
| 84 | </span> |
| 85 | } |
| 86 | } |