SP GlassKit1.1.0

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.

GlassMetricCard running in SP Glass Studio.Capture
Capture of the component running in SP Glass Studio on the iOS 26.5 simulator. Liquid Glass is rendered by the system; this is an image, not a live preview.

Installation

spglass add glass-metric-card

Copies the sources and their dependencies into your project. Set up the CLI.

Swift
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")

import SPGlassKit
Identifierglass-metric-card
ModuleSPGlassData
Kindcontent-component — an opaque themed surface; no glass
PlatformsiOS 26, iPadOS 26, macOS 26
ExampleExamples/Gallery/Data/GlassMetricCardExample.swift

Minimal usage

Swift
GlassMetricCard("Response time", state: .loaded(GlassMetric(value: 182, unit: "ms")))

Variants

VariantUse
Value onlyGlassMetric(value:unit:).
With changechange 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: .neutralThe direction symbol keeps the secondary text colour (default).
tone: .positiveIsGood / .negativeIsGoodThe symbol takes the theme's success or destructive colour. The text keeps the primary text colour.

Parameters

ParameterTypeDefaultMeaning
titleLocalizedStringKey—Name of the figure.
stateGlassLoadState<GlassMetric>—The figure, or why there is none.
onRetry(@MainActor () -> Void)?nilAdds a retry button to the failed state.

GlassMetric:

FieldTypeDefaultMeaning
valueDouble—The figure. A non-finite value shows "—".
unitString?nilUnit, already localised.
changeDouble?nilRelative change as a fraction.
periodString?nilWhat the change is compared with, already localised.
fractionDigitsClosedRange<Int>0...2Fraction digits of the value.
toneGlassMetricTone.neutralWhether a rise is good or bad.

Eyebrow, chip and sparkline

ParameterTypeDefaultMeaning
systemImageString?nilDecorative 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

GlassLoadStateDisplay
.idleA dash; read as "No data".
.loadingA 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

Swift
@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 FormatStyle in the environment's locale.

Limits

  • Changes below 0.05 % display as 0.0 % and are therefore reported as "Unchanged".
  • change is a ratio; an absolute delta ("+12 ms") is not supported in this version — put it in period if 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:) with unit for the code, or prefer a custom view.
  • The card does not load anything: the host owns the state and the retry action.

Example

Complete example (compiled with the package)
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

Identifierglass-metric-card
Version1.1.0
Depends onglass-button, glass-components-support, glass-data-support, glass-foundation
FrameworksSwiftUI
Public symbolsGlassMetric, GlassMetricTone, GlassMetricFormatter, GlassMetricCard
LicenceMIT
Manifestmanifest.json

Files