SPB Git

Repository Public MIT

doc-glass-metric-card.md 3.6 KB

# GlassMetricCard

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

swift
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.

# 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

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.
Files main
Menu

Theme