Created
February 2, 2023 10:10
-
-
Save jegnux/f0a9b7e88d4ffe63710dac6f8a894d79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension View { | |
public func navigationLink(@ViewBuilder to destination: () -> some View) -> some View { | |
NavigationLink { | |
destination() | |
} label: { | |
self | |
} | |
} | |
public func sheetLink(@ViewBuilder to destination: @escaping () -> some View) -> some View { | |
SheetLink { | |
destination() | |
} label: { | |
self | |
} | |
} | |
} | |
public struct SheetLink<Label: View, Destination: View> : View { | |
public init(@ViewBuilder destination: @escaping () -> Destination, @ViewBuilder label: () -> Label) { | |
self.destination = destination | |
self.label = label() | |
} | |
public init(_ titleKey: LocalizedStringKey, @ViewBuilder destination: @escaping () -> Destination) where Label == Text { | |
self.destination = destination | |
self.label = Text(titleKey) | |
} | |
public init<S>(_ title: S, @ViewBuilder destination: @escaping () -> Destination) where S : StringProtocol, Label == Text { | |
self.destination = destination | |
self.label = Text(title) | |
} | |
private let label: Label | |
private let destination: () -> Destination | |
@State private var isPresented: Bool = false | |
public var body: some View { | |
Button { | |
isPresented = true | |
} label: { | |
label | |
} | |
.sheet(isPresented: $isPresented) { | |
destination() | |
} | |
} | |
} | |
struct Previews_SheetLink_Previews: PreviewProvider { | |
static var previews: some View { | |
NavigationStack { | |
Text("Sheet") | |
.sheetLink { | |
Color.red | |
} | |
Text("Push") | |
.navigationLink { | |
Color.red | |
} | |
} | |
} | |
} |
Author
jegnux
commented
Feb 2, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment