Skip to content

Instantly share code, notes, and snippets.

@rubencodes
Created June 7, 2023 16:19
Show Gist options
  • Save rubencodes/d7b863d1c530ec0272a666c67f924b28 to your computer and use it in GitHub Desktop.
Save rubencodes/d7b863d1c530ec0272a666c67f924b28 to your computer and use it in GitHub Desktop.
Conditional Modifier Extension
/* Example Usage
struct ContentView: View {
let isRounded: Bool
var body: some View {
Color.blue
.conditionalModifier { view in
if isRounded {
view.clipShape(Circle())
} else {
view
}
}
.padding()
}
}
*/
extension View {
@ViewBuilder
func conditionalModifier(@ViewBuilder modifier: (_ view: Self) -> some View) -> some View {
modifier(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment