SPB Git

Repository Public MIT

glass-metric-card.md 4.3 KB 99 lines markdown

1# GlassMetricCard23| | |4|---|---|5| Identifier | `glass-metric-card` |6| Module | `SPGlassData` |7| Kind | `content-component` — an opaque themed surface; no glass |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Example | `Examples/Gallery/Data/GlassMetricCardExample.swift` |1011## Minimal usage1213```swift14GlassMetricCard("Response time", state: .loaded(GlassMetric(value: 182, unit: "ms")))15```1617## Variants1819| Variant | Use |20|---|---|21| Value only | `GlassMetric(value:unit:)`. |22| 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". |23| `tone: .neutral` | The direction symbol keeps the secondary text colour (default). |24| `tone: .positiveIsGood` / `.negativeIsGood` | The symbol takes the theme's `success` or `destructive` colour. The text keeps the primary text colour. |2526## Parameters2728| Parameter | Type | Default | Meaning |29|---|---|---|---|30| `title` | `LocalizedStringKey` | — | Name of the figure. |31| `state` | `GlassLoadState<GlassMetric>` | — | The figure, or why there is none. |32| `onRetry` | `(@MainActor () -> Void)?` | `nil` | Adds a retry button to the failed state. |3334`GlassMetric`:3536| Field | Type | Default | Meaning |37|---|---|---|---|38| `value` | `Double` | — | The figure. A non-finite value shows "—". |39| `unit` | `String?` | `nil` | Unit, already localised. |40| `change` | `Double?` | `nil` | Relative change as a fraction. |41| `period` | `String?` | `nil` | What the change is compared with, already localised. |42| `fractionDigits` | `ClosedRange<Int>` | `0...2` | Fraction digits of the value. |43| `tone` | `GlassMetricTone` | `.neutral` | Whether a rise is good or bad. |4445### Eyebrow, chip and sparkline4647| Parameter | Type | Default | Meaning |48|---|---|---|---|49| `systemImage` | `String?` | `nil` | Decorative SF Symbol in the top trailing corner of the card. |50| `GlassMetric.sparkline` | `[Double]` | `[]` | Recent values, oldest first, drawn as a small line under the figure. Fewer than two finite values draw nothing. |5152The 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.5354## States5556| `GlassLoadState` | Display |57|---|---|58| `.idle` | A dash; read as "No data". |59| `.loading` | A redacted placeholder of the loaded layout; read as "Loading". |60| `.loaded(metric)` | Value, unit, change and period. |61| `.empty` | "No data". |62| `.failed(message:)` | A warning symbol labelled "Error", the message (or "Something went wrong" when empty) and the retry button if `onRetry` is set. |6364## Advanced example6566```swift67@State private var revenue: GlassLoadState<GlassMetric> = .loading6869GlassMetricCard("Monthly revenue", state: revenue) {70    Task { await reload() }71}72.task { await reload() }7374func reload() async {75    revenue = .loading76    do {77        let report = try await service.revenue()78        revenue = .loaded(GlassMetric(value: report.total, unit: "$", change: report.change, period: "vs last month", fractionDigits: 2...2, tone: .positiveIsGood))79    } catch {80        revenue = .failed(message: "The report could not be loaded.")81    }82}83```8485## Accessibility8687- A loaded card is one element: title aside, VoiceOver reads the value, the unit and "Up 4.2 %, vs last month".88- The direction is carried by the symbol's shape and by a word; colour (`tone`) is an addition, never the only signal.89- Loading and missing states have explicit labels instead of a silent placeholder.90- Text follows Dynamic Type and wraps; the value uses monospaced digits.91- Values and percentages are formatted with `FormatStyle` in the environment's locale.9293## Limits9495- Changes below 0.05 % display as 0.0 % and are therefore reported as "Unchanged".96- `change` is a ratio; an absolute delta ("+12 ms") is not supported in this version — put it in `period` if needed.97- 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.98- The card does not load anything: the host owns the state and the retry action.99
Files main
Menu

Theme