Skip to content

Instantly share code, notes, and snippets.

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 indyfromoz/e6b387a7d8de7b696d1edc0903d7a16f to your computer and use it in GitHub Desktop.
Save indyfromoz/e6b387a7d8de7b696d1edc0903d7a16f to your computer and use it in GitHub Desktop.
A SwiftUI view modifier for simple preview providers.
#if DEBUG
import SwiftUI
struct PreviewProviderModifier: ViewModifier {
/// Whether or not a basic light mode preview is included in the group.
var includeLightMode: Bool
/// Whether or not a basic dark mode preview is included in the group.
var includeDarkMode: Bool
/// Whether or not right-to-left layout preview is included in the group.
var includeRightToLeftMode: Bool
/// Whether or not a preview with large text is included in the group.
var includeLargeTextMode: Bool
func body(content: Content) -> some View {
Group {
if includeLightMode {
content
.previewDisplayName("Light Mode")
.environment(\.colorScheme, .light)
}
if includeDarkMode {
content
.previewDisplayName("Dark Mode")
.environment(\.colorScheme, .dark)
}
if includeRightToLeftMode {
content
.previewDisplayName("Right To Left")
.environment(\.layoutDirection, .rightToLeft)
}
if includeLargeTextMode {
content
.previewDisplayName("Large Text")
.environment(\.sizeCategory, .accessibilityExtraExtraLarge)
}
}
}
}
extension View {
/// Creates a group of views with various environment settings that are useful for previews.
///
/// - Parameters:
/// - includeLightMode: Whether or not a basic light mode preview is included in the group.
/// - includeDarkMode: Whether or not a basic dark mode preview is included in the group.
/// - includeRightToLeftMode: Whether or not a right-to-left layout preview is included in the group.
/// - includeLargeTextMode: Whether or not a preview with large text is included in the group.
func makeForPreviewProvider(includeLightMode: Bool = true, includeDarkMode: Bool = true, includeRightToLeftMode: Bool = true, includeLargeTextMode: Bool = true) -> some View {
modifier(
PreviewProviderModifier(
includeLightMode: includeLightMode,
includeDarkMode: includeDarkMode,
includeRightToLeftMode: includeRightToLeftMode,
includeLargeTextMode: includeLargeTextMode
)
)
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment