Add snapshot variants for light/dark, Dynamic Type, RTL and locale - #176
Open
BarredEwe wants to merge 2 commits into
Open
Add snapshot variants for light/dark, Dynamic Type, RTL and locale#176BarredEwe wants to merge 2 commits into
BarredEwe wants to merge 2 commits into
Conversation
Render every preview once per variant, driven by the new `snapshot_variants` configuration key or by `.snapshotVariants(_:)` on a single preview. Variants compose with `snapshot_devices` (`MyView-iPhone 14-dark`), and `light` keeps the snapshot name unsuffixed so already recorded references stay valid. Variants are applied through the SwiftUI environment instead of `UITraitCollection`, so they work on iOS, tvOS and macOS alike. Without any configuration and without the modifier nothing changes: one snapshot per preview, rendered exactly as before. Closes #99
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b031df10cd
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
`ContentSizeCategory` is not `Sendable` in the Xcode 16.4 SDK, which broke the `Sendable` conformance of `SnapshotVariant` and its lookup table. Carry the Dynamic Type size as our own `SnapshotVariant.SizeCategory` instead and map it to `ContentSizeCategory` only where it is applied to the environment. The raw value doubles as the configuration name, so the `snapshot_variants` names and `.snapshotVariants([...])` are unchanged.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #99
One preview currently produces exactly one rendering. This adds appearance, Dynamic Type, layout direction and locale as snapshot variants.
Implementation
SnapshotVariantcoverslight,dark,sizeCategory(_:),rightToLeftandlocale(_:), and applies each purely through the SwiftUI environment — noUITraitCollection— so the same code path works on iOS, tvOS and macOS.In the template
assertSnapshotsnow delegates to anassertVariantslayer that sits between the device loop andassertSnapshot, so names compose asName-iPhone 13 Pro-dark.assertSnapshottakes the already-hosted view and preferences, so the default path still renders exactly once and produces byte-identical output when no variants are configured..lightdeliberately has an empty suffix, so a[light, dark]configuration reuses existing reference images and only adds-dark— no one's snapshots are invalidated by upgrading. Other suffixes:-dark,-rtl,-accessibilityXXXL,-locale_ru_RU. An unknown variant name fails loudly, mirroring the existing behavior for unknown devices.The per-preview modifier is backed by a preference key; when it is absent, the configured list applies.
Tests
Config decoding,
makeArguments, and two generator tests (rendered variants array, empty default). On the runtime side,SnapshotVariantTestscovers name parsing and suffixes, and threeMacOSSnapshotTestscases use aVariantProbeview that encodes the environment as a bit mask in its own width — necessary because the host machine's own dark mode and locale otherwise leak into the assertion.make test59/0,make test-cli22/0,make buildclean.