SPB Git

Repository Public MIT

glass-alert.md 3.5 KB

# GlassAlert

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

swift
@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:

  1. Only the first cancel button keeps the cancel role; later ones become regular buttons (their actions are kept).
  2. An alert without buttons gets "OK"; a confirmation without buttons gets "Cancel".
  3. When a destructive button exists and no cancel button does, "Cancel" is added.
  4. 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

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

Theme