Badge
A short status label in one of five tones, with an optional icon.
Loading the live example…
import (
"fmt"
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components"
"github.com/oneo-me/onevra/source/icons/lucide"
)
type statuses struct {
unread int
}
func (s *statuses) View(c *ui.Context) {
ui.Column(c).Gap(16).AlignItems(ui.Center).Children(func() {
ui.Row(c).Gap(8).Wrap().Justify(ui.Center).Children(func() {
components.Badge(c, "Draft")
components.Badge(c, "New", components.BadgeOptions{Tone: components.BadgeAccent})
components.Badge(c, "Passed", components.BadgeOptions{Tone: components.BadgeSuccess, Icon: lucide.Check()})
components.Badge(c, "Pending", components.BadgeOptions{Tone: components.BadgeWarning, Icon: lucide.Clock()})
components.Badge(c, "Failed", components.BadgeOptions{Tone: components.BadgeDanger, Icon: lucide.X()})
})
// A changed text morphs in place.
ui.Row(c).Gap(8).AlignItems(ui.Center).Children(func() {
components.Badge(c, fmt.Sprintf("%d unread", s.unread), components.BadgeOptions{Tone: components.BadgeAccent})
if components.Button(c, "New message", components.ButtonGhost).Clicked() {
s.unread++
}
})
})
}Badges state a status in a word or two. The icon is decorative, so the text must carry the status on its own. A changed text morphs in place.
API
Badge
func Badge(c *ui.Context, text string, options ...BadgeOptions) ui.ElementBadge creates a compact, non-interactive status label. Applications own its text and may override the inherited foreground and background colors.
Keep a badge built in the same place and change its text or icon to morph it: the new label rises in while the old one lifts away, icons crossfade and the pill's width springs to fit. Reduced Motion fades the swap in place and resizes at once. A badge appearing with its parent shows without animation.
pkg.go.devBadgeOptions
BadgeTone
type BadgeTone intBadgeTone selects a badge's status color.
const (
// BadgeNeutral is the bordered surface face.
BadgeNeutral BadgeTone = iota
// BadgeAccent tints the badge with the theme accent.
BadgeAccent
// BadgeSuccess tints the badge with the theme success color.
BadgeSuccess
// BadgeWarning tints the badge with the theme warning color.
BadgeWarning
// BadgeDanger tints the badge with the theme danger color.
BadgeDanger
)