SPB Git

Repository Public MIT

doc-glass-toast.md 4.1 KB

# GlassToast

Identifier glass-toast
Module SPGlassComponents
Kind custom-glass — each toast is one glassSurface; toasts on screen share a GlassGroup
Platforms iOS 26, iPadOS 26, macOS 26
Example Examples/Gallery/Feedback/GlassToastExample.swift

# Minimal usage

swift
@State private var toasts = GlassToastCenter()

content
    .glassToastHost(toasts)

toasts.show("Link copied", systemImage: "doc.on.doc")

# Variants

Variant Use
GlassToast("…") Message, dismissed after 4 seconds.
title:, systemImage: Optional short title and decorative symbol.
action: GlassToastAction("Undo") { … } One button; the default duration becomes 6 seconds. Activating it runs the handler and dismisses the toast.
GlassToast.persistent("…") Stays until dismissed by the user or the host.
id: "saved" Stable identity: showing the same id again replaces the toast in place and restarts its timer.
placement: .top / .bottom Edge of the host, inside the safe area.

# Parameters

GlassToast:

Parameter Type Default Meaning
message String — Text, already localised by the host.
title String? nil Short title above the message.
systemImage String? nil Decorative SF Symbol.
id String random UUID Identity, used for replacement and dismissal.
duration Duration? 4 s, or 6 s with an action Time on screen; raised to GlassToast.minimumDuration (2 s).
action GlassToastAction? nil Optional button.

GlassToastCenter(capacity:sleep:announce:):

Parameter Default Meaning
capacity 3 Largest number of toasts kept (at least 1).
sleep Task.sleep(for:) How a duration is awaited; injectable for tests.
announce AccessibilityNotification.Announcement(_:).post() How a new toast is announced.

Methods: show(_:), dismiss(_:), dismissAll(), performAction(of:); toasts is observable.

# States

  • 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.
  • 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.
  • Nothing is persisted and the centre makes no network call.

# Advanced example

swift
@Environment(GlassToastCenter.self) private var toasts   // placed by glassToastHost

func delete(_ message: Message) {
    store.delete(message)
    toasts.show(GlassToast(String(localized: "Message deleted"), systemImage: "trash", id: "deleted-\(message.id)",
                           action: GlassToastAction(String(localized: "Undo")) { store.restore(message) }))
}

# Accessibility

  • Every toast is announced when shown (title, then message) through AccessibilityNotification.Announcement, so VoiceOver users hear it without moving focus.
  • The symbol is decorative and hidden; the action and dismiss buttons are labelled and keep the minimum hit target.
  • With Reduce Motion toasts fade instead of sliding. With Reduce Transparency the surface is opaque.
  • Durations are never shorter than 2 seconds and default to 4 or 6 seconds.

# Limits

  • 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.
  • 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.
  • 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.
  • 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.
Files main
Menu

Theme