Repository Public MIT
1# GlassToast23| | |4|---|---|5| Identifier | `glass-toast` |6| Module | `SPGlassComponents` |7| Kind | `custom-glass` — each toast is one `glassSurface`; toasts on screen share a `GlassGroup` |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Example | `Examples/Gallery/Feedback/GlassToastExample.swift` |1011## Minimal usage1213```swift14@State private var toasts = GlassToastCenter()1516content17 .glassToastHost(toasts)1819toasts.show("Link copied", systemImage: "doc.on.doc")20```2122## Variants2324| Variant | Use |25|---|---|26| `GlassToast("…")` | Message, dismissed after 4 seconds. |27| `title:`, `systemImage:` | Optional short title and decorative symbol. |28| `action: GlassToastAction("Undo") { … }` | One button; the default duration becomes 6 seconds. Activating it runs the handler and dismisses the toast. |29| `GlassToast.persistent("…")` | Stays until dismissed by the user or the host. |30| `id: "saved"` | Stable identity: showing the same `id` again replaces the toast in place and restarts its timer. |31| `placement: .top` / `.bottom` | Edge of the host, inside the safe area. |3233## Parameters3435`GlassToast`:3637| Parameter | Type | Default | Meaning |38|---|---|---|---|39| `message` | `String` | — | Text, already localised by the host. |40| `title` | `String?` | `nil` | Short title above the message. |41| `systemImage` | `String?` | `nil` | Decorative SF Symbol. |42| `id` | `String` | random UUID | Identity, used for replacement and dismissal. |43| `duration` | `Duration?` | 4 s, or 6 s with an action | Time on screen; raised to `GlassToast.minimumDuration` (2 s). |44| `action` | `GlassToastAction?` | `nil` | Optional button. |4546`GlassToastCenter(capacity:sleep:announce:)`:4748| Parameter | Default | Meaning |49|---|---|---|50| `capacity` | `3` | Largest number of toasts kept (at least 1). |51| `sleep` | `Task.sleep(for:)` | How a duration is awaited; injectable for tests. |52| `announce` | `AccessibilityNotification.Announcement(_:).post()` | How a new toast is announced. |5354Methods: `show(_:)`, `dismiss(_:)`, `dismissAll()`, `performAction(of:)`; `toasts` is observable.5556### Appearance5758The symbol sits in a small circular tile, the action is a compact capsule and the toast uses neutral glass (never the theme's glass tint) so that the message stays legible in every theme.5960## States6162- **Bounded queue**: when a toast arrives while the centre is full, the **oldest toast is dropped** (the newest event is the one worth showing) and its timer is cancelled.63- Each toast has its own cancellable timer, started when it is shown. Dismissing, replacing or dropping a toast cancels its timer, so a stale timer can never remove a newer toast with the same `id`.64- Nothing is persisted and the centre makes no network call.6566## Advanced example6768```swift69@Environment(GlassToastCenter.self) private var toasts // placed by glassToastHost7071func delete(_ message: Message) {72 store.delete(message)73 toasts.show(GlassToast(String(localized: "Message deleted"), systemImage: "trash", id: "deleted-\(message.id)",74 action: GlassToastAction(String(localized: "Undo")) { store.restore(message) }))75}76```7778## Accessibility7980- Every toast is announced when shown (title, then message) through `AccessibilityNotification.Announcement`, so VoiceOver users hear it without moving focus.81- The symbol is decorative and hidden; the action and dismiss buttons are labelled and keep the minimum hit target.82- With Reduce Motion toasts fade instead of sliding. With Reduce Transparency the surface is opaque.83- Durations are never shorter than 2 seconds and default to 4 or 6 seconds.8485## Limits8687- **A toast must never be the only carrier of critical information.** It disappears by itself and can be missed: errors that need a decision belong in a `GlassAlert` or a `GlassBanner`, and an undoable action must remain reversible elsewhere when feasible.88- Timers are not paused while VoiceOver reads the toast or while the pointer hovers it; use `persistent` for anything a user must be able to reach at their own pace.89- Attach `glassToastHost` once per scene to a view that respects the safe area; the overlay does not avoid the keyboard by itself beyond the host's own safe area.90- Timing, ordering, bounds, replacement and cancellation are unit-tested with an injected clock; the on-screen placement and the VoiceOver announcement were not exercised in a running application by the automated tests.91