SPB Git

Repository Public MIT

doc-glass-badge.md 3.0 KB

# GlassBadge

Identifier glass-badge
Module SPGlassComponents
Kind content-component — solid fill, no glass
Platforms iOS 26, iPadOS 26, macOS 26
Example Examples/Gallery/Feedback/GlassBadgeExample.swift

# Minimal usage

swift
GlassBadge(count: unreadCount)

# Variants

Variant Appearance
GlassBadge(count:cap:showsZero:tone:) Number in a capsule; above cap it reads "99+"; hidden at zero unless showsZero.
GlassBadge("New", tone:) Short value in a capsule.
GlassBadge(status: "Online", tone:) Symbol in the tone's colour followed by the status name.

Tones: .neutral, .accent, .success, .warning, .critical. Each status tone has a symbol of a different shape (circle, info.circle, checkmark.circle, exclamationmark.triangle, xmark.octagon).

# Parameters

Parameter Type Default Meaning
count Int — Number of items; negative values count as zero.
cap Int 99 Largest number shown; a cap below 1 counts as 1.
showsZero Bool false Shows "0" instead of hiding the badge.
value String — Short text; an empty string hides the badge.
status String — Status name, already localised by the host.
showsLabel Bool true false shows the status symbol alone.
tone GlassBadgeTone .critical for counts, .neutral for values Meaning and colour.

GlassBadgeFormatter.countText(_:cap:showsZero:locale:) and countAccessibilityLabel(_:cap:) are pure and public.

# States

Hidden (count of zero, empty value) or visible. No interaction.

# Advanced example

swift
HStack {
    Label("Inbox", systemImage: "tray")
    Spacer()
    GlassBadge(count: model.unread, cap: 999)
        .accessibilityLabel("\(model.unread) unread messages")
}

GlassBadge(status: server.isHealthy ? "Operational" : "Outage", tone: server.isHealthy ? .success : .critical)

# Accessibility

  • A count is read as "7 items", or "More than 99" above the cap; replace it with .accessibilityLabel when the unit matters ("7 unread messages").
  • A status badge is read by its label; the symbol is hidden from VoiceOver. With showsLabel: false the label remains the accessibility label and the symbol shape still identifies the tone.
  • Text on a coloured fill uses theme.onColor, the more legible of white and near-black for that fill. Digits follow the locale.
  • The caption font follows Dynamic Type.

# Limits

  • Never rely on the tone's colour for meaning: give a status badge a label, and a value badge a self-explanatory text.
  • With the System theme (roles left to the system) a toned capsule uses a tinted surface with the primary text colour instead of a solid fill, because the contrast of white text on a system colour cannot be measured.
  • A badge is not a button. Do not use it for long text: it does not wrap.
  • For tab bars and list rows prefer the system .badge(_:) modifier.
Files main
Menu

Theme