Skip to content

Instantly share code, notes, and snippets.

@TheWorstProgrammerEver
Created March 5, 2022 08:05
Show Gist options
  • Save TheWorstProgrammerEver/bd8402ba726312f1582694519257d9a1 to your computer and use it in GitHub Desktop.
Save TheWorstProgrammerEver/bd8402ba726312f1582694519257d9a1 to your computer and use it in GitHub Desktop.
SwiftUI StandardPreviews - reusable previews with dark- and light-mode, large- and small-text (Dynamic Type) variations.
import SwiftUI
struct StandardPreviews<Content: View> : View {
var light: Bool = true
var dark: Bool = true
var large: Bool = true
var small: Bool = true
var content: () -> Content
var body: some View {
Group {
if light {
content()
.environment(\.colorScheme, .light)
}
if dark {
content()
.environment(\.colorScheme, .dark)
}
if large {
content()
.environment(\.sizeCategory, .accessibilityExtraLarge)
}
if small {
content()
.environment(\.sizeCategory, .extraSmall)
}
}
}
}
struct StandardPreviews_Previews : PreviewProvider {
static var previews: some View {
StandardPreviews {
NavigationView {
Text("Hello")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment