@astryxdesign/native 0.7.5-canary.577e8ca → 0.7.5-canary.5b691cd

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 (85) hide show
  1. package/apple/Package.swift +7 -1
  2. package/apple/README.md +19 -7
  3. package/apple/Sources/Astryx/Avatar/Avatar.spec.md +210 -0
  4. package/apple/Sources/Astryx/Avatar/Avatar.swift +418 -0
  5. package/apple/Sources/Astryx/Banner/Banner.swift +2 -2
  6. package/apple/Sources/Astryx/Base/BaseProps.swift +16 -0
  7. package/apple/Sources/Astryx/Button/Button.swift +3 -2
  8. package/apple/Sources/Astryx/Card/Card.swift +95 -19
  9. package/apple/Sources/Astryx/Heading/Heading.swift +90 -19
  10. package/apple/Sources/Astryx/Icon/Icon.spec.md +187 -0
  11. package/apple/Sources/Astryx/Icon/Icon.swift +156 -0
  12. package/apple/Sources/Astryx/Text/Text.swift +89 -70
  13. package/apple/Sources/Astryx/Text/TypeStyle.swift +143 -0
  14. package/apple/Sources/Astryx/Theme/AstryxIcons.swift +72 -1
  15. package/apple/Sources/Astryx/Theme/Generated/ButterTheme.swift +390 -406
  16. package/apple/Sources/Astryx/Theme/Generated/ChocolateTheme.swift +390 -406
  17. package/apple/Sources/Astryx/Theme/Generated/DefaultTheme.swift +390 -406
  18. package/apple/Sources/Astryx/Theme/Generated/GothicTheme.swift +390 -406
  19. package/apple/Sources/Astryx/Theme/Generated/MatchaTheme.swift +390 -406
  20. package/apple/Sources/Astryx/Theme/Generated/NeutralTheme.swift +390 -406
  21. package/apple/Sources/Astryx/Theme/Generated/StoneTheme.swift +390 -406
  22. package/apple/Sources/Astryx/Theme/Generated/Tokens.swift +391 -238
  23. package/apple/Sources/Astryx/Theme/Generated/Y2kTheme.swift +390 -406
  24. package/apple/Sources/Astryx/Theme/Shadow.swift +41 -0
  25. package/apple/Sources/Astryx/Theme/Theme.swift +93 -45
  26. package/apple/Sources/Astryx/Tooltip/TooltipPresenter.swift +551 -0
  27. package/apple/Sources/Astryx/Wire/AstryxNodeView.swift +77 -0
  28. package/apple/Sources/AstryxNative/Generated/AstryxNode.swift +6 -0
  29. package/apple/Sources/AstryxNative/Generated/Avatar.swift +163 -0
  30. package/apple/Sources/AstryxNative/Generated/Icon.swift +107 -0
  31. package/apple/Sources/AstryxNative/Generated/IconName.swift +47 -0
  32. package/apple/Sources/AstryxNative/Generated/Switch.swift +2 -26
  33. package/apple/Tests/AstryxTests/AvatarTests.swift +263 -0
  34. package/apple/Tests/AstryxTests/BannerTests.swift +1 -1
  35. package/apple/Tests/AstryxTests/CardTests.swift +143 -3
  36. package/apple/Tests/AstryxTests/EnvironmentTests.swift +92 -2
  37. package/apple/Tests/AstryxTests/HeadingTests.swift +41 -13
  38. package/apple/Tests/AstryxTests/IconTests.swift +189 -0
  39. package/apple/Tests/AstryxTests/Resources/astryx-test-icon.png +0 -0
  40. package/apple/Tests/AstryxTests/TextInputTests.swift +1 -1
  41. package/apple/Tests/AstryxTests/TextTests.swift +90 -6
  42. package/apple/Tests/AstryxTests/ThemeTests.swift +4 -10
  43. package/apple/Tests/AstryxTests/TooltipPresenterTests.swift +220 -0
  44. package/apple/Tests/AstryxTests/TwoThemeTests.swift +5 -5
  45. package/dist/components/Avatar.d.ts +10 -0
  46. package/dist/components/Avatar.d.ts.map +1 -0
  47. package/dist/components/Avatar.js +16 -0
  48. package/dist/components/Icon.d.ts +10 -0
  49. package/dist/components/Icon.d.ts.map +1 -0
  50. package/dist/components/Icon.js +7 -0
  51. package/dist/components/Switch.d.ts.map +1 -1
  52. package/dist/components/Switch.js +13 -0
  53. package/dist/components/index.d.ts.map +1 -1
  54. package/dist/components/index.js +4 -0
  55. package/dist/contract/fromComponentDoc.d.ts +14 -3
  56. package/dist/contract/fromComponentDoc.d.ts.map +1 -1
  57. package/dist/contract/fromComponentDoc.js +16 -1
  58. package/dist/contract/generateSwift.d.ts.map +1 -1
  59. package/dist/contract/generateSwift.js +61 -0
  60. package/dist/contract/generateTypeScript.d.ts.map +1 -1
  61. package/dist/contract/generateTypeScript.js +13 -2
  62. package/dist/contract/parseDocType.d.ts +2 -2
  63. package/dist/contract/parseDocType.d.ts.map +1 -1
  64. package/dist/contract/parseDocType.js +7 -2
  65. package/dist/contract/types.d.ts +11 -0
  66. package/dist/contract/types.d.ts.map +1 -1
  67. package/dist/types/android.d.ts +30 -4
  68. package/dist/types/core.d.ts +21 -2
  69. package/dist/types/ios.d.ts +27 -4
  70. package/dist/types/macos.d.ts +27 -4
  71. package/dist/types/shared.d.ts +27 -4
  72. package/dist/types/windows.d.ts +30 -4
  73. package/package.json +4 -4
  74. package/src/components/Avatar.ts +27 -0
  75. package/src/components/Icon.ts +18 -0
  76. package/src/components/Switch.ts +13 -0
  77. package/src/components/index.ts +4 -0
  78. package/src/contract/contracts.test.ts +70 -0
  79. package/src/contract/fromComponentDoc.ts +40 -4
  80. package/src/contract/generateSwift.ts +68 -0
  81. package/src/contract/generateTypeScript.ts +13 -2
  82. package/src/contract/parseDocType.test.ts +12 -0
  83. package/src/contract/parseDocType.ts +9 -2
  84. package/src/contract/types.ts +7 -0
  85. package/src/theme/swiftIOSTheme.test.ts +78 -12
@@ -0,0 +1,418 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ import AstryxNative
4
+ import SwiftUI
5
+ import os
6
+
7
+ /// A person's or an entity's picture: the image at `src`, else `fallbackSrc`,
8
+ /// else initials from `name`, else a person symbol (core `Avatar`).
9
+ ///
10
+ /// ```swift
11
+ /// Avatar("Ada Lovelace", src: "https://example.com/ada.jpg")
12
+ /// Avatar("Ada Lovelace", size: .lg, shape: .rounded)
13
+ /// Avatar("Ada Lovelace", href: "https://example.com/ada")
14
+ /// Avatar("Ada Lovelace") { showProfile() }
15
+ /// ```
16
+ public struct Avatar: View {
17
+ /// The contract the view draws, the same for both initializers.
18
+ public let props: AvatarProps
19
+ @Environment(\.astryx) private var astryx
20
+
21
+ /// An avatar from its labeled props.
22
+ /// - Parameters:
23
+ /// - name: The person's or entity's name, for initials and the accessible name (core `name`).
24
+ /// - src: The image URL (core `src`).
25
+ /// - fallbackSrc: The image URL to try when `src` fails (core `fallbackSrc`).
26
+ /// - alt: The accessible name, when it differs from `name` (core `alt`).
27
+ /// - size: `xsm` 20, `sm` 24, `md` 36, `lg` 48, or `xl` 128 points; `nil` is `md` (core `size`).
28
+ /// - shape: `circle`, `rounded`, or `square`; `nil` is `circle` (core `shape`).
29
+ /// - tooltip: Words shown where there is a pointer: `nil` or `true` the name, a string those words, `false` none (core `tooltip`).
30
+ /// - href: A URL the system opens when the avatar is tapped; the avatar becomes a link (core `href`).
31
+ /// - onClick: Runs when the avatar is tapped; without `href` the avatar becomes a button (core `onClick`).
32
+ public init(
33
+ _ name: String? = nil,
34
+ src: String? = nil,
35
+ fallbackSrc: String? = nil,
36
+ alt: String? = nil,
37
+ size: AvatarSize? = nil,
38
+ shape: AvatarShape? = nil,
39
+ tooltip: AvatarTooltip? = nil,
40
+ href: String? = nil,
41
+ onClick: (() -> Void)? = nil
42
+ ) {
43
+ self.init(
44
+ AvatarProps(
45
+ src: src, fallbackSrc: fallbackSrc, name: name, alt: alt, size: size, shape: shape, tooltip: tooltip, href: href,
46
+ onClick: onClick
47
+ ))
48
+ }
49
+
50
+ /// An avatar from its generated contract, for generated and wire-driven callers.
51
+ public init(_ props: AvatarProps) {
52
+ self.props = props
53
+ }
54
+
55
+ public var body: some View {
56
+ if props.base.isHidden != true {
57
+ control
58
+ // The Astryx tooltip (library/tooltip.spec.md), as core's Avatar uses
59
+ // core's tooltip: a static avatar is a tab stop and opens on a tap;
60
+ // a link or button keeps its focus and its tap.
61
+ // Touch is core's `auto`: a static avatar opens on a tap; a control's
62
+ // tap belongs to its action.
63
+ .astryxTooltip(tooltipText, hint: tooltipHint, focus: Avatar.tooltipFocus(tap))
64
+ .modifier(AvatarIdentifier(id: props.base.id))
65
+ .onAppear(perform: checkInDevelopment)
66
+ }
67
+ }
68
+
69
+ /// Development checks core makes too: an `href` an app cannot open stops
70
+ /// the build with its value, and an unnamed link or button is flagged.
71
+ private func checkInDevelopment() {
72
+ #if DEBUG
73
+ if Avatar.isUnusableHref(props.href) {
74
+ assertionFailure(
75
+ "Avatar href \"\(props.href ?? "")\" is not an absolute URL an app can open; it draws without a link.")
76
+ }
77
+ if Avatar.needsNameWarning(tap: tap, isNamed: accessibilityLabel != nil) {
78
+ Logger(subsystem: "Astryx", category: "Avatar").warning(
79
+ "An Avatar with href or onClick has no alt or name, so VoiceOver reads it without a name.")
80
+ }
81
+ #endif
82
+ }
83
+
84
+ /// The tooltip's words: the name for `nil` or `true`, the given words, or
85
+ /// none for `false` or blank words, as core resolves it.
86
+ var tooltipText: String? {
87
+ switch props.tooltip ?? .on {
88
+ case .on: Avatar.meaningful(props.name).map(Avatar.trimmed)
89
+ case .off: nil
90
+ case .text(let words): Avatar.meaningful(words).map(Avatar.trimmed)
91
+ }
92
+ }
93
+
94
+ /// The VoiceOver hint: given tooltip words, as core puts them in the
95
+ /// description; the default name is the avatar's own name, not repeated.
96
+ var tooltipHint: String? {
97
+ guard case .text = props.tooltip else { return nil }
98
+ return tooltipText
99
+ }
100
+
101
+ /// A non-empty `href` that is not an absolute URL, such as a web path.
102
+ nonisolated static func isUnusableHref(_ href: String?) -> Bool {
103
+ guard let href, !href.isEmpty else { return false }
104
+ guard let url = URL(string: href) else { return true }
105
+ return url.scheme == nil
106
+ }
107
+
108
+ /// The picture as a link with `href`, a button with only `onClick`, or a
109
+ /// static image (Swift component API, A4). All three draw the same.
110
+ @ViewBuilder private var control: some View {
111
+ if let url = linkURL {
112
+ SwiftUI.Link(destination: url) { picture }
113
+ .buttonStyle(.plain)
114
+ .modifier(AvatarRunsBeforeOpening(onClick: props.onClick))
115
+ .modifier(AvatarTarget(side: side))
116
+ .modifier(AvatarName(label: accessibilityLabel, tap: .link))
117
+ } else if let onClick = props.onClick {
118
+ SwiftUI.Button(action: onClick) { picture }
119
+ .buttonStyle(.plain)
120
+ .modifier(AvatarTarget(side: side))
121
+ .modifier(AvatarName(label: accessibilityLabel, tap: .button))
122
+ } else {
123
+ picture
124
+ // A static avatar whose tap opens its tooltip answers taps across
125
+ // 44 points too.
126
+ .modifier(AvatarTarget(side: Avatar.tapTargetSide(side, takesTaps: tooltipText != nil)))
127
+ .modifier(AvatarName(label: accessibilityLabel, tap: .none))
128
+ }
129
+ }
130
+
131
+ /// How the avatar takes a tap: a link with a usable `href`, else a button
132
+ /// with `onClick`, else none.
133
+ enum Tap: Equatable { case link, button, none }
134
+
135
+ /// How far a tappable avatar's tap area reaches past its side: to 44
136
+ /// points (Swift accessibility, X7).
137
+ nonisolated static func tapOutset(side: CGFloat) -> CGFloat { max(0, (44 - side) / 2) }
138
+
139
+ /// The side a static avatar's tap area grows from: its own when a tap
140
+ /// opens its tooltip, and 44, which adds nothing, when it takes no taps.
141
+ nonisolated static func tapTargetSide(_ side: CGFloat, takesTaps: Bool) -> CGFloat { takesTaps ? side : 44 }
142
+
143
+ /// How keyboard focus reaches the tooltip: a static avatar is made a tab
144
+ /// stop, as core gives it one; a link or button takes focus already.
145
+ nonisolated static func tooltipFocus(_ tap: Tap) -> TooltipFocus { tap == .none ? .tabStop : .control }
146
+
147
+
148
+ var tap: Tap {
149
+ if linkURL != nil { return .link }
150
+ if props.onClick != nil { return .button }
151
+ return .none
152
+ }
153
+
154
+ /// The traits the avatar adds: a named static avatar is an image; a link or
155
+ /// button keeps its own trait and adds none (Swift accessibility, X1).
156
+ nonisolated static func addedTraits(tap: Tap, isNamed: Bool) -> AccessibilityTraits {
157
+ guard isNamed else { return [] }
158
+ switch tap {
159
+ case .none: return .isImage
160
+ case .link: return .isLink
161
+ case .button: return []
162
+ }
163
+ }
164
+
165
+ /// Traits a named avatar drops: a link avatar is a link, not the button
166
+ /// SwiftUI reports for `Link`.
167
+ nonisolated static func removedTraits(tap: Tap, isNamed: Bool) -> AccessibilityTraits {
168
+ isNamed && tap == .link ? .isButton : []
169
+ }
170
+
171
+ /// A link or button avatar needs a name, as core warns in development.
172
+ nonisolated static func needsNameWarning(tap: Tap, isNamed: Bool) -> Bool {
173
+ tap != .none && !isNamed
174
+ }
175
+
176
+ /// The photo; the fallback photo only after it fails or when there is
177
+ /// none, as core does; initials or the symbol while either loads and
178
+ /// after both fail.
179
+ private var picture: some View {
180
+ AvatarImage(url: url(props.src)) {
181
+ plate
182
+ } failure: {
183
+ AvatarImage(url: url(props.fallbackSrc)) { plate } failure: { plate }
184
+ }
185
+ .frame(width: side, height: side)
186
+ .clipShape(AvatarShapeOutline(shape: resolvedShape, radius: cornerRadius))
187
+ }
188
+
189
+ /// The fallback: initials on the neutral plate, else a person symbol.
190
+ private var plate: some View {
191
+ ZStack {
192
+ astryx.tokens.color.neutral
193
+ if let initials {
194
+ // Drawn, not laid out as a Text view, so the initials are never an
195
+ // accessibility element of their own; the name names the avatar.
196
+ let font = initialsFont
197
+ let color = astryx.tokens.color.textSecondary
198
+ Canvas { context, size in
199
+ let text = context.resolve(SwiftUI.Text(initials).font(font).foregroundColor(color))
200
+ context.draw(text, at: CGPoint(x: size.width / 2, y: size.height / 2), anchor: .center)
201
+ }
202
+ .accessibilityHidden(true)
203
+ } else {
204
+ Image(systemName: "person.fill")
205
+ .resizable()
206
+ .scaledToFit()
207
+ .frame(width: side * 0.6, height: side * 0.6)
208
+ .foregroundStyle(astryx.tokens.color.textSecondary)
209
+ .accessibilityHidden(true)
210
+ }
211
+ }
212
+ }
213
+
214
+ /// The side of the avatar in points.
215
+ /// The side of the avatar in points: core's size grown with Dynamic Type,
216
+ /// as the name beside it grows, up to 1.6 times so it never swallows a row.
217
+ var side: CGFloat { Avatar.side(for: props.size ?? .md, textScale: astryx.textScale) }
218
+
219
+ /// The most an avatar grows with Dynamic Type.
220
+ nonisolated static var maximumTextScale: CGFloat { 1.6 }
221
+
222
+ nonisolated static func side(for size: AvatarSize, textScale: CGFloat) -> CGFloat {
223
+ (points(for: size) * min(max(textScale, 1), maximumTextScale)).rounded()
224
+ }
225
+
226
+ var resolvedShape: AvatarShape { props.shape ?? .circle }
227
+
228
+ /// The corner radius for `rounded`, from the theme's element radius.
229
+ private var cornerRadius: CGFloat { astryx.tokens.radius.element }
230
+
231
+ /// Initials drawn at 40% of the side in the theme's body family, medium weight.
232
+ private var initialsFont: Font {
233
+ astryxFont(
234
+ families: astryx.tokens.font.family.body,
235
+ size: side * 0.4,
236
+ weight: astryx.fontWeight(astryx.tokens.font.weight.medium),
237
+ isBoldText: astryx.isBoldText
238
+ )
239
+ }
240
+
241
+ /// The initials the plate shows, or `nil` for the person symbol.
242
+ var initials: String? {
243
+ guard let name = Avatar.meaningful(props.name) else { return nil }
244
+ let initials = Avatar.initials(from: name)
245
+ return initials.isEmpty ? nil : initials
246
+ }
247
+
248
+ /// The name VoiceOver reads: `alt`, else `name`; `nil` when neither
249
+ /// carries anything, and then a static avatar is decorative.
250
+ var accessibilityLabel: String? {
251
+ Avatar.meaningful(props.alt) ?? Avatar.meaningful(props.name)
252
+ }
253
+
254
+ /// The URL the avatar opens, or `nil` when it has no usable `href`.
255
+ var linkURL: URL? {
256
+ // Only an absolute URL the system can open; a malformed or relative
257
+ // `href` leaves the avatar static or a button.
258
+ guard let url = url(props.href), url.scheme != nil else { return nil }
259
+ return url
260
+ }
261
+
262
+ private func url(_ string: String?) -> URL? {
263
+ guard let string, !string.isEmpty else { return nil }
264
+ return URL(string: string)
265
+ }
266
+
267
+ /// Core's named sizes in points.
268
+ nonisolated static func points(for size: AvatarSize) -> CGFloat {
269
+ switch size {
270
+ case .xsm: 20
271
+ case .sm: 24
272
+ case .md: 36
273
+ case .lg: 48
274
+ case .xl: 128
275
+ }
276
+ }
277
+
278
+ /// The string when it carries something, `nil` when it is absent or blank.
279
+ /// Words without their leading and trailing space, as core trims the
280
+ /// tooltip it shows.
281
+ nonisolated static func trimmed(_ value: String) -> String {
282
+ value.trimmingCharacters(in: .whitespacesAndNewlines)
283
+ }
284
+
285
+ nonisolated static func meaningful(_ value: String?) -> String? {
286
+ guard let value, !value.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return nil }
287
+ return value
288
+ }
289
+
290
+ /// Core's initials: the first letter, digit, or emoji of the first and last
291
+ /// words that have one, uppercased. Leading punctuation in a word is skipped.
292
+ nonisolated static func initials(from name: String) -> String {
293
+ let letters = name.split(whereSeparator: { $0.isWhitespace }).compactMap { word in
294
+ word.first(where: canStandForAWord)
295
+ }
296
+ guard let first = letters.first else { return "" }
297
+ guard letters.count > 1, let last = letters.last else { return String(first).uppercased() }
298
+ return (String(first) + String(last)).uppercased()
299
+ }
300
+
301
+ /// A letter, a digit, or an emoji, as core's `\p{L}`, `\p{N}`,
302
+ /// `\p{Extended_Pictographic}`, and `\p{Regional_Indicator}`.
303
+ nonisolated static func canStandForAWord(_ character: Character) -> Bool {
304
+ if character.isLetter || character.isNumber { return true }
305
+ guard let scalar = character.unicodeScalars.first else { return false }
306
+ // `isEmoji` also covers `#`, `*`, and the digits, which are ASCII.
307
+ return scalar.value > 0x7F && scalar.properties.isEmoji
308
+ }
309
+ }
310
+
311
+ /// Draws the image at a URL: `loading` while it loads, `failure` when it
312
+ /// fails or there is no URL.
313
+ private struct AvatarImage<Loading: View, Failure: View>: View {
314
+ let url: URL?
315
+ @ViewBuilder let loading: () -> Loading
316
+ @ViewBuilder let failure: () -> Failure
317
+
318
+ var body: some View {
319
+ if let url {
320
+ AsyncImage(url: url) { phase in
321
+ if let image = phase.image {
322
+ image.resizable().scaledToFill()
323
+ } else if phase.error != nil {
324
+ failure()
325
+ } else {
326
+ loading()
327
+ }
328
+ }
329
+ } else {
330
+ failure()
331
+ }
332
+ }
333
+ }
334
+
335
+ /// Runs `onClick` as a link opens, then hands the URL to whatever opens URLs
336
+ /// above the avatar, so an app's own handler still applies. Without
337
+ /// `onClick` it changes nothing.
338
+ private struct AvatarRunsBeforeOpening: ViewModifier {
339
+ let onClick: (() -> Void)?
340
+ @Environment(\.openURL) private var inheritedOpenURL
341
+
342
+ func body(content: Content) -> some View {
343
+ if let onClick {
344
+ content.environment(\.openURL, OpenURLAction { url in
345
+ onClick()
346
+ inheritedOpenURL(url)
347
+ return .handled
348
+ })
349
+ } else {
350
+ content
351
+ }
352
+ }
353
+ }
354
+
355
+ /// The outline each shape clips to: a circle, the element radius, or square.
356
+ private struct AvatarShapeOutline: Shape {
357
+ let shape: AvatarShape
358
+ let radius: CGFloat
359
+
360
+ func path(in rect: CGRect) -> Path {
361
+ switch shape {
362
+ case .circle: Circle().path(in: rect)
363
+ case .rounded: RoundedRectangle(cornerRadius: radius, style: .continuous).path(in: rect)
364
+ case .square: Rectangle().path(in: rect)
365
+ }
366
+ }
367
+ }
368
+
369
+ /// One element named by the avatar's label. A static avatar without a label
370
+ /// is hidden; an interactive one keeps its link or button trait.
371
+ private struct AvatarName: ViewModifier {
372
+ let label: String?
373
+ let tap: Avatar.Tap
374
+
375
+ func body(content: Content) -> some View {
376
+ let isNamed = label != nil
377
+ let added = Avatar.addedTraits(tap: tap, isNamed: isNamed)
378
+ let removed = Avatar.removedTraits(tap: tap, isNamed: isNamed)
379
+ if let label {
380
+ if tap == .none {
381
+ // One image element named by the name.
382
+ content.accessibilityElement(children: .ignore).accessibilityLabel(label)
383
+ .accessibilityAddTraits(added)
384
+ } else {
385
+ // The link or button itself carries the name, so VoiceOver reads
386
+ // "Grace Hopper, button", not the initials inside it.
387
+ content.accessibilityLabel(label).accessibilityAddTraits(added).accessibilityRemoveTraits(removed)
388
+ }
389
+ } else if tap != .none {
390
+ content
391
+ } else {
392
+ content.accessibilityHidden(true)
393
+ }
394
+ }
395
+ }
396
+
397
+ /// A tappable avatar answers taps across at least 44 by 44 points without
398
+ /// changing its drawn size or its layout (Swift accessibility, X7).
399
+ private struct AvatarTarget: ViewModifier {
400
+ let side: CGFloat
401
+
402
+ func body(content: Content) -> some View {
403
+ content.contentShape(.interaction, Rectangle().inset(by: -Avatar.tapOutset(side: side)))
404
+ }
405
+ }
406
+
407
+ /// The base `id` becomes the accessibility identifier (AST-082 FR7).
408
+ private struct AvatarIdentifier: ViewModifier {
409
+ let id: String?
410
+
411
+ func body(content: Content) -> some View {
412
+ if let id {
413
+ content.accessibilityIdentifier(id)
414
+ } else {
415
+ content
416
+ }
417
+ }
418
+ }
@@ -49,9 +49,9 @@ public struct Banner: View {
49
49
 
50
50
  public var body: some View {
51
51
  HStack(alignment: .firstTextBaseline, spacing: astryx.space(2)) {
52
- if let symbol = astryx.icon(status.iconName) {
52
+ if let icon = astryx.icon(status.iconName) {
53
53
  // Sized with the title so it grows with Dynamic Type.
54
- Image(systemName: symbol)
54
+ icon.sized(astryx.fontSize("text-body-size", 14))
55
55
  .font(astryx.font(size: "text-body-size", 14, weight: "text-body-weight"))
56
56
  .foregroundStyle(astryx.color(status.iconToken))
57
57
  .accessibilityHidden(true)
@@ -0,0 +1,16 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ extension View {
4
+ /// Core's base props on a view: `id` names it for UI tests, and `isHidden`
5
+ /// removes it from layout and from assistive technology, as core's `hidden`
6
+ /// attribute does.
7
+ @ViewBuilder func astryxBase(_ base: AstryxBaseProps) -> some View {
8
+ if base.isHidden == true {
9
+ EmptyView()
10
+ } else if let id = base.id {
11
+ accessibilityIdentifier(id)
12
+ } else {
13
+ self
14
+ }
15
+ }
16
+ }
@@ -70,8 +70,9 @@ public struct Button: View {
70
70
  // The label keeps its place while loading so the width holds; the
71
71
  // spinner draws over it.
72
72
  HStack(spacing: astryx.space(2)) {
73
- if let icon, let symbol = astryx.icon(icon) {
74
- Image(systemName: symbol)
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))
75
76
  }
76
77
  SwiftUI.Text(label).lineLimit(1)
77
78
  }
@@ -1,33 +1,109 @@
1
1
  // Copyright (c) Meta Platforms, Inc. and affiliates.
2
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.
3
+ /// A card: one independent item on the theme's card ground. It takes core's
4
+ /// Card props (`CardProps`) and stacks its content top to bottom, as children
5
+ /// flow inside core's Card.
7
6
  public struct Card<Content: View>: View {
8
- let padding: Int
7
+ public let props: CardProps
9
8
  let content: Content
10
9
  @Environment(\.astryx) private var astryx
11
10
 
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
11
+ /// - Parameters:
12
+ /// - padding: core's `padding`, a step on the spacing scale; step 4 when
13
+ /// nil.
14
+ /// - variant: core's `variant`: the card ground, or `transparent` for none.
15
+ /// Only `default` draws a border.
16
+ /// - elevation: core's `elevation`, the theme's shadow at that depth.
17
+ /// - base: core's base props.
18
+ /// - content: core's `children`.
19
+ public init(
20
+ padding: CardPadding? = nil,
21
+ variant: CardVariant? = nil,
22
+ elevation: CardElevation? = nil,
23
+ base: AstryxBaseProps = AstryxBaseProps(),
24
+ @ViewBuilder content: () -> Content
25
+ ) {
26
+ self.props = CardProps(padding: padding, variant: variant, elevation: elevation, base: base)
15
27
  self.content = content()
16
28
  }
17
29
 
18
30
  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))
31
+ let shape = RoundedRectangle(cornerRadius: astryx.tokens.radius.container, style: .continuous)
32
+ let variant = props.variant ?? .default
33
+ // The border sits inside the padding, so the content's inset is the
34
+ // padding step, as in core.
35
+ SwiftUI.VStack(alignment: .leading, spacing: 0) { content }
36
+ .padding(padding)
27
37
  .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))
38
+ .background(shape.fill(Self.ground(variant, astryx.tokens.color)))
39
+ .overlay {
40
+ if variant == .default {
41
+ shape.strokeBorder(astryx.tokens.color.border, lineWidth: astryx.tokens.size.borderWidth)
42
+ }
43
+ }
44
+ .astryxShadow(
45
+ Self.shadow(props.elevation ?? .none, astryx.tokens.shadow), in: shape,
46
+ borderWidth: variant == .default ? astryx.tokens.size.borderWidth : 0
31
47
  )
48
+ .astryxBase(props.base)
49
+ }
50
+
51
+ var padding: CGFloat {
52
+ let space = astryx.tokens.space
53
+ switch props.padding ?? .v4 {
54
+ case .v0: return space.s0
55
+ case .v0_5: return space.s0_5
56
+ case .v1: return space.s1
57
+ case .v1_5: return space.s1_5
58
+ case .v2: return space.s2
59
+ case .v3: return space.s3
60
+ case .v4: return space.s4
61
+ case .v5: return space.s5
62
+ case .v6: return space.s6
63
+ case .v8: return space.s8
64
+ case .v10: return space.s10
65
+ }
66
+ }
67
+
68
+ /// The ground core's Card draws for a variant.
69
+ static func ground(_ variant: CardVariant, _ color: ColorTokens) -> Color {
70
+ switch variant {
71
+ case .default: color.backgroundCard
72
+ case .transparent: .clear
73
+ case .muted: color.backgroundMuted
74
+ case .blue: color.backgroundBlue
75
+ case .cyan: color.backgroundCyan
76
+ case .gray: color.backgroundGray
77
+ case .green: color.backgroundGreen
78
+ case .orange: color.backgroundOrange
79
+ case .pink: color.backgroundPink
80
+ case .purple: color.backgroundPurple
81
+ case .red: color.backgroundRed
82
+ case .teal: color.backgroundTeal
83
+ case .yellow: color.backgroundYellow
84
+ }
85
+ }
86
+
87
+ /// The theme's shadow at an elevation.
88
+ static func shadow(_ elevation: CardElevation, _ shadow: ShadowTokens) -> [ShadowLayer] {
89
+ switch elevation {
90
+ case .none: []
91
+ case .low: shadow.low
92
+ case .med: shadow.med
93
+ case .high: shadow.high
94
+ }
95
+ }
96
+ }
97
+
98
+ extension Card where Content == AstryxNodeList {
99
+ /// A card from core's props, with its children as the wire tree a logic
100
+ /// core sends; `send` carries the children's callbacks back to the core.
101
+ public init(
102
+ _ props: CardProps,
103
+ children: [AstryxNode],
104
+ send: @escaping @Sendable (AstryxAction) -> Void
105
+ ) {
106
+ self.props = props
107
+ self.content = AstryxNodeList(nodes: children, send: send)
32
108
  }
33
109
  }