Navigation framework for SwiftUI applications. It handles most of navigation operations in one common way similar to NavigationStack. Presentation of sheets, alerts, submodules could be done through the path.
Originally published at xymatic-public
- Declarative Navigation: Define navigation paths and flows in a clear, declarative way.
- Decoupled: Separates navigation logic from your views, improving modularity and testability.
- Child modules: Navigation could show another navigation module with the different route type.
You can add PathNavigation to your project using Swift Package Manager. In your Package.swift file, add the following dependency:
dependencies: [
.package(url: "https://github.com/sclown/PathNavigation.git", from: "0.2.0")
]- Define your navigation routes:
enum AppRoute: Hashable {
case root
case next
case dismiss
}- Define destinations for routes:
@ViewBuilder
func destination(_ path: AppRoute?) -> some View {
switch path {
case .root: HomeView()
case .next: DetailView()
default: EmptyView()
}
}- Define navigation actions:
func route(_ route: AppRoute) {
switch route {
case .root: break
case .next: navigation.present(.next, transition: .sheet)
case .dismiss: navigation.dismiss()
}
}- Create a navigation view model:
let navigation = NavigationViewModel(root: AppRoute.root)- Use PathNavigationView:
struct ContentView: View {
var body: some View {
PathNavigationView(
viewModel: navigation,
destinations: { destination($0) }
)
}
}- Apply navigation in views:
Button("Push") { route(.next) }Check Example project for more details.
Framework comes with supporting modules for handling navigation cases like alerts or pickers. One shows the screen for some input and need results from it. It very convenient to have it as an async function or Combine's Publisher.
PNInputRequestCombine: provides solution with the dependency on CombinePNInputRequest: provides solution without combine, but requires iOS 18
Contributions are welcome! Please feel free to open an issue or submit a pull request.
This project is licensed under the MIT License.