Last active
July 16, 2022 18:32
-
-
Save carlynorama/baaceac7e64fa6ee22385277268858f8 to your computer and use it in GitHub Desktop.
A container that collapses.
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
//Based off of https://rensbr.eu/blog/swiftui-escaping-closures/ | |
struct CollapsableView<Content: View>: View { | |
let content: Content | |
@Binding var isVisible:Bool | |
init(value:Binding<Bool>, @ViewBuilder content: () -> Content) { | |
self.content = content() | |
self._isVisible = value | |
} | |
var body: some View { | |
VStack { | |
if isVisible { | |
content | |
} | |
Button(isVisible ? "↓ open" : "↑ close") { | |
isVisible.toggle() | |
} | |
} | |
} | |
} | |
struct ContentView: View { | |
@State var seeTheThing = true | |
var body: some View { | |
CollapsableView(value: $seeTheThing) { | |
Text("Hi!") | |
} | |
} | |
} | |
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