steid

@jamesgill /

Rust3.4 KBCode·Blame·History·Raw
1use topcoat::{
2 Result,
3 view::{Attributes, View, class, component, view},
4};
5
6/// The visual style of a [`badge`].
7///
8/// [`Default`] is `BadgeVariant::Primary`, used when no variant is given.
9#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
10#[allow(dead_code)]
11pub enum BadgeVariant {
12 /// The primary-filled badge for highlighted statuses.
13 #[default]
14 Primary,
15 /// A muted, tinted fill for neutral statuses.
16 Secondary,
17 /// A hairline-bordered badge on the page background.
18 Outline,
19 /// A destructive-filled badge for errors and warnings.
20 Destructive,
21 /// A merged-filled badge: a pull request that landed.
22 ///
23 /// A variant rather than a `class` passed through `attrs`, because a caller adding
24 /// `bg-merged` to a `Primary` badge sets two background classes on one element and
25 /// **stylesheet order, not class order, decides the winner** — which is the same
26 /// trap [`classes`](BadgeVariant::classes) already documents for the border. It was
27 /// not hypothetical: the merged badge shipped blue first.
28 Merged,
29}
30
31impl BadgeVariant {
32 /// The Tailwind classes for this variant.
33 ///
34 /// Each variant sets its own border color rather than inheriting a
35 /// transparent one from [`BASE`]: with two border-color classes on the
36 /// same element, stylesheet order (not class order) would decide the
37 /// winner.
38 fn classes(self) -> &'static str {
39 match self {
40 Self::Primary => "border-transparent bg-primary text-primary-foreground",
41 Self::Secondary => "border-transparent bg-foreground/5 text-foreground",
42 Self::Outline => "border-border text-foreground",
43 Self::Destructive => "border-transparent bg-destructive text-destructive-foreground",
44 Self::Merged => "border-transparent bg-merged text-merged-foreground",
45 }
46 }
47}
48
49/// The classes shared by every badge, regardless of variant.
50///
51/// Every badge carries a border (colored per variant) so that the `Outline`
52/// variant, which only recolors it, does not change the badge's dimensions.
53const BASE: &str = "inline-flex w-fit shrink-0 items-center justify-center gap-1 rounded-md \
54 border px-2 py-0.5 text-xs font-medium whitespace-nowrap";
55
56/// Builds the full class string for a badge of the given `variant`.
57///
58/// Use it to give badge styling to another element, such as a link:
59///
60/// ```ignore
61/// view! {
62/// <a href="/releases/v2" class=(badge_variants(BadgeVariant::Outline))>"v2.0"</a>
63/// }
64/// ```
65#[must_use]
66pub fn badge_variants(variant: BadgeVariant) -> String {
67 format!("{BASE} {}", variant.classes())
68}
69
70/// A badge component: a small inline pill for statuses, counts, and tags.
71///
72/// The `variant` parameter selects the styling, defaulting to `Primary`. The
73/// `attrs` (such as `class` or `title`) are forwarded to the underlying
74/// `<span>`; a `class` among them is appended to the computed classes. Child
75/// nodes become the badge's content.
76///
77/// ```ignore
78/// view! {
79/// badge(variant: BadgeVariant::Destructive, "Failed")
80/// }
81/// ```
82///
83/// To style another element like a badge, use [`badge_variants`] directly.
84#[component]
85pub async fn badge(
86 #[default] variant: BadgeVariant,
87 #[default] mut attrs: Attributes,
88 #[default] child: View,
89) -> Result {
90 view! {
91 <span class=(class!(BASE, variant.classes(), attrs.remove("class"))) (attrs)>
92 (child)
93 </span>
94 }
95}