forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

badge.rs78 lines, 2.6 KiBHistoryRawDownload
1use topcoat::{
2 Result,
3 view::{Attributes, Child, Class, StaticClass, 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 highlighter-filled badge for highlighted statuses.
13 #[default]
14 Primary,
15 /// A quiet tinted badge for neutral labels such as notebook names.
16 Secondary,
17 /// A hairline-bordered badge on the card surface.
18 Outline,
19 /// A red-tinted badge for errors.
20 Destructive,
21}
22
23impl BadgeVariant {
24 /// Classes for the variant, including its border color. Keep border colors out of
25 /// the shared base to avoid conflicting classes.
26 fn classes(self) -> StaticClass {
27 match self {
28 Self::Primary => class!("border-transparent bg-primary text-primary-foreground"),
29 Self::Secondary => class!("border-transparent bg-foreground/5 text-muted-foreground"),
30 Self::Outline => class!("border-border bg-card text-muted-foreground"),
31 Self::Destructive => class!("border-bad/40 bg-bad/10 text-bad"),
32 }
33 }
34}
35
36/// Classes shared by badge variants. Badges use the mono face at 11px.
37const BASE: StaticClass = class!(
38 "inline-flex w-fit shrink-0 items-center justify-center gap-1 rounded-control",
39 "border px-1.5 py-px font-mono text-[11px] leading-4 font-medium whitespace-nowrap",
40);
41
42/// Builds the full class list for a badge of the given `variant`.
43///
44/// Use it to give badge styling to another element, such as a link:
45///
46/// ```ignore
47/// view! {
48/// <a href="/releases/v2" class=(badge_variants(BadgeVariant::Outline))>"v2.0"</a>
49/// }
50/// ```
51#[must_use]
52pub fn badge_variants(variant: BadgeVariant) -> Class<(StaticClass, StaticClass)> {
53 class!(BASE, variant.classes())
54}
55
56/// A small mono label for a status or count.
57///
58/// `variant` defaults to `Primary`. Pass the label as children and extra attributes
59/// through `attrs`. Attributes go on the `<span>`, with classes added to its classes.
60/// Use [`badge_variants`] to apply the same styling to another element.
61///
62/// ```ignore
63/// view! {
64/// badge(variant: BadgeVariant::Destructive, "Failed")
65/// }
66/// ```
67#[component]
68pub async fn badge(
69 #[default] variant: BadgeVariant,
70 #[default] mut attrs: Attributes,
71 #[default] child: Child<'_>,
72) -> Result<impl View> {
73 Ok(view! {
74 <span class=(class!(BASE, variant.classes(), attrs.remove("class"))) (attrs)>
75 (child)
76 </span>
77 })
78}