PresentationKit is a SwiftUI library that makes it easy to present alerts, sheets, and full screen covers for any type, using observable AlertContext, FullScreenCoverContext, and SheetContext classes.
PresentationKit lets you register a presentation for any identifiable model, and will create and inject unique context values for each modal layer. This lets you use the current contexts to present new content from any view.
PresentationKit can be installed with the Swift Package Manager:
https://github.com/danielsaidi/PresentationKit.git
PresentationKit supports iOS 17, tvOS 17, macOS 14, watchOS 10, and visionOS 1.
PresentationKit makes it easy to present alerts, full screen covers, and sheets, using observable contexts with any kind of models that we want to present.
All we have to do to be able to present a model from anywhere within our app, is to apply a presentation(...) view modifier to the application root:
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.presentation(
for: MyModel.self,
alertContent: { value in
AlertContent(
title: "Alert",
actions: {
Button("OK", action: { print("OK for item #\(value.id)") })
Button("Cancel", role: .cancel, action: {})
},
message: { Text("Alert for item #\(value.id)") }
)
},
coverContent: {
MyModelView(value: $0, title: "Cover")
},
sheetContent: {
MyModelView(value: $0, title: "Sheet")
}
)
}
}
}You can omit any builder that you're not going to use. For instance, you don't have to define an alert content if you don't intend to present your model in an alert.
This will inject presentation contexts into the environment, that we can use to present our model from anywhere:
struct ContentView: View {
@Environment(AlertContext<Model>.self) private var alert
@Environment(FullScreenCoverContext<Model>.self) private var cover
@Environment(SheetContext<Model>.self) private var sheet
private let value = Model(id: 1)
var body: some View {
NavigationStack {
List {
Button("Present an alert") {
alert.present(value)
}
Button("Present a full screen cover") {
cover.present(value)
}
Button("Present a sheet") {
sheet.present(value)
}
}
.navigationTitle("Demo")
}
}
}PresentationKit will create and inject new contexts as we present modals. This means that we only have to add our presentation strategy once, except whein applying multiple presentations. See getting-started for more info.
PresentationKit also has an ErrorAlerter protocol that makes it easy to automatically present error alerts, and a NavigationContext that can be used to observe a navigation path.
For more information regarding more complex presentation strategies and functionality not covered by this guide, please see the getting-started guide.
The online documentation has more information, articles, code examples, etc.
The Demo folder has a demo app that lets you explore the library.
You can become a sponsor to help me dedicate more time on my various open-source tools. Every contribution, no matter the size, makes a real difference in keeping these tools free and actively developed.
Feel free to reach out if you have questions, or want to contribute in any way:
- Website: danielsaidi.com
- E-mail: daniel.saidi@gmail.com
- Bluesky: @danielsaidi@bsky.social
- Mastodon: @danielsaidi@mastodon.social
PresentationKit is available under the MIT license. See the LICENSE file for more info.