Skip to content

sunghyun-k/swiftui-toasts

Repository files navigation

Toasts

A toast notification library for SwiftUI.

Simulator Screen Recording - iPhone 15 Pro - 2024-09-16 at 11 53 37

Simulator Screen Recording - iPhone 16 Pro - 2024-09-18 at 10 53 57

SCR-20240916-kqog

Features

  • Easy-to-use toast notifications
  • Support for custom icons, messages, and buttons
  • Seamless integration with SwiftUI
  • Dark mode support
  • Slide gesture to dismiss
  • Loading state interface with async/await

Usage

  1. Install toast in your root view:
import SwiftUI
import Toasts

@main
struct MyApp: App {
  var body: some Scene {
    WindowGroup {
      ContentView()
        .installToast(position: .bottom)
    }
  }
}
  1. Present a toast:
@Environment(\.presentToast) var presentToast

Button("Show Toast") {
  let toast = ToastValue(
    icon: Image(systemName: "bell"),
    message: "You have a new notification."
  )
  presentToast(toast)
}

Advanced Usage

presentToast(
  message: "Loading...",
  task: {
    // Handle loading task
    return "Success"
  },
  onSuccess: { result in
    ToastValue(icon: Image(systemName: "checkmark.circle"), message: result)
  },
  onFailure: { error in
    ToastValue(icon: Image(systemName: "xmark.circle"), message: error.localizedDescription)
  }
)

Customization

image
  • Remove icon
let toast = ToastValue(
  message: "Message only toast."
)
  • Add button
let toast = ToastValue(
  message: "Toast with action required.",
  button: ToastButton(title: "Confirm", color: .green, action: {
    // Handle button action
  })
)

Requirements

  • iOS 14.0+
  • Swift 5.9+

About

A toast notification library for SwiftUI.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages