Last active
March 17, 2022 20:03
-
-
Save westerlund/e94c1daa8f0ee47f178aecc367a26cf2 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
extension View { | |
// https://www.avanderlee.com/swiftui/conditional-view-modifier/ | |
/// Applies the given transform if the given condition evaluates to `true`. | |
/// - Parameters: | |
/// - condition: The condition to evaluate. | |
/// - transform: The transform to apply to the source `View`. | |
/// - Returns: Either the original `View` or the modified `View` if the condition is `true`. | |
@ViewBuilder func `if`<Content: View>(_ condition: Bool, transform: (Self) -> Content) -> some View { | |
if condition { | |
transform(self) | |
} else { | |
self | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment