Skip to content

Sakshiing/NavigationStackCompat

Repository files navigation

NavigationStackCompat

A lightweight SwiftUI navigation package that brings NavigationStack-style routing to iOS 14 and iOS 15 while still using native NavigationStack on iOS 16+.


✨ Features

  • ✅ Supports iOS 14+
  • ✅ Native NavigationStack on iOS 16+
  • ✅ Automatic NavigationView fallback on iOS 14/15
  • ✅ Route-based navigation
  • ✅ Programmatic push/pop APIs
  • ✅ Pop to root
  • ✅ Deep-link path replacement
  • ✅ Mixed-type path navigation
  • ✅ Optional state restoration
  • ✅ SwiftUI-first API
  • ✅ No UIKit wrappers

📦 Requirements

Platform Version
iOS 14.0+
macOS 12.0+
tvOS 15.0+
watchOS 8.0+
  • Swift 5.9+
  • Xcode 15+

🚀 Installation

Swift Package Manager

Add the package dependency in Xcode:

dependencies: [
    .package(
        url: "https://github.com/Sakshiing/NavigationStackCompat.git",
        from: "1.0.0"
    )
]

⚡ Quick Start

import SwiftUI
import NavigationStackCompat

enum AppRoute: Hashable {
    case detail(id: Int)
    case settings
}

struct ContentView: View {

    @StateObject private var navigator = CompatNavigator<AppRoute>()

    var body: some View {

        CompatNavigationStack(navigator: navigator) {

            List {

                CompatNavigationLink(
                    "Open Detail",
                    value: AppRoute.detail(id: 42)
                )

                Button("Open Settings") {
                    navigator.push(.settings)
                }

                Button("Reset") {
                    navigator.popToRoot()
                }
            }

            .compatNavigationDestination(for: AppRoute.self) { route in

                switch route {

                case .detail(let id):
                    Text("Detail \(id)")

                case .settings:
                    Text("Settings")
                }
            }
        }
    }
}

🧭 Main APIs

navigator.push(.settings)

navigator.pop()

navigator.popToRoot()

navigator.replacePath(with: [
    .detail(id: 1),
    .settings
])

🔀 Mixed-Type Navigation

Use CompatPathNavigator when your navigation path contains multiple value types, similar to SwiftUI's NavigationPath.


🏗 Architecture

iOS 16+   → Native NavigationStack
iOS 14/15 → NavigationView + NavigationLink fallback

The navigator state stays synchronized with the visible navigation hierarchy while supporting:

  • system back button
  • swipe-to-go-back gestures
  • deep-link restoration
  • programmatic navigation

🧪 Testing

Run package tests:

swift test

📄 License

MIT License

See the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages