Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extension View {
/// Conditionally apply modifiers to a view.
func `if`<Content: View>(_ condition: () -> Bool, content: (Self) -> Content) -> some View {
condition() ?
ViewBuilder.buildEither(first: content(self)) :
ViewBuilder.buildEither(second: self)
}
func `if`<Content: View>(_ condition: @autoclosure () -> Bool, content: (Self) -> Content) -> some View {
condition() ?
ViewBuilder.buildEither(first: content(self)) :
ViewBuilder.buildEither(second: self)
}
}
@shaps80

This comment has been minimized.

Copy link
Owner Author

@shaps80 shaps80 commented Dec 9, 2020

Example:

Text("Hello world")
    .if(isHighlighted) { section in
         section.foregroundColor(.red)
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment