SPB Git

Repository Public MIT

doc-glass-alert.md 3.5 KB 80 lines markdown

1# GlassAlert23| | |4|---|---|5| Identifier | `glass-alert` |6| Module | `SPGlassComponents` |7| Kind | `native-wrapper` — the system `.alert` and `.confirmationDialog` |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Example | `Examples/Gallery/Feedback/GlassAlertExample.swift` |1011## Minimal usage1213```swift14@State private var alert: GlassAlert?1516content.glassAlert($alert)1718alert = GlassAlert(title: "Export finished")19```2021## Variants2223| Variant | Use |24|---|---|25| `style: .alert` (default) | Modal alert: information to acknowledge, or a decision with one or two choices. |26| `style: .confirmation` | Confirmation dialog attached to the view: confirming an action the user just started, or choosing among several actions. |27| `GlassAlertButton("Save") { … }` | Regular button. |28| `.destructive("Delete") { … }` | Destructive button. |29| `.cancel()` / `.cancel("Not now") { … }` | Cancel button; the default title is GlassKit's localised "Cancel". |3031## Parameters3233| Parameter | Type | Default | Meaning |34|---|---|---|---|35| `title` | `String` | — | Title, already localised by the host. |36| `message` | `String?` | `nil` | Explanation. |37| `style` | `GlassAlertStyle` | `.alert` | Presentation. |38| `buttons` | `[GlassAlertButton]` | `[]` | Buttons, validated before presentation. |39| `id` | `String` | random UUID | Identity. |4041## States4243The alert is presented while the bound value is non-`nil` and the binding is reset to `nil` on dismissal. Before presentation `GlassAlertValidator.validated(_:style:)` applies these rules:44451. Only the first cancel button keeps the cancel role; later ones become regular buttons (their actions are kept).462. An alert without buttons gets "OK"; a confirmation without buttons gets "Cancel".473. When a destructive button exists and no cancel button does, "Cancel" is added.484. Order: regular buttons, then destructive ones, then cancel.4950The default action (Return key) is the first regular button, otherwise cancel — never a destructive button (`GlassAlertValidator.defaultButtonIndex(in:)`).5152## Advanced example5354```swift55func confirmDeletion(of documents: [Document]) {56    alert = GlassAlert(57        title: String(localized: "Delete \(documents.count) documents?"),58        message: String(localized: "They will be removed from all your devices."),59        style: .confirmation,60        buttons: [61            .destructive(String(localized: "Delete")) { store.delete(documents) },62            GlassAlertButton(String(localized: "Move to archive")) { store.archive(documents) },63        ]   // "Cancel" is added by validation64    )65}66```6768## Accessibility6970- The system presents the alert: focus moves to it, VoiceOver reads title and message, and roles are announced by the system, not conveyed by colour alone.71- Escape and the cancel gesture trigger the cancel button, which validation guarantees whenever a destructive action is offered.72- Titles and messages are plain strings: Dynamic Type and wrapping are the system's.7374## Limits7576- Titles are `String`: localise them in the host (`String(localized:)`). Only the automatically added "OK" and "Cancel" come from GlassKit's resources.77- Alerts cannot contain text fields or custom views through this type; use the system `.alert` directly for that.78- The system decides the final visual order and emphasis of buttons on each platform; validation controls roles, presence and the order given to the system.79- Attach `.glassAlert` once per binding. Validation is unit-tested; the presentation was compiled for iOS and macOS but not exercised in a running application by the automated tests.80
Files main
Menu

Theme