use topcoat::{
Result,
view::{Attributes, Child, Class, StaticClass, View, class, component, view},
};
/// The visual style of a [`badge`].
///
/// [`Default`] is `BadgeVariant::Primary`, used when no variant is given.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[allow(dead_code)]
pub enum BadgeVariant {
/// The highlighter-filled badge for highlighted statuses.
#[default]
Primary,
/// A quiet tinted badge for neutral labels such as notebook names.
Secondary,
/// A hairline-bordered badge on the card surface.
Outline,
/// A red-tinted badge for errors.
Destructive,
}
impl BadgeVariant {
/// Classes for the variant, including its border color. Keep border colors out of
/// the shared base to avoid conflicting classes.
fn classes(self) -> StaticClass {
match self {
Self::Primary => class!("border-transparent bg-primary text-primary-foreground"),
Self::Secondary => class!("border-transparent bg-foreground/5 text-muted-foreground"),
Self::Outline => class!("border-border bg-card text-muted-foreground"),
Self::Destructive => class!("border-bad/40 bg-bad/10 text-bad"),
}
}
}
/// Classes shared by badge variants. Badges use the mono face at 11px.
const BASE: StaticClass = class!(
"inline-flex w-fit shrink-0 items-center justify-center gap-1 rounded-control",
"border px-1.5 py-px font-mono text-[11px] leading-4 font-medium whitespace-nowrap",
);
/// Builds the full class list for a badge of the given `variant`.
///
/// Use it to give badge styling to another element, such as a link:
///
/// ```ignore
/// view! {
/// "v2.0"
/// }
/// ```
#[must_use]
pub fn badge_variants(variant: BadgeVariant) -> Class<(StaticClass, StaticClass)> {
class!(BASE, variant.classes())
}
/// A small mono label for a status or count.
///
/// `variant` defaults to `Primary`. Pass the label as children and extra attributes
/// through `attrs`. Attributes go on the ``, with classes added to its classes.
/// Use [`badge_variants`] to apply the same styling to another element.
///
/// ```ignore
/// view! {
/// badge(variant: BadgeVariant::Destructive, "Failed")
/// }
/// ```
#[component]
pub async fn badge(
#[default] variant: BadgeVariant,
#[default] mut attrs: Attributes,
#[default] child: Child<'_>,
) -> Result {
Ok(view! {
(child)
})
}