Created
February 27, 2020 12:37
-
-
Save sam-w/17405152f117540621b23a4489e32a3f to your computer and use it in GitHub Desktop.
Modal Link
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
struct ModalLink<T, U>: View where T: View, U: View { | |
private let destination: T | |
private let content: () -> U | |
@State private var isPresenting: Bool = false | |
init(destination: T, @ViewBuilder content: @escaping () -> U) { | |
self.destination = destination | |
self.content = content | |
} | |
var body: some View { | |
Button( | |
action: { self.isPresenting = true }, | |
label: { content().disabled(true) } | |
).sheet(isPresented: $isPresenting, content: { self.destination }) | |
} | |
} | |
struct ModalLink_Previews: PreviewProvider { | |
static var previews: some View { | |
ModalLink(destination: Text("Sheet")) { | |
Text("Button") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment