@astryxdesign/native 0.7.5-canary.fd0aba8 → 0.7.6-canary.00146a7

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 (51) hide show
  1. package/apple/Package.swift +0 -2
  2. package/apple/README.md +23 -18
  3. package/apple/Sources/Astryx/Banner/Banner.swift +18 -18
  4. package/apple/Sources/Astryx/Base/BaseProps.swift +5 -0
  5. package/apple/Sources/Astryx/Base/HitArea.swift +16 -0
  6. package/apple/Sources/Astryx/Button/Button.swift +222 -107
  7. package/apple/Sources/Astryx/Localization/AstryxStrings.swift +25 -0
  8. package/apple/Sources/Astryx/Resources/af.lproj/Localizable.strings +21 -0
  9. package/apple/Sources/Astryx/Resources/ar.lproj/Localizable.strings +21 -0
  10. package/apple/Sources/Astryx/Resources/ca.lproj/Localizable.strings +21 -0
  11. package/apple/Sources/Astryx/Resources/cs.lproj/Localizable.strings +21 -0
  12. package/apple/Sources/Astryx/Resources/da.lproj/Localizable.strings +21 -0
  13. package/apple/Sources/Astryx/Resources/de.lproj/Localizable.strings +21 -0
  14. package/apple/Sources/Astryx/Resources/el.lproj/Localizable.strings +21 -0
  15. package/apple/Sources/Astryx/Resources/en.lproj/Localizable.strings +24 -0
  16. package/apple/Sources/Astryx/Resources/es.lproj/Localizable.strings +21 -0
  17. package/apple/Sources/Astryx/Resources/fi.lproj/Localizable.strings +21 -0
  18. package/apple/Sources/Astryx/Resources/fr.lproj/Localizable.strings +21 -0
  19. package/apple/Sources/Astryx/Resources/he.lproj/Localizable.strings +21 -0
  20. package/apple/Sources/Astryx/Resources/hu.lproj/Localizable.strings +21 -0
  21. package/apple/Sources/Astryx/Resources/it.lproj/Localizable.strings +21 -0
  22. package/apple/Sources/Astryx/Resources/ja.lproj/Localizable.strings +21 -0
  23. package/apple/Sources/Astryx/Resources/ko.lproj/Localizable.strings +21 -0
  24. package/apple/Sources/Astryx/Resources/nb.lproj/Localizable.strings +21 -0
  25. package/apple/Sources/Astryx/Resources/nl.lproj/Localizable.strings +21 -0
  26. package/apple/Sources/Astryx/Resources/pl.lproj/Localizable.strings +21 -0
  27. package/apple/Sources/Astryx/Resources/pt-BR.lproj/Localizable.strings +21 -0
  28. package/apple/Sources/Astryx/Resources/pt-PT.lproj/Localizable.strings +21 -0
  29. package/apple/Sources/Astryx/Resources/ro.lproj/Localizable.strings +21 -0
  30. package/apple/Sources/Astryx/Resources/ru.lproj/Localizable.strings +21 -0
  31. package/apple/Sources/Astryx/Resources/sr.lproj/Localizable.strings +21 -0
  32. package/apple/Sources/Astryx/Resources/sv.lproj/Localizable.strings +21 -0
  33. package/apple/Sources/Astryx/Resources/tr.lproj/Localizable.strings +21 -0
  34. package/apple/Sources/Astryx/Resources/uk.lproj/Localizable.strings +21 -0
  35. package/apple/Sources/Astryx/Resources/vi.lproj/Localizable.strings +21 -0
  36. package/apple/Sources/Astryx/Resources/zh-Hans.lproj/Localizable.strings +21 -0
  37. package/apple/Sources/Astryx/Resources/zh-Hant.lproj/Localizable.strings +21 -0
  38. package/apple/Sources/Astryx/TextInput/TextInput.swift +539 -50
  39. package/apple/Sources/Astryx/Theme/Theme.swift +5 -5
  40. package/apple/Sources/Astryx/Wire/AstryxNodeView.swift +2 -0
  41. package/apple/Tests/AstryxTests/BannerTests.swift +5 -2
  42. package/apple/Tests/AstryxTests/ButtonTests.swift +100 -4
  43. package/apple/Tests/AstryxTests/TextInputTests.swift +282 -5
  44. package/apple/Tests/AstryxTests/TwoThemeTests.swift +69 -0
  45. package/package.json +3 -3
  46. package/src/theme/swiftIOSStrings.test.ts +18 -0
  47. package/src/theme/swiftIOSTheme.test.ts +270 -37
  48. package/apple/Sources/Astryx/Avatar/Avatar.spec.md +0 -219
  49. package/apple/Sources/Astryx/Avatar/AvatarStatusDot.spec.md +0 -162
  50. package/apple/Sources/Astryx/Icon/Icon.spec.md +0 -189
  51. package/apple/Sources/Astryx/Thumbnail/Thumbnail.spec.md +0 -191
@@ -27,8 +27,6 @@ let package = Package(
27
27
  name: "Astryx",
28
28
  dependencies: ["AstryxNative"],
29
29
  path: "Sources/Astryx",
30
- // Each component's spec lives beside its view (AST-093 FR1).
31
- exclude: ["Avatar/Avatar.spec.md", "Avatar/AvatarStatusDot.spec.md", "Icon/Icon.spec.md", "Thumbnail/Thumbnail.spec.md"],
32
30
  resources: [.process("Resources")]
33
31
  ),
34
32
  .testTarget(name: "AstryxNativeTests", dependencies: ["AstryxNative"], path: "Tests/AstryxNativeTests"),
package/apple/README.md CHANGED
@@ -26,13 +26,13 @@ apple/
26
26
  │ │ │ └── Generated/ # typed tokens and every published theme, generated; do not edit
27
27
  │ │ ├── Resources/ # core's messages per locale, generated; do not edit
28
28
  │ │ ├── Localization/
29
- │ │ ├── Icon/ # Icon, Icon.spec.md
30
- │ │ ├── Avatar/ # Avatar, Avatar.spec.md
29
+ │ │ ├── Icon/ # Icon
30
+ │ │ ├── Avatar/ # Avatar
31
31
  │ │ ├── Text/ # Text
32
32
  │ │ ├── Heading/ # Heading
33
33
  │ │ ├── Button/ # Button
34
34
  │ │ ├── TextInput/ # TextInput
35
- │ │ ├── Thumbnail/ # Thumbnail, Thumbnail.spec.md
35
+ │ │ ├── Thumbnail/ # Thumbnail
36
36
  │ │ ├── Card/ # Card
37
37
  │ │ └── Banner/ # Banner
38
38
  │ └── AstryxNative/ # contracts, wire, logic host (module AstryxNative)
@@ -46,6 +46,9 @@ apple/
46
46
  └── Tests/
47
47
  ```
48
48
 
49
+ Each component's Swift spec sits beside its core spec, at
50
+ `docs/specs/components/<ComponentRoot>/<Name>.swift.spec.md`.
51
+
49
52
  - **`Sources/Astryx/<Name>/`** holds one folder per component, named as in
50
53
  `packages/core/src/<Name>/`. Each view has core's component name in the
51
54
  `Astryx` module (`Button`, `Text`), which re-exports SwiftUI and the
@@ -53,15 +56,10 @@ apple/
53
56
  that also imports SwiftUI, write `Astryx.Button` and `SwiftUI.Button`. A
54
57
  component's parameters use core's prop names
55
58
  and values (`variant: .primary`, `size: .lg`, `isLoading`), so code reads
56
- the same across web and native. `Text`, `Heading`, and `Card` take the
57
- generated `TextProps`, `HeadingProps`, and `CardProps`, as AST-093 FR5 asks;
58
- the others keep four exceptions, the interim shape FR5 exempts until they move onto the generated
59
- contracts:
60
- - `Button` takes `action` where core has `onClick`.
61
- - `Button` takes `fillsWidth` where core has `width="100%"`.
62
- - `Button` takes `icon` as a shared icon name (`"search"`), which the
63
- generated `ButtonProps` does not carry yet.
64
- - `TextInput` takes a `Binding` where core has `value` and `onChange`.
59
+ the same across web and native. Every view with a core contract takes its
60
+ generated `<Name>Props` (AST-093 FR5) and has a labeled initializer that
61
+ builds the same props; `Banner` has no native contract yet (the generated
62
+ contracts do not include it), so its parameters are hand-written.
65
63
  - **`Templates/`** holds one folder per page template, named after the
66
64
  `astryx template` id. Templates are clone-only, as on the web: you copy a
67
65
  template's folder into your app and own it from then on. They are a
@@ -103,9 +101,9 @@ tokens in the current mode as typed members (`tokens.color.textPrimary`,
103
101
  `tokens.space.s4`, `tokens.font.body.size`), so a misspelled token fails to
104
102
  compile. Font tokens come grouped: each type style (`tokens.font.body`,
105
103
  `tokens.font.heading1`) holds its `size`, `weight`, and `leading`, and
106
- `tokens.font.family`, `.size`, and `.weight` hold the scales. Text sizes grow with Dynamic Type. The components still read some
107
- tokens by core's name (`astryx.color("color-accent")`); moving them onto the
108
- typed members comes next.
104
+ `tokens.font.family`, `.size`, and `.weight` hold the scales. Text sizes grow
105
+ with Dynamic Type. An app reads the same members for its own SwiftUI layout,
106
+ such as stack spacing (`astryx.tokens.space.s4`).
109
107
 
110
108
  `Icon(.search)` draws a shared icon name as the platform's SF Symbol, at
111
109
  core's icon size and color, hidden from VoiceOver unless it has a `label`.
@@ -148,6 +146,15 @@ export:
148
146
  node packages/native/scripts/swift-ios-theme.mjs --theme ./src/acmeTheme.ts#acmeTheme --out ./Sources/Theme
149
147
  ```
150
148
 
149
+ The generator reads the CSS forms its spec lists
150
+ (`specs/swift/library/theme-generator.md`, G7): hex and named colors, `rgb()`,
151
+ `hsl()`, `hwb()`, Lab, LCH, OKLab, OKLCH, `color()` in the listed spaces,
152
+ `color-mix()`, and relative colors (wide-gamut colors kept as Display P3;
153
+ a color outside Display P3 clips, as browsers draw it, with a warning),
154
+ lengths with `calc()`, and `[light, dark]` tuples. A value with no static form,
155
+ such as `currentColor` or `50%`, stops the run naming the theme, mode, token,
156
+ and value; so does a form the generator does not read yet, saying so.
157
+
151
158
  A theme's local tokens are not generated yet; the generator names each one it
152
159
  leaves out.
153
160
 
@@ -198,8 +205,6 @@ system type, read its tokens from `astryx`, and add tests under
198
205
 
199
206
  ## Not here yet
200
207
 
201
- - The views do not use the generated contract types in `AstryxNative` yet.
202
- Moving them onto those types is how a core prop change reaches Swift.
203
208
  - Pixel and behavior parity with the web components has not been measured.
204
- - Increase Contrast, theme-local tokens, and platform theme entries
209
+ - Theme-local tokens and platform theme entries
205
210
  (`defineNativeTheme`, reserved as `Theme.native`) are not wired in.
@@ -13,21 +13,21 @@ public enum AstryxBannerStatus: String, CaseIterable, Sendable {
13
13
  /// theme's or the platform's symbol for it.
14
14
  var iconName: String { rawValue }
15
15
 
16
- var groundToken: String {
16
+ func groundColor(_ color: ColorTokens) -> Color {
17
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"
18
+ case .info: color.accentMuted
19
+ case .warning: color.warningMuted
20
+ case .error: color.errorMuted
21
+ case .success: color.successMuted
22
22
  }
23
23
  }
24
24
 
25
- var iconToken: String {
25
+ func iconColor(_ color: ColorTokens) -> Color {
26
26
  switch self {
27
- case .info: "color-accent"
28
- case .warning: "color-warning"
29
- case .error: "color-error"
30
- case .success: "color-success"
27
+ case .info: color.accent
28
+ case .warning: color.warning
29
+ case .error: color.error
30
+ case .success: color.success
31
31
  }
32
32
  }
33
33
  }
@@ -48,15 +48,15 @@ public struct Banner: View {
48
48
  }
49
49
 
50
50
  public var body: some View {
51
- HStack(alignment: .firstTextBaseline, spacing: astryx.space(2)) {
51
+ HStack(alignment: .firstTextBaseline, spacing: astryx.tokens.space.s2) {
52
52
  if let icon = astryx.icon(status.iconName) {
53
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))
54
+ icon.sized(astryx.tokens.font.body.size * astryx.textScale)
55
+ .font(astryx.font(TypeStyle(astryx.tokens.font.body, families: astryx.tokens.font.family.body)))
56
+ .foregroundStyle(status.iconColor(astryx.tokens.color))
57
57
  .accessibilityHidden(true)
58
58
  }
59
- VStack(alignment: .leading, spacing: astryx.space(1)) {
59
+ VStack(alignment: .leading, spacing: astryx.tokens.space.s1) {
60
60
  Text(title, weight: .semibold)
61
61
  if let description {
62
62
  Text(description, color: .secondary)
@@ -64,10 +64,10 @@ public struct Banner: View {
64
64
  }
65
65
  .frame(maxWidth: .infinity, alignment: .leading)
66
66
  }
67
- .padding(astryx.space(3))
67
+ .padding(astryx.tokens.space.s3)
68
68
  .background(
69
- RoundedRectangle(cornerRadius: astryx.size("radius-container", 12), style: .continuous)
70
- .fill(astryx.color(status.groundToken))
69
+ RoundedRectangle(cornerRadius: astryx.tokens.radius.container, style: .continuous)
70
+ .fill(status.groundColor(astryx.tokens.color))
71
71
  )
72
72
  .accessibilityElement(children: .ignore)
73
73
  .accessibilityLabel(SwiftUI.Text(accessibilityText))
@@ -13,4 +13,9 @@ extension View {
13
13
  self
14
14
  }
15
15
  }
16
+
17
+ /// Core's base `id` on the part it targets, such as a field's control.
18
+ @ViewBuilder func astryxIdentifier(_ id: String?) -> some View {
19
+ if let id { accessibilityIdentifier(id) } else { self }
20
+ }
16
21
  }
@@ -0,0 +1,16 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ extension View {
4
+ /// Answers taps across at least 44 by 44 points around a control drawn
5
+ /// `side` points wide, without growing its layout (accessibility X7).
6
+ func astryxMinimumHitArea(_ side: CGFloat) -> some View {
7
+ let inset = astryxHitInset(side)
8
+ return padding(inset).contentShape(Rectangle()).padding(-inset)
9
+ }
10
+ }
11
+
12
+ /// How far a control `side` points wide reaches past its edges to answer taps
13
+ /// across 44 points.
14
+ func astryxHitInset(_ side: CGFloat) -> CGFloat {
15
+ max(0, (44 - side) / 2)
16
+ }
@@ -1,110 +1,195 @@
1
1
  // Copyright (c) Meta Platforms, Inc. and affiliates.
2
2
  // Adapted from github.com/ejc3/astryx-swift (MIT).
3
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.
4
+ /// A labeled action, painted with the theme's tokens. It takes core's Button
5
+ /// props (`ButtonProps`). With `href` it is a link the system opens. The tap
6
+ /// area reaches the platform's 44-by-44-point minimum even when the drawn
7
+ /// button is smaller.
32
8
  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
9
+ public let props: ButtonProps
41
10
  @Environment(\.astryx) private var astryx
11
+ /// Which part of a Button with `href` holds focus: its link, or the button
12
+ /// it draws while disabled.
13
+ enum Part: Hashable { case link, button }
14
+ @FocusState private var focus: Part?
15
+
16
+ public init(_ props: ButtonProps) {
17
+ self.props = props
18
+ }
42
19
 
43
20
  /// - 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.
21
+ /// - label: core's `label`, the button's words and accessible name.
22
+ /// - variant: core's `variant`; `secondary` when nil.
23
+ /// - size: core's `size`; `md` when nil.
24
+ /// - elevation: core's `elevation`, the theme's shadow at that depth.
25
+ /// - isLoading: core's `isLoading`: a spinner over the label, undimmed and
26
+ /// focusable, "Loading" spoken, presses ignored until it finishes.
27
+ /// - isInterruptible: core's `isInterruptible`: presses still land while
28
+ /// loading, so they can interrupt it.
29
+ /// - isDisabled: core's `isDisabled`.
30
+ /// - width: core's `width`: `content` (default) or `fill`, the whole width
31
+ /// offered.
32
+ /// - tooltip: core's `tooltip`: a tip on hover with a pointer, and a hint
33
+ /// VoiceOver reads; a tap belongs to the button, as on core's touch
34
+ /// screens.
35
+ /// - href: core's `href`: an enabled button becomes a SwiftUI `Link` the
36
+ /// system opens.
37
+ /// - base: core's base props.
38
+ /// - onClick: core's `onClick`, the press.
48
39
  public init(
49
40
  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
41
+ variant: ButtonVariant? = nil,
42
+ size: ButtonSize? = nil,
43
+ elevation: ButtonElevation? = nil,
44
+ isLoading: Bool? = nil,
45
+ isInterruptible: Bool? = nil,
46
+ isDisabled: Bool? = nil,
47
+ width: ButtonWidth? = nil,
48
+ tooltip: String? = nil,
49
+ href: String? = nil,
50
+ base: AstryxBaseProps = AstryxBaseProps(),
51
+ onClick: (() -> Void)? = nil
57
52
  ) {
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
53
+ self.init(
54
+ ButtonProps(
55
+ label: label,
56
+ variant: variant,
57
+ size: size,
58
+ elevation: elevation,
59
+ isLoading: isLoading,
60
+ isInterruptible: isInterruptible,
61
+ isDisabled: isDisabled,
62
+ width: width,
63
+ tooltip: tooltip,
64
+ onClick: onClick,
65
+ href: href,
66
+ base: base
67
+ )
68
+ )
69
+ }
70
+
71
+ var isLoading: Bool { props.isLoading == true }
72
+ var isDisabled: Bool { props.isDisabled == true }
73
+
74
+ /// The destination a Button with `href` opens while enabled, or nil when
75
+ /// it has none or its destination is refused.
76
+ var destination: URL? {
77
+ guard let href = props.href, let safe = Self.safeDestination(href) else { return nil }
78
+ return URL(string: safe)
79
+ }
80
+
81
+ /// The link an enabled button with `href` becomes. A disabled one draws a
82
+ /// disabled button, as core draws a disabled `<button>`.
83
+ var link: URL? { isDisabled ? nil : destination }
84
+
85
+ /// A destination a link may open, normalized as core's shared rule does, or
86
+ /// nil for a script URL or an HTML document in a `data:` URL, which core's
87
+ /// navigation destinations refuse (`family:navigation-destinations` FR4).
88
+ /// Control characters are removed and whitespace trimmed; the scheme is
89
+ /// compared in lowercase without the spaces after its colon, which a data
90
+ /// URL's media type ignores (`data: text/html` is HTML).
91
+ nonisolated static func safeDestination(_ href: String) -> String? {
92
+ let normalized = String(String.UnicodeScalarView(
93
+ href.unicodeScalars.filter { $0.value > 0x1F && $0.value != 0x7F }
94
+ )).trimmingCharacters(in: .whitespacesAndNewlines)
95
+ var comparable = normalized.lowercased()
96
+ if let colon = comparable.firstIndex(of: ":"),
97
+ comparable[..<colon].range(of: "^[a-z][a-z0-9+.-]*$", options: .regularExpression) != nil {
98
+ let rest = comparable[comparable.index(after: colon)...].drop { $0 == " " }
99
+ comparable = String(comparable[...colon]) + rest
100
+ }
101
+ for blocked in ["javascript:", "vbscript:", "data:text/html"] where comparable.hasPrefix(blocked) {
102
+ return nil
103
+ }
104
+ return normalized
66
105
  }
67
106
 
68
107
  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)
108
+ control
109
+ .buttonStyle(style)
110
+ // The shared Astryx tooltip on hover and keyboard focus, as core's
111
+ // Button tooltip; its words are the VoiceOver hint.
112
+ .astryxTooltip(props.tooltip, hint: props.tooltip, focus: .control)
113
+ .accessibilityLabel(SwiftUI.Text(accessibilityText))
114
+ .accessibilityValue(SwiftUI.Text(loadingValue ?? ""))
115
+ .onChange(of: isLoading) { _, loading in
116
+ if loading { AccessibilityNotification.Announcement(AstryxStrings.buttonLoading).post() }
117
+ }
118
+ .astryxBase(props.base)
119
+ }
120
+
121
+ @ViewBuilder private var control: some View {
122
+ if let destination {
123
+ // A Button with `href` keeps its link and its disabled button in one
124
+ // structure; only one is shown and reachable at a time, and focus moves
125
+ // from the link to the button when it becomes disabled and back, so it
126
+ // stays inside the component (AST-085 FR4, core Button FR10).
127
+ ZStack {
128
+ // A link the system opens, read as a link; while loading it stays a
129
+ // link and opens nothing until loading ends, unless interruptible.
130
+ SwiftUI.Link(destination: destination) { content }
131
+ .environment(\.openURL, OpenURLAction { _ in
132
+ let lands = acceptsPress
133
+ if lands { props.onClick?() }
134
+ return lands ? .systemAction : .handled
135
+ })
136
+ .focused($focus, equals: .link)
137
+ .disabled(isDisabled)
138
+ .opacity(isDisabled ? 0 : 1)
139
+ .accessibilityHidden(isDisabled)
140
+ plainButton
141
+ .focused($focus, equals: .button)
142
+ .disabled(!isDisabled)
143
+ .opacity(isDisabled ? 1 : 0)
144
+ .accessibilityHidden(!isDisabled)
78
145
  }
146
+ .onChange(of: isDisabled) { _, disabled in
147
+ if focus != nil { focus = disabled ? .button : .link }
148
+ }
149
+ } else {
150
+ plainButton
151
+ }
152
+ }
153
+
154
+ /// A disabled Button keeps its focus stop, as core keeps it focusable, so
155
+ /// keyboard users reach it and its tooltip; presses never land. VoiceOver
156
+ /// reads the platform's button, dimmed when disabled, in both states, so a
157
+ /// focused Button that becomes disabled keeps one view structure and focus.
158
+ private var plainButton: some View {
159
+ SwiftUI.Button(action: activate) { content }
160
+ .accessibilityAddTraits(Self.accessibilityTraits)
161
+ .accessibilityRepresentation {
162
+ SwiftUI.Button(accessibilityText, action: activate).disabled(isDisabled)
163
+ }
164
+ }
165
+
166
+ private var content: some View {
167
+ // The label keeps its place while loading so the width holds; the
168
+ // spinner draws over it.
169
+ SwiftUI.Text(props.label)
170
+ .lineLimit(1)
79
171
  .opacity(isLoading ? 0 : 1)
80
172
  .overlay {
81
173
  if isLoading {
82
174
  ProgressView().controlSize(.small).tint(foreground)
83
175
  }
84
176
  }
85
- }
86
- .buttonStyle(
87
- AstryxButtonStyle(
88
- astryx: astryx,
89
- size: size,
90
- fillsWidth: fillsWidth,
91
- foreground: foreground,
92
- background: background
93
- )
177
+ }
178
+
179
+ private var style: AstryxButtonStyle {
180
+ AstryxButtonStyle(
181
+ astryx: astryx,
182
+ height: height,
183
+ fillsWidth: props.width == .fill,
184
+ foreground: foreground,
185
+ background: background,
186
+ shadow: shadow,
187
+ isDimmed: isDisabled
94
188
  )
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
189
  }
105
190
 
106
191
  /// The button's accessible name: its label, also while loading.
107
- var accessibilityText: String { label }
192
+ var accessibilityText: String { props.label }
108
193
 
109
194
  /// The traits assistive technology gets: a button.
110
195
  static var accessibilityTraits: AccessibilityTraits { .isButton }
@@ -113,52 +198,81 @@ public struct Button: View {
113
198
  /// catalog message, in the person's language.
114
199
  var loadingValue: String? { isLoading ? AstryxStrings.buttonLoading : nil }
115
200
 
116
- /// Runs the action unless the button is loading.
201
+ /// Whether a press lands now: not while loading, unless interruptible.
202
+ var acceptsPress: Bool { !isDisabled && (!isLoading || props.isInterruptible == true) }
203
+
204
+ /// A disabled Button keeps its focus stop (core Button FR10).
205
+ var keepsFocusWhileDisabled: Bool { isDisabled }
206
+
207
+ /// Runs `onClick` when a press lands.
117
208
  func activate() {
118
- guard !isLoading else { return }
119
- action()
209
+ guard acceptsPress else { return }
210
+ props.onClick?()
211
+ }
212
+
213
+ var height: CGFloat {
214
+ let size = astryx.tokens.size
215
+ switch props.size ?? .md {
216
+ case .sm: return size.elementSm
217
+ case .md: return size.elementMd
218
+ case .lg: return size.elementLg
219
+ }
120
220
  }
121
221
 
122
222
  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")
223
+ let color = astryx.tokens.color
224
+ switch props.variant ?? .secondary {
225
+ case .primary: return color.onAccent
226
+ case .destructive: return color.onError
227
+ case .secondary, .ghost: return color.textPrimary
127
228
  }
128
229
  }
129
230
 
130
231
  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
232
+ let color = astryx.tokens.color
233
+ switch props.variant ?? .secondary {
234
+ case .primary: return color.accent
235
+ case .destructive: return color.error
236
+ case .secondary: return color.neutral
237
+ case .ghost: return .clear
238
+ }
239
+ }
240
+
241
+ var shadow: [ShadowLayer] {
242
+ let shadow = astryx.tokens.shadow
243
+ switch props.elevation ?? .none {
244
+ case .none: return []
245
+ case .low: return shadow.low
246
+ case .med: return shadow.med
247
+ case .high: return shadow.high
136
248
  }
137
249
  }
138
250
  }
139
251
 
140
252
  struct AstryxButtonStyle: ButtonStyle {
141
253
  let astryx: AstryxStyle
142
- let size: AstryxButtonSize
254
+ let height: CGFloat
143
255
  let fillsWidth: Bool
144
256
  let foreground: Color
145
257
  let background: Color
146
- @Environment(\.isEnabled) private var isEnabled
258
+ let shadow: [ShadowLayer]
259
+ /// Draws the disabled look for a disabled Button that keeps its focus stop.
260
+ var isDimmed = false
261
+ @Environment(\.isEnabled) private var environmentEnabled
262
+ private var isEnabled: Bool { environmentEnabled && !isDimmed }
147
263
 
148
264
  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)
265
+ let shape = RoundedRectangle(cornerRadius: astryx.tokens.radius.element, style: .continuous)
266
+ let font = astryx.tokens.font
156
267
  configuration.label
157
- .font(astryx.font(size: "text-label-size", 14, weight: "text-label-weight"))
268
+ .font(astryx.font(TypeStyle(font.label, families: font.family.body)))
158
269
  .foregroundStyle(foreground)
159
- .padding(.horizontal, astryx.space(3))
160
- .frame(maxWidth: fillsWidth ? .infinity : nil, minHeight: height)
270
+ .padding(.horizontal, astryx.tokens.space.s3)
271
+ // Grows with text size, but not so far that one line of a large label
272
+ // becomes a tall slab.
273
+ .frame(maxWidth: fillsWidth ? .infinity : nil, minHeight: height * min(astryx.textScale, 1.4))
161
274
  .background(shape.fill(background))
275
+ .astryxShadow(shadow, in: shape)
162
276
  .opacity(isEnabled ? (configuration.isPressed ? 0.75 : 1) : 0.5)
163
277
  // Drawn at the token size; laid out at least 44 by 44 so the whole
164
278
  // platform-minimum tap area counts.
@@ -166,3 +280,4 @@ struct AstryxButtonStyle: ButtonStyle {
166
280
  .contentShape(Rectangle())
167
281
  }
168
282
  }
283
+
@@ -1,5 +1,6 @@
1
1
  // Copyright (c) Meta Platforms, Inc. and affiliates.
2
2
 
3
+ import AstryxNative
3
4
  import Foundation
4
5
 
5
6
  /// Core's catalog messages, read from `Resources/<locale>.lproj`, which
@@ -44,6 +45,30 @@ enum AstryxStrings {
44
45
  fill(localized("@astryx.thumbnail.remove", "Remove {accessibleName}"), ["accessibleName": accessibleName])
45
46
  }
46
47
 
