Last active
October 31, 2022 10:30
-
-
Save davidsteppenbeck/8f0986c314853c0748d3469d00dc7fc2 to your computer and use it in GitHub Desktop.
A SwiftUI view modifier for simple preview providers.
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
import SwiftUI | |
enum PreviewProviderMode: CaseIterable { | |
/// Use for a light appearance preview. | |
case lightMode | |
/// Use for a dark appearance preview. | |
case darkMode | |
/// Use for a right-to-left layout preview. | |
case rightToLeft | |
/// Use for a preview with a large text size. | |
case largeText | |
/// Use for a redacted placeholder preview. | |
case redacted | |
} | |
fileprivate struct PreviewProviderModifier: ViewModifier { | |
/// The preview provider modes to display. | |
let modes: [PreviewProviderMode] | |
func body(content: Content) -> some View { | |
Group { | |
if modes.contains(.lightMode) { | |
content | |
.previewDisplayName("Light Mode") | |
.environment(\.colorScheme, .light) | |
} | |
if modes.contains(.darkMode) { | |
content | |
.previewDisplayName("Dark Mode") | |
.environment(\.colorScheme, .dark) | |
} | |
if modes.contains(.rightToLeft) { | |
content | |
.previewDisplayName("Right To Left") | |
.environment(\.layoutDirection, .rightToLeft) | |
} | |
if modes.contains(.largeText) { | |
content | |
.previewDisplayName("Large Text") | |
.environment(\.dynamicTypeSize, .accessibility5) | |
} | |
if modes.contains(.redacted) { | |
content | |
.previewDisplayName("Redacted") | |
.redacted(reason: .placeholder) | |
} | |
} | |
} | |
} | |
extension View { | |
/// Creates a group of views with various environment settings that are useful for previews. | |
/// | |
/// - Parameters: | |
/// - modes: The preview provider modes to display, such as light mode, dark mode, right-to-left layout, and large text. | |
func makeForPreviewProvider(_ modes: [PreviewProviderMode] = PreviewProviderMode.allCases) -> some View { | |
modifier(PreviewProviderModifier(modes: modes)) | |
} | |
/// Creates a group of views with various environment settings that are useful for previews. | |
/// | |
/// - Parameters: | |
/// - mode: The preview provider mode to display, such as light mode, dark mode, right-to-left layout, or large text. | |
func makeForPreviewProvider(_ mode: PreviewProviderMode) -> some View { | |
modifier(PreviewProviderModifier(modes: [mode])) | |
} | |
/// Creates a group of views with various environment settings that are useful for previews. | |
/// | |
/// - Parameters: | |
/// - modes: The preview provider modes to be excluded from being displayed. All other modes will be displayed. | |
func makeForPreviewProvider(excluding modes: [PreviewProviderMode]) -> some View { | |
let included = PreviewProviderMode.allCases.filter { modes.contains($0) == false } | |
return modifier(PreviewProviderModifier(modes: included)) | |
} | |
/// Creates a group of views with various environment settings that are useful for previews. | |
/// | |
/// - Parameters: | |
/// - mode: The preview provider mode to be excluded from being displayed. All other modes will be displayed. | |
func makeForPreviewProvider(excluding mode: PreviewProviderMode) -> some View { | |
makeForPreviewProvider(excluding: [mode]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment