GlassAlert
System alert and confirmation dialog described by a value with correct button roles.
- System control
- iOS 26 · iPadOS 26 · macOS 26
- Version 1.1.0
glass-alert
Drawn by the system. GlassKit configures it and leaves the glass to the system.
CaptureInstallation
spglass add glass-alertCopies 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-alert |
|---|---|
| Module | SPGlassComponents |
| Kind | native-wrapper — the system .alert and .confirmationDialog |
| Platforms | iOS 26, iPadOS 26, macOS 26 |
| Example | Examples/Gallery/Feedback/GlassAlertExample.swift |
Minimal usage
@State private var alert: GlassAlert?
content.glassAlert($alert)
alert = GlassAlert(title: "Export finished")Variants
| Variant | Use |
|---|---|
style: .alert (default) | Modal alert: information to acknowledge, or a decision with one or two choices. |
style: .confirmation | Confirmation dialog attached to the view: confirming an action the user just started, or choosing among several actions. |
GlassAlertButton("Save") { … } | Regular button. |
.destructive("Delete") { … } | Destructive button. |
.cancel() / .cancel("Not now") { … } | Cancel button; the default title is GlassKit's localised "Cancel". |
Parameters
| Parameter | Type | Default | Meaning |
|---|---|---|---|
title | String | — | Title, already localised by the host. |
message | String? | nil | Explanation. |
style | GlassAlertStyle | .alert | Presentation. |
buttons | [GlassAlertButton] | [] | Buttons, validated before presentation. |
id | String | random UUID | Identity. |
States
The 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:
- Only the first cancel button keeps the cancel role; later ones become regular buttons (their actions are kept).
- An alert without buttons gets "OK"; a confirmation without buttons gets "Cancel".
- When a destructive button exists and no cancel button does, "Cancel" is added.
- Order: regular buttons, then destructive ones, then cancel.
The default action (Return key) is the first regular button, otherwise cancel — never a destructive button (GlassAlertValidator.defaultButtonIndex(in:)).
Advanced example
func confirmDeletion(of documents: [Document]) {
alert = GlassAlert(
title: String(localized: "Delete \(documents.count) documents?"),
message: String(localized: "They will be removed from all your devices."),
style: .confirmation,
buttons: [
.destructive(String(localized: "Delete")) { store.delete(documents) },
GlassAlertButton(String(localized: "Move to archive")) { store.archive(documents) },
] // "Cancel" is added by validation
)
}Accessibility
- 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.
- Escape and the cancel gesture trigger the cancel button, which validation guarantees whenever a destructive action is offered.
- Titles and messages are plain strings: Dynamic Type and wrapping are the system's.
Limits
- Titles are
String: localise them in the host (String(localized:)). Only the automatically added "OK" and "Cancel" come from GlassKit's resources. - Alerts cannot contain text fields or custom views through this type; use the system
.alertdirectly for that. - 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.
- Attach
.glassAlertonce 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.
Example
import SwiftUI
import SPGlassKit
public struct GlassAlertExample: GlassExample {
public static let componentID = "glass-alert"
@State private var alert: GlassAlert?
@State private var outcome = "Nothing chosen yet"
public init() {}
public var body: some View {
VStack(spacing: 16) {
GlassButton("Show information") {
alert = GlassAlert(title: "Export finished", message: "The sample file was written to Documents.")
}
GlassButton("Unsaved changes…") {
alert = GlassAlert(
title: "Save changes?",
message: "Your edits to “Budget 2027” will be lost otherwise.",
buttons: [
GlassAlertButton("Save") { outcome = "Saved" },
.destructive("Discard") { outcome = "Discarded" },
.cancel { outcome = "Cancelled" },
]
)
}
GlassButton("Delete draft…", systemImage: "trash", role: .destructive) {
// No cancel button is given: validation adds one because the only action is destructive.
alert = GlassAlert(
title: "Delete this draft?",
message: "This cannot be undone.",
style: .confirmation,
buttons: [.destructive("Delete draft") { outcome = "Deleted" }]
)
}
Text(outcome).font(.callout).foregroundStyle(.secondary)
}
.padding()
.glassAlert($alert)
}
}
#Preview("GlassAlert") { GlassAlertExample() }
Registry entry
| Identifier | glass-alert |
|---|---|
| Version | 1.1.0 |
| Depends on | glass-foundation |
| Frameworks | SwiftUI |
| Public symbols | GlassAlertButtonRole, GlassAlertButton, GlassAlertStyle, GlassAlert, GlassAlertValidator |
| Licence | MIT |
| Manifest | manifest.json |
Files
Components/Feedback/GlassAlert.swift9366 bytes · sha256 dfdb049c90ed…