48
+ /// The indicator after an optional field's label.
49
+ static var fieldOptional: String { localized("@astryx.field.optional", "Optional") }
50
+ /// What VoiceOver reads for a read-only field, which SwiftUI cannot mark.
51
+ static var inputReadOnly: String { localized("@astryx.input.readOnly", "Read only") }
52
+ /// The indicator after a required field's label.
53
+ static var fieldRequired: String { localized("@astryx.field.required", "Required") }
54
+ /// The hover and pointer tip on an input's clear button.
55
+ static var clearTooltip: String { localized("@astryx.inputClearButton.tooltip", "Clear") }
56
+
57
+ /// The clear button's accessible name, naming the field.
58
+ static func clearLabel(_ label: String) -> String {
59
+ localized("@astryx.textInput.clearLabel", "Clear {label}").replacingOccurrences(of: "{label}", with: label)
60
+ }
61
+
62
+ /// The accessible name of an input's status button when its message shows
63
+ /// in a tip.
64
+ static func statusButton(_ type: TextInputStatusType) -> String {
65
+ switch type {
66
+ case .error: localized("@astryx.input.statusButton.error", "Error details")
67
+ case .warning: localized("@astryx.input.statusButton.warning", "Warning details")
68
+ case .success: localized("@astryx.input.statusButton.success", "Success details")
69
+ }
70
+ }
71
+
47
72
  static func localized(_ key: String, _ english: String) -> String {
48
73
  resourceBundle.localizedString(forKey: key, value: english, table: nil)
49
74
  }
@@ -6,9 +6,30 @@
6
6
  /* Screen-reader-only live-region announcement while a Button is in its loading state (spinner shown, action in flight). Progressive sense ("work in progress"), not a noun. Kept separate from `typeahead.loading` — translations may diverge. */
7
7
  "@astryx.button.loading" = "Laai tans";
8
8
 
9
+ /* Visible indicator shown next to a form field's label when the field is optional (isOptional). Mutually exclusive with the required indicator. Very short — appears inline after the label text. */
10
+ "@astryx.field.optional" = "Opsioneel";
11
+
12
+ /* Visible indicator shown next to a form field's label when the field must be filled in (isRequired). Mutually exclusive with the optional indicator. Very short — appears inline after the label text. */
13
+ "@astryx.field.required" = "Verpligtend";
14
+
15
+ /* Screen-reader-only description appended to a read-only form control when the platform does not expose aria-readonly for that control's host element. Short state label, not an instruction. */
16
+ "@astryx.input.readOnly" = "Slegs lees";
17
+
18
+ /* Accessible name for the focusable status icon button inside an input when statusVariant is 'tooltip' and the status type is error. Activating or focusing the button reveals the error message in a tooltip. Keep it short — it labels an icon-only button. */
19
+ "@astryx.input.statusButton.error" = "Foutbesonderhede";
20
+
21
+ /* Accessible name for the focusable status icon button inside an input when statusVariant is 'tooltip' and the status type is success. Activating or focusing the button reveals the success message in a tooltip. */
22
+ "@astryx.input.statusButton.success" = "Suksesbesonderhede";
23
+
24
+ /* Accessible name for the focusable status icon button inside an input when statusVariant is 'tooltip' and the status type is warning. Activating or focusing the button reveals the warning message in a tooltip. */
25
+ "@astryx.input.statusButton.warning" = "Waarskuwingbesonderhede";
26
+
9
27
  /* Screen-reader-only default name for a standalone Spinner's role="status" element, used when the consumer supplies neither `aria-label` nor a visible string label. Present-progressive form. Kept separate from `button.loading` and `typeahead.loading` — translations may diverge. */
10
28
  "@astryx.spinner.loading" = "Laai tans";
11
29
 
30
+ /* Screen-reader-only label on the X that clears the value from a TextInput. Example: `Clear Email`, `Clear Name`. */
31
+ "@astryx.textInput.clearLabel" = "Vee {label} uit";
32
+
12
33
  /* Generic screen-reader name for a Thumbnail (image preview) that has no `alt` or `label`. Last-resort fallback so the control is never nameless; prefer a real name via `alt` or `label`. */
13
34
  "@astryx.thumbnail.fallbackName" = "Duimnael";
14
35