router kit

🚀 SwiftUI Navigation Router - A lightweight and flexible SwiftUI navigation manager, built with NavigationStack and supporting customizable routes. Easily manage your app's navigation flow by pushing, popping, and replacing views. Designed to work with Routable protocol for dynamic view generation!

9
1
Swift

RouterKit

SwiftUI Navigation Router


Key Features • How To Use • Credits • License • Changelog

Key Features

  • Protocol-Based Routing: Define your routes using the Routable protocol for flexible and reusable navigation.
  • Dynamic View Management: Push, pop, and replace views dynamically within the navigation stack.
  • Animated and Non-Animated Transitions: Supports both animated and non-animated route transitions.
  • Root View Replacement: Easily replace the root view and reset the navigation stack.
  • Navigation Stack Control: Navigate through a NavigationStack, fully integrated with SwiftUI’s native navigation system.
  • Optimized for SwiftUI: Built with SwiftUI best practices, ensuring smooth and native integration.

How To Use

To use this package, you can simply install using Swift Package Manager and following these steps:

  • Step 1: Define your app’s routes by conforming to the Routable protocol.
import SwiftUI

enum AppRoute: Routable {
    case login
    case home
    case profile(userID: String)
    case settings
    case detail(itemID: Int)
    
    // The builder property returns the corresponding view for each route.
    var view: any View {
        switch self {
            case .login: LoginView()
            case .home: HomeView()
            case .profile(let userID): ProfileView(userID: userID)
            case .settings: SettingsView()
            case .detail(let itemID): DetailView(itemID: itemID)
        }
    }
}
  • Step 2: Setup the RouterView in your main view.
import SwiftUI

struct ContentView: View {
    var body: some View {
        // The RouterView is initialized with the root view of the app.
        RouterView<AppRoute>(rootView: .home)
    }
}
  • Step 3: Use on each page you want.
import SwiftUI

// Import the RouterKit to use in your project
import RouterKit

struct HomeView: View {
    // Every view that was rendered by RouterView has a router inside
    @EnvironmentObject var router: Router<AppRoute>
    
    var body: some View {
        VStack {
            Button("Push view") {
                router.push(to: .profile(userID: '<user-id>'))
            }

            Button("Push view without animation") {
                router.push(to: .profile(userID: '<user-id>'), animate: false)
            }

            Button("Pop view") {
                router.pop()
            }

            Button("Pop to root view") {
                router.popToRoot()
            }

            Button("Replace root view to another") {
                router.replaceRootView(to: .settings)
            }
        }
    }
}

Emailware

This project is an emailware. Meaning, if you liked using this app or it has helped you in any way, I’d like you send me an email at [email protected] about anything you’d want to say about this software. I’d really appreciate it!

Credits

This package was created on Apple Developer Academy.

Support

You may also like…

  • Hub Finder - A social way to use Github
  • Bunnie - Never forget what anime you are watching

License

Apache


berkspar.com  · 
GitHub @berkspar  · 
LinkedIn in/berkspar  ·Â