Skip to content

theosementa/ToastBannerKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ToastBannerKit

A Swift package for easily displaying toast banners in SwiftUI


Features

  • Send toast banner
  • Send toast banner in dynamic island
  • Add examples

How to use?

import ToastBannerKit

Conform an enum to ToastBannerStyleProtocol

enum ToastBannerStyle: String, ToastBannerStyleProtocol {
    case error
    case success
}

Create a ToastBanner model

extension ToastBannerUIModel {
    static let successBanner: ToastBannerUIModel = .init(
        title: "This is a success banner",
        description: "This is a description for my toast banner.",
        style: ToastBannerStyle.success
    )
}

Create an UI for the banner

struct ToastBannerView: View {
    
    // MARK: Dependencies
    let toastBanner: ToastBannerUIModel?
    
    // MARK: - View
    var body: some View {
        if let toastBanner {
            VStack(alignment: .leading, spacing: 8) {
                Text(toastBanner.title)
                
                if let description = toastBanner.description {
                    Text(description)
                        .multilineTextAlignment(.leading)
                }
            }
            .padding()
            .frame(maxWidth: .infinity)
            .background(toastBanner.style.rawValue == ToastBannerStyle.success.rawValue ? Color.green : Color.red)
        }
    }
}

Add the modifier .toastBanner at the top of your app

@main
struct ToastBannerKit_DemoApp: App {
    
    @State private var toastBannerService: ToastBannerService = .shared
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(toastBannerService)
                .toastBanner(item: $toastBannerService.toastBanner) { toastBanner in
                    ToastBannerView(toastBanner: toastBanner)
                }
        }
    }
}

Send a banner

Button {
    toastBannerService.send(.successBanner)
} label: {
    Text("Send a banner")
}

Credits

Handmade by Théo Sementa

About

A Swift package for easily displaying toast banners in SwiftUI

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages