Badge
A small status label.
Web replica of the native GPUI output
StableBetav0.1.0Deprecated
use gpui::{div, prelude::*};
use crate::ui::badge::{Badge, BadgeVariant};
pub fn badge_demo() -> impl IntoElement {
div()
.flex()
.gap_2()
.child(Badge::new("Stable"))
.child(Badge::new("Beta").variant(BadgeVariant::Secondary))
.child(Badge::new("v0.1.0").variant(BadgeVariant::Outline))
.child(Badge::new("Deprecated").variant(BadgeVariant::Destructive))
}Installation
Add the component
$ cargo hyuskui add badgeOr copy it by hand: paste the source below into
src/ui/badge.rsand register it insrc/ui/mod.rs.Make sure the theme is installed
Every component reads colors from
crate::theme. If you have not runcargo hyuskui init, follow the installation guide first.
Usage
use crate::ui::badge::*;Source
This file is yours after install. Change the styles, add variants, or delete what you do not need.
use gpui::{div, prelude::*, px, App, FontWeight, SharedString, Window};
use crate::theme::ActiveTheme;
#[derive(Clone, Copy, Default, PartialEq)]
pub enum BadgeVariant {
#[default]
Default,
Secondary,
Outline,
Destructive,
}
#[derive(IntoElement)]
pub struct Badge {
label: SharedString,
variant: BadgeVariant,
}
impl Badge {
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
variant: BadgeVariant::default(),
}
}
pub fn variant(mut self, variant: BadgeVariant) -> Self {
self.variant = variant;
self
}
}
impl RenderOnce for Badge {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let (bg, fg) = match self.variant {
BadgeVariant::Default => (theme.primary, theme.primary_foreground),
BadgeVariant::Secondary => (theme.secondary, theme.secondary_foreground),
BadgeVariant::Outline => (gpui::transparent_black(), theme.foreground),
BadgeVariant::Destructive => (theme.destructive, theme.destructive_foreground),
};
div()
.flex()
.items_center()
.px(px(8.0))
.py(px(2.0))
.rounded_md()
.border_1()
.border_color(match self.variant {
BadgeVariant::Outline => theme.border,
_ => gpui::transparent_black(),
})
.bg(bg)
.text_color(fg)
.text_xs()
.font_weight(FontWeight::SEMIBOLD)
.child(self.label)
}
}