Skip to content

Instantly share code, notes, and snippets.

@ryanlintott
Last active June 11, 2023 02:45
Show Gist options
  • Save ryanlintott/c2115ca42990527cc9c7c7c53ea701f3 to your computer and use it in GitHub Desktop.
Save ryanlintott/c2115ca42990527cc9c7c7c53ea701f3 to your computer and use it in GitHub Desktop.
A wrapper to create any number of arbitrary state variables using parameter packs for testing bindable values in a preview.
import SwiftUI
struct PreviewState<each T, Content: View>: View {
@State private var value: (repeat each T)
var content: (Binding<(repeat each T)>) -> Content
init(
_ value: repeat each T,
@ViewBuilder content: @escaping (Binding<(repeat each T)>) -> Content
) {
self._value = State(initialValue: (repeat each value))
self.content = content
}
var body: some View {
content($value)
}
}
#Preview {
VStack {
PreviewState(false) {
Toggle("First Value", isOn: $0)
}
PreviewState(false, 1, 10.0) {
Toggle("First Value", isOn: $0.0)
Stepper("Second Value", value: $0.1, in: 0...5)
Slider(value: $0.2, in: 0...100)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment