An open source design system that's fully customizable and agent ready
TypeScript
feat(native): the Swift theme tints stock controls where the accent stays readable, and sets a single-mode theme's color scheme (#8290)
Cindy Zhang2626a477183c
diff --git a/packages/native/apple/Sources/Astryx/Theme/Theme.swift b/packages/native/apple/Sources/Astryx/Theme/Theme.swiftindex e062e6535e..94978e7c25 100644--- a/packages/native/apple/Sources/Astryx/Theme/Theme.swift+++ b/packages/native/apple/Sources/Astryx/Theme/Theme.swift@@ -234,8 +234,85 @@ func astryxTokens(_ theme: Theme, scheme: ColorScheme, contrast: ColorSchemeContextension View {/// Paints this subtree with a theme. Components follow the color scheme and- /// the person's text size either way.+ /// the person's text size either way. The theme also sets SwiftUI's tint+ /// where it stays legible, so stock controls in the subtree carry the brand+ /// (theme environment spec, E2).public func astryxTheme(_ theme: Theme = .default) -> some View {- environment(\.astryxTheme, theme)+ modifier(AstryxThemeModifier(theme: theme))}}++/// Sets the theme and writes the SwiftUI values a theme has an equivalent for,+/// only where they stay legible (theme environment spec, E2). The colors+/// resolve where they are drawn, so a subtree with its own color scheme gets+/// that mode's colors (E3).+private struct AstryxThemeModifier: ViewModifier {+ let theme: Theme++ func body(content: Content) -> some View {+ content+ // Only the tint: an inherited foreground style would override stock+ // controls' own label colors (a link's tint, a prominent button's white).+ .tint(AstryxThemeColor(theme: theme, role: .tint))+ .environment(\.astryxTheme, theme)+ // A single-mode theme sets its subtree's color scheme, so stock controls+ // inside draw the appearance its grounds belong to. Written without a+ // view branch, so switching themes at run time keeps the subtree's state.+ .transformEnvironment(\.colorScheme) { scheme in+ if let mode = theme.singleMode { scheme = mode }+ }+ }+}++extension Theme {+ /// The mode a single-mode theme belongs to, by its page ground: dark for a+ /// dark ground. A theme is single-mode when its page ground, card ground,+ /// primary text, and accent are the same in light and dark; core still fills+ /// a few tokens such a theme leaves out with per-mode defaults, so the whole+ /// token set never matches. Nil for a theme with both modes.+ var singleMode: ColorScheme? {+ let (l, d) = (light.color, dark.color)+ guard l.backgroundBody == d.backgroundBody, l.backgroundCard == d.backgroundCard,+ l.textPrimary == d.textPrimary, l.accent == d.accent+ else { return nil }+ return astryxLuminance(l.backgroundBody) < 0.5 ? .dark : .light+ }+}++/// A theme color that picks light or dark from the color scheme where it is+/// drawn.+struct AstryxThemeColor: ShapeStyle {+ enum Role: Sendable {+ case tint+ }++ let theme: Theme+ let role: Role++ func resolve(in environment: EnvironmentValues) -> Color {+ let color = (environment.colorScheme == .dark ? theme.dark : theme.light).color+ switch role {+ case .tint: return astryxStockTint(color.accent)+ }+ }+}++/// The tint stock controls get from an accent: the accent where the white+/// those controls draw on their tint meets 3:1 against it, as the system's own+/// dark-mode blue does, and the system tint otherwise.+func astryxStockTint(_ accent: Color) -> Color {+ astryxContrast(.white, accent) >= 3 ? accent : .accentColor+}++/// WCAG relative luminance of an opaque sRGB color.+func astryxLuminance(_ color: Color) -> Double {+ let resolved = color.resolve(in: EnvironmentValues())+ return 0.2126 * Double(resolved.linearRed) + 0.7152 * Double(resolved.linearGreen)+ + 0.0722 * Double(resolved.linearBlue)+}++/// WCAG contrast ratio between two opaque colors.+func astryxContrast(_ a: Color, _ b: Color) -> Double {+ let (la, lb) = (astryxLuminance(a), astryxLuminance(b))+ return (max(la, lb) + 0.05) / (min(la, lb) + 0.05)+}diff --git a/packages/native/apple/Tests/AstryxTests/EnvironmentTests.swift b/packages/native/apple/Tests/AstryxTests/EnvironmentTests.swiftindex eb9582888d..a72e6a9050 100644--- a/packages/native/apple/Tests/AstryxTests/EnvironmentTests.swift+++ b/packages/native/apple/Tests/AstryxTests/EnvironmentTests.swift@@ -85,3 +85,93 @@ import Testingenvironment.astryxTheme = .neutral#expect(environment.astryx.color("color-border") == Theme.neutral.light.color.border)}++// Theme environment spec, E2 and E5: the theme writes SwiftUI's tint only,+// so stock controls carry the brand and keep their own label colors.++@MainActor+@Test func theThemeSetsSwiftUIsTint() {+ let swatch = Rectangle().fill(.tint).frame(width: 20, height: 20)+ let light = Pixels(swatch, theme: .default)+ #expect(close(light.at(10, 10), rgba(Theme.default.light.color.accent)))+ let dark = Pixels(swatch, theme: .default, scheme: .dark)+ #expect(close(dark.at(10, 10), rgba(astryxStockTint(Theme.default.dark.color.accent))))+}++@MainActor+@Test func theThemeLeavesThePrimaryForegroundToTheSystem() {+ let swatch = Rectangle().fill(.primary).frame(width: 20, height: 20)+ #expect(Pixels(swatch, theme: .butter).at(10, 10) == Pixels(swatch).at(10, 10))+}++#if os(iOS)+// On macOS, stock buttons and links are AppKit controls, which SwiftUI's image+// renderer does not draw; the iOS Simulator lane checks them.+@MainActor+@Test func aStockLinkAndBorderlessButtonTakeTheTint() {+ let accent = rgba(Theme.default.light.color.accent)+ let link = SwiftUI.Link("Link", destination: URL(string: "https://example.com")!)+ .font(.system(size: 48, weight: .black))+ let borderless = SwiftUI.Button("Go") {}.buttonStyle(.borderless)+ .font(.system(size: 48, weight: .black))+ #expect(Pixels(link.fixedSize(), theme: .default).contains { close($0, accent) })+ #expect(Pixels(borderless.fixedSize(), theme: .default).contains { close($0, accent) })+}++@MainActor+@Test func aProminentButtonKeepsItsWhiteLabelOnTheTint() {+ let button = SwiftUI.Button("Go") {}.buttonStyle(.borderedProminent)+ .font(.system(size: 48, weight: .black))+ let pixels = Pixels(button.fixedSize(), theme: .default)+ #expect(pixels.contains { close($0, rgba(Theme.default.light.color.accent)) })+ #expect(pixels.contains { $0[0] > 245 && $0[1] > 245 && $0[2] > 245 && $0[3] > 245 })+}++#endif++// Theme environment spec, E2: stock controls draw white on their tint, so a+// pale accent is not their tint; a single-mode theme sets its subtree's mode.++@Test func aPaleAccentLeavesStockControlsTheSystemTint() {+ for theme in [Theme.butter, .chocolate, .matcha, .neutral, .stone, .y2k, .gothic] {+ let accent = theme.dark.color.accent+ #expect(astryxContrast(.white, accent) < 3)+ #expect(astryxStockTint(accent) == .accentColor)+ }+ for accent in [Theme.default.light.color.accent, Theme.default.dark.color.accent, Theme.butter.light.color.accent] {+ #expect(astryxContrast(.white, accent) >= 3)+ #expect(astryxStockTint(accent) == accent)+ }+}++@Test func onlyASingleModeThemeSetsItsSubtreesMode() {+ #expect(Theme.gothic.singleMode == .dark)+ for theme in [Theme.default, .butter, .neutral, .stone, .y2k] {+ #expect(theme.singleMode == nil)+ }+}++@MainActor+@Test func gothicInALightAppearanceDrawsStockSurfacesDark() {+ let surface = Rectangle().fill(.background).frame(width: 20, height: 20)+ let plain = Pixels(surface, scheme: .light)+ let gothic = Pixels(surface, theme: .gothic, scheme: .light)+ let brightness = { (p: [Int]) in p[0] + p[1] + p[2] }+ #expect(brightness(gothic.at(10, 10)) < brightness(plain.at(10, 10)) / 2)+}++@MainActor+@Test func aDarkSectionInALightThemeGetsDarkColors() {+ // Core's default dark accent keeps its tint (3.11:1 against white), so the+ // swatch shows the dark-mode decision rather than the system fallback.+ let swatch = Rectangle().fill(.tint).frame(width: 20, height: 20)+ let pixels = Pixels(swatch.environment(\.colorScheme, .dark), theme: .default, scheme: .light)+ #expect(close(pixels.at(10, 10), rgba(Theme.default.dark.color.accent)))+}++@MainActor+@Test func anInnerThemeReplacesTheOuterTint() {+ let swatch = Rectangle().fill(.tint).frame(width: 20, height: 20).astryxTheme(.neutral)+ let pixels = Pixels(swatch, theme: .butter)+ #expect(close(pixels.at(10, 10), rgba(Theme.neutral.light.color.accent)))+}diff --git a/packages/native/specs/swift/library/theme-environment.md b/packages/native/specs/swift/library/theme-environment.mdindex c89a141ac1..8129394bce 100644--- a/packages/native/specs/swift/library/theme-environment.md+++ b/packages/native/specs/swift/library/theme-environment.md@@ -11,9 +11,10 @@In an app built with Astryx, the system's own controls and Astryx componentsmatch, 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.+and right-to-left all come from SwiftUI's own environment. Inside an Astryx+theme, stock SwiftUI controls take the theme's accent as their tint wherever+it stays readable, and otherwise keep their own colors. Astryx adds no second+settings system for an app to keep in step.## Requirements@@ -23,7 +24,7 @@ Astryx adds no second settings system for an app to keep in step.| 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 |+ | `colorScheme` | Chooses the theme's light or dark tokens, so `.preferredColorScheme(_:)` and the system setting drive the mode, except inside a single-mode theme, which sets its subtree's `colorScheme` (E2) || `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 |@@ -34,13 +35,22 @@ Astryx adds no second settings system for an app to keep in step.- **E2 — The theme writes SwiftUI's own values where a native equivalentexists.** `.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` |+ | SwiftUI value | From the theme |+ | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |+ | `.tint(_:)` | `color.accent`, in a mode where white meets 3:1 against it, because stock controls draw white on their tint; the system tint in any other mode |++ A theme whose page ground, card ground, primary text, and accent are the same+ in light and dark (a single-mode theme, such as Gothic) also sets its subtree's `colorScheme` to the mode its page ground+ belongs to, dark for a dark ground, so stock controls inside draw that+ appearance on its grounds.So stock SwiftUI controls (`Toggle`, `Picker`, `ProgressView`, `Menu`,- `List`, `Slider`, `Link`) draw with the brand's accent and text colors.+ `List`, `Slider`, `Link`) draw with the brand's accent wherever it stays+ legible. The theme affects stock SwiftUI controls only through the tint and+ writes no foreground style: an inherited one would override stock controls'+ own label colors, turning a link into plain text and a prominent button's+ white label dark. Plain stock text keeps the system's primary color;+ Astryx's own text uses the theme's. (Decided by cixzhang, 2026-10-25.)Whether the theme also sets the default font is OQ1. Theme values with no SwiftUI environment equivalent are read byAstryx components and Astryx's SwiftUI styles (corner radius, spacing,element heights, borders, and backgrounds), and an app reads spacing for its@@ -62,19 +72,20 @@ Astryx adds no second settings system for an app to keep in step.## 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`. |+| 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)` in light draws butter's accent, and in dark, where butter's pale accent fails 3:1 against white, the system tint; a stock `Link` and a borderless `Button` draw their labels in the tint, and a prominent `Button` keeps its white label; under `.astryxTheme(.gothic)` in a light appearance, a stock surface draws dark. |+| 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.+- `packages/native/apple` has the `Astryx` product: the `Theme` and `Tokens`+ types, the `.astryxTheme(_:)` modifier, which sets the tint and a+ single-mode theme's color scheme, and SwiftUI components that read the theme+ from the `astryx` environment value.## Open questions