Modals and Popovers #52
-
Hi! First of all thanks! Great library :) I would like to know if there is support for popovers and modals. Kind regards, Arne |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello 👋 You can use struct ContentView: View {
@EnvironmentObject private var navigator: Navigator
@State private var showModal = false
var body: some View {
VStack {
NavLink(to: "/modal") {
Text("Go to `/modal`")
}
NavLink(to: "/modal/some/deeper/path") {
Text("Go to a deeper screen in the modal")
}
}
.onChange(of: navigator.path) { newPath in
showModal = newPath.starts(with: "/modal")
}
.onChange(of: showModal) { newShowModal in
if !newShowModal {
navigator.navigate("/")
}
}
.sheet(isPresented: $showModal) {
// Recommended: wrap the modal view in a `Route` to allow for relative paths
// *inside* the `ModalView`.
Route("modal/*", content: ModalView())
}
}
} Basically you check whether the current path is one that should display a sheet. Once the sheet is (manually) dismissed you programmatically navigate to another path. Mind you the code above is not fool proof and will require some tweaking to behave more flexible. But it should give you some idea of having SwiftUI Router work with modals/popovers/whatever. |
Beta Was this translation helpful? Give feedback.
Hello 👋
You can use
.sheet()
et al with SwiftUI Router. Though it does require a bit of code. There are multiple ways to tackle this. Here's a very crude example: