@astryxdesign/native 0.7.5-canary.45b0afd → 0.7.5-canary.4bcd84f

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 (107) hide show
  1. package/README.md +16 -12
  2. package/apple/Package.swift +27 -4
  3. package/apple/README.md +202 -0
  4. package/apple/Sources/Astryx/Banner/Banner.swift +80 -0
  5. package/apple/Sources/Astryx/Button/Button.swift +168 -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/Icon/Icon.spec.md +186 -0
  10. package/apple/Sources/Astryx/Icon/Icon.swift +155 -0
  11. package/apple/Sources/Astryx/Localization/AstryxStrings.swift +22 -0
  12. package/apple/Sources/Astryx/Resources/af.lproj/Localizable.strings +4 -0
  13. package/apple/Sources/Astryx/Resources/ar.lproj/Localizable.strings +4 -0
  14. package/apple/Sources/Astryx/Resources/ca.lproj/Localizable.strings +4 -0
  15. package/apple/Sources/Astryx/Resources/cs.lproj/Localizable.strings +4 -0
  16. package/apple/Sources/Astryx/Resources/da.lproj/Localizable.strings +4 -0
  17. package/apple/Sources/Astryx/Resources/de.lproj/Localizable.strings +4 -0
  18. package/apple/Sources/Astryx/Resources/el.lproj/Localizable.strings +4 -0
  19. package/apple/Sources/Astryx/Resources/en.lproj/Localizable.strings +4 -0
  20. package/apple/Sources/Astryx/Resources/es.lproj/Localizable.strings +4 -0
  21. package/apple/Sources/Astryx/Resources/fi.lproj/Localizable.strings +4 -0
  22. package/apple/Sources/Astryx/Resources/fr.lproj/Localizable.strings +4 -0
  23. package/apple/Sources/Astryx/Resources/he.lproj/Localizable.strings +4 -0
  24. package/apple/Sources/Astryx/Resources/hu.lproj/Localizable.strings +4 -0
  25. package/apple/Sources/Astryx/Resources/it.lproj/Localizable.strings +4 -0
  26. package/apple/Sources/Astryx/Resources/ja.lproj/Localizable.strings +4 -0
  27. package/apple/Sources/Astryx/Resources/ko.lproj/Localizable.strings +4 -0
  28. package/apple/Sources/Astryx/Resources/nb.lproj/Localizable.strings +4 -0
  29. package/apple/Sources/Astryx/Resources/nl.lproj/Localizable.strings +4 -0
  30. package/apple/Sources/Astryx/Resources/pl.lproj/Localizable.strings +4 -0
  31. package/apple/Sources/Astryx/Resources/pt-BR.lproj/Localizable.strings +4 -0
  32. package/apple/Sources/Astryx/Resources/pt-PT.lproj/Localizable.strings +4 -0
  33. package/apple/Sources/Astryx/Resources/ro.lproj/Localizable.strings +4 -0
  34. package/apple/Sources/Astryx/Resources/ru.lproj/Localizable.strings +4 -0
  35. package/apple/Sources/Astryx/Resources/sr.lproj/Localizable.strings +4 -0
  36. package/apple/Sources/Astryx/Resources/sv.lproj/Localizable.strings +4 -0
  37. package/apple/Sources/Astryx/Resources/tr.lproj/Localizable.strings +4 -0
  38. package/apple/Sources/Astryx/Resources/uk.lproj/Localizable.strings +4 -0
  39. package/apple/Sources/Astryx/Resources/vi.lproj/Localizable.strings +4 -0
  40. package/apple/Sources/Astryx/Resources/zh-Hans.lproj/Localizable.strings +4 -0
  41. package/apple/Sources/Astryx/Resources/zh-Hant.lproj/Localizable.strings +4 -0
  42. package/apple/Sources/Astryx/Text/Text.swift +83 -0
  43. package/apple/Sources/Astryx/TextInput/TextInput.swift +81 -0
  44. package/apple/Sources/Astryx/Theme/AstryxIcons.swift +108 -0
  45. package/apple/Sources/Astryx/Theme/Generated/ButterTheme.swift +493 -0
  46. package/apple/Sources/Astryx/Theme/Generated/ChocolateTheme.swift +493 -0
  47. package/apple/Sources/Astryx/Theme/Generated/DefaultTheme.swift +493 -0
  48. package/apple/Sources/Astryx/Theme/Generated/GothicTheme.swift +493 -0
  49. package/apple/Sources/Astryx/Theme/Generated/MatchaTheme.swift +493 -0
  50. package/apple/Sources/Astryx/Theme/Generated/NeutralTheme.swift +493 -0
  51. package/apple/Sources/Astryx/Theme/Generated/StoneTheme.swift +493 -0
  52. package/apple/Sources/Astryx/Theme/Generated/Tokens.swift +1260 -0
  53. package/apple/Sources/Astryx/Theme/Generated/Y2kTheme.swift +493 -0
  54. package/apple/Sources/Astryx/Theme/Theme.swift +273 -0
  55. package/apple/Sources/AstryxNative/Generated/AstryxNode.swift +3 -0
  56. package/apple/Sources/AstryxNative/Generated/Button.swift +14 -2
  57. package/apple/Sources/AstryxNative/Generated/Icon.swift +106 -0
  58. package/apple/Sources/AstryxNative/Generated/IconName.swift +47 -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/IconTests.swift +188 -0
  65. package/apple/Tests/AstryxTests/NamesTests.swift +28 -0
  66. package/apple/Tests/AstryxTests/Pixels.swift +68 -0
  67. package/apple/Tests/AstryxTests/Resources/astryx-test-icon.png +0 -0
  68. package/apple/Tests/AstryxTests/StringsTests.swift +23 -0
  69. package/apple/Tests/AstryxTests/TextInputTests.swift +29 -0
  70. package/apple/Tests/AstryxTests/TextTests.swift +25 -0
  71. package/apple/Tests/AstryxTests/ThemeTests.swift +45 -0
  72. package/apple/Tests/AstryxTests/TwoThemeTests.swift +65 -0
  73. package/dist/components/Button.d.ts.map +1 -1
  74. package/dist/components/Button.js +1 -0
  75. package/dist/components/Icon.d.ts +10 -0
  76. package/dist/components/Icon.d.ts.map +1 -0
  77. package/dist/components/Icon.js +7 -0
  78. package/dist/components/index.d.ts.map +1 -1
  79. package/dist/components/index.js +2 -0
  80. package/dist/contract/fromComponentDoc.d.ts +11 -2
  81. package/dist/contract/fromComponentDoc.d.ts.map +1 -1
  82. package/dist/contract/fromComponentDoc.js +16 -0
  83. package/dist/contract/generateSwift.d.ts.map +1 -1
  84. package/dist/contract/generateSwift.js +33 -0
  85. package/dist/contract/generateTypeScript.d.ts.map +1 -1
  86. package/dist/contract/generateTypeScript.js +11 -2
  87. package/dist/contract/types.d.ts +9 -0
  88. package/dist/contract/types.d.ts.map +1 -1
  89. package/dist/types/android.d.ts +14 -4
  90. package/dist/types/core.d.ts +10 -0
  91. package/dist/types/ios.d.ts +14 -4
  92. package/dist/types/macos.d.ts +14 -4
  93. package/dist/types/shared.d.ts +14 -4
  94. package/dist/types/windows.d.ts +14 -4
  95. package/package.json +6 -4
  96. package/src/components/Button.ts +1 -0
  97. package/src/components/Icon.ts +18 -0
  98. package/src/components/index.ts +2 -0
  99. package/src/contract/contracts.test.ts +37 -0
  100. package/src/contract/fromComponentDoc.test.ts +29 -0
  101. package/src/contract/fromComponentDoc.ts +32 -2
  102. package/src/contract/generateSwift.ts +38 -0
  103. package/src/contract/generateTypeScript.ts +11 -2
  104. package/src/contract/types.ts +9 -0
  105. package/src/icons/swiftIcons.test.ts +22 -0
  106. package/src/theme/swiftIOSStrings.test.ts +49 -0
  107. package/src/theme/swiftIOSTheme.test.ts +223 -0
package/README.md CHANGED
@@ -12,20 +12,23 @@ 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
- | 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. |
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
25
 
26
26
  Contracts cover Badge, Button, Card, Divider, Heading, HStack, ProgressBar,
27
- Selector, Switch, Text, TextInput, and VStack. React Native and Android
28
- bindings are not built yet; see [the experiment matrix](#experiment-matrix).
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
31
+ [the experiment matrix](#experiment-matrix).
29
32
 
30
33
  ```ts
31
34
  // tsconfig.json for an iOS app: "customConditions": ["ios"]
@@ -129,6 +132,7 @@ pnpm -F @astryxdesign/native typecheck # source and tests
129
132
  pnpm -F @astryxdesign/native build # dist/, including platform types
130
133
  pnpm -F @astryxdesign/native typecheck:types # every platform's types, after build
131
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
132
136
  pnpm vitest run --project node packages/native
133
137
  ```
134
138
 
@@ -1,18 +1,41 @@
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
+ // Each component's spec lives beside its view (AST-093 FR1).
31
+ exclude: ["Icon/Icon.spec.md"],
32
+ resources: [.process("Resources")]
33
+ ),
16
34
  .testTarget(name: "AstryxNativeTests", dependencies: ["AstryxNative"], path: "Tests/AstryxNativeTests"),
35
+ .testTarget(
36
+ name: "AstryxTests", dependencies: ["Astryx"], path: "Tests/AstryxTests",
37
+ // A tiny image that stands in for an app-shipped icon asset.
38
+ resources: [.process("Resources")]
39
+ ),
17
40
  ]
