From 2ec6c3a15293efff6083966b38439a4004f25565 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Fri, 5 Apr 2024 10:48:55 -0700 Subject: [PATCH] Add iOS 16-compatible `navigationDestination(item:)` to core (#148) * Add iOS 16-compatible `navigationDestination(item:)` * wip --- .../Documentation.docc/Articles/Bindings.md | 1 - .../SwiftUINavigationCore.md | 18 ++++++++++++- .../NavigationDestination.swift | 25 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 Sources/SwiftUINavigationCore/NavigationDestination.swift diff --git a/Sources/SwiftUINavigation/Documentation.docc/Articles/Bindings.md b/Sources/SwiftUINavigation/Documentation.docc/Articles/Bindings.md index 5dae2c5dfd..07dd305f3c 100644 --- a/Sources/SwiftUINavigation/Documentation.docc/Articles/Bindings.md +++ b/Sources/SwiftUINavigation/Documentation.docc/Articles/Bindings.md @@ -78,7 +78,6 @@ struct SignInView: View { ### Binding transformations -- ``SwiftUI/Binding/isPresent()`` - ``SwiftUI/Binding/removeDuplicates()`` - ``SwiftUI/Binding/removeDuplicates(by:)`` diff --git a/Sources/SwiftUINavigationCore/Documentation.docc/SwiftUINavigationCore.md b/Sources/SwiftUINavigationCore/Documentation.docc/SwiftUINavigationCore.md index 9807e67d2d..ae7be6d215 100644 --- a/Sources/SwiftUINavigationCore/Documentation.docc/SwiftUINavigationCore.md +++ b/Sources/SwiftUINavigationCore/Documentation.docc/SwiftUINavigationCore.md @@ -1,6 +1,6 @@ # ``SwiftUINavigationCore`` -A few core types included in SwiftUI Navigation. +A few core types and modifiers included in SwiftUI Navigation. ## Topics @@ -10,3 +10,19 @@ A few core types included in SwiftUI Navigation. - ``AlertState`` - ``ConfirmationDialogState`` - ``ButtonState`` + +### Alert and dialog modifiers + +- ``SwiftUI/View/alert(item:title:actions:message:)`` +- ``SwiftUI/View/alert(item:title:actions:)`` +- ``SwiftUI/View/confirmationDialog(item:titleVisibility:title:actions:message:)`` +- ``SwiftUI/View/confirmationDialog(item:titleVisibility:title:actions:)`` + +### Bindings + +- ``SwiftUI/Binding/isPresent()`` +- ``SwiftUI/View/bind(_:to:)`` + +### Navigation + +- ``SwiftUI/View/navigationDestination(item:destination:)`` diff --git a/Sources/SwiftUINavigationCore/NavigationDestination.swift b/Sources/SwiftUINavigationCore/NavigationDestination.swift new file mode 100644 index 0000000000..9b593da463 --- /dev/null +++ b/Sources/SwiftUINavigationCore/NavigationDestination.swift @@ -0,0 +1,25 @@ +#if canImport(SwiftUI) + import SwiftUI + + @available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) + extension View { + /// Associates a destination view with a bound value for use within a navigation stack or + /// navigation split view. + /// + /// See `SwiftUI.View.navigationDestination(item:destination:)` for more information. + /// + /// - Parameters: + /// - item: A binding to the data presented, or `nil` if nothing is currently presented. + /// - destination: A view builder that defines a view to display when `item` is not `nil`. + public func navigationDestination( + item: Binding, + @ViewBuilder destination: @escaping (D) -> C + ) -> some View { + navigationDestination(isPresented: item.isPresent()) { + if let item = item.wrappedValue { + destination(item) + } + } + } + } +#endif // canImport(SwiftUI)