Skip to content

Commit 562c935

Browse files
committed
Simplify NavigationLink to use only 'href'
1 parent cf5b7eb commit 562c935

File tree

1 file changed

+9
-47
lines changed

1 file changed

+9
-47
lines changed

Sources/LiveViewNative/Views/Layout Containers/Presentation Containers/NavigationLink.swift

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,56 +21,18 @@ struct NavigationLink<R: CustomRegistry>: View {
2121

2222
@ViewBuilder
2323
public var body: some View {
24-
if let linkOpts = LinkOptions(element: element),
25-
context.coordinator.session.config.navigationMode.supportsLinkState(linkOpts.state),
26-
let url = linkOpts.url(in: context)
27-
{
28-
SwiftUI.NavigationLink(value: LiveNavigationEntry(url: url, coordinator: context.coordinator)) {
24+
if let href = element.attributeValue(for: "href").flatMap({
25+
URL(string: $0, relativeTo: context.coordinator.url)?.appending(path: "").absoluteURL
26+
}) {
27+
SwiftUI.NavigationLink(
28+
value: LiveNavigationEntry(
29+
url: href,
30+
coordinator: context.coordinator
31+
)
32+
) {
2933
context.buildChildren(of: element)
3034
}
3135
.disabled(element.attribute(named: "disabled") != nil)
32-
} else {
33-
// if there are no link options, or the coordinator doesn't support the required navigation, we don't show anything
34-
}
35-
}
36-
}
37-
38-
struct LinkOptions {
39-
let kind: LinkKind
40-
let state: LiveRedirect.Kind
41-
let href: String
42-
43-
init?(element: ElementNode) {
44-
guard let kindStr = element.attributeValue(for: "data-phx-link"),
45-
let kind = LinkKind(rawValue: kindStr),
46-
let stateStr = element.attributeValue(for: "data-phx-link-state"),
47-
let state = LiveRedirect.Kind(rawValue: stateStr) else {
48-
return nil
49-
}
50-
self.kind = kind
51-
self.state = state
52-
self.href = element.attributeValue(for: "data-phx-href")!
53-
}
54-
55-
@MainActor
56-
func url<R: CustomRegistry>(in context: LiveContext<R>) -> URL? {
57-
.init(string: href, relativeTo: context.coordinator.url)?.appending(path: "/").absoluteURL
58-
}
59-
}
60-
61-
enum LinkKind: String {
62-
case redirect
63-
}
64-
65-
extension LiveSessionConfiguration.NavigationMode {
66-
func supportsLinkState(_ state: LiveRedirect.Kind) -> Bool {
67-
switch self {
68-
case .disabled:
69-
return false
70-
case .replaceOnly:
71-
return state == .replace
72-
case .enabled, .splitView:
73-
return true
7436
}
7537
}
7638
}

0 commit comments

Comments
 (0)