infomaniak/swift-modal-presentation
A simple library to safely present sheet and other modals in SwiftUI.
Usage
The library is meant to be a drop-in replacement for presenting sheets with @State or @Published. If you try to present a new sheet while an already existing one is presented, it will automatically close the previous sheet before showing the new one.
For @State with @ModalState
struct ContentView: View {
@ModalState private var isShowingSheet1 = false
@ModalState private var isShowingSheet2 = false
var body: some View {
VStack {
Button("Show sheet 1") {
isShowingSheet1 = true
}
}
.sheet(isPresented: $isShowingSheet1) {
VStack {
Text("Sheet 1")
Button("Show sheet 2") {
isShowingSheet2 = true
}
}
}
.sheet(isPresented: $isShowingSheet2) {
Text("Sheet 2")
}
}
}For @Published in an ObservableObject with @ModalPublished
class SomeObject: ObservableObject {
@ModalPublished var isShowingSheet1 = false
@ModalPublished var isShowingSheet2 = false
}
struct ContentView: View {
@StateObject private var someObject = SomeObject()
var body: some View {
VStack {
Button("Show sheet 1") {
someObject.isShowingSheet1 = true
}
}
.sheet(isPresented: $someObject.isShowingSheet1) {
VStack {
Text("Sheet 1")
Button("Show sheet 2") {
someObject.isShowingSheet2 = true
}
}
}
.sheet(isPresented: $someObject.isShowingSheet2) {
Text("Sheet 2")
}
}
}Optionally you can provide a context to the ModalState/Published(context: "SomeContextKey") to open nested sheets within the same view.
Licence
This package is available under the permissive ApacheV2 licence for you to enjoy.
Package Metadata
Repository: infomaniak/swift-modal-presentation
Default branch: main
README: README.md