GlassMetricCard
A value with unit, change and period, and states for missing, loading and failed data.
- Content
- iOS 26 · iPadOS 26 · macOS 26
- Version 1.1.0
glass-metric-card
Content on an opaque, readable surface. Glass is kept for controls.
CaptureInstallation
spglass add glass-metric-cardCopies the sources and their dependencies into your project. Set up the CLI.
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")
import SPGlassKit| Identifier | glass-metric-card |
|---|---|
| Module | SPGlassData |
| Kind | content-component — an opaque themed surface; no glass |
| Platforms | iOS 26, iPadOS 26, macOS 26 |
| Example | Examples/Gallery/Data/GlassMetricCardExample.swift |
Minimal usage
GlassMetricCard("Response time", state: .loaded(GlassMetric(value: 182, unit: "ms")))Variants
| Variant | Use |
|---|---|
| Value only | GlassMetric(value:unit:). |
| With change | change is a fraction (0.042 = 4.2 %). It is shown as a direction symbol, a direction word and an unsigned percentage: "↗ Up 4.2 %, vs last month". |
tone: .neutral | The direction symbol keeps the secondary text colour (default). |
tone: .positiveIsGood / .negativeIsGood | The symbol takes the theme's success or destructive colour. The text keeps the primary text colour. |
Parameters
| Parameter | Type | Default | Meaning |
|---|---|---|---|
title | LocalizedStringKey | — | Name of the figure. |
state | GlassLoadState<GlassMetric> | — | The figure, or why there is none. |
onRetry | (@MainActor () -> Void)? | nil | Adds a retry button to the failed state. |
GlassMetric:
| Field | Type | Default | Meaning |
|---|---|---|---|
value | Double | — | The figure. A non-finite value shows "—". |
unit | String? | nil | Unit, already localised. |
change | Double? | nil | Relative change as a fraction. |
period | String? | nil | What the change is compared with, already localised. |
fractionDigits | ClosedRange<Int> | 0...2 | Fraction digits of the value. |
tone | GlassMetricTone | .neutral | Whether a rise is good or bad. |
Eyebrow, chip and sparkline
| Parameter | Type | Default | Meaning |
|---|---|---|---|
systemImage | String? | nil | Decorative SF Symbol in the top trailing corner of the card. |
GlassMetric.sparkline | [Double] | [] | Recent values, oldest first, drawn as a small line under the figure. Fewer than two finite values draw nothing. |
The title is an uppercase eyebrow label above a large tabular figure. The change is a chip — direction symbol and magnitude, tinted by the metric's tone — followed by the period; the chip's accessibility label keeps the direction word ("Down 6 %"). GlassMetricFormatter.sparklinePoints(_:) is pure and public.
States
GlassLoadState | Display |
|---|---|
.idle | A dash; read as "No data". |
.loading | A redacted placeholder of the loaded layout; read as "Loading". |
.loaded(metric) | Value, unit, change and period. |
.empty | "No data". |
.failed(message:) | A warning symbol labelled "Error", the message (or "Something went wrong" when empty) and the retry button if onRetry is set. |
Advanced example
@State private var revenue: GlassLoadState<GlassMetric> = .loading
GlassMetricCard("Monthly revenue", state: revenue) {
Task { await reload() }
}
.task { await reload() }
func reload() async {
revenue = .loading
do {
let report = try await service.revenue()
revenue = .loaded(GlassMetric(value: report.total, unit: "$", change: report.change, period: "vs last month", fractionDigits: 2...2, tone: .positiveIsGood))
} catch {
revenue = .failed(message: "The report could not be loaded.")
}
}Accessibility
- A loaded card is one element: title aside, VoiceOver reads the value, the unit and "Up 4.2 %, vs last month".
- The direction is carried by the symbol's shape and by a word; colour (
tone) is an addition, never the only signal. - Loading and missing states have explicit labels instead of a silent placeholder.
- Text follows Dynamic Type and wraps; the value uses monospaced digits.
- Values and percentages are formatted with
FormatStylein the environment's locale.
Limits
- Changes below 0.05 % display as 0.0 % and are therefore reported as "Unchanged".
changeis a ratio; an absolute delta ("+12 ms") is not supported in this version — put it inperiodif needed.- The unit is plain text appended after the value; currency placement rules of the locale are not applied. Format a currency yourself and use
GlassMetric(value:)withunitfor the code, or prefer a custom view. - The card does not load anything: the host owns the state and the retry action.
Example
import SwiftUI
import SPGlassKit
public struct GlassMetricCardExample: GlassExample {
public static let componentID = "glass-metric-card"
// Fictitious figures.
@State private var latency: GlassLoadState<GlassMetric> = .loaded(
GlassMetric(value: 182, unit: "ms", change: -0.062, period: "vs last week", tone: .negativeIsGood)
)
public init() {}
public var body: some View {
ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 220), spacing: 12)], spacing: 12) {
GlassMetricCard("Response time", state: latency) {
latency = .loaded(GlassMetric(value: 175, unit: "ms", change: -0.098, period: "vs last week", tone: .negativeIsGood))
}
GlassMetricCard("Monthly revenue", state: .loaded(
GlassMetric(value: 48_250.5, unit: "$", change: 0.042, period: "vs last month", fractionDigits: 2...2, tone: .positiveIsGood)
))
GlassMetricCard("Bookings", systemImage: "calendar", state: .loaded(
GlassMetric(value: 1284, change: 0.082, period: "vs last week", tone: .positiveIsGood, sparkline: [31, 34, 33, 38, 41, 40, 46])
))
GlassMetricCard("Open tickets", state: .loaded(GlassMetric(value: 37, change: 0, period: "since yesterday")))
GlassMetricCard("Active sessions", state: .loading)
GlassMetricCard("Conversion", state: .empty)
GlassMetricCard("Forecast", state: .idle)
GlassMetricCard("Churn", state: .failed(message: "The report could not be loaded.")) {}
}
.padding()
HStack {
GlassButton("Simulate a failure", size: .small) { latency = .failed(message: "The probe did not answer.") }
GlassButton("Reload", size: .small) { latency = .loading }
}
.padding(.bottom)
}
}
}
#Preview("GlassMetricCard") { GlassMetricCardExample().glassTheme(.emerald) }
Registry entry
| Identifier | glass-metric-card |
|---|---|
| Version | 1.1.0 |
| Depends on | glass-button, glass-components-support, glass-data-support, glass-foundation |
| Frameworks | SwiftUI |
| Public symbols | GlassMetric, GlassMetricTone, GlassMetricFormatter, GlassMetricCard |
| Licence | MIT |
| Manifest | manifest.json |
Files
Data/Data/GlassMetricCard.swift16812 bytes · sha256 0f475f61957a…