@astryxdesign/native 0.7.5-canary.563dc53 → 0.7.5-canary.577e8ca

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/README.md +44 -24
  2. package/apple/Package.swift +21 -4
  3. package/apple/README.md +192 -0
  4. package/apple/Sources/Astryx/Banner/Banner.swift +80 -0
  5. package/apple/Sources/Astryx/Button/Button.swift +167 -0
  6. package/apple/Sources/Astryx/Card/Card.swift +33 -0
  7. package/apple/Sources/Astryx/Exports.swift +8 -0
  8. package/apple/Sources/Astryx/Heading/Heading.swift +46 -0
  9. package/apple/Sources/Astryx/Localization/AstryxStrings.swift +22 -0
  10. package/apple/Sources/Astryx/Resources/af.lproj/Localizable.strings +4 -0
  11. package/apple/Sources/Astryx/Resources/ar.lproj/Localizable.strings +4 -0
  12. package/apple/Sources/Astryx/Resources/ca.lproj/Localizable.strings +4 -0
  13. package/apple/Sources/Astryx/Resources/cs.lproj/Localizable.strings +4 -0
  14. package/apple/Sources/Astryx/Resources/da.lproj/Localizable.strings +4 -0
  15. package/apple/Sources/Astryx/Resources/de.lproj/Localizable.strings +4 -0
  16. package/apple/Sources/Astryx/Resources/el.lproj/Localizable.strings +4 -0
  17. package/apple/Sources/Astryx/Resources/en.lproj/Localizable.strings +4 -0
  18. package/apple/Sources/Astryx/Resources/es.lproj/Localizable.strings +4 -0
  19. package/apple/Sources/Astryx/Resources/fi.lproj/Localizable.strings +4 -0
  20. package/apple/Sources/Astryx/Resources/fr.lproj/Localizable.strings +4 -0
  21. package/apple/Sources/Astryx/Resources/he.lproj/Localizable.strings +4 -0
  22. package/apple/Sources/Astryx/Resources/hu.lproj/Localizable.strings +4 -0
  23. package/apple/Sources/Astryx/Resources/it.lproj/Localizable.strings +4 -0
  24. package/apple/Sources/Astryx/Resources/ja.lproj/Localizable.strings +4 -0
  25. package/apple/Sources/Astryx/Resources/ko.lproj/Localizable.strings +4 -0
  26. package/apple/Sources/Astryx/Resources/nb.lproj/Localizable.strings +4 -0
  27. package/apple/Sources/Astryx/Resources/nl.lproj/Localizable.strings +4 -0
  28. package/apple/Sources/Astryx/Resources/pl.lproj/Localizable.strings +4 -0
  29. package/apple/Sources/Astryx/Resources/pt-BR.lproj/Localizable.strings +4 -0
  30. package/apple/Sources/Astryx/Resources/pt-PT.lproj/Localizable.strings +4 -0
  31. package/apple/Sources/Astryx/Resources/ro.lproj/Localizable.strings +4 -0
  32. package/apple/Sources/Astryx/Resources/ru.lproj/Localizable.strings +4 -0
  33. package/apple/Sources/Astryx/Resources/sr.lproj/Localizable.strings +4 -0
  34. package/apple/Sources/Astryx/Resources/sv.lproj/Localizable.strings +4 -0
  35. package/apple/Sources/Astryx/Resources/tr.lproj/Localizable.strings +4 -0
  36. package/apple/Sources/Astryx/Resources/uk.lproj/Localizable.strings +4 -0
  37. package/apple/Sources/Astryx/Resources/vi.lproj/Localizable.strings +4 -0
  38. package/apple/Sources/Astryx/Resources/zh-Hans.lproj/Localizable.strings +4 -0
  39. package/apple/Sources/Astryx/Resources/zh-Hant.lproj/Localizable.strings +4 -0
  40. package/apple/Sources/Astryx/Text/Text.swift +83 -0
  41. package/apple/Sources/Astryx/TextInput/TextInput.swift +81 -0
  42. package/apple/Sources/Astryx/Theme/AstryxIcons.swift +37 -0
  43. package/apple/Sources/Astryx/Theme/Generated/ButterTheme.swift +493 -0
  44. package/apple/Sources/Astryx/Theme/Generated/ChocolateTheme.swift +493 -0
  45. package/apple/Sources/Astryx/Theme/Generated/DefaultTheme.swift +493 -0
  46. package/apple/Sources/Astryx/Theme/Generated/GothicTheme.swift +493 -0
  47. package/apple/Sources/Astryx/Theme/Generated/MatchaTheme.swift +493 -0
  48. package/apple/Sources/Astryx/Theme/Generated/NeutralTheme.swift +493 -0
  49. package/apple/Sources/Astryx/Theme/Generated/StoneTheme.swift +493 -0
  50. package/apple/Sources/Astryx/Theme/Generated/Tokens.swift +1260 -0
  51. package/apple/Sources/Astryx/Theme/Generated/Y2kTheme.swift +493 -0
  52. package/apple/Sources/Astryx/Theme/Theme.swift +270 -0
  53. package/apple/Sources/AstryxNative/Generated/AstryxNode.swift +3 -0
  54. package/apple/Sources/AstryxNative/Generated/Badge.swift +89 -0
  55. package/apple/Sources/AstryxNative/Generated/Button.swift +14 -2
  56. package/apple/Sources/AstryxNative/Logic/AstryxLogicHost.swift +102 -0
  57. package/apple/Tests/AstryxNativeTests/ControlPropsTests.swift +2 -0
  58. package/apple/Tests/AstryxNativeTests/LogicHostTests.swift +85 -0
  59. package/apple/Tests/AstryxTests/BannerTests.swift +47 -0
  60. package/apple/Tests/AstryxTests/ButtonTests.swift +72 -0
  61. package/apple/Tests/AstryxTests/CardTests.swift +19 -0
  62. package/apple/Tests/AstryxTests/EnvironmentTests.swift +87 -0
  63. package/apple/Tests/AstryxTests/HeadingTests.swift +40 -0
  64. package/apple/Tests/AstryxTests/NamesTests.swift +28 -0
  65. package/apple/Tests/AstryxTests/Pixels.swift +68 -0
  66. package/apple/Tests/AstryxTests/StringsTests.swift +23 -0
  67. package/apple/Tests/AstryxTests/TextInputTests.swift +29 -0
  68. package/apple/Tests/AstryxTests/TextTests.swift +25 -0
  69. package/apple/Tests/AstryxTests/ThemeTests.swift +45 -0
  70. package/apple/Tests/AstryxTests/TwoThemeTests.swift +65 -0
  71. package/dist/components/Badge.d.ts +10 -0
  72. package/dist/components/Badge.d.ts.map +1 -0
  73. package/dist/components/Badge.js +7 -0
  74. package/dist/components/Button.d.ts.map +1 -1
  75. package/dist/components/Button.js +1 -0
  76. package/dist/components/index.d.ts.map +1 -1
  77. package/dist/components/index.js +2 -0
  78. package/dist/contract/fromComponentDoc.d.ts +8 -0
  79. package/dist/contract/fromComponentDoc.d.ts.map +1 -1
  80. package/dist/contract/fromComponentDoc.js +15 -0
  81. package/dist/contract/types.d.ts +5 -0
  82. package/dist/contract/types.d.ts.map +1 -1
  83. package/dist/types/android.d.ts +14 -4
  84. package/dist/types/core.d.ts +6 -0
  85. package/dist/types/ios.d.ts +14 -4
  86. package/dist/types/macos.d.ts +14 -4
  87. package/dist/types/shared.d.ts +14 -4
  88. package/dist/types/windows.d.ts +14 -4
  89. package/package.json +5 -3
  90. package/src/components/Badge.ts +18 -0
  91. package/src/components/Button.ts +1 -0
  92. package/src/components/index.ts +2 -0
  93. package/src/contract/contracts.test.ts +17 -0
  94. package/src/contract/fromComponentDoc.test.ts +29 -0
  95. package/src/contract/fromComponentDoc.ts +28 -0
  96. package/src/contract/types.ts +7 -0
  97. package/src/icons/swiftIcons.test.ts +22 -0
  98. package/src/theme/swiftIOSStrings.test.ts +49 -0
  99. package/src/theme/swiftIOSTheme.test.ts +223 -0
package/README.md CHANGED
@@ -12,18 +12,22 @@ rests on evidence.
12
12
 
13
13
  ## What is here today
14
14
 
15
- | Part | Path | What it does |
16
- | -------------- | --------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
17
- | Contracts | `src/contract/` | Reads a core component's documentation and splits its props into a portable **core contract** and a **web-only** part, with a reason for each web-only prop. Checks prop names against Swift and Kotlin keywords and platform toolkit names. |
18
- | Platform types | `@astryxdesign/native/components` | TypeScript props per platform, chosen by type-resolution condition: `ios`, `android`, `macos`, `windows`, or `shared` for the core contract only. |
19
- | Swift binding | `apple/` | A Swift package with props generated from the same contracts. iOS and macOS props sit behind `#if os(...)`. |
20
- | Native theme | `defineNativeTheme` | Durable theme intent (`material: 'opaque' \| 'translucent'`) plus platform entries scoped by OS version, such as Liquid Glass from iOS 26. Never sets tokens. |
21
- | Icons | `NATIVE_ICON_DEFAULTS` | Astryx's built-in icon names mapped to SF Symbols on Apple platforms and Material Symbols on Android. |
22
- | Wire | `AstryxNode`, `AstryxAction` | Each component as JSON a TypeScript core sends a native host (arm C), and the action a host sends back when a view calls a callback. Swift decodes `<Name>Node` and turns it into `<Name>Props` with `viewProps(send:)`. |
23
- | Logic core | `@astryxdesign/native/logic` | `createLogicCore` holds a screen's state, builds its node tree, and reduces actions; `connectBridge` carries it to a native host as JSON. ECMAScript built-ins only, so it runs in Hermes. Shaped after EJ Campbell's astryx-swift spike. |
24
-
25
- Contracts cover Button, Card, Divider, Heading, HStack, ProgressBar, Selector,
26
- Switch, Text, TextInput, and VStack. React Native and Android bindings are not built yet; see
15
+ | Part | Path | What it does |
16
+ | -------------- | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
17
+ | Contracts | `src/contract/` | Reads a core component's documentation and splits its props into a portable **core contract** and a **web-only** part, with a reason for each web-only prop. Checks prop names against Swift and Kotlin keywords and platform toolkit names. |
18
+ | Platform types | `@astryxdesign/native/components` | TypeScript props per platform, chosen by type-resolution condition: `ios`, `android`, `macos`, `windows`, or `shared` for the core contract only. |
19
+ | Swift binding | `apple/` | A Swift package with two products: `AstryxNative`, props generated from the same contracts (iOS and macOS props sit behind `#if os(...)`) plus the logic host, and `Astryx`, SwiftUI views drawn with core's tokens. Clone-only page templates sit in `apple/Templates/`. See [its README](apple/README.md). |
20
+ | Native theme | `defineNativeTheme` | Durable theme intent (`material: 'opaque' \| 'translucent'`) plus platform entries scoped by OS version, such as Liquid Glass from iOS 26. Never sets tokens. |
21
+ | Icons | `NATIVE_ICON_DEFAULTS` | Astryx's built-in icon names mapped to SF Symbols on Apple platforms and Material Symbols on Android. |
22
+ | Wire | `AstryxNode`, `AstryxAction` | Each component as JSON a TypeScript core sends a native host (arm C), and the action a host sends back when a view calls a callback. Swift decodes `<Name>Node` and turns it into `<Name>Props` with `viewProps(send:)`. |
23
+ | Logic core | `@astryxdesign/native/logic` | `createLogicCore` holds a screen's state, builds its node tree, and reduces actions; `connectBridge` carries it to a native host as JSON. ECMAScript built-ins only, so it runs in Hermes. Shaped after EJ Campbell's astryx-swift spike. |
24
+ | Logic host | `AstryxLogicHost` (Swift) | Runs a logic core bundle in JavaScriptCore, decodes each tree into `AstryxNode`, and sends views' actions back. A Hermes host speaks the same bridge. |
25
+
26
+ Contracts cover Badge, Button, Card, Divider, Heading, HStack, ProgressBar,
27
+ Selector, Switch, Text, TextInput, and VStack. The `Astryx` Swift product draws
28
+ Text, Heading, Button, TextInput, Card, and Banner, and `apple/Templates/` the
29
+ `login` template, in SwiftUI.
30
+ React Native and Android bindings are not built yet; see
27
31
  [the experiment matrix](#experiment-matrix).
28
32
 
29
33
  ```ts
@@ -90,14 +94,17 @@ const action: AstryxAction = {
90
94
  ```
91
95
 
92
96
  ```swift
93
- let node = try JSONDecoder().decode(AstryxNode.self, from: snapshot)
94
- if case .switch(let wifi) = node {
95
- let props = wifi.viewProps { action in core.dispatch(action.json) }
97
+ let host = AstryxLogicHost(bundle: bundleSource) // calls the bundle's global astryxStart(host)
98
+ if case .switch(let wifi) = host.root {
99
+ let props = wifi.viewProps(send: host.send)
96
100
  }
97
101
  ```
98
102
 
99
103
  ```ts
104
+ // tsconfig: "customConditions": ["ios"]
105
+ import type {AstryxAction, AstryxNode} from '@astryxdesign/native/components';
100
106
  import {connectBridge, createLogicCore} from '@astryxdesign/native/logic';
107
+ import type {Bridge, BridgeHost} from '@astryxdesign/native/logic';
101
108
 
102
109
  const core = createLogicCore<{wifi: boolean}, AstryxNode, AstryxAction>({
103
110
  initial: {wifi: true},
@@ -112,13 +119,10 @@ const core = createLogicCore<{wifi: boolean}, AstryxNode, AstryxAction>({
112
119
  ? {wifi: action.args[0]}
113
120
  : state,
114
121
  });
115
- // In the app's JavaScript engine: the host reads snapshots and dispatches actions.
116
- const bridge = connectBridge(core, {
117
- connected() {},
118
- changed(json) {
119
- /* to Swift */
120
- },
121
- });
122
+ // The bundle's entry: the native host calls it with its two calls in and
123
+ // gets back the bridge it reads snapshots from and dispatches actions to.
124
+ (globalThis as {astryxStart?: (host: BridgeHost) => Bridge}).astryxStart =
125
+ host => connectBridge(core, host);
122
126
  ```
123
127
 
124
128
  ## Working on it
@@ -128,6 +132,7 @@ pnpm -F @astryxdesign/native typecheck # source and tests
128
132
  pnpm -F @astryxdesign/native build # dist/, including platform types
129
133
  pnpm -F @astryxdesign/native typecheck:types # every platform's types, after build
130
134
  pnpm -F @astryxdesign/native generate:apple # regenerate apple/Sources/.../Generated
135
+ pnpm -F @astryxdesign/native generate:swift-ios # regenerate the Astryx product's typed tokens, every published theme, and strings
131
136
  pnpm vitest run --project node packages/native
132
137
  ```
133
138
 
@@ -144,8 +149,23 @@ files are ignored on `main`.
144
149
  Boundaries, checked by `pnpm check:package-boundaries`:
145
150
 
146
151
  - `@astryxdesign/core` never imports this package.
147
- - This package's source never imports core. It reads core's documentation
148
- files as data at build time.
152
+ - No file in this package, in any folder, imports core, apart from the
153
+ build-time reads AST-082 FR16 lists until core's portable entry ships: the
154
+ theme generator in `scripts/` may import `@astryxdesign/core/theme` and
155
+ `@astryxdesign/core/theme/tokens`; `scripts/swift-ios-strings.mjs` may
156
+ read core's locale catalogs (`@astryxdesign/core/locales/*.json`); and
157
+ `scripts/core-docs.mjs` reads core's component documentation and built-in
158
+ icon names as data.
159
+ - The check reads source text in every folder except `node_modules`, `dist`,
160
+ `.build`, and `.swiftpm`. It flags any other module specifier that names
161
+ core (`import`, `require`, `require.resolve`, `import.meta.resolve`, or a
162
+ relative path into `packages/core`). It also flags a path to core's package,
163
+ written as adjacent string segments (`'@astryxdesign', 'core'`,
164
+ `'node_modules', '@astryxdesign/core'`, `'packages', 'core'`) or inside one
165
+ string (`node_modules/@astryxdesign/core`, `packages/core`). In
166
+ `scripts/core-docs.mjs` it allows exactly one such path, the `CORE_ROOT`
167
+ definition, and only the listed reads under `CORE_ROOT`. It does not follow
168
+ paths computed in other ways.
149
169
 
150
170
  ## Experiment matrix
151
171
 
@@ -1,18 +1,35 @@
1
1
  // swift-tools-version: 6.0
2
2
  // Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
 
4
- // Experimental Astryx for Apple platforms (AST-082). The contract types under
5
- // Sources/AstryxNative/Generated are generated by `pnpm generate:apple`; do not
6
- // edit them by hand.
4
+ // Experimental Astryx for Apple platforms (AST-082). Two products:
5
+ // - `Astryx`: SwiftUI views painted with the theme's tokens, the theme
6
+ // environment, and core's localized messages (Sources/Astryx).
7
+ // - `AstryxNative`: the contract types generated by `pnpm generate:apple`
8
+ // (Sources/AstryxNative/Generated; do not edit them by hand) and the logic
9
+ // host.
10
+ // Page templates are a separate package in Templates/, with no products,
11
+ // because they are clone-only source and are never published.
7
12
 
8
13
  import PackageDescription
9
14
 
10
15
  let package = Package(
11
16
  name: "AstryxNative",
17
+ defaultLocalization: "en",
12
18
  platforms: [.iOS(.v17), .macOS(.v14)],
13
- products: [.library(name: "AstryxNative", targets: ["AstryxNative"])],
19
+ products: [
20
+ .library(name: "Astryx", targets: ["Astryx"]),
21
+ .library(name: "AstryxNative", targets: ["AstryxNative"]),
22
+ ],
14
23
  targets: [
15
24
  .target(name: "AstryxNative", path: "Sources/AstryxNative"),
25
+ // Resources holds core's catalog messages per locale, generated.
26
+ .target(
27
+ name: "Astryx",
28
+ dependencies: ["AstryxNative"],
29
+ path: "Sources/Astryx",
30
+ resources: [.process("Resources")]
31
+ ),
16
32
  .testTarget(name: "AstryxNativeTests", dependencies: ["AstryxNative"], path: "Tests/AstryxNativeTests"),
33
+ .testTarget(name: "AstryxTests", dependencies: ["Astryx"], path: "Tests/AstryxTests"),
17
34
  ]
18
35
  )
@@ -0,0 +1,192 @@
1
+ # Astryx for Apple platforms
2
+
3
+ > **Experimental. Canary only.** Part of `@astryxdesign/native`. APIs change
4
+ > without notice.
5
+
6
+ One Swift package with two products:
7
+
8
+ - **`Astryx`**: SwiftUI views painted with the theme's tokens, the theme
9
+ environment, and core's localized messages. It is arm A of the AST-082
10
+ experiment (pure native: platform views painted with Astryx's tokens).
11
+ - **`AstryxNative`**: contract types generated from core's component
12
+ documentation, the wire a TypeScript logic core sends, and the logic host
13
+ that runs one (arm C).
14
+
15
+ Page templates built from the views are a separate package with no products,
16
+ because templates are clone-only and never published.
17
+
18
+ ## Layout
19
+
20
+ ```text
21
+ apple/
22
+ ├── Package.swift # products Astryx and AstryxNative
23
+ ├── Sources/
24
+ │ ├── Astryx/ # SwiftUI views (module Astryx)
25
+ │ │ ├── Theme/ # tokens, the theme environment, Dynamic Type
26
+ │ │ │ └── Generated/ # typed tokens and every published theme, generated; do not edit
27
+ │ │ ├── Resources/ # core's messages per locale, generated; do not edit
28
+ │ │ ├── Localization/
29
+ │ │ ├── Text/ # Text
30
+ │ │ ├── Heading/ # Heading
31
+ │ │ ├── Button/ # Button
32
+ │ │ ├── TextInput/ # TextInput
33
+ │ │ ├── Card/ # Card
34
+ │ │ └── Banner/ # Banner
35
+ │ └── AstryxNative/ # contracts, wire, logic host (module AstryxNative)
36
+ │ └── Generated/ # generated; do not edit
37
+ ├── Tests/
38
+ │ ├── AstryxTests/
39
+ │ └── AstryxNativeTests/
40
+ └── Templates/ # page templates to copy; its own package, never published
41
+ ├── Package.swift # no products; compiles and tests the templates
42
+ ├── Login/ # LoginTemplate, from `astryx template login`
43
+ └── Tests/
44
+ ```
45
+
46
+ - **`Sources/Astryx/<Name>/`** holds one folder per component, named as in
47
+ `packages/core/src/<Name>/`. Each view has core's component name in the
48
+ `Astryx` module (`Button`, `Text`), which re-exports SwiftUI and the
49
+ generated contracts, so an app file writes `import Astryx` alone. In a file
50
+ that also imports SwiftUI, write `Astryx.Button` and `SwiftUI.Button`. A
51
+ component's parameters use core's prop names
52
+ and values (`variant: .primary`, `size: .lg`, `isLoading`), so code reads
53
+ the same across web and native, with six exceptions, the interim shape
54
+ AST-093 FR5 exempts until the components move onto the generated contracts:
55
+ - `Button` takes `action` where core has `onClick`.
56
+ - `Button` takes `fillsWidth` where core has `width="100%"`.
57
+ - `Button` takes `icon` as a shared icon name (`"search"`), which the
58
+ generated `ButtonProps` does not carry yet.
59
+ - `TextInput` takes a `Binding` where core has `value` and `onChange`.
60
+ - `Heading`'s `level` is optional where core requires it.
61
+ - `Card` takes `padding` as a whole spacing step, so it cannot spell
62
+ core's `0.5` and `1.5` steps.
63
+ - **`Templates/`** holds one folder per page template, named after the
64
+ `astryx template` id. Templates are clone-only, as on the web: you copy a
65
+ template's folder into your app and own it from then on. They are a
66
+ separate package with no products, so nothing imports them, and
67
+ `@astryxdesign/native` leaves them out of its npm package. A template is
68
+ built only from `Astryx` views and SwiftUI layout, and matches the web
69
+ template's content and states. Rules a template needs, such as validation,
70
+ live in a plain value type beside the view so tests can check them without a
71
+ screen.
72
+
73
+ ## Use it
74
+
75
+ Depend on the `Astryx` product. To start from a template, copy its folder,
76
+ such as `Templates/Login/`, into your app's sources; it then belongs to your
77
+ app.
78
+
79
+ ```swift
80
+ import Astryx
81
+
82
+ @main
83
+ struct DemoApp: App {
84
+ var body: some Scene {
85
+ WindowGroup {
86
+ LoginTemplate().astryxTheme() // copied from Templates/Login/
87
+ }
88
+ }
89
+ }
90
+ ```
91
+
92
+ `astryxTheme(_:)` paints a subtree with a theme; without it, components use
93
+ core's default theme. Either way they follow Dark Mode and the person's text
94
+ size.
95
+
96
+ ## Theme tokens
97
+
98
+ Components read every color, size, and font from the `astryx` environment
99
+ value, never from a constant of their own. `astryx.tokens` holds the theme's
100
+ tokens in the current mode as typed members (`tokens.color.textPrimary`,
101
+ `tokens.space.s4`, `tokens.font.bodySize`), so a misspelled token fails to
102
+ compile. Text sizes grow with Dynamic Type. The components still read some
103
+ tokens by core's name (`astryx.color("color-accent")`); moving them onto the
104
+ typed members comes next.
105
+
106
+ Font families come from the theme's `font-family-*` stacks: the first family
107
+ the device has wins, and a system family such as `-apple-system` or
108
+ `sans-serif` is the system font. Icons use core's shared icon names
109
+ (`success`, `warning`, …), drawn as an SF Symbol by default; a theme replaces
110
+ one with `Theme(…, icons: ["success": "checkmark.seal.fill"])`.
111
+
112
+ `Sources/Astryx/Theme/Generated/` is written by the Swift theme generator
113
+ (`../specs/swift/library/theme-generator.md`) from core's own resolver, light
114
+ and dark:
115
+
116
+ - `Tokens.swift`: the typed token surface, from core's token names.
117
+ - One `<Name>Theme.swift` per theme, declaring `Theme.<name>`: core's
118
+ defaults (`.default`) and every published theme package (`.butter`,
119
+ `.chocolate`, `.gothic`, `.matcha`, `.neutral`, `.stone`, `.y2k`).
120
+
121
+ ```swift
122
+ LoginTemplate().astryxTheme(.neutral)
123
+ ```
124
+
125
+ Regenerate after a theme or core token change:
126
+
127
+ ```sh
128
+ pnpm -F @astryxdesign/native generate:swift-ios
129
+ ```
130
+
131
+ A product generates its own theme the same way, from its theme's module and
132
+ export:
133
+
134
+ ```sh
135
+ node packages/native/scripts/swift-ios-theme.mjs --theme ./src/acmeTheme.ts#acmeTheme --out ./Sources/Theme
136
+ ```
137
+
138
+ A theme's local tokens are not generated yet; the generator names each one it
139
+ leaves out.
140
+
141
+ **Fonts.** A theme's font families are carried as data. Astryx never bundles
142
+ a font: a component walks the theme's family list in order and uses the first
143
+ family the app has registered; a system family in the list (such as
144
+ `-apple-system` or `sans-serif`) is the system font, and a list with no
145
+ registered family falls back to the system font. To draw a theme's brand face, bundle and register it
146
+ in the app. Faces the published themes name (body, heading, code):
147
+
148
+ | Theme | Faces to bundle |
149
+ | ------------ | ------------------------------------------ |
150
+ | `.default` | none (system fonts) |
151
+ | `.butter` | Outfit, JetBrains Mono |
152
+ | `.chocolate` | Albert Sans, Fraunces, JetBrains Mono |
153
+ | `.gothic` | Fustat, JetBrains Mono |
154
+ | `.matcha` | DM Sans, Playwrite US Trad, JetBrains Mono |
155
+ | `.neutral` | Figtree |
156
+ | `.stone` | Figtree, Montserrat, JetBrains Mono |
157
+ | `.y2k` | Poppins, JetBrains Mono |
158
+
159
+ The same command writes
160
+ `Sources/Astryx/Resources/<locale>.lproj/Localizable.strings` from core's
161
+ locale catalogs, so words the components speak to assistive technology, such
162
+ as a loading button's "Loading", follow the person's language. Tests in
163
+ `@astryxdesign/native` fail when either committed output drifts from core.
164
+
165
+ ## Working on it
166
+
167
+ On a Mac with Xcode or the Swift command-line tools:
168
+
169
+ ```sh
170
+ cd packages/native/apple
171
+ swift test # both products
172
+ cd Templates && swift test # templates, against the Astryx product
173
+ ```
174
+
175
+ Both packages also declare macOS 14 so they build and test on a Mac without a
176
+ simulator; iOS 17 is the target. Open `Templates/Package.swift` in Xcode to
177
+ run `LoginTemplate` in an iOS simulator through its preview.
178
+
179
+ To add a component, create `Sources/Astryx/<Name>/<Name>.swift` declaring
180
+ `<Name>` with core's prop names, writing `SwiftUI.<Name>` wherever it means the
181
+ system type, read its tokens from `astryx`, and add tests under
182
+ `Tests/AstryxTests/`. To add a template, create
183
+ `Templates/<Name>/<Name>Template.swift` from the output of
184
+ `astryx template <id>`, with tests under `Templates/Tests/`.
185
+
186
+ ## Not here yet
187
+
188
+ - The views do not use the generated contract types in `AstryxNative` yet.
189
+ Moving them onto those types is how a core prop change reaches Swift.
190
+ - Pixel and behavior parity with the web components has not been measured.
191
+ - Increase Contrast, theme-local tokens, and platform theme entries
192
+ (`defineNativeTheme`, reserved as `Theme.native`) are not wired in.
@@ -0,0 +1,80 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ import SwiftUI
4
+
5
+ /// Core's `Banner` statuses.
6
+ public enum AstryxBannerStatus: String, CaseIterable, Sendable {
7
+ case info
8
+ case warning
9
+ case error
10
+ case success
11
+
12
+ /// The shared icon name for this status (AST-082 FR17), drawn as the
13
+ /// theme's or the platform's symbol for it.
14
+ var iconName: String { rawValue }
15
+
16
+ var groundToken: String {
17
+ switch self {
18
+ case .info: "color-accent-muted"
19
+ case .warning: "color-warning-muted"
20
+ case .error: "color-error-muted"
21
+ case .success: "color-success-muted"
22
+ }
23
+ }
24
+
25
+ var iconToken: String {
26
+ switch self {
27
+ case .info: "color-accent"
28
+ case .warning: "color-warning"
29
+ case .error: "color-error"
30
+ case .success: "color-success"
31
+ }
32
+ }
33
+ }
34
+
35
+ /// A message about the page or a section of it. The status sets the color
36
+ /// and icon only; the title must say what happened, because the icon is
37
+ /// decorative to VoiceOver.
38
+ public struct Banner: View {
39
+ let status: AstryxBannerStatus
40
+ let title: String
41
+ let description: String?
42
+ @Environment(\.astryx) private var astryx
43
+
44
+ public init(status: AstryxBannerStatus, title: String, description: String? = nil) {
45
+ self.status = status
46
+ self.title = title
47
+ self.description = description
48
+ }
49
+
50
+ public var body: some View {
51
+ HStack(alignment: .firstTextBaseline, spacing: astryx.space(2)) {
52
+ if let symbol = astryx.icon(status.iconName) {
53
+ // Sized with the title so it grows with Dynamic Type.
54
+ Image(systemName: symbol)
55
+ .font(astryx.font(size: "text-body-size", 14, weight: "text-body-weight"))
56
+ .foregroundStyle(astryx.color(status.iconToken))
57
+ .accessibilityHidden(true)
58
+ }
59
+ VStack(alignment: .leading, spacing: astryx.space(1)) {
60
+ Text(title, weight: .semibold)
61
+ if let description {
62
+ Text(description, color: .secondary)
63
+ }
64
+ }
65
+ .frame(maxWidth: .infinity, alignment: .leading)
66
+ }
67
+ .padding(astryx.space(3))
68
+ .background(
69
+ RoundedRectangle(cornerRadius: astryx.size("radius-container", 12), style: .continuous)
70
+ .fill(astryx.color(status.groundToken))
71
+ )
72
+ .accessibilityElement(children: .ignore)
73
+ .accessibilityLabel(SwiftUI.Text(accessibilityText))
74
+ }
75
+
76
+ /// What VoiceOver reads: the title, then the description.
77
+ var accessibilityText: String {
78
+ description.map { "\(title), \($0)" } ?? title
79
+ }
80
+ }
@@ -0,0 +1,167 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ // Adapted from github.com/ejc3/astryx-swift (MIT).
3
+
4
+ import SwiftUI
5
+
6
+ /// Core's `Button` variants.
7
+ public enum AstryxButtonVariant: String, CaseIterable, Sendable {
8
+ case primary
9
+ case secondary
10
+ case ghost
11
+ case destructive
12
+ }
13
+
14
+ /// Core's `Button` sizes: `size-element-sm`, `-md`, and `-lg` high.
15
+ public enum AstryxButtonSize: String, CaseIterable, Sendable {
16
+ case sm
17
+ case md
18
+ case lg
19
+
20
+ var heightToken: String { "size-element-\(rawValue)" }
21
+ var fallbackHeight: CGFloat {
22
+ switch self {
23
+ case .sm: 28
24
+ case .md: 32
25
+ case .lg: 36
26
+ }
27
+ }
28
+ }
29
+
30
+ /// A labeled action, painted with the theme's tokens. The tap area reaches
31
+ /// the platform's 44-by-44-point minimum even when the drawn button is smaller.
32
+ public struct Button: View {
33
+ let label: String
34
+ let variant: AstryxButtonVariant
35
+ let size: AstryxButtonSize
36
+ let icon: String?
37
+ let isLoading: Bool
38
+ let isDisabled: Bool
39
+ let fillsWidth: Bool
40
+ let action: () -> Void
41
+ @Environment(\.astryx) private var astryx
42
+
43
+ /// - Parameters:
44
+ /// - icon: a shared icon name, such as `"search"`, shown before the
45
+ /// label; the theme's symbol for it, else the platform default.
46
+ /// - fillsWidth: the button takes the whole width it is offered, as
47
+ /// core's `width="100%"` does.
48
+ public init(
49
+ label: String,
50
+ variant: AstryxButtonVariant = .secondary,
51
+ size: AstryxButtonSize = .md,
52
+ icon: String? = nil,
53
+ isLoading: Bool = false,
54
+ isDisabled: Bool = false,
55
+ fillsWidth: Bool = false,
56
+ action: @escaping () -> Void
57
+ ) {
58
+ self.label = label
59
+ self.variant = variant
60
+ self.size = size
61
+ self.icon = icon
62
+ self.isLoading = isLoading
63
+ self.isDisabled = isDisabled
64
+ self.fillsWidth = fillsWidth
65
+ self.action = action
66
+ }
67
+
68
+ public var body: some View {
69
+ SwiftUI.Button(action: activate) {
70
+ // The label keeps its place while loading so the width holds; the
71
+ // spinner draws over it.
72
+ HStack(spacing: astryx.space(2)) {
73
+ if let icon, let symbol = astryx.icon(icon) {
74
+ Image(systemName: symbol)
75
+ }
76
+ SwiftUI.Text(label).lineLimit(1)
77
+ }
78
+ .opacity(isLoading ? 0 : 1)
79
+ .overlay {
80
+ if isLoading {
81
+ ProgressView().controlSize(.small).tint(foreground)
82
+ }
83
+ }
84
+ }
85
+ .buttonStyle(
86
+ AstryxButtonStyle(
87
+ astryx: astryx,
88
+ size: size,
89
+ fillsWidth: fillsWidth,
90
+ foreground: foreground,
91
+ background: background
92
+ )
93
+ )
94
+ // As core's Button: a loading button stays undimmed and focusable, keeps
95
+ // its label, and says Loading; taps do nothing until it finishes.
96
+ .disabled(isDisabled)
97
+ .accessibilityLabel(SwiftUI.Text(accessibilityText))
98
+ .accessibilityAddTraits(Self.accessibilityTraits)
99
+ .accessibilityValue(SwiftUI.Text(loadingValue ?? ""))
100
+ .onChange(of: isLoading) { _, loading in
101
+ if loading { AccessibilityNotification.Announcement(AstryxStrings.buttonLoading).post() }
102
+ }
103
+ }
104
+
105
+ /// The button's accessible name: its label, also while loading.
106
+ var accessibilityText: String { label }
107
+
108
+ /// The traits assistive technology gets: a button.
109
+ static var accessibilityTraits: AccessibilityTraits { .isButton }
110
+
111
+ /// What assistive technology reads after the label while loading: core's
112
+ /// catalog message, in the person's language.
113
+ var loadingValue: String? { isLoading ? AstryxStrings.buttonLoading : nil }
114
+
115
+ /// Runs the action unless the button is loading.
116
+ func activate() {
117
+ guard !isLoading else { return }
118
+ action()
119
+ }
120
+
121
+ private var foreground: Color {
122
+ switch variant {
123
+ case .primary: astryx.color("color-on-accent")
124
+ case .destructive: astryx.color("color-on-error")
125
+ case .secondary, .ghost: astryx.color("color-text-primary")
126
+ }
127
+ }
128
+
129
+ private var background: Color {
130
+ switch variant {
131
+ case .primary: astryx.color("color-accent")
132
+ case .destructive: astryx.color("color-error")
133
+ case .secondary: astryx.color("color-neutral")
134
+ case .ghost: .clear
135
+ }
136
+ }
137
+ }
138
+
139
+ struct AstryxButtonStyle: ButtonStyle {
140
+ let astryx: AstryxStyle
141
+ let size: AstryxButtonSize
142
+ let fillsWidth: Bool
143
+ let foreground: Color
144
+ let background: Color
145
+ @Environment(\.isEnabled) private var isEnabled
146
+
147
+ func makeBody(configuration: Configuration) -> some View {
148
+ let shape = RoundedRectangle(
149
+ cornerRadius: astryx.size("radius-element", 8),
150
+ style: .continuous
151
+ )
152
+ // Grows with text size, but not so far that one line of a large label
153
+ // becomes a tall slab.
154
+ let height = astryx.size(size.heightToken, size.fallbackHeight) * min(astryx.textScale, 1.4)
155
+ configuration.label
156
+ .font(astryx.font(size: "text-label-size", 14, weight: "text-label-weight"))
157
+ .foregroundStyle(foreground)
158
+ .padding(.horizontal, astryx.space(3))
159
+ .frame(maxWidth: fillsWidth ? .infinity : nil, minHeight: height)
160
+ .background(shape.fill(background))
161
+ .opacity(isEnabled ? (configuration.isPressed ? 0.75 : 1) : 0.5)
162
+ // Drawn at the token size; laid out at least 44 by 44 so the whole
163
+ // platform-minimum tap area counts.
164
+ .frame(minWidth: 44, minHeight: 44)
165
+ .contentShape(Rectangle())
166
+ }
167
+ }
@@ -0,0 +1,33 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ import SwiftUI
4
+
5
+ /// A card: one independent item on the theme's card ground, with a hairline
6
+ /// border and the container radius.
7
+ public struct Card<Content: View>: View {
8
+ let padding: Int
9
+ let content: Content
10
+ @Environment(\.astryx) private var astryx
11
+
12
+ /// - Parameter padding: a step on the spacing ladder, as core's `padding`.
13
+ public init(padding: Int = 4, @ViewBuilder content: () -> Content) {
14
+ self.padding = padding
15
+ self.content = content()
16
+ }
17
+
18
+ public var body: some View {
19
+ let shape = RoundedRectangle(
20
+ cornerRadius: astryx.size("radius-container", 12),
21
+ style: .continuous
22
+ )
23
+ // Several child views stack in one card, top to bottom with no gap of
24
+ // their own, as children flow inside core's Card.
25
+ VStack(alignment: .leading, spacing: 0) { content }
26
+ .padding(astryx.space(padding))
27
+ .frame(maxWidth: .infinity, alignment: .leading)
28
+ .background(shape.fill(astryx.color("color-background-card")))
29
+ .overlay(
30
+ shape.strokeBorder(astryx.color("color-border"), lineWidth: astryx.size("border-width", 1))
31
+ )
32
+ }
33
+ }
@@ -0,0 +1,8 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ // `import Astryx` is the only import an app file needs: it brings SwiftUI and
4
+ // the generated contracts with it. In a file that imports only Astryx, a name
5
+ // Astryx shares with SwiftUI (`Button`, `Text`) means Astryx's view, and
6
+ // `SwiftUI.Button` names the system one (Swift component API spec, A1, A1a).
7
+ @_exported import AstryxNative
8
+ @_exported import SwiftUI