@astryxdesign/native 0.7.6-canary.8c8f0da → 0.7.6-canary.8d1864b

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.
package/apple/README.md CHANGED
@@ -29,6 +29,7 @@ apple/
29
29
  │ │ ├── Icon/ # Icon
30
30
  │ │ ├── Avatar/ # Avatar
31
31
  │ │ ├── Item/ # Item
32
+ │ │ ├── List/ # ListItem
32
33
  │ │ ├── Text/ # Text
33
34
  │ │ ├── Heading/ # Heading
34
35
  │ │ ├── Button/ # Button
@@ -302,9 +302,13 @@ public struct Avatar<Status: View>: View {
302
302
 
303
303
  /// The URL the avatar opens, or `nil` when it has no usable `href`.
304
304
  var linkURL: URL? {
305
- // Only an absolute URL the system can open; a malformed or relative
306
- // `href` leaves the avatar static or a button.
307
- 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 }
308
312
  return url
309
313
  }
310
314
 
@@ -29,6 +29,8 @@ public struct Item<Marker: View, StartContent: View, EndContent: View>: View {
29
29
  let startContent: StartContent
30
30
  let endContent: EndContent
31
31
  @Environment(\.astryx) private var astryx
32
+ @Environment(\.astryxItemListTreatment) private var listTreatment
33
+ @State private var wordsHeight: CGFloat = 0
32
34
 
33
35
  /// A row from its labeled props. A single trailing closure is the start
34
36
  /// content; set `onClick` by label or as a trailing `onClick:` closure.
@@ -131,29 +133,31 @@ public struct Item<Marker: View, StartContent: View, EndContent: View>: View {
131
133
  /// A link or button row's fill and its press, painted by the button style
132
134
  /// so the press sits above a selected or highlighted fill, as core's does.
133
135
  private var pressStyle: ItemPressStyle {
134
- ItemPressStyle(radius: astryx.tokens.radius.element, fill: background, pressed: astryx.tokens.color.overlayPressed)
136
+ ItemPressStyle(radius: cornerRadius, fill: background, pressed: astryx.tokens.color.overlayPressed)
135
137
  }
136
138
 
137
139
  /// The row's drawing; a tappable row is at least `minHeight` tall. A static
138
140
  /// row paints its own fill; a link or button row's style paints it.
139
141
  private func row(minHeight: CGFloat?, paintsFill: Bool) -> some View {
140
142
  HStack(alignment: rowAlignment, spacing: astryx.tokens.space.s2) {
141
- slot(marker)
142
- slot(startContent)
143
+ markerSlot
144
+ // A list's treatment is for this row only, not for rows nested in its slots.
145
+ slot(startContent.modifier(ItemSlotOutsideList()))
143
146
  // A disabled row dims its words and end content, as core does; the
144
147
  // marker and start content keep their own look (a checkbox shows its
145
148
  // own disabled state).
146
149
  words
147
150
  .frame(maxWidth: .infinity, alignment: .leading)
151
+ .modifier(ItemWordsHeight(isMeasured: measuresWords, height: $wordsHeight))
148
152
  .opacity(isDisabled ? 0.5 : 1)
149
- slot(endContent)
153
+ slot(endContent.modifier(ItemSlotOutsideList()))
150
154
  .opacity(isDisabled ? 0.5 : 1)
151
155
  }
152
156
  .padding(.horizontal, inlinePadding)
153
157
  .padding(.vertical, blockPadding)
154
158
  .frame(maxWidth: .infinity, minHeight: minHeight, alignment: .leading)
155
159
  .background(
156
- RoundedRectangle(cornerRadius: astryx.tokens.radius.element, style: .continuous)
160
+ RoundedRectangle(cornerRadius: cornerRadius, style: .continuous)
157
161
  .fill(paintsFill ? background ?? .clear : .clear)
158
162
  )
159
163
  .contentShape(Rectangle())
@@ -170,6 +174,43 @@ public struct Item<Marker: View, StartContent: View, EndContent: View>: View {
170
174
  }
171
175
  }
172
176
 
177
+ /// The marker. Inside a list it centers on the label's first line,
178
+ /// whatever the row's alignment, as core's list markers do.
179
+ @ViewBuilder private var markerSlot: some View {
180
+ if listTreatment?.markerOnFirstLine == true {
181
+ let center = firstLineBoxHeight / 2
182
+ switch resolvedAlign {
183
+ case .firstLine:
184
+ slot(marker)
185
+ case .start:
186
+ marker.alignmentGuide(.top) { d in d.height / 2 - center }
187
+ case .center:
188
+ let words = wordsHeight
189
+ marker.alignmentGuide(VerticalAlignment.center) { d in words / 2 - center + d.height / 2 }
190
+ }
191
+ } else {
192
+ slot(marker)
193
+ }
194
+ }
195
+
196
+ /// Whether the row measures its words: only a centre-aligned row in a list
197
+ /// with markers, which places its marker from the words' height.
198
+ private var measuresWords: Bool {
199
+ listTreatment?.markerOnFirstLine == true && resolvedAlign == .center
200
+ }
201
+
202
+ /// The height of the label's first line box: the theme's leading at the
203
+ /// current text size.
204
+ private var firstLineBoxHeight: CGFloat {
205
+ astryx.tokens.font.body.leading * astryx.tokens.font.body.size * astryx.textScale
206
+ }
207
+
208
+ /// The row's corner radius: the theme's element radius, or square inside a
209
+ /// list with dividers.
210
+ private var cornerRadius: CGFloat {
211
+ listTreatment?.squareCorners == true ? 0 : astryx.tokens.radius.element
212
+ }
213
+
173
214
  @ViewBuilder private var words: some View {
174
215
  let label = SwiftUI.Text(props.label)
175
216
  .font(astryx.font(size: "text-body-size", 14, weight: "text-body-weight"))
@@ -237,9 +278,15 @@ public struct Item<Marker: View, StartContent: View, EndContent: View>: View {
237
278
  }
238
279
  }
239
280
 
240
- /// Inline padding: spacing 2, or 3 when spacious.
281
+ /// Inline padding: the list's own inset when a list sets one, else core's
282
+ /// for the density.
241
283
  var inlinePadding: CGFloat {
242
- (props.density ?? .balanced) == .spacious ? astryx.tokens.space.s3 : astryx.tokens.space.s2
284
+ listTreatment?.inlineInset ?? Item.inlinePadding(density: props.density, tokens: astryx.tokens)
285
+ }
286
+
287
+ /// Core's inline padding for a density: spacing 2, or 3 when spacious.
288
+ static func inlinePadding(density: ItemDensity?, tokens: Tokens) -> CGFloat {
289
+ (density ?? .balanced) == .spacious ? tokens.space.s3 : tokens.space.s2
243
290
  }
244
291
 
245
292
  /// The space core's line height adds to the font's own line, grown with
@@ -352,3 +399,48 @@ private struct ItemAccessibility: ViewModifier {
352
399
  content.accessibilityAddTraits(Item<EmptyView, EmptyView, EmptyView>.addedTraits(isSelected: isSelected))
353
400
  }
354
401
  }
402
+
403
+ /// Measures the words' height only when the row needs it.
404
+ private struct ItemWordsHeight: ViewModifier {
405
+ let isMeasured: Bool
406
+ @Binding var height: CGFloat
407
+
408
+ func body(content: Content) -> some View {
409
+ if isMeasured {
410
+ content.onGeometryChange(for: CGFloat.self) { $0.size.height } action: { height = $0 }
411
+ } else {
412
+ content
413
+ }
414
+ }
415
+ }
416
+
417
+ /// A row's slots sit outside its list: a row nested in a slot takes neither
418
+ /// the list's treatment, its look, nor a number from it.
419
+ private struct ItemSlotOutsideList: ViewModifier {
420
+ func body(content: Content) -> some View {
421
+ content
422
+ .environment(\.astryxItemListTreatment, nil)
423
+ .environment(\.astryxListContext, nil)
424
+ .environment(\.astryxListNumbers, nil)
425
+ }
426
+ }
427
+
428
+ /// How a list draws the rows it holds: the marker on the first line, its own
429
+ /// inline inset (zero for a flush list), and square corners between dividers.
430
+ struct ItemListTreatment: Equatable {
431
+ var markerOnFirstLine: Bool
432
+ var inlineInset: CGFloat?
433
+ var squareCorners: Bool
434
+ }
435
+
436
+ private struct ItemListTreatmentKey: EnvironmentKey {
437
+ static let defaultValue: ItemListTreatment? = nil
438
+ }
439
+
440
+ extension EnvironmentValues {
441
+ /// Set by `ListItem` on the `Item` it draws; `nil` for an `Item` alone.
442
+ var astryxItemListTreatment: ItemListTreatment? {
443
+ get { self[ItemListTreatmentKey.self] }
444
+ set { self[ItemListTreatmentKey.self] = newValue }
445
+ }
446
+ }
@@ -0,0 +1,255 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ import AstryxNative
4
+ import SwiftUI
5
+
6
+ /// A row of a `List` (core `ListItem`): an `Item` that takes its density,
7
+ /// marker, dividers, and edge from the list that holds it. Outside a list it
8
+ /// draws as a plain `Item`.
9
+ ///
10
+ /// ```swift
11
+ /// ListItem("Preheat the oven", description: "To 180 degrees")
12
+ /// ListItem("Wi-Fi", description: "Home") {
13
+ /// Icon(.check)
14
+ /// } onClick: { openWiFi() }
15
+ /// ```
16
+ public struct ListItem<StartContent: View, EndContent: View>: View {
17
+ /// The contract the view draws, the same for both initializers.
18
+ public let props: ListItemProps
19
+ let startContent: StartContent
20
+ let endContent: EndContent
21
+ @Environment(\.astryx) private var astryx
22
+ @Environment(\.astryxListContext) private var list
23
+ @Environment(\.astryxListNumbers) private var numbers
24
+ /// This row's identity among its list's rows, for its number.
25
+ @State private var rowID = UUID()
26
+
27
+ /// A list row from its labeled props. A single trailing closure is the start
28
+ /// content; set `onClick` by label or as a trailing `onClick:` closure.
29
+ /// - Parameters:
30
+ /// - label: The row's main words (core `label`).
31
+ /// - description: Supporting words under the label (core `description`).
32
+ /// - align: How the start and end content line up with the words; `nil` is `center` (core `align`).
33
+ /// - href: A URL the system opens when the row is tapped; the row becomes a link (core `href`).
34
+ /// - isDisabled: Dims the row; a button or link row stays reachable and does nothing (core `isDisabled`).
35
+ /// - isSelected: Draws the row as selected and reads it as selected (core `isSelected`).
36
+ /// - startContent: Content before the words, such as an `Icon` or an `Avatar` (core `startContent`).
37
+ /// - endContent: Content after the words (core `endContent`).
38
+ /// - onClick: Runs when the row is tapped; the row becomes a button when it has no `href` (core `onClick`).
39
+ public init(
40
+ _ label: String,
41
+ description: String? = nil,
42
+ align: ListItemAlign? = nil,
43
+ href: String? = nil,
44
+ isDisabled: Bool? = nil,
45
+ isSelected: Bool? = nil,
46
+ @ViewBuilder startContent: () -> StartContent = { EmptyView() },
47
+ @ViewBuilder endContent: () -> EndContent = { EmptyView() },
48
+ onClick: (() -> Void)? = nil
49
+ ) {
50
+ self.init(
51
+ ListItemProps(
52
+ label: label, description: description, onClick: onClick, href: href, align: align,
53
+ isDisabled: isDisabled, isSelected: isSelected),
54
+ startContent: startContent, endContent: endContent)
55
+ }
56
+
57
+ /// A list row from its generated contract, for generated and wire-driven callers.
58
+ public init(
59
+ _ props: ListItemProps,
60
+ @ViewBuilder startContent: () -> StartContent = { EmptyView() },
61
+ @ViewBuilder endContent: () -> EndContent = { EmptyView() }
62
+ ) {
63
+ self.props = props
64
+ self.startContent = startContent()
65
+ self.endContent = endContent()
66
+ }
67
+
68
+ public var body: some View {
69
+ Item(itemProps, startContent: { startContent }, endContent: { endContent }, marker: { marker })
70
+ .environment(\.astryxItemListTreatment, treatment)
71
+ // A row in a list announces itself so the list can number its rows in
72
+ // order, as core's CSS counter counts list items.
73
+ .preference(key: ListRowsKey.self, value: list == nil ? [] : [rowID])
74
+ }
75
+
76
+ /// The `Item` this row draws: its own props, with the list's density.
77
+ var itemProps: ItemProps {
78
+ ItemProps(
79
+ label: props.label, description: props.description,
80
+ align: props.align.flatMap { ItemAlign(rawValue: $0.rawValue) },
81
+ density: list?.density, onClick: props.onClick, href: props.href, isSelected: props.isSelected,
82
+ isDisabled: props.isDisabled, base: props.base)
83
+ }
84
+
85
+ /// How the list draws this row: the marker on the first line, no inline
86
+ /// inset in a flush list, and square corners between dividers.
87
+ var treatment: ItemListTreatment? {
88
+ guard let list else { return nil }
89
+ return ItemListTreatment(
90
+ markerOnFirstLine: list.marker != nil,
91
+ inlineInset: list.isFlush ? 0 : nil,
92
+ squareCorners: list.hasDividers)
93
+ }
94
+
95
+ /// The list's marker for this row: a dot, a ring, or its number.
96
+ @ViewBuilder var marker: some View {
97
+ switch list?.marker {
98
+ case .disc:
99
+ Circle()
100
+ .fill(astryx.tokens.color.textPrimary)
101
+ .frame(width: ListItem.markerDot, height: ListItem.markerDot)
102
+ .frame(width: ListItem.markerColumn(astryx))
103
+ .accessibilityHidden(!ListItem.markerIsRead(.disc))
104
+ case .circle:
105
+ Circle()
106
+ .strokeBorder(astryx.tokens.color.textPrimary, lineWidth: 1)
107
+ .frame(width: ListItem.markerDot, height: ListItem.markerDot)
108
+ .frame(width: ListItem.markerColumn(astryx))
109
+ .accessibilityHidden(!ListItem.markerIsRead(.circle))
110
+ case .decimal:
111
+ ListMarkerNumber(
112
+ text: ListItem.number(ordinal: numbers?.number(for: rowID) ?? 1), row: rowID, numbers: numbers,
113
+ inset: rowInset)
114
+ case nil:
115
+ EmptyView()
116
+ }
117
+ }
118
+
119
+ /// The row's inline inset, which a wide number may reach into: none in a
120
+ /// flush list, else core's padding for the list's density.
121
+ var rowInset: CGFloat {
122
+ guard let list else { return 0 }
123
+ return list.isFlush ? 0 : Item<EmptyView, EmptyView, EmptyView>.inlinePadding(density: list.density, tokens: astryx.tokens)
124
+ }
125
+
126
+ /// Whether VoiceOver reads a marker: a number is read as part of the row;
127
+ /// a dot or ring is decoration and hidden.
128
+ nonisolated static func markerIsRead(_ marker: ListContext.Marker) -> Bool { marker == .decimal }
129
+
130
+ /// The marker column: core's `space.s4`, grown with Dynamic Type as the
131
+ /// number in it grows, and the same width on every row.
132
+ static func markerColumn(_ astryx: AstryxStyle) -> CGFloat {
133
+ (astryx.tokens.space.s4 * max(astryx.textScale, 1)).rounded()
134
+ }
135
+
136
+ /// A number marker's column: the marker column, or wider when the list's
137
+ /// widest number does not fit it and the row's inline inset together, so
138
+ /// every row of the list widens alike and no number moves its own row's
139
+ /// words (core's numbered markers; `spec:AST-061` FR11).
140
+ nonisolated static func numberColumn(base: CGFloat, widest: CGFloat, inset: CGFloat) -> CGFloat {
141
+ max(base, widest - inset)
142
+ }
143
+
144
+ /// Core's marker dot: 6 points.
145
+ nonisolated static var markerDot: CGFloat { 6 }
146
+
147
+ /// A decimal marker's words: the row's number and a period, as core's
148
+ /// `counter(…) "."`.
149
+ nonisolated static func number(ordinal: Int) -> String { "\(ordinal)." }
150
+ }
151
+
152
+ /// A decimal marker: the number and its period in the body type with tabular
153
+ /// figures, ending at the column's end so every period in the list lines up,
154
+ /// as core's numbered markers do. A number wider than the column reaches into
155
+ /// the row's inline inset; past that, the list's shared column widens.
156
+ struct ListMarkerNumber: View {
157
+ let text: String
158
+ var row: UUID? = nil
159
+ var numbers: ListNumbers? = nil
160
+ var inset: CGFloat = 0
161
+ @Environment(\.astryx) private var astryx
162
+ @State private var natural: CGFloat = 0
163
+
164
+ var body: some View {
165
+ let column = ListItem<EmptyView, EmptyView>.numberColumn(
166
+ base: ListItem<EmptyView, EmptyView>.markerColumn(astryx), widest: numbers?.widest ?? natural, inset: inset)
167
+ SwiftUI.Text(text)
168
+ .font(astryx.font(size: "text-body-size", 14, weight: "text-body-weight"))
169
+ .monospacedDigit()
170
+ .foregroundStyle(astryx.tokens.color.textPrimary)
171
+ .fixedSize()
172
+ .onGeometryChange(for: CGFloat.self) { $0.size.width } action: { width in
173
+ natural = width
174
+ if let row { numbers?.report(row, width: width) }
175
+ }
176
+ .frame(width: column, alignment: .trailing)
177
+ }
178
+ }
179
+
180
+ /// The list a `ListItem` sits in, as `List` describes it to its rows.
181
+ struct ListContext: Equatable {
182
+ /// The kind of marker each row draws.
183
+ enum Marker: Equatable { case disc, circle, decimal }
184
+
185
+ var density: ItemDensity?
186
+ var hasDividers: Bool
187
+ var isFlush: Bool
188
+ var marker: Marker?
189
+ }
190
+
191
+ private struct ListContextKey: EnvironmentKey {
192
+ static let defaultValue: ListContext? = nil
193
+ }
194
+
195
+ /// The numbers a list gives its rows, by row, in the order the rows appear.
196
+ @MainActor @Observable
197
+ final class ListNumbers {
198
+ private(set) var numbers: [UUID: Int] = [:]
199
+
200
+ nonisolated init() {}
201
+
202
+ /// The widest number any row of the list draws, for the shared column.
203
+ private(set) var widest: CGFloat = 0
204
+ private var widths: [UUID: CGFloat] = [:]
205
+
206
+ /// Numbers the rows in order, the first one `start`, and forgets the widths
207
+ /// of rows that left the list.
208
+ func assign(_ rows: [UUID], start: Int) {
209
+ let numbered = Dictionary(rows.enumerated().map { ($0.element, start + $0.offset) }, uniquingKeysWith: { first, _ in first })
210
+ if numbered != numbers { numbers = numbered }
211
+ let kept = widths.filter { numbered[$0.key] != nil }
212
+ if kept.count != widths.count {
213
+ widths = kept
214
+ updateWidest()
215
+ }
216
+ }
217
+
218
+ /// Records the width a row's number draws at.
219
+ func report(_ row: UUID, width: CGFloat) {
220
+ guard widths[row] != width else { return }
221
+ widths[row] = width
222
+ updateWidest()
223
+ }
224
+
225
+ private func updateWidest() {
226
+ let next = widths.values.max() ?? 0
227
+ if next != widest { widest = next }
228
+ }
229
+
230
+ func number(for row: UUID) -> Int? { numbers[row] }
231
+ }
232
+
233
+ /// The rows of a list, in order: each `ListItem` adds its own identity.
234
+ struct ListRowsKey: PreferenceKey {
235
+ static let defaultValue: [UUID] = []
236
+ static func reduce(value: inout [UUID], nextValue: () -> [UUID]) { value.append(contentsOf: nextValue()) }
237
+ }
238
+
239
+ private struct ListNumbersKey: EnvironmentKey {
240
+ static let defaultValue: ListNumbers? = nil
241
+ }
242
+
243
+ extension EnvironmentValues {
244
+ /// The list around a row; `nil` outside a list.
245
+ var astryxListContext: ListContext? {
246
+ get { self[ListContextKey.self] }
247
+ set { self[ListContextKey.self] = newValue }
248
+ }
249
+
250
+ /// The numbers of the rows of the list around a row.
251
+ var astryxListNumbers: ListNumbers? {
252
+ get { self[ListNumbersKey.self] }
253
+ set { self[ListNumbersKey.self] = newValue }
254
+ }
255
+ }
@@ -33,6 +33,12 @@ public struct AstryxNodeView: View {
33
33
  } marker: {
34
34
  AstryxNodeList(nodes: item.props.marker ?? [], send: send)
35
35
  }
36
+ case .listItem(let row):
37
+ ListItem(row.viewProps(send: send)) {
38
+ AstryxNodeList(nodes: row.props.startContent ?? [], send: send)
39
+ } endContent: {
40
+ AstryxNodeList(nodes: row.props.endContent ?? [], send: send)
41
+ }
36
42
  default: AstryxUnsupportedNode(component: Self.componentName(node))
37
43
  }
