SPB Git

Repository Public MIT

example-glass-toast.txt 1.6 KB 38 lines text

1import SwiftUI2import SPGlassKit34public struct GlassToastExample: GlassExample {5    public static let componentID = "glass-toast"6    @State private var toasts = GlassToastCenter(capacity: 3)7    @State private var savedCount = 08    @State private var restored = false910    public init() {}1112    public var body: some View {13        VStack(spacing: 16) {14            GlassButton("Save draft", systemImage: "tray.and.arrow.down") {15                savedCount += 116                // A stable identifier: saving twice replaces the toast instead of stacking a duplicate.17                toasts.show(GlassToast("Draft saved (\(savedCount))", systemImage: "checkmark.circle", id: "saved"))18            }19            GlassButton("Delete message", systemImage: "trash", role: .destructive) {20                restored = false21                toasts.show(GlassToast("Message deleted", systemImage: "trash", action: GlassToastAction("Undo") { restored = true }))22            }23            GlassButton("Show offline notice", systemImage: "wifi.slash") {24                toasts.show(.persistent("You are offline", title: "Connection", systemImage: "wifi.slash", id: "offline"))25            }26            GlassButton("Dismiss all") { toasts.dismissAll() }27            Text(restored ? "Message restored" : "\(toasts.toasts.count) toasts on screen")28                .font(.callout)29                .foregroundStyle(.secondary)30        }31        .padding()32        .frame(maxWidth: .infinity, maxHeight: .infinity)33        .glassToastHost(toasts)34    }35}3637#Preview("GlassToast") { GlassToastExample() }38
Files main
Menu

Theme