NavigationLink.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. // TODO: This documentation could probably be clarified a bit more (potentially with
  2. // some practical examples).
  3. /// A navigation primitive that appends a value to the current navigation path on click.
  4. ///
  5. /// Unlike Apple's SwiftUI API, a `NavigationLink` can be outside of a `NavigationStack`
  6. /// as long as they share the same `NavigationPath`.
  7. public struct NavigationLink: View {
  8. public var body: some View {
  9. Button(label) {
  10. path.wrappedValue.append(value)
  11. }
  12. }
  13. /// The label to display on the button.
  14. private let label: String
  15. /// The value to append to the navigation path when clicked.
  16. private let value: any Codable
  17. /// The navigation path to append to when clicked.
  18. private let path: Binding<NavigationPath>
  19. /// Creates a navigation link that presents the view corresponding to a value.
  20. /// The link is handled by whatever ``NavigationStack`` is sharing the same
  21. /// navigation path.
  22. ///
  23. /// - Parameters:
  24. /// - label: The label to display on the button.
  25. /// - value: The value to append to the navigation path when clicked.
  26. /// - path: The navigation path to append to when clicked.
  27. public init(_ label: String, value: some Codable, path: Binding<NavigationPath>) {
  28. self.label = label
  29. self.value = value
  30. self.path = path
  31. }
  32. }