38
44
  }
@@ -50,6 +56,7 @@ public struct AstryxNodeView: View {
50
56
  case .hStack: "HStack"
51
57
  case .icon: "Icon"
52
58
  case .item: "Item"
59
+ case .listItem: "ListItem"
53
60
  case .progressBar: "ProgressBar"
54
61
  case .selector: "Selector"
55
62
  case .switch: "Switch"
@@ -17,6 +17,7 @@ public enum AstryxNode: Decodable, Equatable, Sendable {
17
17
  case hStack(HStackNode)
18
18
  case icon(IconNode)
19
19
  case item(ItemNode)
20
+ case listItem(ListItemNode)
20
21
  case progressBar(ProgressBarNode)
21
22
  case selector(SelectorNode)
22
23
  case `switch`(SwitchNode)
@@ -38,6 +39,7 @@ public enum AstryxNode: Decodable, Equatable, Sendable {
38
39
  case .hStack(let node): return node.key
39
40
  case .icon(let node): return node.key
40
41
  case .item(let node): return node.key
42
+ case .listItem(let node): return node.key
41
43
  case .progressBar(let node): return node.key
42
44
  case .selector(let node): return node.key
43
45
  case .`switch`(let node): return node.key
@@ -67,6 +69,7 @@ public enum AstryxNode: Decodable, Equatable, Sendable {
67
69
  case "HStack": self = .hStack(try HStackNode(from: decoder))
68
70
  case "Icon": self = .icon(try IconNode(from: decoder))
69
71
  case "Item": self = .item(try ItemNode(from: decoder))
72
+ case "ListItem": self = .listItem(try ListItemNode(from: decoder))
70
73
  case "ProgressBar": self = .progressBar(try ProgressBarNode(from: decoder))
71
74
  case "Selector": self = .selector(try SelectorNode(from: decoder))
72
75
  case "Switch": self = .`switch`(try SwitchNode(from: decoder))
@@ -0,0 +1,122 @@
1
+ // Generated by @astryxdesign/native. Do not edit.
2
+ public enum ListItemAlign: String, CaseIterable, Codable, Sendable {
3
+ case center = "center"
4
+ case start = "start"
5
+ case firstLine = "first-line"
6
+ }
7
+ #if os(iOS)
8
+ public struct ListItemProps {
9
+ /// Base props attach to this part on every platform.
10
+ public static let baseTarget: AstryxBaseTarget = .root
11
+ public var base: AstryxBaseProps
12
+ public var label: String
13
+ public var description: String?
14
+ public var onClick: (() -> Void)?
15
+ public var href: String?
16
+ public var align: ListItemAlign?
17
+ public var isDisabled: Bool?
18
+ public var isSelected: Bool?
19
+ public init(label: String, description: String? = nil, onClick: (() -> Void)? = nil, href: String? = nil, align: ListItemAlign? = nil, isDisabled: Bool? = nil, isSelected: Bool? = nil, base: AstryxBaseProps = AstryxBaseProps()) {
20
+ self.label = label
21
+ self.description = description
22
+ self.onClick = onClick
23
+ self.href = href
24
+ self.align = align
25
+ self.isDisabled = isDisabled
26
+ self.isSelected = isSelected
27
+ self.base = base
28
+ }
29
+ }
30
+ public struct ListItemNode: Decodable, Equatable, Sendable {
31
+ public struct Props: Decodable, Equatable, Sendable {
32
+ public var label: String
33
+ public var description: String?
34
+ public var startContent: [AstryxNode]?
35
+ public var endContent: [AstryxNode]?
36
+ public var href: String?
37
+ public var align: ListItemAlign?
38
+ public var isDisabled: Bool?
39
+ public var isSelected: Bool?
40
+ }
41
+ public let key: String
42
+ public let base: AstryxBaseProps?
43
+ public let props: Props
44
+ /// The callbacks the core listens to, by prop name.
45
+ public let events: [String]?
46
+
47
+ public func listens(_ event: String) -> Bool { events?.contains(event) ?? false }
48
+
49
+ /// The contract a ListItem view takes; its callbacks send actions to the core.
50
+ public func viewProps(send: @escaping @Sendable (AstryxAction) -> Void) -> ListItemProps {
51
+ let key = self.key
52
+ return ListItemProps(
53
+ label: props.label,
54
+ description: props.description,
55
+ onClick: listens("onClick") ? { send(AstryxAction(key: key, component: "ListItem", event: "onClick", args: [])) } : nil,
56
+ href: props.href,
57
+ align: props.align,
58
+ isDisabled: props.isDisabled,
59
+ isSelected: props.isSelected,
60
+ base: base ?? AstryxBaseProps()
61
+ )
62
+ }
63
+ }
64
+ #endif
65
+ #if os(macOS)
66
+ public struct ListItemProps {
67
+ /// Base props attach to this part on every platform.
68
+ public static let baseTarget: AstryxBaseTarget = .root
69
+ public var base: AstryxBaseProps
70
+ public var label: String
71
+ public var description: String?
72
+ public var onClick: (() -> Void)?
73
+ public var href: String?
74
+ public var align: ListItemAlign?
75
+ public var isDisabled: Bool?
76
+ public var isSelected: Bool?
77
+ public init(label: String, description: String? = nil, onClick: (() -> Void)? = nil, href: String? = nil, align: ListItemAlign? = nil, isDisabled: Bool? = nil, isSelected: Bool? = nil, base: AstryxBaseProps = AstryxBaseProps()) {
78
+ self.label = label
79
+ self.description = description
80
+ self.onClick = onClick
81
+ self.href = href
82
+ self.align = align
83
+ self.isDisabled = isDisabled
84
+ self.isSelected = isSelected
85
+ self.base = base
86
+ }
87
+ }
88
+ public struct ListItemNode: Decodable, Equatable, Sendable {
89
+ public struct Props: Decodable, Equatable, Sendable {
90
+ public var label: String
91
+ public var description: String?
92
+ public var startContent: [AstryxNode]?
93
+ public var endContent: [AstryxNode]?
94
+ public var href: String?
95
+ public var align: ListItemAlign?
96
+ public var isDisabled: Bool?
97
+ public var isSelected: Bool?
98
+ }
99
+ public let key: String
100
+ public let base: AstryxBaseProps?
101
+ public let props: Props
102
+ /// The callbacks the core listens to, by prop name.
103
+ public let events: [String]?
104
+
105
+ public func listens(_ event: String) -> Bool { events?.contains(event) ?? false }
106
+
107
+ /// The contract a ListItem view takes; its callbacks send actions to the core.
108
+ public func viewProps(send: @escaping @Sendable (AstryxAction) -> Void) -> ListItemProps {
109
+ let key = self.key
110
+ return ListItemProps(
111
+ label: props.label,
112
+ description: props.description,
113
+ onClick: listens("onClick") ? { send(AstryxAction(key: key, component: "ListItem", event: "onClick", args: [])) } : nil,
114
+ href: props.href,
115
+ align: props.align,
116
+ isDisabled: props.isDisabled,
117
+ isSelected: props.isSelected,
118
+ base: base ?? AstryxBaseProps()
119
+ )
120
+ }
121
+ }
122
+ #endif
@@ -125,6 +125,20 @@ import Testing
125
125
  #expect(Avatar("Ada").linkURL == nil)
126
126
  }
127
127
 
128
+ // family:navigation-destinations FR4: a script URL or an HTML document in a
129
+ // data URL never makes the avatar a link; it stays a button with onClick, or
130
+ // static, as Swift Button and Item refuse them.
131
+ @MainActor
132
+ @Test func aRefusedDestinationLeavesTheAvatarWithoutALink() {
133
+ for refused in ["javascript:alert(1)", " JavaScript: alert(1)", "vbscript:msgbox", "data:text/html,<p>", "data: text/html,<p>"] {
134
+ #expect(Avatar("Ada", href: refused).linkURL == nil, "\(refused)")
135
+ #expect(Avatar("Ada", href: refused).tap == .none, "\(refused)")
136
+ #expect(Avatar("Ada", href: refused, onClick: {}).tap == .button, "\(refused)")
137
+ }
138
+ // Other schemes still open, as before.
139
+ #expect(Avatar("Ada", href: "data:image/png;base64,AAAA").linkURL != nil)
140
+ }
141
+
128
142
  @MainActor
129
143
  @Test func avatarsReadTheirTapAndTraits() {
130
144
  #expect(Avatar("Ada").tap == .none)