Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Forked from inamiy/SwiftUI-pipe.swift
Created August 19, 2021 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriseidhof/1f340e588de9b657dd3f452d13f9a583 to your computer and use it in GitHub Desktop.
Save chriseidhof/1f340e588de9b657dd3f452d13f9a583 to your computer and use it in GitHub Desktop.
import SwiftUI
extension View {
func pipe<R>(@ViewBuilder _ f: (Self) -> R) -> R {
f(self)
}
}
struct ContentView: View {
@State var flag: Bool = false
var body: some View {
Rectangle()
.frame(width: flag ? 300 : 200)
// 1. ternary conditional operator
// .foregroundColor(flag ? .red : .blue)
// 2. pipe
.pipe {
if flag {
$0.foregroundColor(.red)
} else {
$0.foregroundColor(.blue)
}
}
.onTapGesture {
withAnimation {
flag.toggle()
}
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment