Skip to content

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

TypeScript

feat(native): Swift Card takes core's props; theme shadows reach Swift (#8307)

Cindy Zhang372cc2a88582
diff --git a/packages/native/apple/README.md b/packages/native/apple/README.md
index d7e315de85..344f83c5f7 100644
--- a/packages/native/apple/README.md
+++ b/packages/native/apple/README.md
@@ -51,17 +51,15 @@ apple/
that also imports SwiftUI, write `Astryx.Button` and `SwiftUI.Button`. A
component's parameters use core's prop names
and values (`variant: .primary`, `size: .lg`, `isLoading`), so code reads
- the same across web and native. `Text` and `Heading` take the generated
- `TextProps` and `HeadingProps`, as AST-093 FR5 asks; the others keep five
- exceptions, the interim shape FR5 exempts until they move onto the generated
+ the same across web and native. `Text`, `Heading`, and `Card` take the
+ generated `TextProps`, `HeadingProps`, and `CardProps`, as AST-093 FR5 asks;
+ the others keep four exceptions, the interim shape FR5 exempts until they move onto the generated
contracts:
- `Button` takes `action` where core has `onClick`.
- `Button` takes `fillsWidth` where core has `width="100%"`.
- `Button` takes `icon` as a shared icon name (`"search"`), which the
generated `ButtonProps` does not carry yet.
- `TextInput` takes a `Binding` where core has `value` and `onChange`.
- - `Card` takes `padding` as a whole spacing step, so it cannot spell
- core's `0.5` and `1.5` steps.
- **`Templates/`** holds one folder per page template, named after the
`astryx template` id. Templates are clone-only, as on the web: you copy a
template's folder into your app and own it from then on. They are a
diff --git a/packages/native/apple/Sources/Astryx/Card/Card.swift b/packages/native/apple/Sources/Astryx/Card/Card.swift
index c27dfc5704..24ee57c057 100644
--- a/packages/native/apple/Sources/Astryx/Card/Card.swift
+++ b/packages/native/apple/Sources/Astryx/Card/Card.swift
@@ -1,33 +1,106 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
-import SwiftUI
-
-/// A card: one independent item on the theme's card ground, with a hairline
-/// border and the container radius.
+/// A card: one independent item on the theme's card ground. It takes core's
+/// Card props (`CardProps`) and stacks its content top to bottom, as children
+/// flow inside core's Card.
public struct Card<Content: View>: View {
- let padding: Int
+ public let props: CardProps
let content: Content
@Environment(\.astryx) private var astryx
- /// - Parameter padding: a step on the spacing ladder, as core's `padding`.
- public init(padding: Int = 4, @ViewBuilder content: () -> Content) {
- self.padding = padding
+ /// - Parameters:
+ /// - padding: core's `padding`, a step on the spacing scale; step 4 when
+ /// nil.
+ /// - variant: core's `variant`: the card ground, or `transparent` for none.
+ /// Only `default` draws a border.
+ /// - elevation: core's `elevation`, the theme's shadow at that depth.
+ /// - base: core's base props.
+ /// - content: core's `children`.
+ public init(
+ padding: CardPadding? = nil,
+ variant: CardVariant? = nil,
+ elevation: CardElevation? = nil,
+ base: AstryxBaseProps = AstryxBaseProps(),
+ @ViewBuilder content: () -> Content
+ ) {
+ self.props = CardProps(padding: padding, variant: variant, elevation: elevation, base: base)
self.content = content()
}
public var body: some View {
- let shape = RoundedRectangle(
- cornerRadius: astryx.size("radius-container", 12),
- style: .continuous
- )
- // Several child views stack in one card, top to bottom with no gap of
- // their own, as children flow inside core's Card.
- VStack(alignment: .leading, spacing: 0) { content }
- .padding(astryx.space(padding))
+ let shape = RoundedRectangle(cornerRadius: astryx.tokens.radius.container, style: .continuous)
+ let variant = props.variant ?? .default
+ // The border sits inside the padding, so the content's inset is the
+ // padding step, as in core.
+ SwiftUI.VStack(alignment: .leading, spacing: 0) { content }
+ .padding(padding)
.frame(maxWidth: .infinity, alignment: .leading)
- .background(shape.fill(astryx.color("color-background-card")))
- .overlay(
- shape.strokeBorder(astryx.color("color-border"), lineWidth: astryx.size("border-width", 1))
- )
+ .background(shape.fill(Self.ground(variant, astryx.tokens.color)))
+ .overlay {
+ if variant == .default {
+ shape.strokeBorder(astryx.tokens.color.border, lineWidth: astryx.tokens.size.borderWidth)
+ }
+ }
+ .astryxShadow(Self.shadow(props.elevation ?? .none, astryx.tokens.shadow), in: shape)
+ .astryxBase(props.base)
+ }
+
+ var padding: CGFloat {
+ let space = astryx.tokens.space
+ switch props.padding ?? .v4 {
+ case .v0: return space.s0
+ case .v0_5: return space.s0_5
+ case .v1: return space.s1
+ case .v1_5: return space.s1_5
+ case .v2: return space.s2
+ case .v3: return space.s3
+ case .v4: return space.s4
+ case .v5: return space.s5
+ case .v6: return space.s6
+ case .v8: return space.s8
+ case .v10: return space.s10
+ }
+ }
+
+ /// The ground core's Card draws for a variant.
+ static func ground(_ variant: CardVariant, _ color: ColorTokens) -> Color {
+ switch variant {
+ case .default: color.backgroundCard
+ case .transparent: .clear
+ case .muted: color.backgroundMuted
+ case .blue: color.backgroundBlue
+ case .cyan: color.backgroundCyan
+ case .gray: color.backgroundGray
+ case .green: color.backgroundGreen
+ case .orange: color.backgroundOrange
+ case .pink: color.backgroundPink
+ case .purple: color.backgroundPurple
+ case .red: color.backgroundRed
+ case .teal: color.backgroundTeal
+ case .yellow: color.backgroundYellow
+ }
+ }
+
+ /// The theme's shadow at an elevation.
+ static func shadow(_ elevation: CardElevation, _ shadow: ShadowTokens) -> [ShadowLayer] {
+ switch elevation {
+ case .none: []
+ case .low: shadow.low
+ case .med: shadow.med
+ case .high: shadow.high
+ }
+ }
+}
+
+extension Card where Content == AstryxNodeList {
+ /// A card from core's props, with its children as the wire tree a logic
+ /// core sends; `send` carries the children's callbacks back to the core.
+ public init(
+ _ props: CardProps,
+ children: [AstryxNode],
+ send: @escaping @Sendable (AstryxAction) -> Void
+ ) {
+ self.props = props
+ self.content = AstryxNodeList(nodes: children, send: send)
}
}
diff --git a/packages/native/apple/Sources/Astryx/Theme/Generated/ButterTheme.swift b/packages/native/apple/Sources/Astryx/Theme/Generated/ButterTheme.swift
index 8c90e73458..a99b55afa6 100644
--- a/packages/native/apple/Sources/Astryx/Theme/Generated/ButterTheme.swift
+++ b/packages/native/apple/Sources/Astryx/Theme/Generated/ButterTheme.swift
@@ -214,6 +214,11 @@ extension Theme {
size: FontSizeTokens(base: 14, lg: 18, s2xl: 27, s2xs: 7, s3xl: 34, s3xs: 6, s4xl: 43, s4xs: 5, s5xl: 53, sm: 11, xl: 22, xs: 9),
supporting: TypeStyleTokens(size: 12, weight: 400, leading: 1.4545),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 4, blur: 6, color: Color(astryxRGBA: 0x1D1C111A)), ShadowLayer(x: 0, y: 12, blur: 24, color: Color(astryxRGBA: 0x1D1C1126))],
+ low: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x1D1C110D)), ShadowLayer(x: 0, y: 4, blur: 8, color: Color(astryxRGBA: 0x1D1C111A))],
+ med: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x1D1C110D)), ShadowLayer(x: 0, y: 4, blur: 12, color: Color(astryxRGBA: 0x1D1C111A))]
)
),
dark: Tokens(
@@ -423,6 +428,11 @@ extension Theme {
size: FontSizeTokens(base: 14, lg: 18, s2xl: 27, s2xs: 7, s3xl: 34, s3xs: 6, s4xl: 43, s4xs: 5, s5xl: 53, sm: 11, xl: 22, xs: 9),
supporting: TypeStyleTokens(size: 12, weight: 400, leading: 1.4545),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 4, blur: 6, color: Color(astryxRGBA: 0x1D1C111A)), ShadowLayer(x: 0, y: 12, blur: 24, color: Color(astryxRGBA: 0x1D1C1126))],
+ low: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x1D1C110D)), ShadowLayer(x: 0, y: 4, blur: 8, color: Color(astryxRGBA: 0x1D1C111A))],
+ med: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x1D1C110D)), ShadowLayer(x: 0, y: 4, blur: 12, color: Color(astryxRGBA: 0x1D1C111A))]
)
)
)
diff --git a/packages/native/apple/Sources/Astryx/Theme/Generated/ChocolateTheme.swift b/packages/native/apple/Sources/Astryx/Theme/Generated/ChocolateTheme.swift
index 95db11faa2..3e41843ab0 100644
--- a/packages/native/apple/Sources/Astryx/Theme/Generated/ChocolateTheme.swift
+++ b/packages/native/apple/Sources/Astryx/Theme/Generated/ChocolateTheme.swift
@@ -214,6 +214,11 @@ extension Theme {
size: FontSizeTokens(base: 14, lg: 17, s2xl: 24, s2xs: 8, s3xl: 29, s3xs: 7, s4xl: 35, s4xs: 6, s5xl: 42, sm: 12, xl: 20, xs: 10),
supporting: TypeStyleTokens(size: 12, weight: 400, leading: 1.6667),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 4, blur: 6, color: Color(astryxRGBA: 0x4A35201A)), ShadowLayer(x: 0, y: 12, blur: 24, color: Color(astryxRGBA: 0x4A352026))],
+ low: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x4A35200D)), ShadowLayer(x: 0, y: 4, blur: 8, color: Color(astryxRGBA: 0x4A35201A))],
+ med: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x4A35200D)), ShadowLayer(x: 0, y: 4, blur: 12, color: Color(astryxRGBA: 0x4A35201A))]
)
),
dark: Tokens(
@@ -423,6 +428,11 @@ extension Theme {
size: FontSizeTokens(base: 14, lg: 17, s2xl: 24, s2xs: 8, s3xl: 29, s3xs: 7, s4xl: 35, s4xs: 6, s5xl: 42, sm: 12, xl: 20, xs: 10),
supporting: TypeStyleTokens(size: 12, weight: 400, leading: 1.6667),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 4, blur: 6, color: Color(astryxRGBA: 0x4A35201A)), ShadowLayer(x: 0, y: 12, blur: 24, color: Color(astryxRGBA: 0x4A352026))],
+ low: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x4A35200D)), ShadowLayer(x: 0, y: 4, blur: 8, color: Color(astryxRGBA: 0x4A35201A))],
+ med: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x4A35200D)), ShadowLayer(x: 0, y: 4, blur: 12, color: Color(astryxRGBA: 0x4A35201A))]
)
)
)
diff --git a/packages/native/apple/Sources/Astryx/Theme/Generated/DefaultTheme.swift b/packages/native/apple/Sources/Astryx/Theme/Generated/DefaultTheme.swift
index 8291cde0bb..a7fe446957 100644
--- a/packages/native/apple/Sources/Astryx/Theme/Generated/DefaultTheme.swift
+++ b/packages/native/apple/Sources/Astryx/Theme/Generated/DefaultTheme.swift
@@ -214,6 +214,11 @@ extension Theme {
size: FontSizeTokens(base: 14, lg: 17, s2xl: 24, s2xs: 8, s3xl: 29, s3xs: 7, s4xl: 35, s4xs: 6, s5xl: 42, sm: 12, xl: 20, xs: 10),
supporting: TypeStyleTokens(size: 12, weight: 400, leading: 1.6667),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 2, blur: 2, color: Color(astryxRGBA: 0x0000001A)), ShadowLayer(x: 0, y: 8, blur: 24, color: Color(astryxRGBA: 0x0000001A))],
+ low: [ShadowLayer(x: 0, y: 1, blur: 1, color: Color(astryxRGBA: 0x0000001A)), ShadowLayer(x: 0, y: 2, blur: 8, color: Color(astryxRGBA: 0x0000001A))],
+ med: [ShadowLayer(x: 0, y: 1, blur: 2, color: Color(astryxRGBA: 0x0000001A)), ShadowLayer(x: 0, y: 2, blur: 12, color: Color(astryxRGBA: 0x0000001A))]
)
),
dark: Tokens(
@@ -423,6 +428,11 @@ extension Theme {
size: FontSizeTokens(base: 14, lg: 17, s2xl: 24, s2xs: 8, s3xl: 29, s3xs: 7, s4xl: 35, s4xs: 6, s5xl: 42, sm: 12, xl: 20, xs: 10),
supporting: TypeStyleTokens(size: 12, weight: 400, leading: 1.6667),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 2, blur: 2, color: Color(astryxRGBA: 0x00000033)), ShadowLayer(x: 0, y: 8, blur: 24, color: Color(astryxRGBA: 0x0000004D))],
+ low: [ShadowLayer(x: 0, y: 1, blur: 1, color: Color(astryxRGBA: 0x00000033)), ShadowLayer(x: 0, y: 2, blur: 8, color: Color(astryxRGBA: 0x00000033))],
+ med: [ShadowLayer(x: 0, y: 1, blur: 2, color: Color(astryxRGBA: 0x00000033)), ShadowLayer(x: 0, y: 2, blur: 12, color: Color(astryxRGBA: 0x00000033))]
)
)
)
diff --git a/packages/native/apple/Sources/Astryx/Theme/Generated/GothicTheme.swift b/packages/native/apple/Sources/Astryx/Theme/Generated/GothicTheme.swift
index c8ed0fead5..8c903b66f8 100644
--- a/packages/native/apple/Sources/Astryx/Theme/Generated/GothicTheme.swift
+++ b/packages/native/apple/Sources/Astryx/Theme/Generated/GothicTheme.swift
@@ -214,6 +214,11 @@ extension Theme {
size: FontSizeTokens(base: 16, lg: 20, s2xl: 31, s2xs: 8, s3xl: 39, s3xs: 7, s4xl: 49, s4xs: 5, s5xl: 61, sm: 13, xl: 25, xs: 10),
supporting: TypeStyleTokens(size: 13, weight: 400, leading: 1.5385),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 4, blur: 6, color: Color(astryxRGBA: 0x00000040)), ShadowLayer(x: 0, y: 12, blur: 24, color: Color(astryxRGBA: 0x0000004D))],
+ low: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x00000033)), ShadowLayer(x: 0, y: 4, blur: 8, color: Color(astryxRGBA: 0x00000040))],
+ med: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x00000033)), ShadowLayer(x: 0, y: 4, blur: 12, color: Color(astryxRGBA: 0x00000040))]
)
),
dark: Tokens(
@@ -423,6 +428,11 @@ extension Theme {
size: FontSizeTokens(base: 16, lg: 20, s2xl: 31, s2xs: 8, s3xl: 39, s3xs: 7, s4xl: 49, s4xs: 5, s5xl: 61, sm: 13, xl: 25, xs: 10),
supporting: TypeStyleTokens(size: 13, weight: 400, leading: 1.5385),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 4, blur: 6, color: Color(astryxRGBA: 0x00000040)), ShadowLayer(x: 0, y: 12, blur: 24, color: Color(astryxRGBA: 0x0000004D))],
+ low: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x00000033)), ShadowLayer(x: 0, y: 4, blur: 8, color: Color(astryxRGBA: 0x00000040))],
+ med: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x00000033)), ShadowLayer(x: 0, y: 4, blur: 12, color: Color(astryxRGBA: 0x00000040))]
)
)
)
diff --git a/packages/native/apple/Sources/Astryx/Theme/Generated/MatchaTheme.swift b/packages/native/apple/Sources/Astryx/Theme/Generated/MatchaTheme.swift
index 9fc6fa079a..d60f6de1a6 100644
--- a/packages/native/apple/Sources/Astryx/Theme/Generated/MatchaTheme.swift
+++ b/packages/native/apple/Sources/Astryx/Theme/Generated/MatchaTheme.swift
@@ -214,6 +214,11 @@ extension Theme {
size: FontSizeTokens(base: 16, lg: 20, s2xl: 31, s2xs: 8, s3xl: 39, s3xs: 7, s4xl: 49, s4xs: 5, s5xl: 61, sm: 13, xl: 25, xs: 10),
supporting: TypeStyleTokens(size: 13, weight: 400, leading: 1.5385),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 4, blur: 6, color: Color(astryxRGBA: 0x3E481D1A)), ShadowLayer(x: 0, y: 12, blur: 24, color: Color(astryxRGBA: 0x3E481D26))],
+ low: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x3E481D0D)), ShadowLayer(x: 0, y: 4, blur: 8, color: Color(astryxRGBA: 0x3E481D1A))],
+ med: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x3E481D0D)), ShadowLayer(x: 0, y: 4, blur: 12, color: Color(astryxRGBA: 0x3E481D1A))]
)
),
dark: Tokens(
@@ -423,6 +428,11 @@ extension Theme {
size: FontSizeTokens(base: 16, lg: 20, s2xl: 31, s2xs: 8, s3xl: 39, s3xs: 7, s4xl: 49, s4xs: 5, s5xl: 61, sm: 13, xl: 25, xs: 10),
supporting: TypeStyleTokens(size: 13, weight: 400, leading: 1.5385),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 4, blur: 6, color: Color(astryxRGBA: 0x3E481D1A)), ShadowLayer(x: 0, y: 12, blur: 24, color: Color(astryxRGBA: 0x3E481D26))],
+ low: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x3E481D0D)), ShadowLayer(x: 0, y: 4, blur: 8, color: Color(astryxRGBA: 0x3E481D1A))],
+ med: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x3E481D0D)), ShadowLayer(x: 0, y: 4, blur: 12, color: Color(astryxRGBA: 0x3E481D1A))]
)
)
)
diff --git a/packages/native/apple/Sources/Astryx/Theme/Generated/NeutralTheme.swift b/packages/native/apple/Sources/Astryx/Theme/Generated/NeutralTheme.swift
index c07ddbf90d..d0465483ac 100644
--- a/packages/native/apple/Sources/Astryx/Theme/Generated/NeutralTheme.swift
+++ b/packages/native/apple/Sources/Astryx/Theme/Generated/NeutralTheme.swift
@@ -214,6 +214,11 @@ extension Theme {
size: FontSizeTokens(base: 14, lg: 17, s2xl: 24, s2xs: 8, s3xl: 29, s3xs: 7, s4xl: 35, s4xs: 6, s5xl: 42, sm: 12, xl: 20, xs: 10),
supporting: TypeStyleTokens(size: 12, weight: 400, leading: 1.6667),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 4, blur: 6, color: Color(astryxRGBA: 0x0000001A)), ShadowLayer(x: 0, y: 12, blur: 24, color: Color(astryxRGBA: 0x00000026)), ShadowLayer(x: 0, y: 0, blur: 0, spread: 1, isInset: true, color: Color(astryxRGBA: 0x00000000))],
+ low: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x0000000D)), ShadowLayer(x: 0, y: 4, blur: 8, color: Color(astryxRGBA: 0x0000001A)), ShadowLayer(x: 0, y: 0, blur: 0, spread: 1, isInset: true, color: Color(astryxRGBA: 0x00000000))],
+ med: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x0000000D)), ShadowLayer(x: 0, y: 4, blur: 12, color: Color(astryxRGBA: 0x0000001A)), ShadowLayer(x: 0, y: 0, blur: 0, spread: 1, isInset: true, color: Color(astryxRGBA: 0x00000000))]
)
),
dark: Tokens(
@@ -423,6 +428,11 @@ extension Theme {
size: FontSizeTokens(base: 14, lg: 17, s2xl: 24, s2xs: 8, s3xl: 29, s3xs: 7, s4xl: 35, s4xs: 6, s5xl: 42, sm: 12, xl: 20, xs: 10),
supporting: TypeStyleTokens(size: 12, weight: 400, leading: 1.6667),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 4, blur: 6, color: Color(astryxRGBA: 0x00000080)), ShadowLayer(x: 0, y: 12, blur: 24, color: Color(astryxRGBA: 0x000000B3)), ShadowLayer(x: 0, y: 0, blur: 0, spread: 1, isInset: true, color: Color(astryxRGBA: 0xFFFFFF26))],
+ low: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x00000040)), ShadowLayer(x: 0, y: 4, blur: 8, color: Color(astryxRGBA: 0x00000066)), ShadowLayer(x: 0, y: 0, blur: 0, spread: 1, isInset: true, color: Color(astryxRGBA: 0xFFFFFF14))],
+ med: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x00000059)), ShadowLayer(x: 0, y: 4, blur: 12, color: Color(astryxRGBA: 0x00000080)), ShadowLayer(x: 0, y: 0, blur: 0, spread: 1, isInset: true, color: Color(astryxRGBA: 0xFFFFFF1F))]
)
)
)
diff --git a/packages/native/apple/Sources/Astryx/Theme/Generated/StoneTheme.swift b/packages/native/apple/Sources/Astryx/Theme/Generated/StoneTheme.swift
index ff58e5f88f..8baea45145 100644
--- a/packages/native/apple/Sources/Astryx/Theme/Generated/StoneTheme.swift
+++ b/packages/native/apple/Sources/Astryx/Theme/Generated/StoneTheme.swift
@@ -214,6 +214,11 @@ extension Theme {
size: FontSizeTokens(base: 14, lg: 18, s2xl: 27, s2xs: 7, s3xl: 34, s3xs: 6, s4xl: 43, s4xs: 5, s5xl: 53, sm: 11, xl: 22, xs: 9),
supporting: TypeStyleTokens(size: 12, weight: 400, leading: 1.4545),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 4, blur: 6, color: Color(astryxRGBA: 0x25252A1A)), ShadowLayer(x: 0, y: 12, blur: 24, color: Color(astryxRGBA: 0x25252A26))],
+ low: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x25252A0D)), ShadowLayer(x: 0, y: 4, blur: 8, color: Color(astryxRGBA: 0x25252A1A))],
+ med: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x25252A0D)), ShadowLayer(x: 0, y: 4, blur: 12, color: Color(astryxRGBA: 0x25252A1A))]
)
),
dark: Tokens(
@@ -423,6 +428,11 @@ extension Theme {
size: FontSizeTokens(base: 14, lg: 18, s2xl: 27, s2xs: 7, s3xl: 34, s3xs: 6, s4xl: 43, s4xs: 5, s5xl: 53, sm: 11, xl: 22, xs: 9),
supporting: TypeStyleTokens(size: 12, weight: 400, leading: 1.4545),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 4, blur: 6, color: Color(astryxRGBA: 0x25252A1A)), ShadowLayer(x: 0, y: 12, blur: 24, color: Color(astryxRGBA: 0x25252A26))],
+ low: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x25252A0D)), ShadowLayer(x: 0, y: 4, blur: 8, color: Color(astryxRGBA: 0x25252A1A))],
+ med: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x25252A0D)), ShadowLayer(x: 0, y: 4, blur: 12, color: Color(astryxRGBA: 0x25252A1A))]
)
)
)
diff --git a/packages/native/apple/Sources/Astryx/Theme/Generated/Tokens.swift b/packages/native/apple/Sources/Astryx/Theme/Generated/Tokens.swift
index 22bb40feb5..d9b67944ce 100644
--- a/packages/native/apple/Sources/Astryx/Theme/Generated/Tokens.swift
+++ b/packages/native/apple/Sources/Astryx/Theme/Generated/Tokens.swift
@@ -11,19 +11,44 @@ public struct Tokens: Equatable, Sendable {
public var radius: RadiusTokens
public var size: SizeTokens
public var font: FontTokens
+ public var shadow: ShadowTokens
public init(
color: ColorTokens,
space: SpaceTokens,
radius: RadiusTokens,
size: SizeTokens,
- font: FontTokens
+ font: FontTokens,
+ shadow: ShadowTokens
) {
self.color = color
self.space = space
self.radius = radius
self.size = size
self.font = font
+ self.shadow = shadow
+ }
+}
+
+/// One layer of a theme's shadow, as CSS `box-shadow` draws it: a drop
+/// shadow (an offset and a blur), or an inset ring `spread` points wide.
+public struct ShadowLayer: Equatable, Sendable {
+ public var x: CGFloat
+ public var y: CGFloat
+ public var blur: CGFloat
+ public var spread: CGFloat
+ public var isInset: Bool
+ public var color: Color
+
+ public init(
+ x: CGFloat, y: CGFloat, blur: CGFloat, spread: CGFloat = 0, isInset: Bool = false, color: Color
+ ) {
+ self.x = x
+ self.y = y
+ self.blur = blur
+ self.spread = spread
+ self.isInset = isInset
+ self.color = color
}
}
@@ -1260,3 +1285,34 @@ public struct FontWeightTokens: Equatable, Sendable {
self.semibold = semibold
}
}
+
+/// The theme's `shadow` tokens, one typed member per token.
+public struct ShadowTokens: Equatable, Sendable {
+ /// `--shadow-high`
+ public var high: [ShadowLayer]
+ /// `--shadow-low`
+ public var low: [ShadowLayer]
+ /// `--shadow-med`
+ public var med: [ShadowLayer]
+
+ public init(
+ high: [ShadowLayer],
+ low: [ShadowLayer],
+ med: [ShadowLayer]
+ ) {
+ self.high = high
+ self.low = low
+ self.med = med
+ }
+
+ /// A token by its CSS name, for code that only has the name. Components
+ /// read the typed members instead.
+ func value(named token: String) -> [ShadowLayer]? {
+ switch token {
+ case "shadow-high": high
+ case "shadow-low": low
+ case "shadow-med": med
+ default: nil
+ }
+ }
+}
diff --git a/packages/native/apple/Sources/Astryx/Theme/Generated/Y2kTheme.swift b/packages/native/apple/Sources/Astryx/Theme/Generated/Y2kTheme.swift
index 7573501a0d..d689c3211f 100644
--- a/packages/native/apple/Sources/Astryx/Theme/Generated/Y2kTheme.swift
+++ b/packages/native/apple/Sources/Astryx/Theme/Generated/Y2kTheme.swift
@@ -214,6 +214,11 @@ extension Theme {
size: FontSizeTokens(base: 16, lg: 20, s2xl: 31, s2xs: 8, s3xl: 39, s3xs: 7, s4xl: 49, s4xs: 5, s5xl: 61, sm: 13, xl: 25, xs: 10),
supporting: TypeStyleTokens(size: 12, weight: 400, leading: 1.5385),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 4, blur: 6, color: Color(astryxRGBA: 0x2D241B1A)), ShadowLayer(x: 0, y: 12, blur: 24, color: Color(astryxRGBA: 0x2D241B26))],
+ low: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x2D241B0D)), ShadowLayer(x: 0, y: 4, blur: 8, color: Color(astryxRGBA: 0x2D241B1A))],
+ med: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x2D241B0D)), ShadowLayer(x: 0, y: 4, blur: 12, color: Color(astryxRGBA: 0x2D241B1A))]
)
),
dark: Tokens(
@@ -423,6 +428,11 @@ extension Theme {
size: FontSizeTokens(base: 16, lg: 20, s2xl: 31, s2xs: 8, s3xl: 39, s3xs: 7, s4xl: 49, s4xs: 5, s5xl: 61, sm: 13, xl: 25, xs: 10),
supporting: TypeStyleTokens(size: 12, weight: 400, leading: 1.5385),
weight: FontWeightTokens(bold: 700, medium: 500, normal: 400, semibold: 600)
+ ),
+ shadow: ShadowTokens(
+ high: [ShadowLayer(x: 0, y: 4, blur: 6, color: Color(astryxRGBA: 0x2D241B1A)), ShadowLayer(x: 0, y: 12, blur: 24, color: Color(astryxRGBA: 0x2D241B26))],
+ low: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x2D241B0D)), ShadowLayer(x: 0, y: 4, blur: 8, color: Color(astryxRGBA: 0x2D241B1A))],
+ med: [ShadowLayer(x: 0, y: 2, blur: 4, color: Color(astryxRGBA: 0x2D241B0D)), ShadowLayer(x: 0, y: 4, blur: 12, color: Color(astryxRGBA: 0x2D241B1A))]
)
)
)
diff --git a/packages/native/apple/Sources/Astryx/Theme/Shadow.swift b/packages/native/apple/Sources/Astryx/Theme/Shadow.swift
new file mode 100644
index 0000000000..41a3d06601
--- /dev/null
+++ b/packages/native/apple/Sources/Astryx/Theme/Shadow.swift
@@ -0,0 +1,39 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+extension View {
+ /// Draws a theme shadow around this view's shape, as CSS `box-shadow` does:
+ /// drop layers only outside the shape, so a see-through view shows no
+ /// shadow beneath it, and inset rings inside its edge.
+ func astryxShadow<S: InsettableShape>(_ layers: [ShadowLayer], in shape: S) -> some View {
+ background {
+ ZStack {
+ ForEach(Array(layers.enumerated()), id: \.offset) { _, layer in
+ if !layer.isInset {
+ // A CSS blur is twice SwiftUI's shadow radius.
+ shape.fill(.black)
+ .shadow(color: layer.color, radius: layer.blur / 2, x: layer.x, y: layer.y)
+ }
+ }
+ }
+ .mask {
+ // Everything but the shape itself.
+ Rectangle()
+ .padding(-Self.shadowReach(layers))
+ .overlay(shape.fill(.black).blendMode(.destinationOut))
+ .compositingGroup()
+ }
+ }
+ .overlay {
+ ForEach(Array(layers.enumerated()), id: \.offset) { _, layer in
+ if layer.isInset {
+ shape.strokeBorder(layer.color, lineWidth: layer.spread)
+ }
+ }
+ }
+ }
+
+ /// How far past the shape the layers can draw.
+ fileprivate static func shadowReach(_ layers: [ShadowLayer]) -> CGFloat {
+ layers.map { $0.blur + max(abs($0.x), abs($0.y)) }.max().map { $0 + 4 } ?? 0
+ }
+}
diff --git a/packages/native/apple/Sources/Astryx/Wire/AstryxNodeView.swift b/packages/native/apple/Sources/Astryx/Wire/AstryxNodeView.swift
new file mode 100644
index 0000000000..84336b5966
--- /dev/null
+++ b/packages/native/apple/Sources/Astryx/Wire/AstryxNodeView.swift
@@ -0,0 +1,75 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/// Draws one node of a wire tree a logic core sends (AST-082 FR19) with the
+/// Astryx view for its component; `send` carries its callbacks back to the
+/// core. A node this package cannot draw yet, or could not read, renders as
+/// unsupported in its place, and its siblings still render.
+public struct AstryxNodeView: View {
+ let node: AstryxNode
+ let send: @Sendable (AstryxAction) -> Void
+
+ public init(_ node: AstryxNode, send: @escaping @Sendable (AstryxAction) -> Void) {
+ self.node = node
+ self.send = send
+ }
+
+ public var body: some View {
+ switch node {
+ case .text(let text): Text(text.viewProps(send: send))
+ case .heading(let heading): Heading(heading.viewProps(send: send))
+ case .card(let card): Card(card.viewProps(send: send), children: card.props.children ?? [], send: send)
+ case .icon(let icon): Icon(icon.viewProps(send: send))
+ default: AstryxUnsupportedNode(component: Self.componentName(node))
+ }
+ }
+
+ /// The component a node names on the wire.
+ static func componentName(_ node: AstryxNode) -> String {
+ switch node {
+ case .badge: "Badge"
+ case .button: "Button"
+ case .card: "Card"
+ case .divider: "Divider"
+ case .heading: "Heading"
+ case .hStack: "HStack"
+ case .icon: "Icon"
+ case .progressBar: "ProgressBar"
+ case .selector: "Selector"
+ case .switch: "Switch"
+ case .text: "Text"
+ case .textInput: "TextInput"
+ case .vStack: "VStack"
+ case .unsupported(let component, _): component
+ }
+ }
+}
+
+/// A node drawn in place of one the package cannot draw: a dashed box naming
+/// the component, so a developer sees what is missing and the rest of the tree
+/// keeps its layout.
+struct AstryxUnsupportedNode: View {
+ let component: String
+ @Environment(\.astryx) private var astryx
+
+ var body: some View {
+ let shape = RoundedRectangle(cornerRadius: astryx.tokens.radius.element, style: .continuous)
+ SwiftUI.Text(component)
+ .font(astryx.font(TypeStyle(astryx.tokens.font.code, families: astryx.tokens.font.family.code)))
+ .foregroundStyle(astryx.tokens.color.textSecondary)
+ .padding(astryx.tokens.space.s2)
+ .overlay(shape.strokeBorder(astryx.tokens.color.borderEmphasized, style: StrokeStyle(lineWidth: astryx.tokens.size.borderWidth, dash: [4, 3])))
+ .accessibilityLabel(SwiftUI.Text(component))
+ }
+}
+
+/// Wire nodes drawn in order, as a content slot holds them.
+public struct AstryxNodeList: View {
+ let nodes: [AstryxNode]
+ let send: @Sendable (AstryxAction) -> Void
+
+ public var body: some View {
+ ForEach(nodes, id: \.key) { node in
+ AstryxNodeView(node, send: send)
+ }
+ }
+}
diff --git a/packages/native/apple/Templates/Login/LoginTemplate.swift b/packages/native/apple/Templates/Login/LoginTemplate.swift
index c352f5f2d5..da2c936d23 100644
--- a/packages/native/apple/Templates/Login/LoginTemplate.swift
+++ b/packages/native/apple/Templates/Login/LoginTemplate.swift
@@ -68,7 +68,7 @@ struct LoginTemplateContent: View {
Text("Product Inc.", size: .lg, weight: .bold)
}
- Card(padding: 8) {
+ Card(padding: .v8) {
VStack(alignment: .leading, spacing: astryx.space(4)) {
VStack(spacing: astryx.space(1)) {
Heading("Sign in", level: .v2)
diff --git a/packages/native/apple/Tests/AstryxTests/CardTests.swift b/packages/native/apple/Tests/AstryxTests/CardTests.swift
index 84f0c08164..5365cb4dc3 100644
--- a/packages/native/apple/Tests/AstryxTests/CardTests.swift
+++ b/packages/native/apple/Tests/AstryxTests/CardTests.swift
@@ -6,7 +6,7 @@ import Testing
@MainActor
@Test func severalChildrenStackInOneCard() {
- let card = Astryx.Card(padding: 0) {
+ let card = Astryx.Card(padding: .v0) {
Color(astryxHex: "#FF0000FF").frame(width: 80, height: 40)
Color(astryxHex: "#0000FFFF").frame(width: 80, height: 40)
}
@@ -17,3 +17,118 @@ import Testing
#expect(close(pixels.at(40, 20), rgba("#FF0000FF")))
#expect(close(pixels.at(40, 60), rgba("#0000FFFF")))
}
+
+// Swift component API spec, A2 and A3: Card takes core's Card props, and its
+// props initializer takes the children as wire nodes.
+
+@MainActor
+@Test func bothInitializersBuildTheSameProps() {
+ let labeled = Astryx.Card(padding: .v0_5, variant: .muted, elevation: .low) { Color.clear }
+ let fromProps = Astryx.Card(CardProps(padding: .v0_5, variant: .muted, elevation: .low), children: []) { _ in }
+ #expect(labeled.props.padding == fromProps.props.padding)
+ #expect(labeled.props.variant == fromProps.props.variant)
+ #expect(labeled.props.elevation == fromProps.props.elevation)
+}
+
+@MainActor
+@Test func everyVariantDrawsCoresGroundAndOnlyDefaultHasABorder() {
+ let color = Theme.default.light.color
+ #expect(Astryx.Card<Color>.ground(.default, color) == color.backgroundCard)
+ #expect(Astryx.Card<Color>.ground(.transparent, color) == .clear)
+ #expect(Astryx.Card<Color>.ground(.muted, color) == color.backgroundMuted)
+ #expect(Astryx.Card<Color>.ground(.teal, color) == color.backgroundTeal)
+}
+
+@MainActor
+@Test func elevationReadsTheThemesShadows() {
+ let shadow = Theme.neutral.light.shadow
+ #expect(Astryx.Card<Color>.shadow(.none, shadow).isEmpty)
+ #expect(Astryx.Card<Color>.shadow(.low, shadow) == shadow.low)
+ #expect(Astryx.Card<Color>.shadow(.high, shadow) == shadow.high)
+ // Neutral's shadows end with a ring, as its CSS does.
+ #expect(shadow.low.last?.isInset == true)
+}
+
+@MainActor
+@Test func paddingHalfStepsAreCoresSpacingTokens() {
+ let child = Color(astryxHex: "#FF0000FF").frame(width: 20, height: 20)
+ let half = Pixels(Astryx.Card(padding: .v0_5) { child }.fixedSize())
+ let four = Pixels(Astryx.Card(padding: .v4) { child }.fixedSize())
+ #expect(four.height > half.height)
+}
+
+@MainActor
+@Test func anElevatedCardDrawsAShadowBelowIt() {
+ let flat = Pixels(Astryx.Card(elevation: Astryx.CardElevation.none) { Color.clear.frame(width: 40, height: 40) }.frame(width: 80).padding(20))
+ let raised = Pixels(Astryx.Card(elevation: .high) { Color.clear.frame(width: 40, height: 40) }.frame(width: 80).padding(20))
+ let below = (raised.width / 2, raised.height - 12)
+ #expect(raised.at(below.0, below.1)[3] > flat.at(below.0, below.1)[3])
+}
+
+@MainActor
+@Test func aTransparentCardDrawsNoGroundAndNoBorder() {
+ let card = Pixels(Astryx.Card(variant: .transparent) { Color.clear.frame(width: 40, height: 40) }.frame(width: 80))
+ #expect(card.contains { $0[3] > 0 } == false)
+}
+
+@MainActor
+@Test func aCardFromWireNodesDrawsItsChildren() {
+ let json = """
+ {"component":"Card","key":"c","props":{"padding":0,"children":[
+ {"component":"Text","key":"t","props":{"children":"Hello"}}]}}
+ """
+ let node = try? JSONDecoder().decode(AstryxNode.self, from: Data(json.utf8))
+ #expect(node != nil)
+ if let node {
+ let pixels = Pixels(AstryxNodeView(node) { _ in }.frame(width: 120))
+ #expect(pixels.height > 10)
+ }
+}
+
+// AST-082 FR19: a node the package cannot draw renders as unsupported in its
+// place, and its siblings still render.
+
+@MainActor
+@Test func aNodeThePackageCannotDrawRendersAsUnsupportedInPlace() {
+ let json = """
+ {"component":"Card","key":"c","props":{"padding":0,"children":[
+ {"component":"Chart","key":"x","props":{}},
+ {"component":"Text","key":"t","props":{"children":"Below"}}]}}
+ """
+ let node = try? JSONDecoder().decode(AstryxNode.self, from: Data(json.utf8))
+ guard case .card(let card)? = node else {
+ Issue.record("the card did not decode")
+ return
+ }
+ let children = card.props.children ?? []
+ #expect(children.count == 2)
+ #expect(AstryxNodeView.componentName(children[0]) == "Chart")
+ #expect(AstryxNodeView.componentName(children[1]) == "Text")
+ let both = Pixels(AstryxNodeView(node!) { _ in }.frame(width: 160))
+ let textOnly = Pixels(Astryx.Card(padding: .v0) { Astryx.Text("Below") }.frame(width: 160))
+ #expect(both.height > textOnly.height)
+}
+
+@MainActor
+@Test func aCardGrowsWithItsTextAtTheLargestTextSize() {
+ let card = Astryx.Card { Astryx.Text("Revenue this quarter") }.frame(width: 220)
+ let large = Pixels(card)
+ let largest = Pixels(card, typeSize: .accessibility5)
+ #expect(largest.height > large.height * 2)
+}
+
+@MainActor
+@Test func onlyTheDefaultVariantDrawsABorder() {
+ // A theme whose border token is a color nothing else uses: a card draws
+ // that color exactly when it draws a border.
+ var theme = Theme.default
+ theme.light.color.border = Color(astryxRGBA: 0xFF00FFFF)
+ let magenta = [255, 0, 255, 255]
+ for variant in CardVariant.allCases {
+ // Visible content, so a see-through variant still draws an image.
+ let card = Astryx.Card(variant: variant) { Color.black.frame(width: 40, height: 40) }
+ .frame(width: 80)
+ let hasBorder = Pixels(card, theme: theme).contains { close($0, magenta, within: 4) }
+ #expect(hasBorder == (variant == .default), "\(variant.rawValue)")
+ }
+}
diff --git a/packages/native/scripts/swift-ios-theme.d.mts b/packages/native/scripts/swift-ios-theme.d.mts
index 6b9b68df49..668eab035f 100644
--- a/packages/native/scripts/swift-ios-theme.d.mts
+++ b/packages/native/scripts/swift-ios-theme.d.mts
@@ -22,6 +22,18 @@ export declare function swiftThemeName(themeName: string): string;
export declare function themeFileName(swiftName: string): string;
export declare function toHexColor(value: string): string | null;
export declare function toPoints(value: string): number | null;
+export interface ShadowLayer {
+ x: number;
+ y: number;
+ blur: number;
+ spread: number;
+ isInset: boolean;
+ color: string;
+}
+export declare function toShadow(
+ value: string,
+ mode: 'light' | 'dark',
+): ShadowLayer[] | null;
export declare function toFamilies(value: string): string[] | null;
export declare function valueForMode(
value: string,
diff --git a/packages/native/scripts/swift-ios-theme.mjs b/packages/native/scripts/swift-ios-theme.mjs
index 700010e204..eeb2f51993 100644
--- a/packages/native/scripts/swift-ios-theme.mjs
+++ b/packages/native/scripts/swift-ios-theme.mjs
@@ -36,10 +36,11 @@ export const SWIFT_TOKENS_FILE = path.join(SWIFT_THEME_DIR, 'Tokens.swift');
const THEMES_DIR = path.join(REPO_ROOT, 'packages', 'themes');
const REGENERATE = 'pnpm -F @astryxdesign/native generate:swift-ios';
-// G5: the token groups Swift keeps. Everything else (shadows, motion, layout
-// breakpoints) stays out until a component needs it.
+// G5: the token groups Swift keeps, the elevation shadows included. Everything
+// else (inset rings, motion, layout breakpoints) stays out until a component
+// needs it.
const KEEP =
- /^--(color-|spacing-|radius-|size-element-|border-width$|font-size-|font-weight-|font-family-|text-(heading-\d|display-\d|body|large|label|supporting|code)-)/;
+ /^--(color-|spacing-|radius-|size-element-|border-width$|font-size-|font-weight-|font-family-|text-(heading-\d|display-\d|body|large|label|supporting|code)-|shadow-(low|med|high)$)/;
const REM = 16;
// Words Swift reserves as identifiers. A member or theme name that equals one
@@ -103,6 +104,9 @@ export function swiftMember(token) {
if ((m = /^text-(.+)-(size|weight|leading)$/.exec(name))) {
return {group: 'font', member: `${camel(m[1].split('-'))}.${m[2]}`};
}
+ if ((m = /^shadow-(.+)$/.exec(name))) {
+ return {group: 'shadow', member: camel(m[1].split('-'))};
+ }
return null;
}
@@ -187,6 +191,38 @@ export function toHexColor(value) {
const byte = c => hex2(Math.round(c * 255 * 1e6) / 1e6);
return `#${byte(channel(h + 1 / 3))}${byte(channel(h))}${byte(channel(h - 1 / 3))}${byte(alpha)}`;
}
+ m =
+ /^oklch\(\s*([\d.]+%?)\s+([\d.]+%?)\s+(-?[\d.]+)(?:deg)?(?:\s*\/\s*([\d.]+%?))?\s*\)$/i.exec(
+ v,
+ );
+ if (m) {
+ // Exact OKLCH → OKLab → linear sRGB → sRGB, clipped to the sRGB gamut,
+ // then each channel rounded to the nearest of 256 steps, halves up (G7).
+ const part = (text, percentOf) =>
+ text.endsWith('%')
+ ? (parseFloat(text) / 100) * percentOf
+ : parseFloat(text);
+ const L = part(m[1], 1);
+ const C = part(m[2], 0.4);
+ const hue = (parseFloat(m[3]) * Math.PI) / 180;
+ const a = C * Math.cos(hue);
+ const b = C * Math.sin(hue);
+ const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3;
+ const mm = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3;
+ const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3;
+ const linear = [
+ 4.0767416621 * l - 3.3077115913 * mm + 0.2309699292 * s,
+ -1.2684380046 * l + 2.6097574011 * mm - 0.3413193965 * s,
+ -0.0041960863 * l - 0.7034186147 * mm + 1.707614701 * s,
+ ];
+ const encode = c => {
+ const x = Math.min(1, Math.max(0, c));
+ return x <= 0.0031308 ? 12.92 * x : 1.055 * x ** (1 / 2.4) - 0.055;
+ };
+ const alpha = m[4] === undefined ? 1 : part(m[4], 1);
+ const byte = c => hex2(Math.round(c * 255 * 1e6) / 1e6);
+ return `#${linear.map(c => byte(encode(c))).join('')}${byte(alpha)}`;
+ }
return NAMED_COLORS[v.toLowerCase()] ?? null;
}
@@ -210,6 +246,51 @@ export function valueForMode(value, mode) {
return value;
}
+/** Splits a CSS list at its top-level commas. */
+function splitTopLevel(value) {
+ const parts = [];
+ let depth = 0;
+ let start = 0;
+ for (let i = 0; i < value.length; i++) {
+ const c = value[i];
+ if (c === '(') depth++;
+ else if (c === ')') depth--;
+ else if (c === ',' && depth === 0) {
+ parts.push(value.slice(start, i));
+ start = i + 1;
+ }
+ }
+ parts.push(value.slice(start));
+ return parts.map(part => part.trim());
+}
+
+/**
+ * A CSS `box-shadow` list as shadow layers in one mode, or null (G7). A drop
+ * layer is `x y blur color` with no spread; an inset layer is a ring,
+ * `inset 0 0 0 <width> color`. A `light-dark()` color is taken per mode. Any
+ * other layer has no exact SwiftUI form, so the list is null.
+ */
+export function toShadow(value, mode) {
+ const layers = [];
+ for (const layer of splitTopLevel(value.trim())) {
+ const m =
+ /^(inset\s+)?(\S+)\s+(\S+)\s+(\S+)(?:\s+(\S+))?\s+((?:light-dark|rgba?|hsla?|oklch)\(.*\)|#[0-9A-Fa-f]+|[a-z]+)$/s.exec(
+ layer,
+ );
+ if (!m) return null;
+ const [x, y, blur] = [m[2], m[3], m[4]].map(toPoints);
+ const spread = m[5] == null ? 0 : toPoints(m[5]);
+ const color = toHexColor(valueForMode(m[6], mode));
+ if (x == null || y == null || blur == null || spread == null || !color) {
+ return null;
+ }
+ const isInset = m[1] != null;
+ if (isInset ? x !== 0 || y !== 0 || blur !== 0 : spread !== 0) return null;
+ layers.push({x, y, blur, spread, isInset, color});
+ }
+ return layers.length ? layers : null;
+}
+
/** A CSS length or unitless number as points, or null. */
export function toPoints(value) {
const m = /^(-?[\d.]+)(rem|px)?$/.exec(value.trim());
@@ -239,6 +320,7 @@ const GROUP_TYPES = {
radius: 'CGFloat',
size: 'CGFloat',
font: null, // per member: [String] for families, CGFloat otherwise
+ shadow: '[ShadowLayer]',
};
const GROUP_STRUCTS = {
color: 'ColorTokens',
@@ -246,6 +328,7 @@ const GROUP_STRUCTS = {
radius: 'RadiusTokens',
size: 'SizeTokens',
font: 'FontTokens',
+ shadow: 'ShadowTokens',
};
const GROUPS = Object.keys(GROUP_STRUCTS);
@@ -306,8 +389,8 @@ async function resolveMode(theme, mode) {
resolveThemeTokens(theme === CORE_DEFAULTS ? null : theme, {mode}),
)) {
if (!KEEP.test(token) || typeof value !== 'string') continue;
- value = valueForMode(value, mode);
const {group, member} = swiftMember(token);
+ if (group !== 'shadow') value = valueForMode(value, mode);
const fail = kind => {
throw new Error(
`${theme.name} ${mode}: ${token} is not a ${kind} Swift can read: ${value}`,
@@ -315,6 +398,8 @@ async function resolveMode(theme, mode) {
};
let typed;
if (group === 'color') typed = toHexColor(value) ?? fail('color');
+ else if (group === 'shadow')
+ typed = toShadow(value, mode) ?? fail('shadow');
else if (isFamily(token)) typed = toFamilies(value) ?? fail('font family');
else typed = toPoints(value) ?? fail('size');
out[token] = {group, member, value: typed};
@@ -510,7 +595,7 @@ export function renderTokenTypes(surface) {
.map(m => ` self.${m.member} = ${m.member}`)
.join('\n');
const lookupType =
- group === 'color' ? 'Color' : group === 'font' ? null : 'CGFloat';
+ group === 'font' ? null : (GROUP_TYPES[group] ?? 'CGFloat');
const lookup =
lookupType === null
? ` /// A size, weight, or line-height token by its CSS name, for code that
@@ -573,6 +658,28 @@ ${GROUPS.map(g => ` self.${g} = ${g}`).join('\n')}
}
}
+/// One layer of a theme's shadow, as CSS \`box-shadow\` draws it: a drop
+/// shadow (an offset and a blur), or an inset ring \`spread\` points wide.
+public struct ShadowLayer: Equatable, Sendable {
+ public var x: CGFloat
+ public var y: CGFloat
+ public var blur: CGFloat
+ public var spread: CGFloat
+ public var isInset: Bool
+ public var color: Color
+
+ public init(
+ x: CGFloat, y: CGFloat, blur: CGFloat, spread: CGFloat = 0, isInset: Bool = false, color: Color
+ ) {
+ self.x = x
+ self.y = y
+ self.blur = blur
+ self.spread = spread
+ self.isInset = isInset
+ self.color = color
+ }
+}
+
${structs}
`;
}
@@ -581,6 +688,13 @@ function swiftValue(type, value) {
if (type === 'Color') return `Color(astryxRGBA: 0x${value.slice(1)})`;
if (type === '[String]')
return `[${value.map(v => JSON.stringify(v)).join(', ')}]`;
+ if (type === '[ShadowLayer]')
+ return `[${value
+ .map(
+ l =>
+ `ShadowLayer(x: ${swiftNumber(l.x)}, y: ${swiftNumber(l.y)}, blur: ${swiftNumber(l.blur)}${l.isInset ? `, spread: ${swiftNumber(l.spread)}, isInset: true` : ''}, color: ${swiftValue('Color', l.color)})`,
+ )
+ .join(', ')}]`;
return swiftNumber(value);
}
diff --git a/packages/native/specs/swift/library/theme-generator.md b/packages/native/specs/swift/library/theme-generator.md
index f752c4118b..da8b83ed9a 100644
--- a/packages/native/specs/swift/library/theme-generator.md
+++ b/packages/native/specs/swift/library/theme-generator.md
@@ -37,8 +37,8 @@ re-implements theme logic.
- **G3 — One shared token type, one file per theme.**
- `apple/Sources/Astryx/Theme/Generated/Tokens.swift` declares the typed
token surface: `Tokens` with one member per group (`color`, `space`,
- `radius`, `size`, `font`), each a struct with one typed member per token,
- except `font`, whose members are the sub-groups G6 names: a
+ `radius`, `size`, `font`, `shadow`), each a struct with one typed member
+ per token, except `font`, whose members are the sub-groups G6 names: a
`TypeStyleTokens` per type style and `FontFamilyTokens`, `FontSizeTokens`,
and `FontWeightTokens` for the scales.
It is generated from core's resolved token names, which are the same for
@@ -67,7 +67,8 @@ re-implements theme logic.
`size-element-*`, `border-width`, `font-size-*`, `font-weight-*`,
`font-family-*`, and `text-<style>-*` for `heading-1` to `heading-6`,
`display-1` to `display-3`, `body`, `large`, `label`, `supporting`, and
- `code`, the type styles core's Text and Heading draw. Core fills every shared token a
+ `code`, the type styles core's Text and Heading draw, and `shadow-low`,
+ `shadow-med`, and `shadow-high`, the elevations core's Card and Button draw. Core fills every shared token a
theme leaves out, so every theme has the same shared members. Adding a group
changes this spec, the generator, and the Swift reader in one pull request,
together with the first component that reads it.
@@ -84,6 +85,7 @@ re-implements theme logic.
| `text-<style>-<part>` | `font.<style>.<part>`, one `TypeStyleTokens` per style with `size`, `weight`, and `leading`: `text-body-size` → `font.body.size`, `text-heading-1-weight` → `font.heading1.weight` |
| `font-family-<rest>` | `font.family.<rest>`: `font-family-body` → `font.family.body` |
| `font-size-<rest>`, `font-weight-<rest>` | `font.size.<rest>`, `font.weight.<rest>`, with a name that starts with a digit taking `s`: `font-size-2xl` → `font.size.s2xl`, `font-size-base` → `font.size.base`, `font-weight-bold` → `font.weight.bold` |
+ | `shadow-<level>` | `shadow.<level>`: `shadow-low` → `shadow.low` |
Font tokens group by kind because each type style is several tokens a
view reads together: a view takes `font.body` whole, and a style missing its
@@ -95,14 +97,22 @@ re-implements theme logic.
- Colors are `Color` values in sRGB, from `#RGB`, `#RGBA`, `#RRGGBB`,
`#RRGGBBAA`, `rgb()`/`rgba()` and `hsl()`/`hsla()` (comma or space
separated, hue in degrees or unitless, alpha as a number or a percentage),
- and the keywords `black`, `white`, and `transparent`. Every color is
- stored as 8-bit sRGB channels: an `hsl()` value is converted exactly, then
- each channel is rounded to the nearest of 256 steps, halves up.
+ `oklch()` (lightness as a number or a percentage, chroma as a number or a
+ percentage of 0.4, hue in degrees, optional alpha), and the keywords
+ `black`, `white`, and `transparent`. Every color is stored as 8-bit sRGB
+ channels: an `hsl()` or `oklch()` value is converted exactly (`oklch()`
+ through OKLab, clipped to the sRGB gamut), then each channel is rounded to
+ the nearest of 256 steps, halves up.
- Lengths are `CGFloat` points: `rem` × 16, `px` × 1. Unitless numbers
(weights, line-height ratios) are `CGFloat`.
- Font families are `[String]`, quotes removed, in the CSS stack's order.
- - Any other value in a kept group, such as `oklch()`, `color-mix()`,
- `calc()`, an unresolved `var()`, or `light-dark()`, fails the run with the
+ - Shadows are `[ShadowLayer]`, one per `box-shadow` layer in order: a drop
+ layer `x y blur color` with no spread, or an inset ring
+ `inset 0 0 0 <width> color`. A `light-dark()` color inside a layer is
+ taken per mode.
+ - Any other value in a kept group, such as `color-mix()`,
+ `calc()`, an unresolved `var()`, a nested `light-dark()` outside a shadow
+ layer, or a shadow layer with any other shape, fails the run with the
theme, mode, token, and value. A token present in one mode and missing in
the other fails the same way. Nothing is dropped or guessed.
- **G8 — Theme-local tokens wait for a per-mode source.** A theme's local
@@ -149,22 +159,22 @@ re-implements theme logic.
## Acceptance checks
-| Requirement | Check |
-| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
-| G1 | A theme whose resolved token differs from its raw `theme.tokens` entry (a reference) shows the resolved value in Swift. A `DefinedTheme` with web component overrides generates the same file as the same theme without them. |
-| G2 | A source check finds no import of core in `packages/native/` outside the build-time reads AST-082 FR16 lists (the two theme entries, core's component documentation, its built-in icon names, and its locale catalogs; later only the portable entry). Generated Swift imports only SwiftUI. |
-| G3 | `swiftc -typecheck` compiles `Tokens.swift` and every theme file together; each theme file declares one `Theme` static named as G3 says; two themes with the same name fail the run. |
-| G4 | Generating twice, and on two CI machines, gives byte-identical output. |
-| G5 | Every theme's light and dark values fill every member of `Tokens`, equal to core's resolved names filtered by the G5 groups. |
-| G6 | Unit tests map every row of the G6 table; two tokens that collide fail with both names; a type style without its leading fails naming the style; `Tokens.swift` declares one `TypeStyleTokens` and every theme fills `font.body`, `font.display1`, and `font.heading1` as one. A Swift fixture referencing `theme.color.textPrimray` fails to compile. |
-| G7 | A token written as `light-dark(#000, #fff)` generates black in light and white in dark. `hsl(210 100% 50%)` generates the same color as `#0080FF`. Unit tests convert every listed form; `oklch(0 0 0 / 5%)` in a kept group fails with theme, mode, token, and value in the message; `"Figtree", -apple-system, sans-serif` becomes `["Figtree", "-apple-system", "sans-serif"]`. |
-| G8 | Neutral's local tokens generate nothing today and each is reported; a fixture whose local tokens come back per mode generates its values keyed by their exact custom-property names in that theme's `local` maps, none in `Tokens`. |
-| G9 | A source check finds no token lookup by string in any component. |
-| G10 | On iOS and macOS, a Swift test with no bundled Figtree resolves `font.family.body` to the system font and with a registered test font resolves to that font; on iOS, both scale between Large and AX5. |
-| G11 | `Theme.native` exists, is documented, and holds no values. |
-| G12 | A test reads `packages/themes/*/package.json` and finds one generated file per non-private package plus `default`. A fixture product theme renders through `renderSwiftTheme`. |
-| G13 | Editing one generated value or member name by hand fails the drift test with the regenerate command in the message. |
-| G14 | The README's generated-Swift note lists the generated theme files. |
+| Requirement | Check |
+| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| G1 | A theme whose resolved token differs from its raw `theme.tokens` entry (a reference) shows the resolved value in Swift. A `DefinedTheme` with web component overrides generates the same file as the same theme without them. |
+| G2 | A source check finds no import of core in `packages/native/` outside the build-time reads AST-082 FR16 lists (the two theme entries, core's component documentation, its built-in icon names, and its locale catalogs; later only the portable entry). Generated Swift imports only SwiftUI. |
+| G3 | `swiftc -typecheck` compiles `Tokens.swift` and every theme file together; each theme file declares one `Theme` static named as G3 says; two themes with the same name fail the run. |
+| G4 | Generating twice, and on two CI machines, gives byte-identical output. |
+| G5 | Every theme's light and dark values fill every member of `Tokens`, equal to core's resolved names filtered by the G5 groups. |
+| G6 | Unit tests map every row of the G6 table; two tokens that collide fail with both names; a type style without its leading fails naming the style; `Tokens.swift` declares one `TypeStyleTokens` and every theme fills `font.body`, `font.display1`, and `font.heading1` as one. A Swift fixture referencing `theme.color.textPrimray` fails to compile. |
+| G7 | A token written as `light-dark(#000, #fff)` generates black in light and white in dark. `hsl(210 100% 50%)` generates the same color as `#0080FF`. Unit tests convert every listed form; `oklch(0 0 0 / 5%)` generates `#0000000D`; `color-mix(in srgb, red, blue)` in a kept group fails with theme, mode, token, and value in the message; neutral's `--shadow-low` generates two drop layers and an inset ring, its ring color taken per mode; `"Figtree", -apple-system, sans-serif` becomes `["Figtree", "-apple-system", "sans-serif"]`. |
+| G8 | Neutral's local tokens generate nothing today and each is reported; a fixture whose local tokens come back per mode generates its values keyed by their exact custom-property names in that theme's `local` maps, none in `Tokens`. |
+| G9 | A source check finds no token lookup by string in any component. |
+| G10 | On iOS and macOS, a Swift test with no bundled Figtree resolves `font.family.body` to the system font and with a registered test font resolves to that font; on iOS, both scale between Large and AX5. |
+| G11 | `Theme.native` exists, is documented, and holds no values. |
+| G12 | A test reads `packages/themes/*/package.json` and finds one generated file per non-private package plus `default`. A fixture product theme renders through `renderSwiftTheme`. |
+| G13 | Editing one generated value or member name by hand fails the drift test with the regenerate command in the message. |
+| G14 | The README's generated-Swift note lists the generated theme files. |
## Decisions
@@ -215,7 +225,8 @@ re-implements theme logic.
tokens; neutral declares local tokens, stored as `light-dark(…)` strings.
- Published theme packages: butter, chocolate, gothic, matcha, neutral, stone,
y2k (probe is private). Their raw values use `oklch()` and `color-mix()` outside the G5 groups
- (shadows, component styles), and `color-mix()` inside it only in
+ (component styles), neutral's shadows use `oklch()` with an inset ring, and
+ `color-mix()` appears inside the groups only in
`--color-syntax-added` and `--color-syntax-removed`.
## Open questions
diff --git a/packages/native/src/theme/swiftIOSTheme.test.ts b/packages/native/src/theme/swiftIOSTheme.test.ts
index b6553214b3..86d99643c3 100644
--- a/packages/native/src/theme/swiftIOSTheme.test.ts
+++ b/packages/native/src/theme/swiftIOSTheme.test.ts
@@ -18,6 +18,7 @@ import {
toHexColor,
toPoints,
renderTokenTypes,
+ toShadow,
tokenSurface,
valueForMode,
} from '../../scripts/swift-ios-theme.mjs';
@@ -43,6 +44,9 @@ describe('swift theme generator: names (G6)', () => {
'--text-body-size': 'font.body.size',
'--text-heading-1-weight': 'font.heading1.weight',
'--text-display-1-leading': 'font.display1.leading',
+ '--text-code-leading': 'font.code.leading',
+ '--shadow-low': 'shadow.low',
+ '--shadow-high': 'shadow.high',
};
for (const [token, expected] of Object.entries(cases)) {
const mapped = swiftMember(token);
@@ -107,7 +111,11 @@ describe('swift theme generator: values (G7)', () => {
expect(toHexColor('#fff')).toBe('#FFFFFFFF');
expect(toHexColor('rgba(5, 54, 89, 0.1)')).toBe('#0536591A');
expect(toHexColor('transparent')).toBe('#00000000');
- expect(toHexColor('oklch(0 0 0 / 5%)')).toBeNull();
+ expect(toHexColor('oklch(0 0 0 / 5%)')).toBe('#0000000D');
+ expect(toHexColor('oklch(1 0 0 / 8%)')).toBe('#FFFFFF14');
+ expect(toHexColor('oklch(0.5 0 0)')).toBe('#636363FF');
+ expect(toHexColor('oklch(50% 0 0)')).toBe('#636363FF');
+ expect(toHexColor('color-mix(in srgb, red, blue)')).toBeNull();
expect(toHexColor('light-dark(#000, #fff)')).toBeNull();
expect(toHexColor('hsl(0 100% 50%)')).toBe('#FF0000FF');
expect(toHexColor('hsl(210 100% 50%)')).toBe('#0080FFFF');
@@ -124,6 +132,23 @@ describe('swift theme generator: values (G7)', () => {
expect(toFamilies('var(--font-family-body)')).toBeNull();
});
+ it('reads box-shadow lists as drop layers and inset rings (G7)', () => {
+ const neutralLow =
+ '0 2px 4px light-dark(oklch(0 0 0 / 5%), oklch(0 0 0 / 25%)), 0 4px 8px light-dark(oklch(0 0 0 / 10%), oklch(0 0 0 / 40%)), inset 0 0 0 1px light-dark(transparent, oklch(1 0 0 / 8%))';
+ expect(toShadow(neutralLow, 'light')).toEqual([
+ {x: 0, y: 2, blur: 4, spread: 0, isInset: false, color: '#0000000D'},
+ {x: 0, y: 4, blur: 8, spread: 0, isInset: false, color: '#0000001A'},
+ {x: 0, y: 0, blur: 0, spread: 1, isInset: true, color: '#00000000'},
+ ]);
+ expect(toShadow(neutralLow, 'dark')?.[2]?.color).toBe('#FFFFFF14');
+ expect(toShadow('0 2px 4px #4a35200D', 'light')).toEqual([
+ {x: 0, y: 2, blur: 4, spread: 0, isInset: false, color: '#4A35200D'},
+ ]);
+ // No exact SwiftUI form: a drop spread, or a blurred or offset inset.
+ expect(toShadow('0 2px 4px 1px #000', 'light')).toBeNull();
+ expect(toShadow('inset 0 1px 2px #000', 'light')).toBeNull();
+ });
+
it('takes light-dark() per mode (G1)', async () => {
expect(valueForMode('light-dark(#000, #fff)', 'light')).toBe('#000');
expect(valueForMode('light-dark(#000, #fff)', 'dark')).toBe('#fff');
@@ -143,10 +168,10 @@ describe('swift theme generator: values (G7)', () => {
it('fails on a value Swift cannot read, naming theme, mode, token, and value', async () => {
const theme = await defineTheme({
name: 'acme-brand',
- tokens: {'--color-accent': 'oklch(0.6 0.2 250)'},
+ tokens: {'--color-accent': 'color-mix(in srgb, red, blue)'},
});
await expect(renderSwiftTheme(theme)).rejects.toThrow(
- 'acme-brand light: --color-accent is not a color Swift can read: oklch(0.6 0.2 250)',
+ 'acme-brand light: --color-accent is not a color Swift can read: color-mix(in srgb, red, blue)',
);
});
});