From 96b1e80d2548ea6245de7b990cf7c7db4883ac3f Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Wed, 11 Dec 2024 11:25:43 -0500 Subject: [PATCH] Remove `phx-replace` and replace with `data-phx-link-state` --- CHANGELOG.md | 2 +- .../NavigationLink.swift | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31b88623f..98beaba97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - LiveViewNative.SwiftUI.Client -- `NavigationLink` can perform a replace navigation by adding the `phx-replace` attribute +- `NavigationLink` can perform a replace navigation by setting the `data-phx-link-state` attribute to `replace` - `NavigationLink` takes a `destination` template to customize the connecting phase View for its navigation event. ### Changed diff --git a/Sources/LiveViewNative/Views/Layout Containers/Presentation Containers/NavigationLink.swift b/Sources/LiveViewNative/Views/Layout Containers/Presentation Containers/NavigationLink.swift index 91cc1acda..c814d700d 100644 --- a/Sources/LiveViewNative/Views/Layout Containers/Presentation Containers/NavigationLink.swift +++ b/Sources/LiveViewNative/Views/Layout Containers/Presentation Containers/NavigationLink.swift @@ -21,11 +21,11 @@ private let logger = Logger(subsystem: "LiveViewNative", category: "NavigationLi /// /// ``` /// -/// Use the `phx-replace` attribute to do a replace navigation instead of a push. +/// Use the `data-phx-link-state` attribute to do a `replace` navigation instead of a `push`. /// This will replace the current route with the destination. /// /// ```html -/// +/// /// More Information /// /// ``` @@ -57,8 +57,13 @@ struct NavigationLink: View { @_documentation(visibility: public) private var destination: String? - @LiveAttribute("phx-replace") - private var replace: Bool = false + @LiveAttribute("data-phx-link-state") + private var linkState: LinkState = .push + + enum LinkState: String, AttributeDecodable { + case push + case replace + } @LiveElementIgnored @Environment(\._anyNavigationTransition) @@ -82,7 +87,8 @@ struct NavigationLink: View { } else { nil } - if replace { + switch linkState { + case .replace: SwiftUI.Button { Task { @MainActor in try await $liveElement.context.coordinator.session.redirect( @@ -98,7 +104,7 @@ struct NavigationLink: View { } label: { $liveElement.children() } - } else { + case .push: SwiftUI.NavigationLink( value: LiveNavigationEntry( url: url,