ModalPresentationKit is a lightweight SwiftUI library that simplifies modal presentations using a presenter pattern. Manage sheets and full-screen covers effortlessly.
Right now two presentation styles are available:
- Full Screen Cover
- Sheet
- Easy Modal Management: Present and dismiss modals with minimal code.
- Data Passing Support: Pass data to your modals seamlessly.
- Simple Integration: Add to your project and start using immediately.
- Example App Available: An example app is included in the package.
- In Xcode, go to File > Add Packages...
- Enter the repository URL:
https://github.com/yourusername/ModalPresentationKit.git
- Select the package and add it to your project.
Create an enum conforming to ModalDestination
, where each case represents a modal view.
import SwiftUI
import ModalPresentationKit
enum ModalDestinations: ModalDestination {
case exampleView
case detailView(data: String) // For passing data
var body: some View {
switch self {
case .exampleView:
ExampleView()
case .detailView(let data):
DetailView(data: data)
}
}
}
Set up ModalPresenter
in your App
or main view.
import SwiftUI
import ModalPresentationKit
@main
struct YourApp: App {
@StateObject var presenter = ModalPresenter<ModalDestinations>()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(presenter)
.modalPresenter(presenter)
}
}
}
Use the presenter to show modals from your views.
import SwiftUI
import ModalPresentationKit
struct ContentView: View {
@EnvironmentObject var presenter: ModalPresenter<ModalDestinations>
var body: some View {
VStack {
Button("Show Example View") {
presenter.present(destination: .exampleView, style: .sheet)
}
Button("Show Detail View with Data") {
presenter.present(destination: .detailView(data: "Hello, World!"), style: .fullScreenCover)
}
}
}
}
Inside your modal views, call presenter.dismiss()
to close the modal.
import SwiftUI
import ModalPresentationKit
struct ExampleView: View {
@EnvironmentObject var presenter: ModalPresenter<ModalDestinations>
var body: some View {
VStack {
Text("Example View")
Button("Dismiss") {
presenter.dismiss()
}
}
}
}
To pass data, use associated values in your enum and access them in your modal view.
// In ModalDestinations enum
case detailView(data: String)
// In DetailView
struct DetailView: View {
let data: String
var body: some View {
Text(data)
}
}
An ExampleApp demonstrating how to use ModalPresentationKit is included in the package.