@astryxdesign/native 0.7.6-canary.eae71cb → 0.7.6-canary.eb282ba

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/README.md +13 -0
  2. package/apple/Sources/Astryx/Avatar/Avatar.swift +9 -12
  3. package/apple/Sources/Astryx/Base/SafeDestination.swift +48 -0
  4. package/apple/Sources/Astryx/Button/Button.swift +3 -21
  5. package/apple/Sources/Astryx/Item/Item.swift +446 -0
  6. package/apple/Sources/Astryx/List/ListItem.swift +255 -0
  7. package/apple/Sources/Astryx/Wire/AstryxNodeView.swift +16 -0
  8. package/apple/Sources/AstryxNative/Generated/AstryxNode.swift +6 -0
  9. package/apple/Sources/AstryxNative/Generated/Item.swift +173 -0
  10. package/apple/Sources/AstryxNative/Generated/ListItem.swift +122 -0
  11. package/apple/Tests/AstryxTests/AvatarTests.swift +14 -0
  12. package/apple/Tests/AstryxTests/ItemTests.swift +295 -0
  13. package/apple/Tests/AstryxTests/ListItemTests.swift +234 -0
  14. package/dist/components/Item.d.ts +10 -0
  15. package/dist/components/Item.d.ts.map +1 -0
  16. package/dist/components/Item.js +22 -0
  17. package/dist/components/ListItem.d.ts +10 -0
  18. package/dist/components/ListItem.d.ts.map +1 -0
  19. package/dist/components/ListItem.js +20 -0
  20. package/dist/components/index.d.ts.map +1 -1
  21. package/dist/components/index.js +4 -0
  22. package/dist/types/android.d.ts +29 -3
  23. package/dist/types/core.d.ts +30 -0
  24. package/dist/types/ios.d.ts +29 -3
  25. package/dist/types/macos.d.ts +29 -3
  26. package/dist/types/shared.d.ts +29 -3
  27. package/dist/types/windows.d.ts +29 -3
  28. package/package.json +3 -3
  29. package/src/components/Item.ts +34 -0
  30. package/src/components/ListItem.ts +32 -0
  31. package/src/components/index.ts +4 -0
  32. package/src/contract/contracts.test.ts +60 -0
  33. package/src/theme/swiftIOSTheme.test.ts +372 -38
@@ -0,0 +1,295 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ import AstryxNative
4
+ import SwiftUI
5
+ import Testing
6
+ @testable import Astryx
7
+
8
+ // Swift Item spec (component:swift/Item): core's row, its slots in reading
9
+ // order, density, alignment, and states, drawn the same static, as a button,
10
+ // or as a link.
11
+
12
+ @MainActor
13
+ @Test func trailingClosuresFillTheSlotsTheyName() {
14
+ let start = Item("Ada") { Color.red }
15
+ let both = Item("Ada") { Color.red } endContent: { Color.blue }
16
+ let end = Item("Ada", endContent: { Color.blue })
17
+ let pressable = Item("Settings", onClick: {})
18
+ let trailingPress = Item("Archive") { Color.red } onClick: {}
19
+ #expect(type(of: start) == Item<EmptyView, Color, EmptyView>.self)
20
+ #expect(type(of: both) == Item<EmptyView, Color, Color>.self)
21
+ #expect(type(of: end) == Item<EmptyView, EmptyView, Color>.self)
22
+ #expect(type(of: pressable) == Item<EmptyView, EmptyView, EmptyView>.self)
23
+ #expect(pressable.props.onClick != nil)
24
+ #expect(type(of: trailingPress) == Item<EmptyView, Color, EmptyView>.self)
25
+ #expect(trailingPress.props.onClick != nil)
26
+ }
27
+
28
+ @MainActor
29
+ @Test func itemInitializersBuildTheSameProps() {
30
+ let labeled = Item(
31
+ "Ada", description: "Mathematician", align: .firstLine, density: .spacious, labelLines: 2,
32
+ descriptionLines: 3, layout: .inline, href: "https://example.com", isHighlighted: true, isSelected: true,
33
+ isDisabled: true
34
+ ).props
35
+ let fromProps = Item(
36
+ ItemProps(
37
+ label: "Ada", description: "Mathematician", align: .firstLine, density: .spacious, labelLines: 2,
38
+ descriptionLines: 3, layout: .inline, href: "https://example.com", isHighlighted: true, isSelected: true,
39
+ isDisabled: true)
40
+ ).props
41
+ #expect(labeled.label == fromProps.label)
42
+ #expect(labeled.description == fromProps.description)
43
+ #expect(labeled.align == fromProps.align)
44
+ #expect(labeled.density == fromProps.density)
45
+ #expect(labeled.labelLines == fromProps.labelLines)
46
+ #expect(labeled.descriptionLines == fromProps.descriptionLines)
47
+ #expect(labeled.layout == fromProps.layout)
48
+ #expect(labeled.href == fromProps.href)
49
+ #expect(labeled.isHighlighted == fromProps.isHighlighted)
50
+ #expect(labeled.isSelected == fromProps.isSelected)
51
+ #expect(labeled.isDisabled == fromProps.isDisabled)
52
+ #expect(labeled.base == fromProps.base)
53
+ }
54
+
55
+ @MainActor
56
+ @Test func densityAddsCoresBlockPadding() {
57
+ let tokens = Theme.default.light.space
58
+ let compact = Pixels(Item("Ada", density: .compact).frame(width: 240))
59
+ let balanced = Pixels(Item("Ada").frame(width: 240))
60
+ let spacious = Pixels(Item("Ada", density: .spacious).frame(width: 240))
61
+ #expect(balanced.height - compact.height == Int(2 * (tokens.s2 - tokens.s1)))
62
+ #expect(spacious.height - balanced.height == Int(2 * (tokens.s3 - tokens.s2)))
63
+ }
64
+
65
+ @MainActor
66
+ @Test func aTappableRowIsAtLeast44PointsTallAndAStaticOneKeepsCoresHeight() {
67
+ let still = Pixels(Item("Ada", density: .compact).frame(width: 240))
68
+ let button = Pixels(Item("Ada", density: .compact, onClick: {}).frame(width: 240))
69
+ let link = Pixels(Item("Ada", density: .compact, href: "https://example.com").frame(width: 240))
70
+ #expect(still.height < 44)
71
+ #expect(button.height == 44)
72
+ #expect(link.height == 44)
73
+ }
74
+
75
+ @MainActor
76
+ @Test func selectedWinsOverHighlightedAndBothUseTheTheme() {
77
+ let accentMuted = rgba(Theme.default.light.color.accentMuted)
78
+ let overlay = rgba(Theme.default.light.color.overlayHover)
79
+ let selected = Pixels(Item("Ada", isHighlighted: true, isSelected: true).frame(width: 240).background(Color.white))
80
+ let highlighted = Pixels(Item("Ada", isHighlighted: true).frame(width: 240).background(Color.white))
81
+ let plain = Pixels(Item("Ada").frame(width: 240).background(Color.white))
82
+ let x = 230, y = 10
83
+ #expect(close(selected.at(x, y), onWhite(accentMuted), within: 3))
84
+ #expect(close(highlighted.at(x, y), onWhite(overlay), within: 3))
85
+ #expect(close(plain.at(x, y), [255, 255, 255, 255], within: 1))
86
+ }
87
+
88
+ @MainActor
89
+ @Test func anItemRendersFromTwoThemesWithNoColorOfItsOwn() {
90
+ let view = Item("Ada", isSelected: true).frame(width: 240).background(Color.white)
91
+ let neutral = Pixels(view, theme: .neutral).at(230, 10)
92
+ let butter = Pixels(view, theme: .butter).at(230, 10)
93
+ #expect(close(neutral, onWhite(rgba(Theme.neutral.light.color.accentMuted)), within: 3))
94
+ #expect(close(butter, onWhite(rgba(Theme.butter.light.color.accentMuted)), within: 3))
95
+ }
96
+
97
+ @MainActor
98
+ @Test func aDisabledRowDimsItsWordsAndEndContentNotItsStartContent() {
99
+ let red = [255, 0, 0, 255]
100
+ let swatch = { Color(red: 1, green: 0, blue: 0).frame(width: 12, height: 12) }
101
+ let start = Pixels(Item("Ada", isDisabled: true, startContent: swatch).frame(width: 240))
102
+ let end = Pixels(Item("Ada", isDisabled: true, endContent: swatch).frame(width: 240))
103
+ let enabledEnd = Pixels(Item("Ada", endContent: swatch).frame(width: 240))
104
+ #expect(start.contains { close($0, red) })
105
+ #expect(!end.contains { close($0, red) })
106
+ #expect(enabledEnd.contains { close($0, red) })
107
+ }
108
+
109
+ @MainActor
110
+ @Test func alignPlacesTheSlotsOnTheWords() {
111
+ let tokens = Theme.default.light
112
+ let marker = { Color(red: 1, green: 0, blue: 0).frame(width: 8, height: 8) }
113
+ let start = Pixels(Item("Ada", description: "Mathematician", align: .start, startContent: marker).frame(width: 240))
114
+ let centered = Pixels(Item("Ada", description: "Mathematician", startContent: marker).frame(width: 240))
115
+ let red = [255, 0, 0, 255]
116
+ let x = Int(tokens.space.s2) + 4
117
+ // `start`: the slot's top is on the row's top padding.
118
+ #expect(close(start.at(x, Int(tokens.space.s2) + 1), red))
119
+ #expect(!close(start.at(x, start.height / 2 + 6), red))
120
+ // `center`: the slot sits in the middle of the row.
121
+ #expect(close(centered.at(x, centered.height / 2), red))
122
+ }
123
+
124
+ @MainActor
125
+ @Test func aLineTakesTheThemesLeading() {
126
+ let tokens = Theme.default.light
127
+ let row = Pixels(Item("Ada", density: .compact).frame(width: 240))
128
+ #expect(abs(row.height - Int((2 * tokens.space.s1 + tokens.font.body.leading * tokens.font.body.size).rounded())) <= 1)
129
+ }
130
+
131
+ @MainActor
132
+ @Test func firstLineCentersATallSlotOnTheLabelsFirstLine() {
133
+ let red = [255, 0, 0, 255]
134
+ let tall = { Color(red: 1, green: 0, blue: 0).frame(width: 10, height: 40) }
135
+ func slotCenter(_ p: Pixels) -> Double {
136
+ let rows = (0..<p.height).filter { y in close(p.at(14, y), red) }
137
+ return Double(rows.first! + rows.last!) / 2
138
+ }
139
+ /// The middle of the label's ink: the first run of drawn rows in the words.
140
+ func labelCenter(_ p: Pixels) -> Double {
141
+ let ink = (0..<p.height).map { y in (30..<120).contains { x in p.at(x, y)[3] > 128 } }
142
+ let top = ink.firstIndex(of: true)!
143
+ var bottom = top
144
+ while bottom + 1 < ink.count && ink[bottom + 1] { bottom += 1 }
145
+ return Double(top + bottom) / 2
146
+ }
147
+ let first = Pixels(Item("Ada", description: "Mathematician", align: .firstLine, startContent: tall).frame(width: 240))
148
+ let center = Pixels(Item("Ada", description: "Mathematician", startContent: tall).frame(width: 240))
149
+ #expect(abs(slotCenter(first) - labelCenter(first)) <= 1.5)
150
+ // `center` puts the same slot on the middle of the words instead.
151
+ #expect(abs(slotCenter(center) - labelCenter(center)) > 4)
152
+ }
153
+
154
+ @MainActor
155
+ @Test func rowsReadTheirTapAndState() {
156
+ #expect(Item("Ada").tap == .none)
157
+ #expect(Item("Ada", onClick: {}).tap == .button)
158
+ #expect(Item("Ada", href: "https://example.com", onClick: {}).tap == .link)
159
+ #expect(Item<EmptyView, EmptyView, EmptyView>.addedTraits(isSelected: true) == .isSelected)
160
+ #expect(Item<EmptyView, EmptyView, EmptyView>.addedTraits(isSelected: false) == [])
161
+ }
162
+
163
+ @MainActor
164
+ @Test func rowsGrowWithTheLargestTextSize() {
165
+ let large = Pixels(Item("Ada", description: "Mathematician").frame(width: 320))
166
+ let largest = Pixels(Item("Ada", description: "Mathematician").frame(width: 320), typeSize: .accessibility5)
167
+ #expect(largest.height > large.height * 2)
168
+ let tappable = Pixels(Item("Ada", onClick: {}).frame(width: 320), typeSize: .accessibility5)
169
+ #expect(tappable.height > 44)
170
+ }
171
+
172
+ @MainActor
173
+ @Test func aWebPathIsAnItemHrefTheAppCannotOpen() {
174
+ #expect(Item<EmptyView, EmptyView, EmptyView>.isUnusableHref("/calendar"))
175
+ #expect(!Item<EmptyView, EmptyView, EmptyView>.isUnusableHref("https://example.com/calendar"))
176
+ #expect(!Item<EmptyView, EmptyView, EmptyView>.isUnusableHref(nil))
177
+ // The check reads the href as the link does: a padded absolute URL opens
178
+ // as a link and is not flagged.
179
+ #expect(!Item<EmptyView, EmptyView, EmptyView>.isUnusableHref(" https://example.com "))
180
+ #expect(Item("Ada", href: " https://example.com ").linkURL == URL(string: "https://example.com"))
181
+ }
182
+
183
+ @MainActor
184
+ @Test func anInlineLabelWithoutADescriptionStillTruncates() {
185
+ let words = "A label long enough to need more than the row's width at one line"
186
+ let stacked = Pixels(Item(words).frame(width: 160))
187
+ let inline = Pixels(Item(words, layout: .inline).frame(width: 160))
188
+ #expect(inline.width == 160)
189
+ #expect(inline.height == stacked.height)
190
+ // The end content stays inside the row instead of being pushed past it.
191
+ let withEnd = Pixels(Item(words, layout: .inline, endContent: { Color(red: 1, green: 0, blue: 0).frame(width: 10, height: 10) }).frame(width: 160))
192
+ #expect(withEnd.contains { close($0, [255, 0, 0, 255]) })
193
+ }
194
+
195
+ @MainActor
196
+ @Test func aPressShowsAboveASelectedFill() {
197
+ let tokens = Theme.neutral.light.color
198
+ let rest = Pixels(ItemPressBackground(radius: 0, fill: tokens.accentMuted, pressed: tokens.overlayPressed, isPressed: false).frame(width: 8, height: 8))
199
+ let pressed = Pixels(ItemPressBackground(radius: 0, fill: tokens.accentMuted, pressed: tokens.overlayPressed, isPressed: true).frame(width: 8, height: 8))
200
+ #expect(!close(rest.at(4, 4), pressed.at(4, 4), within: 3))
201
+ }
202
+
203
+ @Test func lineCountsComeFromTheContract() {
204
+ // Unset is one line; otherwise core's shared count: zero, a fraction, or a
205
+ // count too large to reach shows every line, as core's unclamped row does.
206
+ #expect(Item<EmptyView, EmptyView, EmptyView>.lines(nil) == 1)
207
+ #expect(Item<EmptyView, EmptyView, EmptyView>.lines(0) == nil)
208
+ #expect(Item<EmptyView, EmptyView, EmptyView>.lines(3) == 3)
209
+ #expect(Item<EmptyView, EmptyView, EmptyView>.lines(.infinity) == nil)
210
+ #expect(Item<EmptyView, EmptyView, EmptyView>.lines(1e300) == nil)
211
+ }
212
+
213
+ @MainActor
214
+ @Test func aLongLabelTruncatesToItsLines() {
215
+ let words = "A label long enough to wrap onto more than one line in a narrow row"
216
+ let one = Pixels(Item(words).frame(width: 160))
217
+ let three = Pixels(Item(words, labelLines: 3).frame(width: 160))
218
+ #expect(three.height > one.height)
219
+ }
220
+
221
+ @MainActor
222
+ @Test func hrefMakesALinkAndStatesReadFromTheContract() {
223
+ #expect(Item("Ada", href: "https://example.com/ada").linkURL == URL(string: "https://example.com/ada"))
224
+ #expect(Item("Ada", href: "").linkURL == nil)
225
+ #expect(Item("Ada", href: "not a url").linkURL == nil)
226
+ #expect(Item("Ada", href: "/calendar").linkURL == nil)
227
+ // A script URL or an HTML document in a data URL stays inert, as core's
228
+ // navigation destinations refuse them; the row is static or a button.
229
+ for refused in ["javascript:alert(1)", " JavaScript: alert(1)", "vbscript:msgbox", "data:text/html,<p>", "data: text/html,<p>"] {
230
+ #expect(Item("Ada", href: refused).linkURL == nil, "\(refused)")
231
+ #expect(Item("Ada", href: refused).tap == .none, "\(refused)")
232
+ }
233
+ #expect(Item("Ada", href: "javascript:alert(1)", onClick: {}).tap == .button)
234
+ #expect(Item("Ada", isSelected: true).isSelected)
235
+ #expect(!Item("Ada").isSelected)
236
+ #expect(Item("Ada", isDisabled: true).isDisabled)
237
+ }
238
+
239
+ @MainActor
240
+ @Test func aHiddenItemDrawsNothing() {
241
+ let hidden = Pixels(
242
+ HStack(spacing: 0) {
243
+ Color.clear.frame(width: 1, height: 1)
244
+ Item(ItemProps(label: "Ada", base: AstryxBaseProps(isHidden: true)))
245
+ }.fixedSize()
246
+ )
247
+ #expect(hidden.width == 1)
248
+ }
249
+
250
+ /// A theme color drawn over white.
251
+ private func onWhite(_ c: [Int]) -> [Int] {
252
+ (0..<3).map { (c[$0] * c[3] + 255 * (255 - c[3])) / 255 } + [255]
253
+ }
254
+
255
+ // AST-082 FR19: an Item node from a logic core draws as Item.
256
+ @MainActor
257
+ @Test func anItemNodeDrawsAsAnItem() {
258
+ let json = #"{"component":"Item","key":"i","props":{"label":"Ada Lovelace"}}"#
259
+ guard let node = try? JSONDecoder().decode(AstryxNode.self, from: Data(json.utf8)) else {
260
+ Issue.record("the item did not decode")
261
+ return
262
+ }
263
+ #expect(AstryxNodeView.componentName(node) == "Item")
264
+ let wired = Pixels(AstryxNodeView(node) { _ in }.frame(width: 200))
265
+ let direct = Pixels(Item("Ada Lovelace").frame(width: 200))
266
+ #expect(wired.height == direct.height)
267
+ }
268
+
269
+ // AST-082 FR19: an Item node's content slots draw their nodes, as a direct
270
+ // Item draws its slots.
271
+ @MainActor
272
+ @Test func anItemNodeDrawsItsContentSlots() {
273
+ let json = """
274
+ {"component":"Item","key":"i","props":{"label":"Ada Lovelace",
275
+ "startContent":[{"component":"Avatar","key":"a","props":{"name":"Ada Lovelace","size":"sm"}}],
276
+ "endContent":[{"component":"Text","key":"t","props":{"children":"Admin"}}]}}
277
+ """
278
+ guard let node = try? JSONDecoder().decode(AstryxNode.self, from: Data(json.utf8)) else {
279
+ Issue.record("the item did not decode")
280
+ return
281
+ }
282
+ let wired = Pixels(AstryxNodeView(node) { _ in }.frame(width: 240))
283
+ let direct = Pixels(
284
+ Item("Ada Lovelace") { Avatar("Ada Lovelace", size: .sm) } endContent: { Astryx.Text("Admin") }.frame(width: 240))
285
+ let bare = Pixels(Item("Ada Lovelace").frame(width: 240))
286
+ #expect(wired.width == direct.width && wired.height == direct.height)
287
+ // The slots draw: the wired row is not the bare row. The leading 40
288
+ // points hold the avatar, which a bare row leaves empty.
289
+ var leadingDiffers = false
290
+ for y in 0..<min(wired.height, bare.height) {
291
+ for x in 0..<40 where wired.at(x, y) != bare.at(x, y) { leadingDiffers = true }
292
+ }
293
+ #expect(leadingDiffers)
294
+ }
295
+
@@ -0,0 +1,234 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ import AstryxNative
4
+ import SwiftUI
5
+ import Testing
6
+ @testable import Astryx
7
+
8
+ // Swift ListItem spec (component:swift/ListItem): an Item that takes its
9
+ // density, marker, dividers, and edge from its list.
10
+
11
+ private let red = [255, 0, 0, 255]
12
+ @MainActor private func swatch() -> some View { Color(red: 1, green: 0, blue: 0).frame(width: 10, height: 10) }
13
+
14
+ @MainActor
15
+ private func inList<V: View>(_ view: V, _ context: ListContext) -> some View {
16
+ view.environment(\.astryxListContext, context).frame(width: 240)
17
+ }
18
+
19
+ @MainActor
20
+ @Test func listItemInitializersBuildTheSameProps() {
21
+ let labeled = ListItem("Ada", description: "Mathematician", align: .start, href: "https://example.com", isDisabled: true, isSelected: true).props
22
+ let fromProps = ListItem(ListItemProps(label: "Ada", description: "Mathematician", href: "https://example.com", align: .start, isDisabled: true, isSelected: true)).props
23
+ #expect(labeled.label == fromProps.label)
24
+ #expect(labeled.description == fromProps.description)
25
+ #expect(labeled.align == fromProps.align)
26
+ #expect(labeled.href == fromProps.href)
27
+ #expect(labeled.isDisabled == fromProps.isDisabled)
28
+ #expect(labeled.isSelected == fromProps.isSelected)
29
+ #expect(labeled.base == fromProps.base)
30
+ }
31
+
32
+ @MainActor
33
+ @Test func aListItemAloneDrawsAsAnItem() {
34
+ let row = Pixels(ListItem("Ada", description: "Mathematician").frame(width: 240))
35
+ let item = Pixels(Item("Ada", description: "Mathematician").frame(width: 240))
36
+ #expect(row.width == item.width && row.height == item.height)
37
+ }
38
+
39
+ @MainActor
40
+ @Test func aListItemTakesItsListsDensity() {
41
+ let compact = Pixels(inList(ListItem("Ada"), ListContext(density: .compact, hasDividers: false, isFlush: false)))
42
+ let spacious = Pixels(inList(ListItem("Ada"), ListContext(density: .spacious, hasDividers: false, isFlush: false)))
43
+ let tokens = Theme.default.light.space
44
+ #expect(spacious.height - compact.height == Int(2 * (tokens.s3 - tokens.s1)))
45
+ }
46
+
47
+ @MainActor
48
+ @Test func aFlushListDropsTheRowsInlineInset() {
49
+ let padded = Pixels(inList(ListItem("Ada", startContent: { swatch() }), ListContext(hasDividers: false, isFlush: false)))
50
+ let flush = Pixels(inList(ListItem("Ada", startContent: { swatch() }), ListContext(hasDividers: false, isFlush: true)))
51
+ #expect(!close(padded.at(1, padded.height / 2), red))
52
+ #expect(close(flush.at(1, flush.height / 2), red))
53
+ }
54
+
55
+ @MainActor
56
+ @Test func rowsBetweenDividersHaveSquareCorners() {
57
+ let accent = Theme.default.light.color.accentMuted
58
+ let rounded = Pixels(inList(ListItem("Ada", isSelected: true), ListContext(hasDividers: false, isFlush: false)))
59
+ let square = Pixels(inList(ListItem("Ada", isSelected: true), ListContext(hasDividers: true, isFlush: false)))
60
+ #expect(rounded.at(0, 0)[3] < 8)
61
+ #expect(close(square.at(0, 0), plate(accent), within: 6))
62
+ }
63
+
64
+ @MainActor
65
+ @Test func aDecimalListNumbersItsRows() {
66
+ #expect(ListItem<EmptyView, EmptyView>.number(ordinal: 3) == "3.")
67
+ let numbers = ListNumbers()
68
+ let first = UUID(), second = UUID()
69
+ numbers.assign([first, second], start: 3)
70
+ #expect(numbers.number(for: first) == 3 && numbers.number(for: second) == 4)
71
+ let numbered = Pixels(inList(ListItem("MMMMMM"), ListContext(hasDividers: false, isFlush: false, marker: .decimal)))
72
+ let plain = Pixels(inList(ListItem("MMMMMM"), ListContext(hasDividers: false, isFlush: false)))
73
+ // The number takes a marker column, so a run of blank columns opens
74
+ // between it and the words; without a marker the words start at once.
75
+ func longestBlankRun(_ p: Pixels) -> Int {
76
+ var longest = 0, run = 0
77
+ for x in 8..<80 {
78
+ let blank = (0..<p.height).allSatisfy { y in p.at(x, y)[3] < 64 }
79
+ run = blank ? run + 1 : 0
80
+ longest = max(longest, run)
81
+ }
82
+ return longest
83
+ }
84
+ #expect(longestBlankRun(numbered) >= 5)
85
+ #expect(longestBlankRun(plain) < 5)
86
+ }
87
+
88
+ @MainActor
89
+ @Test func aMarkerSitsOnTheLabelsFirstLineWhateverTheAlignment() {
90
+ func dotCenter(_ p: Pixels) -> Double {
91
+ let rows = (0..<p.height).filter { y in (8..<24).contains { x in p.at(x, y)[3] > 200 } }
92
+ return Double(rows.first! + rows.last!) / 2
93
+ }
94
+ func labelCenter(_ p: Pixels) -> Double {
95
+ let ink = (0..<p.height).map { y in (40..<140).contains { x in p.at(x, y)[3] > 128 } }
96
+ let top = ink.firstIndex(of: true)!
97
+ var bottom = top
98
+ while bottom + 1 < ink.count && ink[bottom + 1] { bottom += 1 }
99
+ return Double(top + bottom) / 2
100
+ }
101
+ let context = ListContext(hasDividers: false, isFlush: false, marker: .disc)
102
+ for align in [ListItemAlign.center, .start, .firstLine] {
103
+ let row = Pixels(inList(ListItem("Ada", description: "Mathematician", align: align), context))
104
+ #expect(abs(dotCenter(row) - labelCenter(row)) <= 1.5, "\(align)")
105
+ }
106
+ }
107
+
108
+ /// A translucent theme color as the renderer stores it, premultiplied.
109
+ private func plate(_ color: Color) -> [Int] {
110
+ let c = rgba(color)
111
+ return [c[0] * c[3] / 255, c[1] * c[3] / 255, c[2] * c[3] / 255, c[3]]
112
+ }
113
+
114
+ @MainActor
115
+ @Test func aWideNumberKeepsTheListsTextEdge() {
116
+ // The column is space.s4 wide while a number fits it and the row's inset,
117
+ // so labels line up; "10." reaches into a balanced row's 8-point inset.
118
+ let s4 = Int(Theme.default.light.space.s4)
119
+ #expect(Pixels(ListMarkerNumber(text: "1.", inset: 8)).width == s4)
120
+ #expect(Pixels(ListMarkerNumber(text: "10.", inset: 8)).width == s4)
121
+ }
122
+
123
+ @MainActor
124
+ @Test func numbersEndAtTheColumnsEndSoPeriodsLineUp() {
125
+ // Core's numbered markers end where the marker ends, with tabular figures,
126
+ // so "1." and "10." put their periods at the same place.
127
+ func rightmostInk(_ p: Pixels) -> Int {
128
+ (0..<p.width).last { x in (0..<p.height).contains { y in p.at(x, y)[3] > 64 } } ?? -1
129
+ }
130
+ let one = Pixels(ListMarkerNumber(text: "1.", inset: 8))
131
+ let ten = Pixels(ListMarkerNumber(text: "10.", inset: 8))
132
+ #expect(rightmostInk(one) >= 0)
133
+ #expect(abs(rightmostInk(one) - rightmostInk(ten)) <= 1)
134
+ // The number sits at the column's end, not its start.
135
+ #expect(rightmostInk(one) >= one.width - 3)
136
+ }
137
+
138
+ @Test func aNumberWiderThanTheColumnAndInsetWidensTheSharedColumn() {
139
+ typealias Row = ListItem<EmptyView, EmptyView>
140
+ #expect(Row.numberColumn(base: 16, widest: 20, inset: 8) == 16)
141
+ #expect(Row.numberColumn(base: 16, widest: 40, inset: 8) == 32)
142
+ #expect(Row.numberColumn(base: 16, widest: 30, inset: 0) == 30)
143
+ }
144
+
145
+ @MainActor
146
+ @Test func aListSharesItsWidestNumber() {
147
+ let numbers = ListNumbers()
148
+ let first = UUID(), second = UUID()
149
+ numbers.assign([first, second], start: 1)
150
+ numbers.report(first, width: 20)
151
+ numbers.report(second, width: 30)
152
+ #expect(numbers.widest == 30)
153
+ // A row that leaves the list no longer widens it.
154
+ numbers.assign([first], start: 1)
155
+ #expect(numbers.widest == 20)
156
+ }
157
+
158
+ @Test func onlyANumberMarkerIsRead() {
159
+ #expect(ListItem<EmptyView, EmptyView>.markerIsRead(.decimal))
160
+ #expect(!ListItem<EmptyView, EmptyView>.markerIsRead(.disc))
161
+ #expect(!ListItem<EmptyView, EmptyView>.markerIsRead(.circle))
162
+ }
163
+
164
+ @MainActor
165
+ @Test func aMarkerStaysOnTheFirstLineAtTheLargestTextSize() {
166
+ let context = ListContext(hasDividers: false, isFlush: false, marker: .disc)
167
+ let large = Pixels(inList(ListItem("Ada", description: "Mathematician"), context))
168
+ let largest = Pixels(inList(ListItem("Ada", description: "Mathematician"), context), typeSize: .accessibility5)
169
+ #expect(largest.height > large.height)
170
+ // The dot sits in the top part of the row, by the first line, not its middle.
171
+ // The marker column grows with text, so look across it.
172
+ let rows = (0..<largest.height).filter { y in (8..<60).contains { x in largest.at(x, y)[3] > 200 } }
173
+ #expect(!rows.isEmpty && rows.last! < largest.height / 2)
174
+ }
175
+
176
+ @MainActor
177
+ @Test func markersDrawInEachThemesPrimaryText() {
178
+ for theme in [Theme.neutral, Theme.butter] {
179
+ let drawn = Pixels(inList(ListItem("Ada"), ListContext(hasDividers: false, isFlush: false, marker: .disc)), theme: theme)
180
+ let ink = (0..<drawn.height).flatMap { y in (8..<24).map { x in drawn.at(x, y) } }.filter { $0[3] == 255 }
181
+ #expect(ink.contains { close($0, rgba(theme.light.color.textPrimary), within: 8) })
182
+ }
183
+ }
184
+
185
+ @MainActor
186
+ @Test func aRowNestedInASlotTakesNoListLook() {
187
+ // A nested row has no marker column, so its words start at its own inset.
188
+ let context = ListContext(hasDividers: false, isFlush: true, marker: .disc)
189
+ let nested = Pixels(inList(ListItem("Outer", endContent: { ListItem("MMMM").frame(width: 100) }), context))
190
+ let alone = Pixels(ListItem("MMMM").frame(width: 100))
191
+ let start = { (p: Pixels, from: Int) in (from..<p.width).first { x in (0..<p.height).contains { y in p.at(x, y)[3] > 128 } } ?? -1 }
192
+ #expect(start(nested, 140) - 140 == start(alone, 0))
193
+ }
194
+
195
+ @MainActor
196
+ @Test func aNumberFitsItsColumnAtTheLargestTextSize() {
197
+ // The column grows with the number, so a one-digit number never runs into
198
+ // its label at the largest text size.
199
+ let column = Pixels(ListMarkerNumber(text: "9."), typeSize: .accessibility5).width
200
+ let drawn = Pixels(
201
+ ZStack(alignment: .leading) {
202
+ Color.clear.frame(width: 200, height: 80)
203
+ ListMarkerNumber(text: "9.")
204
+ },
205
+ typeSize: .accessibility5)
206
+ let rightmostInk = (0..<200).last { x in (0..<drawn.height).contains { y in drawn.at(x, y)[3] > 64 } } ?? 0
207
+ #expect(column > Int(Theme.default.light.space.s4))
208
+ #expect(rightmostInk < column)
209
+ }
210
+
211
+ // AST-082 FR19: a ListItem node draws as a ListItem, with its content slots.
212
+ @MainActor
213
+ @Test func aListItemNodeDrawsItsContentSlots() {
214
+ let json = """
215
+ {"component":"ListItem","key":"r","props":{"label":"Ada Lovelace",
216
+ "startContent":[{"component":"Avatar","key":"a","props":{"name":"Ada Lovelace","size":"sm"}}]}}
217
+ """
218
+ guard let node = try? JSONDecoder().decode(AstryxNode.self, from: Data(json.utf8)) else {
219
+ Issue.record("the row did not decode")
220
+ return
221
+ }
222
+ #expect(AstryxNodeView.componentName(node) == "ListItem")
223
+ let wired = Pixels(AstryxNodeView(node) { _ in }.frame(width: 240))
224
+ let direct = Pixels(ListItem("Ada Lovelace") { Avatar("Ada Lovelace", size: .sm) }.frame(width: 240))
225
+ let bare = Pixels(ListItem("Ada Lovelace").frame(width: 240))
226
+ #expect(wired.width == direct.width && wired.height == direct.height)
227
+ // The slot draws: the leading 40 points hold the avatar, which a bare row
228
+ // leaves empty.
229
+ var leadingDiffers = false
230
+ for y in 0..<min(wired.height, bare.height) {
231
+ for x in 0..<40 where wired.at(x, y) != bare.at(x, y) { leadingDiffers = true }
232
+ }
233
+ #expect(leadingDiffers)
234
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @file components/Item.ts
3
+ * @input None
4
+ * @output What @astryxdesign/native adds to the core Item contract
5
+ * @position Native additions read by fromComponentDoc together with core's
6
+ * Item documentation
7
+ */
8
+ import type { NativeAdditions } from '../contract/fromComponentDoc.js';
9
+ export declare const item: NativeAdditions;
10
+ //# sourceMappingURL=Item.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Item.d.ts","sourceRoot":"","sources":["../../src/components/Item.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,iCAAiC,CAAC;AAErE,eAAO,MAAM,IAAI,EAAE,eAqBlB,CAAC"}
@@ -0,0 +1,22 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ export const item = {
3
+ name: 'Item',
4
+ doc: { file: 'Item/Item', component: 'Item' },
5
+ baseTarget: 'root',
6
+ slots: {
7
+ label: 'text',
8
+ description: 'text',
9
+ marker: 'content',
10
+ startContent: 'content',
11
+ endContent: 'content',
12
+ },
13
+ callbacks: ['onClick'],
14
+ sameMeaning: { onClick: 'a press, as in Compose' },
15
+ webOnly: {
16
+ target: 'HTML link target',
17
+ rel: 'HTML link relationship',
18
+ 'data-testid': 'web test selector; native platforms use the base id',
19
+ swipeActions: "a native platform's swipe actions work only inside its own scrolling list, so a row elsewhere could not honor them",
20
+ swipeBehavior: 'applies only with swipeActions',
21
+ },
22
+ };
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @file components/ListItem.ts
3
+ * @input None
4
+ * @output What @astryxdesign/native adds to the core ListItem contract
5
+ * @position Native additions read by fromComponentDoc together with core's
6
+ * ListItem documentation
7
+ */
8
+ import type { NativeAdditions } from '../contract/fromComponentDoc.js';
9
+ export declare const listItem: NativeAdditions;
10
+ //# sourceMappingURL=ListItem.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ListItem.d.ts","sourceRoot":"","sources":["../../src/components/ListItem.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,iCAAiC,CAAC;AAErE,eAAO,MAAM,QAAQ,EAAE,eAmBtB,CAAC"}
@@ -0,0 +1,20 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ export const listItem = {
3
+ name: 'ListItem',
4
+ doc: { file: 'List/ListItem' },
5
+ baseTarget: 'root',
6
+ slots: {
7
+ label: 'text',
8
+ description: 'text',
9
+ startContent: 'content',
10
+ endContent: 'content',
11
+ },
12
+ callbacks: ['onClick'],
13
+ sameMeaning: { onClick: 'a press, as in Compose' },
14
+ webOnly: {
15
+ target: 'HTML link target',
16
+ rel: 'HTML link relationship',
17
+ swipeActions: "a native platform's swipe actions work only inside its own scrolling list, so a row elsewhere could not honor them",
18
+ swipeBehavior: 'applies only with swipeActions',
19
+ },
20
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/components/index.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,iCAAiC,CAAC;AAkBrE,eAAO,MAAM,iBAAiB,EAAE,SAAS,eAAe,EAiBvD,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/components/index.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,iCAAiC,CAAC;AAoBrE,eAAO,MAAM,iBAAiB,EAAE,SAAS,eAAe,EAmBvD,CAAC"}
@@ -8,6 +8,8 @@ import { divider } from './Divider.js';
8
8
  import { heading } from './Heading.js';
9
9
  import { hStack } from './HStack.js';
10
10
  import { icon } from './Icon.js';
11
+ import { item } from './Item.js';
12
+ import { listItem } from './ListItem.js';
11
13
  import { progressBar } from './ProgressBar.js';
12
14
  import { selector } from './Selector.js';
13
15
  import { switchControl } from './Switch.js';
@@ -25,6 +27,8 @@ export const NATIVE_COMPONENTS = [
25
27
  heading,
26
28
  hStack,
27
29
  icon,
30
+ item,
31
+ listItem,
28
32
  progressBar,
29
33
  selector,
30
34
  switchControl,