Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

spec(native): the Swift theme rides on SwiftUI's environment both ways (#8040)

Cindy Zhang50df97712aba
diff --git a/packages/native/specs/swift/library/README.md b/packages/native/specs/swift/library/README.md
index 0fdf608b35..049dabacc6 100644
--- a/packages/native/specs/swift/library/README.md
+++ b/packages/native/specs/swift/library/README.md
@@ -9,9 +9,10 @@ These specs apply AST-082 (native-first surfaces) and AST-093 (native component
guidelines). Where one of them and an approved record disagree, the record
wins.
-| File | Concern |
-| -------------------- | ------------------------------------------------------------------------------------- |
-| `accessibility.md` | How core's accessibility semantics reach VoiceOver and Apple's accessibility settings |
-| `component-api.md` | How a SwiftUI view is named and takes its generated contract |
-| `slots.md` | How each slot kind (text, icon, content) appears in SwiftUI |
-| `theme-generator.md` | How any Astryx theme becomes a Swift `Theme` |
+| File | Concern |
+| ---------------------- | ------------------------------------------------------------------------------------- |
+| `accessibility.md` | How core's accessibility semantics reach VoiceOver and Apple's accessibility settings |
+| `component-api.md` | How a SwiftUI view is named and takes its generated contract |
+| `slots.md` | How each slot kind (text, icon, content) appears in SwiftUI |
+| `theme-environment.md` | How the theme rides on SwiftUI's environment in both directions |
+| `theme-generator.md` | How any Astryx theme becomes a Swift `Theme` |
diff --git a/packages/native/specs/swift/library/theme-environment.md b/packages/native/specs/swift/library/theme-environment.md
new file mode 100644
index 0000000000..e1e2a7bcbe
--- /dev/null
+++ b/packages/native/specs/swift/library/theme-environment.md
@@ -0,0 +1,83 @@
+# Theme environment (Swift)
+
+- Status: draft
+- Relation: extends AST-093 FR4 and AST-082 FR10, FR11; builds on
+ `packages/native/specs/swift/library/theme-generator.md` (the `Theme` and
+ `Tokens` it generates, and its font family reader) and
+ `packages/native/specs/swift/library/component-api.md` (names).
+
+## Summary
+
+In an app built with Astryx, the system's own controls and Astryx components
+match, and the person's system settings are honored everywhere: dark mode,
+Increase Contrast, text size, Reduce Motion, Reduce Transparency, Bold Text,
+and right-to-left all come from SwiftUI's own environment, and a stock SwiftUI
+`Toggle` or `ProgressView` inside an Astryx theme takes the theme's accent.
+Astryx adds no second settings system for an app to keep in step.
+
+## Requirements
+
+- **E1 — Settings are read from SwiftUI, never duplicated.** Components and the
+ theme read these environment values and Astryx offers no API of its own for
+ any of them:
+
+ | SwiftUI value | What Astryx does with it |
+ | --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+ | `colorScheme` | Chooses the theme's light or dark tokens, so `.preferredColorScheme(_:)` and the system setting drive the mode |
+ | `colorSchemeContrast` | When increased, borders and separators use `color.borderEmphasized` instead of `color.border` |
+ | `dynamicTypeSize` | Scales text, through text-style-relative fonts, and the metrics beside text, through `@ScaledMetric` |
+ | `accessibilityReduceMotion` | Movement becomes a cross-fade or no animation |
+ | `accessibilityReduceTransparency` | Translucent surfaces become opaque |
+ | `layoutDirection` | Leading and trailing follow it |
+ | `legibilityWeight` | Bold Text: system and text-style fonts follow it by themselves; a custom face from the theme's family list is drawn heavier when it is `.bold`: ultra light → thin, thin → light, light → regular, regular → semibold, medium → semibold, semibold → bold, bold → heavy, heavy → black, black stays black |
+
+- **E2 — The theme writes SwiftUI's own values where a native equivalent
+ exists.** `.astryxTheme(_:)` sets, for the subtree, in the current mode:
+
+ | SwiftUI value | From the theme |
+ | --------------------------------------------------------- | ---------------------------------------------------------------- |
+ | `.tint(_:)` | `color.accent` |
+ | `.foregroundStyle(_:_:_:)` (primary, secondary, tertiary) | `color.textPrimary`, `color.textSecondary`, `color.textDisabled` |
+
+ So stock SwiftUI controls (`Toggle`, `Picker`, `ProgressView`, `Menu`,
+ `List`, `Slider`, `Link`) draw with the brand's accent and text colors.
+ Whether the theme also sets the default font is OQ1. Theme values with no SwiftUI environment equivalent are read only
+ by Astryx components: corner radius, spacing, element heights, borders, and
+ backgrounds. Control size has no clean mapping: Astryx sizes are a per-
+ component prop, so the theme sets no `controlSize`.
+
+- **E3 — The mode always follows SwiftUI.** A token value is chosen from the
+ environment's `colorScheme` where it is read, so a subtree with its own
+ `.preferredColorScheme` or `.environment(\.colorScheme, …)` gets that mode's
+ tokens.
+- **E4 — No modifier still means the platform's settings.** Without
+ `.astryxTheme(_:)`, components use core's default theme and still follow
+ `colorScheme` and every E1 value; only E2's writes are absent.
+- **E5 — Subtrees can change theme.** `.astryxTheme(_:)` on a subtree replaces
+ the theme for that subtree only, including E2's writes.
+- **E6 — The theme is one environment value.** The theme travels as a single
+ environment value declared with `@Entry`; components read it with
+ `@Environment` and never through a singleton.
+
+## Acceptance checks
+
+| Requirement | Check |
+| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| E1, E3 | A view test with only `.preferredColorScheme(.dark)` (no other Astryx setting) draws an Astryx component with dark tokens; with `colorSchemeContrast` increased, a Card draws `color.borderEmphasized`. With `legibilityWeight` set to `.bold`, an Astryx Text in butter's registered body face at regular weight resolves to semibold, and at bold to heavy. |
+| E2 | A stock SwiftUI `Toggle` inside `.astryxTheme(.butter)` draws butter's accent; a plain `SwiftUI.Text` inside it draws butter's primary text color. |
+| E4 | With no `.astryxTheme(_:)`, an Astryx component under `.preferredColorScheme(.dark)` draws core's default dark tokens. |
+| E5 | A subtree with `.astryxTheme(.neutral)` inside `.astryxTheme(.butter)` draws neutral's accent, and its stock `Toggle` does too. |
+| E6 | A source check finds no static or singleton theme state; the environment key is declared with `@Entry`. |
+
+## Current state
+
+- On `main`, the Swift package `packages/native/apple` holds generated props,
+ the wire, and the logic host. There is no Swift theme, theme modifier, or
+ SwiftUI component.
+
+## Open questions
+
+- **OQ1 — Should the theme set the default font for all text in its subtree?**
+ It brands stock SwiftUI text, and it also changes text an app draws itself.
+ Recommendation: set it, because a theme's typeface is part of its identity
+ and an app can still set its own `.font(_:)` on any view.