frzi / swiftui-router

Path-based routing in SwiftUI
MIT License
919 stars 44 forks source link

View extension for `SwitchRoutes` #47

Closed hxperl closed 2 years ago

hxperl commented 2 years ago

Whenever I write View code that uses SwitchRoutes, it use a lot of brackets { } , which makes the code hard to read. For example when writing MainView, actual View code placed GeometryReader -> SwitchRoutes -> Route

struct MainView: View {
  var body: some View {
    GeometryReader { _ in
      SwitchRoutes {
        Route("child1") {
          ChildView()
        }

        Route("child2") {
          ChildView2()
        }

        Route {
          // main view code here
          VStack {
            Text("MainView")
          }
        }
      }
    }
  }
}

so that I added extension functions

struct MainView: View {
  var body: some View {
    VStack {
      Text("MainView")
    }
    .switchRoutes(
      content: {
        Route("child1") {
          ChildView()
        }

        Route("child2") {
          ChildView2()
        }
      }
    )
  }
}
frzi commented 2 years ago

Hi, thanks for the pull request.

Unfortunately this is a very user specific usecase of SwitchRoutes and therefore has no place in the package itself. It also forces a GeometryReader around the content of the Route. SwiftUI Router tries to make as little decisions for the developers as possible when it comes to layout et al.

Also, if you dislike the multitude use of brackets, Routes have an initializer with @autoclosure 😉

Route("news", content: NewsView())
Route("settings", content: SettingsView())