18
41
  )
@@ -0,0 +1,202 @@
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
+ │ │ ├── Icon/ # Icon, Icon.spec.md
30
+ │ │ ├── Text/ # Text
31
+ │ │ ├── Heading/ # Heading
32
+ │ │ ├── Button/ # Button
33
+ │ │ ├── TextInput/ # TextInput
34
+ │ │ ├── Card/ # Card
35
+ │ │ └── Banner/ # Banner
36
+ │ └── AstryxNative/ # contracts, wire, logic host (module AstryxNative)
37
+ │ └── Generated/ # generated; do not edit
38
+ ├── Tests/
39
+ │ ├── AstryxTests/
40
+ │ └── AstryxNativeTests/
41
+ └── Templates/ # page templates to copy; its own package, never published
42
+ ├── Package.swift # no products; compiles and tests the templates
43
+ ├── Login/ # LoginTemplate, from `astryx template login`
44
+ └── Tests/
45
+ ```
46
+
47
+ - **`Sources/Astryx/<Name>/`** holds one folder per component, named as in
48
+ `packages/core/src/<Name>/`. Each view has core's component name in the
49
+ `Astryx` module (`Button`, `Text`), which re-exports SwiftUI and the
50
+ generated contracts, so an app file writes `import Astryx` alone. In a file
51
+ that also imports SwiftUI, write `Astryx.Button` and `SwiftUI.Button`. A
52
+ component's parameters use core's prop names
53
+ and values (`variant: .primary`, `size: .lg`, `isLoading`), so code reads
54
+ the same across web and native, with six exceptions, the interim shape
55
+ AST-093 FR5 exempts until the components move onto the generated contracts:
56
+ - `Button` takes `action` where core has `onClick`.
57
+ - `Button` takes `fillsWidth` where core has `width="100%"`.
58
+ - `Button` takes `icon` as a shared icon name (`"search"`), which the
59
+ generated `ButtonProps` does not carry yet.
60
+ - `TextInput` takes a `Binding` where core has `value` and `onChange`.
61
+ - `Heading`'s `level` is optional where core requires it.
62
+ - `Card` takes `padding` as a whole spacing step, so it cannot spell
63
+ core's `0.5` and `1.5` steps.
64
+ - **`Templates/`** holds one folder per page template, named after the
65
+ `astryx template` id. Templates are clone-only, as on the web: you copy a
66
+ template's folder into your app and own it from then on. They are a
67
+ separate package with no products, so nothing imports them, and
68
+ `@astryxdesign/native` leaves them out of its npm package. A template is
69
+ built only from `Astryx` views and SwiftUI layout, and matches the web
70
+ template's content and states. Rules a template needs, such as validation,
71
+ live in a plain value type beside the view so tests can check them without a
72
+ screen.
73
+
74
+ ## Use it
75
+
76
+ Depend on the `Astryx` product. To start from a template, copy its folder,
77
+ such as `Templates/Login/`, into your app's sources; it then belongs to your
78
+ app.
79
+
80
+ ```swift
81
+ import Astryx
82
+
83
+ @main
84
+ struct DemoApp: App {
85
+ var body: some Scene {
86
+ WindowGroup {
87
+ LoginTemplate().astryxTheme() // copied from Templates/Login/
88
+ }
89
+ }
90
+ }
91
+ ```
92
+
93
+ `astryxTheme(_:)` paints a subtree with a theme; without it, components use
94
+ core's default theme. Either way they follow Dark Mode and the person's text
95
+ size.
96
+
97
+ ## Theme tokens
98
+
99
+ Components read every color, size, and font from the `astryx` environment
100
+ value, never from a constant of their own. `astryx.tokens` holds the theme's
101
+ tokens in the current mode as typed members (`tokens.color.textPrimary`,
102
+ `tokens.space.s4`, `tokens.font.bodySize`), so a misspelled token fails to
103
+ compile. Text sizes grow with Dynamic Type. The components still read some
104
+ tokens by core's name (`astryx.color("color-accent")`); moving them onto the
105
+ typed members comes next.
106
+
107
+ `Icon(.search)` draws a shared icon name as the platform's SF Symbol, at
108
+ core's icon size and color, hidden from VoiceOver unless it has a `label`.
109
+ `IconName` holds every built-in name as a static member; a theme maps a name
110
+ to another symbol or to an image the app ships, or registers a
111
+ `<library>:<name>` name, with
112
+ `Theme(…, icons: ["acme:rocket": "airplane", "close": .assetName("brand.close")])`.
113
+ An asset the app does not ship stops a development build with a message that
114
+ names it, and draws nothing in a release build.
115
+
116
+ Font families come from the theme's `font-family-*` stacks: the first family
117
+ the device has wins, and a system family such as `-apple-system` or
118
+ `sans-serif` is the system font. Icons use core's shared icon names
119
+ (`success`, `warning`, …), drawn as an SF Symbol by default; a theme replaces
120
+ one with `Theme(…, icons: ["success": "checkmark.seal.fill"])`.
121
+
122
+ `Sources/Astryx/Theme/Generated/` is written by the Swift theme generator
123
+ (`../specs/swift/library/theme-generator.md`) from core's own resolver, light
124
+ and dark:
125
+
126
+ - `Tokens.swift`: the typed token surface, from core's token names.
127
+ - One `<Name>Theme.swift` per theme, declaring `Theme.<name>`: core's
128
+ defaults (`.default`) and every published theme package (`.butter`,
129
+ `.chocolate`, `.gothic`, `.matcha`, `.neutral`, `.stone`, `.y2k`).
130
+
131
+ ```swift
132
+ LoginTemplate().astryxTheme(.neutral)
133
+ ```
134
+
135
+ Regenerate after a theme or core token change:
136
+
137
+ ```sh
138
+ pnpm -F @astryxdesign/native generate:swift-ios
139
+ ```
140
+
141
+ A product generates its own theme the same way, from its theme's module and
142
+ export:
143
+
144
+ ```sh
145
+ node packages/native/scripts/swift-ios-theme.mjs --theme ./src/acmeTheme.ts#acmeTheme --out ./Sources/Theme
146
+ ```
147
+
148
+ A theme's local tokens are not generated yet; the generator names each one it
149
+ leaves out.
150
+
151
+ **Fonts.** A theme's font families are carried as data. Astryx never bundles
152
+ a font: a component walks the theme's family list in order and uses the first
153
+ family the app has registered; a system family in the list (such as
154
+ `-apple-system` or `sans-serif`) is the system font, and a list with no
155
+ registered family falls back to the system font. To draw a theme's brand face, bundle and register it
156
+ in the app. Faces the published themes name (body, heading, code):
157
+
158
+ | Theme | Faces to bundle |
159
+ | ------------ | ------------------------------------------ |
160
+ | `.default` | none (system fonts) |
161
+ | `.butter` | Outfit, JetBrains Mono |
162
+ | `.chocolate` | Albert Sans, Fraunces, JetBrains Mono |
163
+ | `.gothic` | Fustat, JetBrains Mono |
164
+ | `.matcha` | DM Sans, Playwrite US Trad, JetBrains Mono |
165
+ | `.neutral` | Figtree |
166
+ | `.stone` | Figtree, Montserrat, JetBrains Mono |
167
+ | `.y2k` | Poppins, JetBrains Mono |
168
+
169
+ The same command writes
170
+ `Sources/Astryx/Resources/<locale>.lproj/Localizable.strings` from core's
171
+ locale catalogs, so words the components speak to assistive technology, such
172
+ as a loading button's "Loading", follow the person's language. Tests in
173
+ `@astryxdesign/native` fail when either committed output drifts from core.
174
+
175
+ ## Working on it
176
+
177
+ On a Mac with Xcode or the Swift command-line tools:
178
+
179
+ ```sh
180
+ cd packages/native/apple
181
+ swift test # both products
182
+ cd Templates && swift test # templates, against the Astryx product
183
+ ```
184
+
185
+ Both packages also declare macOS 14 so they build and test on a Mac without a
186
+ simulator; iOS 17 is the target. Open `Templates/Package.swift` in Xcode to
187
+ run `LoginTemplate` in an iOS simulator through its preview.
188
+
189
+ To add a component, create `Sources/Astryx/<Name>/<Name>.swift` declaring
190
+ `<Name>` with core's prop names, writing `SwiftUI.<Name>` wherever it means the
191
+ system type, read its tokens from `astryx`, and add tests under
192
+ `Tests/AstryxTests/`. To add a template, create
193
+ `Templates/<Name>/<Name>Template.swift` from the output of
194
+ `astryx template <id>`, with tests under `Templates/Tests/`.
195
+
196
+ ## Not here yet
197
+
198
+ - The views do not use the generated contract types in `AstryxNative` yet.
199
+ Moving them onto those types is how a core prop change reaches Swift.
200
+ - Pixel and behavior parity with the web components has not been measured.
201
+ - Increase Contrast, theme-local tokens, and platform theme entries
202
+ (`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 icon = astryx.icon(status.iconName) {
53
+ // Sized with the title so it grows with Dynamic Type.
54
+ icon.sized(astryx.fontSize("text-body-size", 14))
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,168 @@
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 image = astryx.icon(icon) {
74
+ // An asset matches the label's text size, as a symbol does.
75
+ image.sized(astryx.fontSize("text-label-size", 14))
76
+ }
77
+ SwiftUI.Text(label).lineLimit(1)
78
+ }
79
+ .opacity(isLoading ? 0 : 1)
80
+ .overlay {
81
+ if isLoading {
82
+ ProgressView().controlSize(.small).tint(foreground)
83
+ }
84
+ }
85
+ }
86
+ .buttonStyle(
87
+ AstryxButtonStyle(
88
+ astryx: astryx,
89
+ size: size,
90
+ fillsWidth: fillsWidth,
91
+ foreground: foreground,
92
+ background: background
93
+ )
94
+ )
95
+ // As core's Button: a loading button stays undimmed and focusable, keeps
96
+ // its label, and says Loading; taps do nothing until it finishes.
97
+ .disabled(isDisabled)
98
+ .accessibilityLabel(SwiftUI.Text(accessibilityText))
99
+ .accessibilityAddTraits(Self.accessibilityTraits)
100
+ .accessibilityValue(SwiftUI.Text(loadingValue ?? ""))
101
+ .onChange(of: isLoading) { _, loading in
102
+ if loading { AccessibilityNotification.Announcement(AstryxStrings.buttonLoading).post() }
103
+ }
104
+ }
105
+
106
+ /// The button's accessible name: its label, also while loading.
107
+ var accessibilityText: String { label }
108
+
109
+ /// The traits assistive technology gets: a button.
110
+ static var accessibilityTraits: AccessibilityTraits { .isButton }
111
+
112
+ /// What assistive technology reads after the label while loading: core's
113
+ /// catalog message, in the person's language.
114
+ var loadingValue: String? { isLoading ? AstryxStrings.buttonLoading : nil }
115
+
116
+ /// Runs the action unless the button is loading.
117
+ func activate() {
118
+ guard !isLoading else { return }
119
+ action()
120
+ }
121
+
122
+ private var foreground: Color {
123
+ switch variant {
124
+ case .primary: astryx.color("color-on-accent")
125
+ case .destructive: astryx.color("color-on-error")
126
+ case .secondary, .ghost: astryx.color("color-text-primary")
127
+ }
128
+ }
129
+
130
+ private var background: Color {
131
+ switch variant {
132
+ case .primary: astryx.color("color-accent")
133
+ case .destructive: astryx.color("color-error")
134
+ case .secondary: astryx.color("color-neutral")
135
+ case .ghost: .clear
136
+ }
137
+ }
138
+ }
139
+
140
+ struct AstryxButtonStyle: ButtonStyle {
141
+ let astryx: AstryxStyle
142
+ let size: AstryxButtonSize
143
+ let fillsWidth: Bool
144
+ let foreground: Color
145
+ let background: Color
146
+ @Environment(\.isEnabled) private var isEnabled
147
+
148
+ func makeBody(configuration: Configuration) -> some View {
149
+ let shape = RoundedRectangle(
150
+ cornerRadius: astryx.size("radius-element", 8),
151
+ style: .continuous
152
+ )
153
+ // Grows with text size, but not so far that one line of a large label
154
+ // becomes a tall slab.
155
+ let height = astryx.size(size.heightToken, size.fallbackHeight) * min(astryx.textScale, 1.4)
156
+ configuration.label
157
+ .font(astryx.font(size: "text-label-size", 14, weight: "text-label-weight"))
158
+ .foregroundStyle(foreground)
159
+ .padding(.horizontal, astryx.space(3))
160
+ .frame(maxWidth: fillsWidth ? .infinity : nil, minHeight: height)
161
+ .background(shape.fill(background))
162
+ .opacity(isEnabled ? (configuration.isPressed ? 0.75 : 1) : 0.5)
163
+ // Drawn at the token size; laid out at least 44 by 44 so the whole
164
+ // platform-minimum tap area counts.
165
+ .frame(minWidth: 44, minHeight: 44)
166
+ .contentShape(Rectangle())
167
+ }
168
+ }
@@ -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
@@ -0,0 +1,46 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ // Adapted from github.com/ejc3/astryx-swift (MIT).
3
+
4
+ import SwiftUI
5
+
6
+ /// A heading at levels 1 through 6, painted with the theme's heading tokens
7
+ /// and exposed to VoiceOver as a heading.
8
+ public struct Heading: View {
9
+ let content: String
10
+ let level: Int
11
+ @Environment(\.astryx) private var astryx
12
+
13
+ public init(_ content: String, level: Int = 2) {
14
+ self.content = content
15
+ self.level = min(max(level, 1), 6)
16
+ }
17
+
18
+ public var body: some View {
19
+ SwiftUI.Text(content)
20
+ .font(
21
+ astryx.font(
22
+ size: "text-heading-\(level)-size",
23
+ 20,
24
+ weight: "text-heading-\(level)-weight",
25
+ family: "font-family-heading"
26
+ )
27
+ )
28
+ .foregroundStyle(astryx.color("color-text-primary"))
29
+ .accessibilityAddTraits(Self.accessibilityTraits)
30
+ .accessibilityHeading(headingLevel)
31
+ }
32
+
33
+ /// The traits assistive technology gets: a header.
34
+ static var accessibilityTraits: AccessibilityTraits { .isHeader }
35
+
36
+ var headingLevel: AccessibilityHeadingLevel {
37
+ switch level {
38
+ case 1: .h1
39
+ case 2: .h2
40
+ case 3: .h3
41
+ case 4: .h4
42
+ case 5: .h5
43
+ default: .h6
44
+ }
45
+ }
46
+ }