@astryxdesign/native 0.7.6-canary.3ccdb9e → 0.7.6-canary.3d44c58

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 (33) hide show
  1. package/apple/Package.swift +0 -2
  2. package/apple/README.md +18 -3
  3. package/apple/Sources/Astryx/Avatar/Avatar.swift +9 -12
  4. package/apple/Sources/Astryx/Base/SafeDestination.swift +48 -0
  5. package/apple/Sources/Astryx/Button/Button.swift +3 -21
  6. package/apple/Sources/Astryx/Item/Item.swift +354 -0
  7. package/apple/Sources/Astryx/Wire/AstryxNodeView.swift +9 -0
  8. package/apple/Sources/AstryxNative/Generated/AstryxNode.swift +3 -0
  9. package/apple/Sources/AstryxNative/Generated/Item.swift +173 -0
  10. package/apple/Tests/AstryxTests/AvatarTests.swift +14 -0
  11. package/apple/Tests/AstryxTests/ItemTests.swift +295 -0
  12. package/dist/components/Item.d.ts +10 -0
  13. package/dist/components/Item.d.ts.map +1 -0
  14. package/dist/components/Item.js +22 -0
  15. package/dist/components/index.d.ts.map +1 -1
  16. package/dist/components/index.js +2 -0
  17. package/dist/types/android.d.ts +16 -3
  18. package/dist/types/core.d.ts +18 -0
  19. package/dist/types/ios.d.ts +16 -3
  20. package/dist/types/macos.d.ts +16 -3
  21. package/dist/types/shared.d.ts +16 -3
  22. package/dist/types/windows.d.ts +16 -3
  23. package/package.json +3 -3
  24. package/src/components/Item.ts +34 -0
  25. package/src/components/index.ts +2 -0
  26. package/src/contract/contracts.test.ts +39 -0
  27. package/src/theme/swiftIOSTheme.test.ts +372 -38
  28. package/apple/Sources/Astryx/Avatar/Avatar.spec.md +0 -219
  29. package/apple/Sources/Astryx/Avatar/AvatarStatusDot.spec.md +0 -162
  30. package/apple/Sources/Astryx/Button/Button.spec.md +0 -174
  31. package/apple/Sources/Astryx/Icon/Icon.spec.md +0 -189
  32. package/apple/Sources/Astryx/TextInput/TextInput.spec.md +0 -179
  33. 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", "Button/Button.spec.md", "Icon/Icon.spec.md", "TextInput/TextInput.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,14 @@ 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
+ │ │ ├── Item/ # Item
31
32
  │ │ ├── Text/ # Text
32
33
  │ │ ├── Heading/ # Heading
33
34
  │ │ ├── Button/ # Button
34
35
  │ │ ├── TextInput/ # TextInput
35
- │ │ ├── Thumbnail/ # Thumbnail, Thumbnail.spec.md
36
+ │ │ ├── Thumbnail/ # Thumbnail
36
37
  │ │ ├── Card/ # Card
37
38
  │ │ └── Banner/ # Banner
38
39
  │ └── AstryxNative/ # contracts, wire, logic host (module AstryxNative)
@@ -46,6 +47,9 @@ apple/
46
47
  └── Tests/
47
48
  ```
48
49
 
50
+ Each component's Swift spec sits beside its core spec, at
51
+ `docs/specs/components/<ComponentRoot>/<Name>.swift.spec.md`.
52
+
49
53
  - **`Sources/Astryx/<Name>/`** holds one folder per component, named as in
50
54
  `packages/core/src/<Name>/`. Each view has core's component name in the
51
55
  `Astryx` module (`Button`, `Text`), which re-exports SwiftUI and the
@@ -143,6 +147,17 @@ export:
143
147
  node packages/native/scripts/swift-ios-theme.mjs --theme ./src/acmeTheme.ts#acmeTheme --out ./Sources/Theme
144
148
  ```
145
149
 
150
+ The generator reads the CSS forms its spec lists
151
+ (`specs/swift/library/theme-generator.md`, G7): hex and named colors, `rgb()`,
152
+ `hsl()`, `hwb()`, Lab, LCH, OKLab, OKLCH, `color()` in the listed spaces,
153
+ `color-mix()`, and relative colors (wide-gamut colors kept as Display P3),
154
+ lengths with `calc()`, and `[light, dark]` tuples. A value it can only
155
+ approximate, such as a color outside Display P3 (clipped, as browsers draw it),
156
+ prints a warning after the run; a value with no static form, such as
157
+ `currentColor` or `50%`, takes core's default for that token with a warning, or
158
+ stops the run when core has none. A form the generator does not read yet stops
159
+ the run, saying so. Add `--strict` to fail on any warning.
160
+
146
161
  A theme's local tokens are not generated yet; the generator names each one it
147
162
  leaves out.
148
163
 
@@ -77,10 +77,7 @@ public struct Avatar<Status: View>: View {
77
77
  /// the build with its value, and an unnamed link or button is flagged.
78
78
  private func checkInDevelopment() {
79
79
  #if DEBUG
80
- if Avatar.isUnusableHref(props.href) {
81
- assertionFailure(
82
- "Avatar href \"\(props.href ?? "")\" is not an absolute URL an app can open; it draws without a link.")
83
- }
80
+ astryxCheckHref(props.href, in: "Avatar")
84
81
  // The avatar's own name: a status label alone does not name a person.
85
82
  let ownName = Avatar.meaningful(props.alt) ?? Avatar.meaningful(props.name)
86
83
  if Avatar.needsNameWarning(tap: tap, isNamed: ownName != nil) {
@@ -108,11 +105,7 @@ public struct Avatar<Status: View>: View {
108
105
  }
109
106
 
110
107
  /// A non-empty `href` that is not an absolute URL, such as a web path.
111
- nonisolated static func isUnusableHref(_ href: String?) -> Bool {
112
- guard let href, !href.isEmpty else { return false }
113
- guard let url = URL(string: href) else { return true }
114
- return url.scheme == nil
115
- }
108
+ nonisolated static func isUnusableHref(_ href: String?) -> Bool { astryxIsUnusableHref(href) }
116
109
 
117
110
  /// The picture as a link with `href`, a button with only `onClick`, or a
118
111
  /// static image (Swift component API, A4). All three draw the same.
@@ -309,9 +302,13 @@ public struct Avatar<Status: View>: View {
309
302
 
310
303
  /// The URL the avatar opens, or `nil` when it has no usable `href`.
311
304
  var linkURL: URL? {
312
- // Only an absolute URL the system can open; a malformed or relative
313
- // `href` leaves the avatar static or a button.
314
- guard let url = url(props.href), url.scheme != nil else { return nil }
305
+ // Only an absolute URL the system can open, and never one core's
306
+ // navigation destinations refuse (a script URL or an HTML `data:` URL);
307
+ // a malformed, relative, or refused `href` leaves the avatar static or a
308
+ // button.
309
+ guard let href = props.href, astryxSafeDestination(href) != nil,
310
+ let url = url(href), url.scheme != nil
311
+ else { return nil }
315
312
  return url
316
313
  }
317
314
 
@@ -0,0 +1,48 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ import Foundation
4
+
5
+ /// An `href` as a link reads it: control characters removed and surrounding
6
+ /// whitespace trimmed, as core's shared destination rule does.
7
+ func astryxNormalizedHref(_ href: String) -> String {
8
+ String(String.UnicodeScalarView(
9
+ href.unicodeScalars.filter { $0.value > 0x1F && $0.value != 0x7F }
10
+ )).trimmingCharacters(in: .whitespacesAndNewlines)
11
+ }
12
+
13
+ /// A destination a link may open, normalized as core's shared rule does, or
14
+ /// nil for a script URL or an HTML document in a `data:` URL, which core's
15
+ /// navigation destinations refuse (`family:navigation-destinations` FR4).
16
+ /// The scheme is compared in lowercase without the spaces after its colon,
17
+ /// which a data URL's media type ignores (`data: text/html` is HTML).
18
+ func astryxSafeDestination(_ href: String) -> String? {
19
+ let normalized = astryxNormalizedHref(href)
20
+ var comparable = normalized.lowercased()
21
+ if let colon = comparable.firstIndex(of: ":"),
22
+ comparable[..<colon].range(of: "^[a-z][a-z0-9+.-]*$", options: .regularExpression) != nil {
23
+ let rest = comparable[comparable.index(after: colon)...].drop { $0 == " " }
24
+ comparable = String(comparable[...colon]) + rest
25
+ }
26
+ for blocked in ["javascript:", "vbscript:", "data:text/html"] where comparable.hasPrefix(blocked) {
27
+ return nil
28
+ }
29
+ return normalized
30
+ }
31
+
32
+ /// A non-empty `href` that is not an absolute URL, such as a web path. Pass
33
+ /// the `href` as the view's link reads it, so the check and the link agree.
34
+ func astryxIsUnusableHref(_ href: String?) -> Bool {
35
+ guard let href, !href.isEmpty else { return false }
36
+ guard let url = URL(string: href) else { return true }
37
+ return url.scheme == nil
38
+ }
39
+
40
+ /// Stops a development build on an `href` an app cannot open, naming the
41
+ /// component and the value; a release build draws the view without a link.
42
+ func astryxCheckHref(_ href: String?, in component: String) {
43
+ #if DEBUG
44
+ if astryxIsUnusableHref(href) {
45
+ assertionFailure("\(component) href \"\(href ?? "")\" is not an absolute URL an app can open; it draws without a link.")
46
+ }
47
+ #endif
48
+ }
@@ -82,27 +82,9 @@ public struct Button: View {
82
82
  /// disabled button, as core draws a disabled `<button>`.
83
83
  var link: URL? { isDisabled ? nil : destination }
84
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
105
- }
85
+ /// A destination a link may open, or nil when core's navigation
86
+ /// destinations refuse it (`astryxSafeDestination`).
87
+ nonisolated static func safeDestination(_ href: String) -> String? { astryxSafeDestination(href) }
106
88
 
107
89
  public var body: some View {
108
90
  control
@@ -0,0 +1,354 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ import AstryxNative
4
+ import SwiftUI
5
+
6
+ #if canImport(UIKit)
7
+ import UIKit
8
+ #elseif canImport(AppKit)
9
+ import AppKit
10
+ #endif
11
+
12
+ /// A row: optional marker and leading content, a label with an optional
13
+ /// description, and optional trailing content (core `Item`). With `onClick`
14
+ /// it is a button and with `href` a link; either way it draws the same row.
15
+ ///
16
+ /// ```swift
17
+ /// Item("Ada Lovelace", description: "Mathematician") {
18
+ /// Icon(.check)
19
+ /// } endContent: {
20
+ /// Icon(.chevronRight, color: .secondary)
21
+ /// }
22
+ /// Item("Notifications", onClick: openNotifications)
23
+ /// Item("Archive") { Icon(.upload) } onClick: { archive() }
24
+ /// ```
25
+ public struct Item<Marker: View, StartContent: View, EndContent: View>: View {
26
+ /// The contract the view draws, the same for every initializer.
27
+ public let props: ItemProps
28
+ let marker: Marker
29
+ let startContent: StartContent
30
+ let endContent: EndContent
31
+ @Environment(\.astryx) private var astryx
32
+
33
+ /// A row from its labeled props. A single trailing closure is the start
34
+ /// content; set `onClick` by label or as a trailing `onClick:` closure.
35
+ /// - Parameters:
36
+ /// - label: The row's main words (core `label`).
37
+ /// - description: Supporting words under or beside the label (core `description`).
38
+ /// - align: How the start and end content line up with the words: `center`, `start`, or `firstLine`; `nil` is `center` (core `align`).
39
+ /// - density: `compact`, `balanced`, or `spacious` padding; `nil` is `balanced` (core `density`).
40
+ /// - labelLines: Lines the label may take before it truncates; `nil` is one (core `labelLines`).
41
+ /// - descriptionLines: Lines the description may take before it truncates; `nil` is one (core `descriptionLines`).
42
+ /// - layout: `stacked` puts the description under the label, `inline` beside it; `nil` is `stacked` (core `layout`).
43
+ /// - href: A URL the system opens when the row is tapped; the row becomes a link and `onClick` does not run (core `href`).
44
+ /// - isHighlighted: Draws the row's highlighted background (core `isHighlighted`).
45
+ /// - isSelected: Draws the row as selected and reads it as selected (core `isSelected`).
46
+ /// - isDisabled: Dims the row; a button or link row stays reachable and does nothing (core `isDisabled`).
47
+ /// - startContent: Content before the words: an `Icon`, an `Avatar`, or a checkbox (core `startContent`).
48
+ /// - endContent: Content after the words, such as a badge, a value, or an icon (core `endContent`).
49
+ /// - marker: A list marker before the start content; pass every slot by label when you set it (core `marker`).
50
+ /// - onClick: Runs when the row is tapped; the row becomes a button when it has no `href` (core `onClick`).
51
+ public init(
52
+ _ label: String,
53
+ description: String? = nil,
54
+ align: ItemAlign? = nil,
55
+ density: ItemDensity? = nil,
56
+ labelLines: Double? = nil,
57
+ descriptionLines: Double? = nil,
58
+ layout: ItemLayout? = nil,
59
+ href: String? = nil,
60
+ isHighlighted: Bool? = nil,
61
+ isSelected: Bool? = nil,
62
+ isDisabled: Bool? = nil,
63
+ @ViewBuilder startContent: () -> StartContent = { EmptyView() },
64
+ @ViewBuilder endContent: () -> EndContent = { EmptyView() },
65
+ @ViewBuilder marker: () -> Marker = { EmptyView() },
66
+ onClick: (() -> Void)? = nil
67
+ ) {
68
+ self.init(
69
+ ItemProps(
70
+ label: label, description: description, align: align, density: density, labelLines: labelLines,
71
+ descriptionLines: descriptionLines, layout: layout, onClick: onClick, href: href,
72
+ isHighlighted: isHighlighted, isSelected: isSelected, isDisabled: isDisabled
73
+ ),
74
+ startContent: startContent, endContent: endContent, marker: marker
75
+ )
76
+ }
77
+
78
+ /// A row from its generated contract, for generated and wire-driven callers.
79
+ public init(
80
+ _ props: ItemProps,
81
+ @ViewBuilder startContent: () -> StartContent = { EmptyView() },
82
+ @ViewBuilder endContent: () -> EndContent = { EmptyView() },
83
+ @ViewBuilder marker: () -> Marker = { EmptyView() }
84
+ ) {
85
+ self.props = props
86
+ self.marker = marker()
87
+ self.startContent = startContent()
88
+ self.endContent = endContent()
89
+ }
90
+
91
+ public var body: some View {
92
+ control
93
+ .onAppear(perform: checkInDevelopment)
94
+ .astryxBase(props.base)
95
+ }
96
+
97
+ /// An `href` an app cannot open, such as a web path, stops a development
98
+ /// build with its value; a release build draws the row without a link.
99
+ private func checkInDevelopment() {
100
+ astryxCheckHref(props.href.map(astryxNormalizedHref), in: "Item")
101
+ }
102
+
103
+ /// A non-empty `href` that is not an absolute URL, read as the row's link
104
+ /// reads it (trimmed, control characters removed).
105
+ nonisolated static func isUnusableHref(_ href: String?) -> Bool {
106
+ astryxIsUnusableHref(href.map(astryxNormalizedHref))
107
+ }
108
+
109
+ /// The row as a link with `href`, a button with only `onClick`, or static
110
+ /// (Swift component API, A4). All three draw the same row.
111
+ @ViewBuilder private var control: some View {
112
+ if let url = linkURL {
113
+ SwiftUI.Link(destination: url) { row(minHeight: Item.minimumTarget, paintsFill: false) }
114
+ .buttonStyle(pressStyle)
115
+ .disabled(isDisabled)
116
+ .modifier(ItemAccessibility(isSelected: isSelected))
117
+ } else if let onClick = props.onClick {
118
+ SwiftUI.Button(action: onClick) { row(minHeight: Item.minimumTarget, paintsFill: false) }
119
+ .buttonStyle(pressStyle)
120
+ .disabled(isDisabled)
121
+ .modifier(ItemAccessibility(isSelected: isSelected))
122
+ } else {
123
+ row(minHeight: nil, paintsFill: true)
124
+ .accessibilityElement(children: .combine)
125
+ // A disabled row reads as dimmed, as core marks it aria-disabled.
126
+ .disabled(isDisabled)
127
+ .modifier(ItemAccessibility(isSelected: isSelected))
128
+ }
129
+ }
130
+
131
+ /// A link or button row's fill and its press, painted by the button style
132
+ /// so the press sits above a selected or highlighted fill, as core's does.
133
+ private var pressStyle: ItemPressStyle {
134
+ ItemPressStyle(radius: astryx.tokens.radius.element, fill: background, pressed: astryx.tokens.color.overlayPressed)
135
+ }
136
+
137
+ /// The row's drawing; a tappable row is at least `minHeight` tall. A static
138
+ /// row paints its own fill; a link or button row's style paints it.
139
+ private func row(minHeight: CGFloat?, paintsFill: Bool) -> some View {
140
+ HStack(alignment: rowAlignment, spacing: astryx.tokens.space.s2) {
141
+ slot(marker)
142
+ slot(startContent)
143
+ // A disabled row dims its words and end content, as core does; the
144
+ // marker and start content keep their own look (a checkbox shows its
145
+ // own disabled state).
146
+ words
147
+ .frame(maxWidth: .infinity, alignment: .leading)
148
+ .opacity(isDisabled ? 0.5 : 1)
149
+ slot(endContent)
150
+ .opacity(isDisabled ? 0.5 : 1)
151
+ }
152
+ .padding(.horizontal, inlinePadding)
153
+ .padding(.vertical, blockPadding)
154
+ .frame(maxWidth: .infinity, minHeight: minHeight, alignment: .leading)
155
+ .background(
156
+ RoundedRectangle(cornerRadius: astryx.tokens.radius.element, style: .continuous)
157
+ .fill(paintsFill ? background ?? .clear : .clear)
158
+ )
159
+ .contentShape(Rectangle())
160
+ }
161
+
162
+ /// A slot's content. For `firstLine`, its center sits on the center of
163
+ /// the label's first line box, however tall the slot is.
164
+ @ViewBuilder private func slot<Content: View>(_ content: Content) -> some View {
165
+ if resolvedAlign == .firstLine {
166
+ let lineCenterAboveBaseline = firstLineCenterAboveBaseline
167
+ content.alignmentGuide(.firstTextBaseline) { d in d.height / 2 + lineCenterAboveBaseline }
168
+ } else {
169
+ content
170
+ }
171
+ }
172
+
173
+ @ViewBuilder private var words: some View {
174
+ let label = SwiftUI.Text(props.label)
175
+ .font(astryx.font(size: "text-body-size", 14, weight: "text-body-weight"))
176
+ .foregroundStyle(astryx.tokens.color.textPrimary)
177
+ .lineLimit(Item.lines(props.labelLines))
178
+ .modifier(ItemLeading(extra: extraLeading(astryx.tokens.font.body.leading, size: astryx.tokens.font.body.size)))
179
+ let description = props.description.map { words in
180
+ SwiftUI.Text(words)
181
+ .font(astryx.font(size: "text-supporting-size", 12, weight: "text-supporting-weight"))
182
+ .foregroundStyle(astryx.tokens.color.textSecondary)
183
+ .lineLimit(Item.lines(props.descriptionLines))
184
+ .modifier(
185
+ ItemLeading(extra: extraLeading(astryx.tokens.font.supporting.leading, size: astryx.tokens.font.supporting.size)))
186
+ }
187
+ // Inline only with a description, as core's: a label alone keeps its
188
+ // width limit and truncates.
189
+ if (props.layout ?? .stacked) == .inline, description != nil {
190
+ HStack(alignment: .firstTextBaseline, spacing: astryx.tokens.space.s1) {
191
+ label.fixedSize(horizontal: true, vertical: false)
192
+ description
193
+ }
194
+ } else {
195
+ VStack(alignment: .leading, spacing: 0) {
196
+ label
197
+ description
198
+ }
199
+ }
200
+ }
201
+
202
+ var resolvedAlign: ItemAlign { props.align ?? .center }
203
+
204
+ private var rowAlignment: VerticalAlignment {
205
+ switch resolvedAlign {
206
+ case .center: .center
207
+ case .start: .top
208
+ case .firstLine: .firstTextBaseline
209
+ }
210
+ }
211
+
212
+ /// How far the center of the label's first line sits above its baseline:
213
+ /// half the font's line height above the line's bottom, less the descent,
214
+ /// in the font's own metrics at the current text size. The theme's extra
215
+ /// leading is split evenly above and below, so it does not move the center.
216
+ var firstLineCenterAboveBaseline: CGFloat {
217
+ let metrics = astryxSystemFontMetrics(astryx.tokens.font.body.size)
218
+ return (metrics.ascender - metrics.lineHeight / 2) * astryx.textScale
219
+ }
220
+
221
+ var isDisabled: Bool { props.isDisabled == true }
222
+ var isSelected: Bool { props.isSelected == true }
223
+
224
+ /// Selected wins over highlighted, as core's styles order them.
225
+ var background: Color? {
226
+ if isSelected { return astryx.tokens.color.accentMuted }
227
+ if props.isHighlighted == true { return astryx.tokens.color.overlayHover }
228
+ return nil
229
+ }
230
+
231
+ /// Block padding per density: spacing 1, 2, or 3.
232
+ var blockPadding: CGFloat {
233
+ switch props.density ?? .balanced {
234
+ case .compact: astryx.tokens.space.s1
235
+ case .balanced: astryx.tokens.space.s2
236
+ case .spacious: astryx.tokens.space.s3
237
+ }
238
+ }
239
+
240
+ /// Inline padding: spacing 2, or 3 when spacious.
241
+ var inlinePadding: CGFloat {
242
+ (props.density ?? .balanced) == .spacious ? astryx.tokens.space.s3 : astryx.tokens.space.s2
243
+ }
244
+
245
+ /// The space core's line height adds to the font's own line, grown with
246
+ /// Dynamic Type, so a line takes the theme's leading as on the web. The
247
+ /// leading tokens are unitless line heights, as in CSS.
248
+ private func extraLeading(_ leading: CGFloat, size: CGFloat) -> CGFloat {
249
+ max(0, leading * size - astryxSystemLineHeight(size)) * astryx.textScale
250
+ }
251
+
252
+ /// The URL the row opens, or `nil` when it has no usable `href`.
253
+ var linkURL: URL? {
254
+ // Only an absolute URL the system can open, and never one core's
255
+ // navigation destinations refuse (a script URL or an HTML `data:` URL);
256
+ // a malformed, relative, or refused `href` leaves the row static or a
257
+ // button.
258
+ guard let href = props.href, let safe = astryxSafeDestination(href), !safe.isEmpty,
259
+ let url = URL(string: safe), url.scheme != nil
260
+ else { return nil }
261
+ return url
262
+ }
263
+
264
+ /// How the row takes a tap: a link with a usable `href`, else a button
265
+ /// with `onClick`, else none.
266
+ enum Tap: Equatable { case link, button, none }
267
+
268
+ var tap: Tap {
269
+ if linkURL != nil { return .link }
270
+ if props.onClick != nil { return .button }
271
+ return .none
272
+ }
273
+
274
+ /// The traits the row adds to what its control already carries: selected
275
+ /// when selected. A link or button keeps its own trait (X1).
276
+ nonisolated static func addedTraits(isSelected: Bool) -> AccessibilityTraits {
277
+ isSelected ? .isSelected : []
278
+ }
279
+
280
+ /// A tappable row is at least 44 points tall (Swift accessibility, X7).
281
+ static var minimumTarget: CGFloat { 44 }
282
+
283
+ /// The line limit for `labelLines` or `descriptionLines`: one when unset,
284
+ /// as core's row truncates a label to one line by default, and otherwise
285
+ /// core's shared count (`astryxLineLimit`), so zero shows every line.
286
+ nonisolated static func lines(_ value: Double?) -> Int? { astryxLineLimit(value ?? 1) }
287
+ }
288
+
289
+ /// Paints a link or button row's fill and, while pressed, the pressed
290
+ /// overlay above it, as core's interactive rows do.
291
+ struct ItemPressStyle: ButtonStyle {
292
+ let radius: CGFloat
293
+ /// The selected or highlighted fill, if any.
294
+ let fill: Color?
295
+ let pressed: Color
296
+
297
+ func makeBody(configuration: Configuration) -> some View {
298
+ configuration.label
299
+ .background(ItemPressBackground(radius: radius, fill: fill, pressed: pressed, isPressed: configuration.isPressed))
300
+ }
301
+ }
302
+
303
+ /// The fill with the press above it.
304
+ struct ItemPressBackground: View {
305
+ let radius: CGFloat
306
+ let fill: Color?
307
+ let pressed: Color
308
+ let isPressed: Bool
309
+
310
+ var body: some View {
311
+ let shape = RoundedRectangle(cornerRadius: radius, style: .continuous)
312
+ ZStack {
313
+ shape.fill(fill ?? .clear)
314
+ shape.fill(isPressed ? pressed : .clear)
315
+ }
316
+ }
317
+ }
318
+
319
+ /// Spreads the extra leading between lines and half above and below, as a
320
+ /// CSS line height does.
321
+ private struct ItemLeading: ViewModifier {
322
+ let extra: CGFloat
323
+
324
+ func body(content: Content) -> some View {
325
+ content.lineSpacing(extra).padding(.vertical, extra / 2)
326
+ }
327
+ }
328
+
329
+ /// The system font's own line height at a size.
330
+ func astryxSystemLineHeight(_ size: CGFloat) -> CGFloat {
331
+ astryxSystemFontMetrics(size).lineHeight
332
+ }
333
+
334
+ /// The system font's ascender and line height at a size.
335
+ func astryxSystemFontMetrics(_ size: CGFloat) -> (ascender: CGFloat, lineHeight: CGFloat) {
336
+ #if canImport(UIKit)
337
+ let font = UIFont.systemFont(ofSize: size)
338
+ return (font.ascender, font.lineHeight)
339
+ #elseif canImport(AppKit)
340
+ let font = NSFont.systemFont(ofSize: size)
341
+ return (font.ascender, font.ascender - font.descender + font.leading)
342
+ #else
343
+ return (size * 0.95, size * 1.2)
344
+ #endif
345
+ }
346
+
347
+ /// A selected row reads as selected (Swift accessibility, X1).
348
+ private struct ItemAccessibility: ViewModifier {
349
+ let isSelected: Bool
350
+
351
+ func body(content: Content) -> some View {
352
+ content.accessibilityAddTraits(Item<EmptyView, EmptyView, EmptyView>.addedTraits(isSelected: isSelected))
353
+ }
354
+ }
@@ -25,6 +25,14 @@ public struct AstryxNodeView: View {
25
25
  Avatar(avatar.viewProps(send: send)) { AstryxNodeList(nodes: avatar.props.status ?? [], send: send) }
26
26
  case .avatarStatusDot(let dot): AvatarStatusDot(dot.viewProps(send: send))
27
27
  case .thumbnail(let thumbnail): Thumbnail(thumbnail.viewProps(send: send))
28
+ case .item(let item):
29
+ Item(item.viewProps(send: send)) {
30
+ AstryxNodeList(nodes: item.props.startContent ?? [], send: send)
31
+ } endContent: {
32
+ AstryxNodeList(nodes: item.props.endContent ?? [], send: send)
33
+ } marker: {
34
+ AstryxNodeList(nodes: item.props.marker ?? [], send: send)
35
+ }
28
36
  default: AstryxUnsupportedNode(component: Self.componentName(node))
29
37
  }
30
38
  }
@@ -41,6 +49,7 @@ public struct AstryxNodeView: View {
41
49
  case .heading: "Heading"
42
50
  case .hStack: "HStack"
43
51
  case .icon: "Icon"
52
+ case .item: "Item"
44
53
  case .progressBar: "ProgressBar"
45
54
  case .selector: "Selector"
46
55
  case .switch: "Switch"
@@ -16,6 +16,7 @@ public enum AstryxNode: Decodable, Equatable, Sendable {
16
16
  case heading(HeadingNode)
17
17
  case hStack(HStackNode)
18
18
  case icon(IconNode)
19
+ case item(ItemNode)
19
20
  case progressBar(ProgressBarNode)
20
21
  case selector(SelectorNode)
21
22
  case `switch`(SwitchNode)
@@ -36,6 +37,7 @@ public enum AstryxNode: Decodable, Equatable, Sendable {
36
37
  case .heading(let node): return node.key
37
38
  case .hStack(let node): return node.key
38
39
  case .icon(let node): return node.key
40
+ case .item(let node): return node.key
39
41
  case .progressBar(let node): return node.key
40
42
  case .selector(let node): return node.key
41
43
  case .`switch`(let node): return node.key
@@ -64,6 +66,7 @@ public enum AstryxNode: Decodable, Equatable, Sendable {
64
66
  case "Heading": self = .heading(try HeadingNode(from: decoder))
65
67
  case "HStack": self = .hStack(try HStackNode(from: decoder))
66
68
  case "Icon": self = .icon(try IconNode(from: decoder))
69
+ case "Item": self = .item(try ItemNode(from: decoder))
67
70
  case "ProgressBar": self = .progressBar(try ProgressBarNode(from: decoder))
68
71
  case "Selector": self = .selector(try SelectorNode(from: decoder))
69
72
  case "Switch": self = .`switch`(try SwitchNode(from: decoder))