@agent-native/design-system 0.0.0-stage → 0.229.0

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 (222) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +78 -2
  3. package/THIRD_PARTY_NOTICES.md +58 -0
  4. package/css/appearances.css +91 -0
  5. package/css/main.css +174 -0
  6. package/css/parts/accordion.css +104 -0
  7. package/css/parts/alert-dialog.css +159 -0
  8. package/css/parts/app-shell.css +45 -0
  9. package/css/parts/aspect-ratio.css +20 -0
  10. package/css/parts/autocomplete.css +366 -0
  11. package/css/parts/avatar.css +108 -0
  12. package/css/parts/badge.css +100 -0
  13. package/css/parts/blockquote.css +23 -0
  14. package/css/parts/button.css +238 -0
  15. package/css/parts/callout.css +108 -0
  16. package/css/parts/card.css +144 -0
  17. package/css/parts/checkbox-cards.css +59 -0
  18. package/css/parts/checkbox-group.css +20 -0
  19. package/css/parts/checkbox.css +127 -0
  20. package/css/parts/code.css +194 -0
  21. package/css/parts/collapsible.css +40 -0
  22. package/css/parts/combobox.css +171 -0
  23. package/css/parts/context-menu.css +18 -0
  24. package/css/parts/data-list.css +130 -0
  25. package/css/parts/dialog.css +251 -0
  26. package/css/parts/drawer.css +289 -0
  27. package/css/parts/em.css +16 -0
  28. package/css/parts/field.css +55 -0
  29. package/css/parts/fieldset.css +34 -0
  30. package/css/parts/form.css +18 -0
  31. package/css/parts/heading.css +25 -0
  32. package/css/parts/icon-button.css +30 -0
  33. package/css/parts/input.css +206 -0
  34. package/css/parts/inset.css +90 -0
  35. package/css/parts/item.css +208 -0
  36. package/css/parts/kbd.css +126 -0
  37. package/css/parts/link.css +99 -0
  38. package/css/parts/menu.css +342 -0
  39. package/css/parts/menubar.css +71 -0
  40. package/css/parts/meter.css +112 -0
  41. package/css/parts/navigation-menu.css +199 -0
  42. package/css/parts/number-field.css +124 -0
  43. package/css/parts/otp-field.css +137 -0
  44. package/css/parts/page-header.css +45 -0
  45. package/css/parts/popover.css +149 -0
  46. package/css/parts/preview-card.css +116 -0
  47. package/css/parts/progress.css +159 -0
  48. package/css/parts/quote.css +18 -0
  49. package/css/parts/radio-cards.css +188 -0
  50. package/css/parts/radio-group.css +124 -0
  51. package/css/parts/scroll-area.css +95 -0
  52. package/css/parts/segmented-control.css +191 -0
  53. package/css/parts/select.css +378 -0
  54. package/css/parts/separator.css +48 -0
  55. package/css/parts/sidebar.css +211 -0
  56. package/css/parts/skeleton.css +65 -0
  57. package/css/parts/slider.css +156 -0
  58. package/css/parts/spinner.css +52 -0
  59. package/css/parts/strong.css +15 -0
  60. package/css/parts/switch.css +138 -0
  61. package/css/parts/tab-nav.css +48 -0
  62. package/css/parts/table.css +177 -0
  63. package/css/parts/tabs.css +214 -0
  64. package/css/parts/text-area.css +140 -0
  65. package/css/parts/text.css +245 -0
  66. package/css/parts/toast.css +302 -0
  67. package/css/parts/toggle-group.css +25 -0
  68. package/css/parts/toggle.css +181 -0
  69. package/css/parts/toolbar.css +133 -0
  70. package/css/parts/tooltip.css +61 -0
  71. package/css/parts/visually-hidden.css +21 -0
  72. package/css/styles.css +73 -0
  73. package/css/themes/agent-native.css +2321 -0
  74. package/css/themes/analytics.css +1159 -0
  75. package/css/themes/assets.css +1159 -0
  76. package/css/themes/baseline.css +1107 -0
  77. package/css/themes/brain.css +1159 -0
  78. package/css/themes/calendar.css +1159 -0
  79. package/css/themes/chat.css +1159 -0
  80. package/css/themes/clips.css +1159 -0
  81. package/css/themes/content.css +1159 -0
  82. package/css/themes/crm.css +1159 -0
  83. package/css/themes/design.css +1159 -0
  84. package/css/themes/desktop.css +1159 -0
  85. package/css/themes/dispatch.css +1159 -0
  86. package/css/themes/docs.css +1159 -0
  87. package/css/themes/factory.css +1159 -0
  88. package/css/themes/forms.css +1159 -0
  89. package/css/themes/mail.css +1159 -0
  90. package/css/themes/plan.css +1159 -0
  91. package/css/themes/slides.css +1159 -0
  92. package/css/themes/tasks.css +1159 -0
  93. package/css/tokens.css +1249 -0
  94. package/package.json +67 -4
  95. package/src/catalog/components/alert-dialog.ts +9 -0
  96. package/src/catalog/components/aspect-ratio.ts +6 -0
  97. package/src/catalog/components/autocomplete.ts +23 -0
  98. package/src/catalog/components/avatar.ts +16 -0
  99. package/src/catalog/components/badge.ts +19 -0
  100. package/src/catalog/components/blockquote.ts +10 -0
  101. package/src/catalog/components/button.ts +24 -0
  102. package/src/catalog/components/callout.ts +17 -0
  103. package/src/catalog/components/card.ts +10 -0
  104. package/src/catalog/components/checkbox-cards.ts +14 -0
  105. package/src/catalog/components/checkbox-group.ts +13 -0
  106. package/src/catalog/components/checkbox.ts +13 -0
  107. package/src/catalog/components/code.ts +22 -0
  108. package/src/catalog/components/combobox.ts +23 -0
  109. package/src/catalog/components/context-menu.ts +20 -0
  110. package/src/catalog/components/data-list.ts +14 -0
  111. package/src/catalog/components/dialog.ts +9 -0
  112. package/src/catalog/components/drawer.ts +9 -0
  113. package/src/catalog/components/em.ts +7 -0
  114. package/src/catalog/components/heading.ts +11 -0
  115. package/src/catalog/components/index.ts +54 -0
  116. package/src/catalog/components/input.ts +23 -0
  117. package/src/catalog/components/inset.ts +15 -0
  118. package/src/catalog/components/item.ts +21 -0
  119. package/src/catalog/components/kbd.ts +9 -0
  120. package/src/catalog/components/link.ts +19 -0
  121. package/src/catalog/components/menu.ts +20 -0
  122. package/src/catalog/components/menubar.ts +20 -0
  123. package/src/catalog/components/meter.ts +19 -0
  124. package/src/catalog/components/number-field.ts +10 -0
  125. package/src/catalog/components/otp-field.ts +9 -0
  126. package/src/catalog/components/popover.ts +9 -0
  127. package/src/catalog/components/preview-card.ts +9 -0
  128. package/src/catalog/components/progress.ts +24 -0
  129. package/src/catalog/components/quote.ts +7 -0
  130. package/src/catalog/components/radio-cards.ts +14 -0
  131. package/src/catalog/components/radio-group.ts +13 -0
  132. package/src/catalog/components/scroll-area.ts +15 -0
  133. package/src/catalog/components/segmented-control.ts +8 -0
  134. package/src/catalog/components/select.ts +26 -0
  135. package/src/catalog/components/separator.ts +14 -0
  136. package/src/catalog/components/skeleton.ts +8 -0
  137. package/src/catalog/components/slider.ts +19 -0
  138. package/src/catalog/components/spinner.ts +15 -0
  139. package/src/catalog/components/strong.ts +7 -0
  140. package/src/catalog/components/switch.ts +19 -0
  141. package/src/catalog/components/tab-nav.ts +18 -0
  142. package/src/catalog/components/table.ts +34 -0
  143. package/src/catalog/components/tabs.ts +18 -0
  144. package/src/catalog/components/text-area.ts +24 -0
  145. package/src/catalog/components/text.ts +11 -0
  146. package/src/catalog/components/toast.ts +12 -0
  147. package/src/catalog/components/toggle-group.ts +17 -0
  148. package/src/catalog/components/toggle.ts +17 -0
  149. package/src/catalog/components/toolbar.ts +6 -0
  150. package/src/catalog/index.ts +4 -0
  151. package/src/catalog/parts.ts +915 -0
  152. package/src/catalog/prop-types-source.ts +93 -0
  153. package/src/catalog/prop-types.generated.ts +1402 -0
  154. package/src/catalog/props.ts +225 -0
  155. package/src/css.ts +318 -0
  156. package/src/generated-css.ts +21 -0
  157. package/src/index.ts +8 -0
  158. package/src/model/compare.ts +127 -0
  159. package/src/model/dom.ts +253 -0
  160. package/src/model/ids.ts +25 -0
  161. package/src/model/index.ts +6 -0
  162. package/src/model/styles.ts +254 -0
  163. package/src/model/types.ts +97 -0
  164. package/src/model/validate.ts +133 -0
  165. package/src/themes/appearances.ts +105 -0
  166. package/src/themes/baseline.ts +27 -0
  167. package/src/themes/index.ts +85 -0
  168. package/src/themes/reference.ts +9 -0
  169. package/src/themes/shadcn.ts +239 -0
  170. package/src/themes/types.ts +53 -0
  171. package/src/tokens/at.ts +13 -0
  172. package/src/tokens/base.ts +48 -0
  173. package/src/tokens/color/bezier.ts +111 -0
  174. package/src/tokens/color/index.ts +2 -0
  175. package/src/tokens/color/radix-generator.ts +516 -0
  176. package/src/tokens/dtcg.ts +298 -0
  177. package/src/tokens/files/foundations.tokens.json +318 -0
  178. package/src/tokens/files/palette.tokens.json +58 -0
  179. package/src/tokens/files/themes/agent-native.dark.tokens.json +405 -0
  180. package/src/tokens/files/themes/agent-native.light.tokens.json +405 -0
  181. package/src/tokens/files/themes/analytics.dark.tokens.json +391 -0
  182. package/src/tokens/files/themes/analytics.light.tokens.json +391 -0
  183. package/src/tokens/files/themes/assets.dark.tokens.json +391 -0
  184. package/src/tokens/files/themes/assets.light.tokens.json +391 -0
  185. package/src/tokens/files/themes/brain.dark.tokens.json +391 -0
  186. package/src/tokens/files/themes/brain.light.tokens.json +391 -0
  187. package/src/tokens/files/themes/calendar.dark.tokens.json +391 -0
  188. package/src/tokens/files/themes/calendar.light.tokens.json +391 -0
  189. package/src/tokens/files/themes/chat.dark.tokens.json +391 -0
  190. package/src/tokens/files/themes/chat.light.tokens.json +391 -0
  191. package/src/tokens/files/themes/clips.dark.tokens.json +391 -0
  192. package/src/tokens/files/themes/clips.light.tokens.json +391 -0
  193. package/src/tokens/files/themes/content.dark.tokens.json +391 -0
  194. package/src/tokens/files/themes/content.light.tokens.json +391 -0
  195. package/src/tokens/files/themes/crm.dark.tokens.json +391 -0
  196. package/src/tokens/files/themes/crm.light.tokens.json +391 -0
  197. package/src/tokens/files/themes/design.dark.tokens.json +391 -0
  198. package/src/tokens/files/themes/design.light.tokens.json +391 -0
  199. package/src/tokens/files/themes/desktop.dark.tokens.json +391 -0
  200. package/src/tokens/files/themes/desktop.light.tokens.json +391 -0
  201. package/src/tokens/files/themes/dispatch.dark.tokens.json +391 -0
  202. package/src/tokens/files/themes/dispatch.light.tokens.json +391 -0
  203. package/src/tokens/files/themes/docs.dark.tokens.json +391 -0
  204. package/src/tokens/files/themes/docs.light.tokens.json +391 -0
  205. package/src/tokens/files/themes/factory.dark.tokens.json +391 -0
  206. package/src/tokens/files/themes/factory.light.tokens.json +391 -0
  207. package/src/tokens/files/themes/forms.dark.tokens.json +391 -0
  208. package/src/tokens/files/themes/forms.light.tokens.json +391 -0
  209. package/src/tokens/files/themes/mail.dark.tokens.json +391 -0
  210. package/src/tokens/files/themes/mail.light.tokens.json +391 -0
  211. package/src/tokens/files/themes/plan.dark.tokens.json +391 -0
  212. package/src/tokens/files/themes/plan.light.tokens.json +391 -0
  213. package/src/tokens/files/themes/slides.dark.tokens.json +391 -0
  214. package/src/tokens/files/themes/slides.light.tokens.json +391 -0
  215. package/src/tokens/files/themes/tasks.dark.tokens.json +391 -0
  216. package/src/tokens/files/themes/tasks.light.tokens.json +391 -0
  217. package/src/tokens/palette.ts +354 -0
  218. package/src/tokens/primitives.ts +84 -0
  219. package/src/tokens/roles.ts +269 -0
  220. package/src/tokens/round-trip.ts +277 -0
  221. package/src/tokens/source.ts +302 -0
  222. package/tsconfig.json +26 -0
@@ -0,0 +1,253 @@
1
+ import type { ModelRow, RowBadge, RowEffect } from "./types.js";
2
+
3
+ /**
4
+ * The attributes every part writes on the element that draws a row, in every
5
+ * part set. State is not here: it is read from the element's ARIA and native
6
+ * state (aria-current, aria-selected, aria-checked, aria-expanded, disabled,
7
+ * aria-busy), so two part sets that agree on state also agree on what
8
+ * assistive technology hears. The one exception is `complete`, which ARIA
9
+ * has no state for.
10
+ */
11
+ export interface RowAttributes {
12
+ "data-row-id": string;
13
+ "data-part": string;
14
+ "data-row-kind"?: string;
15
+ "data-row-effect"?: string;
16
+ "data-row-badge"?: string;
17
+ "data-row-slot"?: string;
18
+ "data-row-regions"?: string;
19
+ "data-row-hosts"?: string;
20
+ "data-row-complete"?: "true";
21
+ id?: string;
22
+ }
23
+
24
+ export function encodeEffect(effect: RowEffect): string {
25
+ switch (effect.kind) {
26
+ case "navigate":
27
+ return `navigate:${effect.to}`;
28
+ case "action":
29
+ return `action:${effect.effectId}`;
30
+ case "open":
31
+ return `open:${effect.target}`;
32
+ }
33
+ }
34
+
35
+ export function encodeBadge(badge: RowBadge): string {
36
+ switch (badge.kind) {
37
+ case "count":
38
+ return `count:${badge.value}`;
39
+ case "dot":
40
+ return "dot";
41
+ case "label":
42
+ return badge.tone
43
+ ? `label-${badge.tone}:${badge.value}`
44
+ : `label:${badge.value}`;
45
+ }
46
+ }
47
+
48
+ /** `region:slot` pairs in region order, so both part sets write the same text. */
49
+ export function encodeRegions(
50
+ regions: Readonly<Record<string, string>>,
51
+ ): string {
52
+ return Object.entries(regions)
53
+ .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
54
+ .map(([region, slot]) => `${region}:${slot}`)
55
+ .join(" ");
56
+ }
57
+
58
+ export function rowAttributes(row: ModelRow): RowAttributes {
59
+ const attributes: RowAttributes = {
60
+ "data-row-id": row.id,
61
+ "data-part": row.part,
62
+ };
63
+ if (row.kind) attributes["data-row-kind"] = row.kind;
64
+ if (row.effect) attributes["data-row-effect"] = encodeEffect(row.effect);
65
+ if (row.state?.badge) {
66
+ attributes["data-row-badge"] = encodeBadge(row.state.badge);
67
+ }
68
+ if (row.slot) attributes["data-row-slot"] = row.slot;
69
+ if (row.hosts === "any") {
70
+ attributes["data-row-hosts"] = "any";
71
+ } else if (row.hosts && row.hosts.length > 0) {
72
+ attributes["data-row-hosts"] = row.hosts.join(" ");
73
+ }
74
+ if (row.state?.complete) attributes["data-row-complete"] = "true";
75
+ if (row.regions && Object.keys(row.regions).length > 0) {
76
+ attributes["data-row-regions"] = encodeRegions(row.regions);
77
+ }
78
+ if (row.anchor) attributes.id = row.anchor;
79
+ return attributes;
80
+ }
81
+
82
+ /** What a rendered row exposes, independent of how it is drawn. */
83
+ export interface RowSnapshot {
84
+ id: string;
85
+ part: string;
86
+ parentId: string | null;
87
+ /** Position among its siblings under the same parent row. */
88
+ position: number;
89
+ kind: string | null;
90
+ name: string;
91
+ selected: boolean;
92
+ checked: "true" | "false" | "mixed" | null;
93
+ expanded: boolean | null;
94
+ disabled: boolean;
95
+ pending: boolean;
96
+ value: string | null;
97
+ effect: string | null;
98
+ badge: string | null;
99
+ slot: string | null;
100
+ regions: string | null;
101
+ hosts: string | null;
102
+ complete: boolean;
103
+ }
104
+
105
+ const ROW_SELECTOR = "[data-row-id]";
106
+ const CONTROL_SELECTOR =
107
+ "input,select,textarea,button,[role=switch],[role=checkbox],[role=radio],[role=option],[role=menuitem],[role=menuitemcheckbox],[role=menuitemradio],[role=tab],[role=treeitem],a[href]";
108
+
109
+ function ownText(element: Element): string {
110
+ let text = "";
111
+ for (const node of element.childNodes) {
112
+ if (node.nodeType === 3) {
113
+ text += node.textContent ?? "";
114
+ } else if (node.nodeType === 1) {
115
+ const child = node as Element;
116
+ if (child.matches(ROW_SELECTOR)) continue;
117
+ if (child.getAttribute("aria-hidden") === "true") continue;
118
+ text += ` ${ownText(child)} `;
119
+ }
120
+ }
121
+ return text;
122
+ }
123
+
124
+ function normalizeSpace(value: string): string {
125
+ return value.replace(/\s+/g, " ").trim();
126
+ }
127
+
128
+ function labelledByText(element: Element): string | null {
129
+ const ids = element.getAttribute("aria-labelledby");
130
+ if (!ids) return null;
131
+ const document = element.ownerDocument;
132
+ const text = ids
133
+ .split(/\s+/)
134
+ .map((id) => document.getElementById(id)?.textContent ?? "")
135
+ .join(" ");
136
+ return normalizeSpace(text) || null;
137
+ }
138
+
139
+ function rowControl(row: Element): Element {
140
+ if (row.matches(CONTROL_SELECTOR)) return row;
141
+ for (const candidate of row.querySelectorAll(CONTROL_SELECTOR)) {
142
+ if (candidate.closest(ROW_SELECTOR) === row) return candidate;
143
+ }
144
+ return row;
145
+ }
146
+
147
+ /**
148
+ * The accessible name, in the order assistive technology resolves the common
149
+ * cases: aria-label, aria-labelledby, an explicit `[data-row-label]`, then
150
+ * the row's own text.
151
+ */
152
+ export function rowName(row: Element): string {
153
+ const control = rowControl(row);
154
+ for (const element of control === row ? [row] : [row, control]) {
155
+ const label = element.getAttribute("aria-label");
156
+ if (label && normalizeSpace(label)) return normalizeSpace(label);
157
+ const labelledBy = labelledByText(element);
158
+ if (labelledBy) return labelledBy;
159
+ }
160
+ const explicit = row.querySelector("[data-row-label]");
161
+ if (explicit && explicit.closest(ROW_SELECTOR) === row) {
162
+ return normalizeSpace(explicit.textContent);
163
+ }
164
+ return normalizeSpace(ownText(row));
165
+ }
166
+
167
+ function booleanAttribute(element: Element, name: string): boolean | null {
168
+ const value = element.getAttribute(name);
169
+ if (value === null) return null;
170
+ return value !== "false";
171
+ }
172
+
173
+ function snapshotRow(row: Element, position: number): RowSnapshot {
174
+ const control = rowControl(row);
175
+ const both = control === row ? [row] : [row, control];
176
+ const first = (read: (element: Element) => string | null) =>
177
+ both.map(read).find((value) => value !== null) ?? null;
178
+
179
+ const current = first((element) => element.getAttribute("aria-current"));
180
+ const selected =
181
+ (current !== null && current !== "false") ||
182
+ first((element) => element.getAttribute("aria-selected")) === "true";
183
+
184
+ const nativeChecked =
185
+ control instanceof HTMLInputElement &&
186
+ (control.type === "checkbox" || control.type === "radio")
187
+ ? control.indeterminate
188
+ ? "mixed"
189
+ : String(control.checked)
190
+ : null;
191
+ const ariaChecked = first((element) => element.getAttribute("aria-checked"));
192
+ const checked = (nativeChecked ?? ariaChecked) as RowSnapshot["checked"];
193
+
194
+ const expanded = both
195
+ .map((element) => booleanAttribute(element, "aria-expanded"))
196
+ .find((value) => value !== null);
197
+
198
+ const disabled = both.some(
199
+ (element) =>
200
+ element.hasAttribute("disabled") ||
201
+ element.getAttribute("aria-disabled") === "true",
202
+ );
203
+
204
+ const value =
205
+ control instanceof HTMLInputElement &&
206
+ control.type !== "checkbox" &&
207
+ control.type !== "radio"
208
+ ? control.value
209
+ : control instanceof HTMLSelectElement ||
210
+ control instanceof HTMLTextAreaElement
211
+ ? control.value
212
+ : first((element) => element.getAttribute("aria-valuetext"));
213
+
214
+ const parent = row.parentElement?.closest(ROW_SELECTOR) ?? null;
215
+ return {
216
+ id: row.getAttribute("data-row-id") ?? "",
217
+ part: row.getAttribute("data-part") ?? "",
218
+ parentId: parent?.getAttribute("data-row-id") ?? null,
219
+ position,
220
+ kind: row.getAttribute("data-row-kind"),
221
+ name: rowName(row),
222
+ selected,
223
+ checked: checked === null ? null : checked,
224
+ expanded: expanded ?? null,
225
+ disabled,
226
+ pending: both.some(
227
+ (element) => element.getAttribute("aria-busy") === "true",
228
+ ),
229
+ value,
230
+ effect: row.getAttribute("data-row-effect"),
231
+ badge: row.getAttribute("data-row-badge"),
232
+ slot: row.getAttribute("data-row-slot"),
233
+ regions: row.getAttribute("data-row-regions"),
234
+ hosts: row.getAttribute("data-row-hosts"),
235
+ complete: row.getAttribute("data-row-complete") === "true",
236
+ };
237
+ }
238
+
239
+ /**
240
+ * Every rendered row under `root`, in document order. Rows rendered into a
241
+ * portal are included when the portal is inside `root` (pass `document`).
242
+ */
243
+ export function captureRows(root: ParentNode): RowSnapshot[] {
244
+ const positions = new Map<string | null, number>();
245
+ return [...root.querySelectorAll(ROW_SELECTOR)].map((row) => {
246
+ const parentId =
247
+ row.parentElement?.closest(ROW_SELECTOR)?.getAttribute("data-row-id") ??
248
+ null;
249
+ const position = positions.get(parentId) ?? 0;
250
+ positions.set(parentId, position + 1);
251
+ return snapshotRow(row, position);
252
+ });
253
+ }
@@ -0,0 +1,25 @@
1
+ const SEGMENT = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
2
+
3
+ function hash(value: string): string {
4
+ let h = 0x811c9dc5;
5
+ for (let index = 0; index < value.length; index += 1) {
6
+ h ^= value.charCodeAt(index);
7
+ h = Math.imul(h, 0x01000193) >>> 0;
8
+ }
9
+ return h.toString(36);
10
+ }
11
+
12
+ /**
13
+ * One row id segment from app data (a record id, an email, a provider name).
14
+ * A value that is already a valid segment is returned as is; any other value
15
+ * is slugged and suffixed with a hash of the original, so two values that
16
+ * slug alike (`a.b@x.com`, `a-b@x.com`) still get different ids.
17
+ */
18
+ export function rowIdSegment(value: string): string {
19
+ if (SEGMENT.test(value)) return value;
20
+ const slug = value
21
+ .toLowerCase()
22
+ .replace(/[^a-z0-9]+/g, "-")
23
+ .replace(/^-+|-+$/g, "");
24
+ return slug ? `${slug}-${hash(value)}` : `x-${hash(value)}`;
25
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./compare.js";
2
+ export * from "./dom.js";
3
+ export * from "./ids.js";
4
+ export * from "./styles.js";
5
+ export * from "./types.js";
6
+ export * from "./validate.js";
@@ -0,0 +1,254 @@
1
+ /**
2
+ * Painted-style capture, shared by the gallery's parity runner and the
3
+ * fidelity check. Values are normalized so that layers and borders that
4
+ * paint nothing compare equal however a stylesheet spells them.
5
+ */
6
+
7
+ export const PAINTED_PROPERTIES = [
8
+ "display",
9
+ "position",
10
+ "align-items",
11
+ "justify-content",
12
+ "gap",
13
+ "box-sizing",
14
+ "width",
15
+ "height",
16
+ "min-width",
17
+ "padding-top",
18
+ "padding-right",
19
+ "padding-bottom",
20
+ "padding-left",
21
+ "border-top-width",
22
+ "border-right-width",
23
+ "border-bottom-width",
24
+ "border-left-width",
25
+ "border-top-style",
26
+ "border-top-color",
27
+ "border-right-color",
28
+ "border-bottom-color",
29
+ "border-left-color",
30
+ "border-top-left-radius",
31
+ "border-top-right-radius",
32
+ "border-bottom-right-radius",
33
+ "border-bottom-left-radius",
34
+ "background-color",
35
+ "background-image",
36
+ "color",
37
+ "box-shadow",
38
+ "font-family",
39
+ "font-size",
40
+ "line-height",
41
+ "font-weight",
42
+ "letter-spacing",
43
+ "text-align",
44
+ "white-space",
45
+ "user-select",
46
+ "cursor",
47
+ "opacity",
48
+ "pointer-events",
49
+ "outline-style",
50
+ "outline-width",
51
+ "outline-color",
52
+ "outline-offset",
53
+ "transition-property",
54
+ "transition-duration",
55
+ "transition-timing-function",
56
+ "scale",
57
+ "transform",
58
+ "translate",
59
+ "text-decoration-line",
60
+ "text-underline-offset",
61
+ "overflow-x",
62
+ "overflow-y",
63
+ "z-index",
64
+ "backdrop-filter",
65
+ ] as const;
66
+
67
+ export const ICON_PROPERTIES = [
68
+ "width",
69
+ "height",
70
+ "flex-shrink",
71
+ "pointer-events",
72
+ ];
73
+
74
+ const BORDER_SIDES = ["top", "right", "bottom", "left"] as const;
75
+
76
+ function normalizeShadow(value: string): string {
77
+ const layers = value
78
+ .split(/,(?![^(]*\))/)
79
+ .map((layer) => layer.trim())
80
+ .filter(
81
+ (layer) =>
82
+ layer !== "none" &&
83
+ !/^(?:rgba?\([^)]*\)|oklab\([^)]*\)|\S+) 0px 0px 0px 0px(?: inset)?$/.test(
84
+ layer,
85
+ ),
86
+ );
87
+ return layers.length > 0 ? layers.join(", ") : "none";
88
+ }
89
+
90
+ function paintedValue(style: CSSStyleDeclaration, property: string): string {
91
+ const value = style.getPropertyValue(property);
92
+ if (property === "box-shadow") return normalizeShadow(value);
93
+ if (
94
+ (property === "outline-width" || property === "outline-color") &&
95
+ style.getPropertyValue("outline-style") === "none"
96
+ ) {
97
+ return "(no outline)";
98
+ }
99
+ const side = BORDER_SIDES.find((name) => property === `border-${name}-color`);
100
+ if (side && style.getPropertyValue(`border-${side}-width`) === "0px") {
101
+ return "(no border)";
102
+ }
103
+ return value;
104
+ }
105
+
106
+ export type CapturedStyles = Record<string, string>;
107
+
108
+ /** Every painted property of an element and its first icon. */
109
+ export function captureStyles(element: Element): CapturedStyles {
110
+ const style = getComputedStyle(element);
111
+ const captured: CapturedStyles = {};
112
+ for (const property of PAINTED_PROPERTIES) {
113
+ captured[property] = paintedValue(style, property);
114
+ }
115
+ const icon = element.querySelector("svg");
116
+ if (icon) {
117
+ const iconStyle = getComputedStyle(icon);
118
+ for (const property of ICON_PROPERTIES) {
119
+ captured[`svg ${property}`] = iconStyle.getPropertyValue(property);
120
+ }
121
+ }
122
+ return captured;
123
+ }
124
+
125
+ export interface StyleDifference {
126
+ case: string;
127
+ property: string;
128
+ part: string;
129
+ legacy: string;
130
+ }
131
+
132
+ /** Every property whose painted value differs between two captures. */
133
+ export function diffStyles(
134
+ key: string,
135
+ part: CapturedStyles,
136
+ legacy: CapturedStyles,
137
+ ): StyleDifference[] {
138
+ const differences: StyleDifference[] = [];
139
+ for (const property of new Set([
140
+ ...Object.keys(part),
141
+ ...Object.keys(legacy),
142
+ ])) {
143
+ if (part[property] !== legacy[property]) {
144
+ differences.push({
145
+ case: key,
146
+ property,
147
+ part: part[property] ?? "(missing)",
148
+ legacy: legacy[property] ?? "(missing)",
149
+ });
150
+ }
151
+ }
152
+ return differences;
153
+ }
154
+
155
+ /**
156
+ * Where a row lives in a product that has no `data-row-id`: the original app a
157
+ * reference part set reproduces. One selector per row id.
158
+ */
159
+ export interface RowLocator {
160
+ rowId: string;
161
+ selector: string;
162
+ }
163
+
164
+ export interface LocatorReport {
165
+ tagged: string[];
166
+ /** Selectors that matched nothing. */
167
+ missing: string[];
168
+ /** Selectors that matched more than one element; none of these is tagged. */
169
+ ambiguous: string[];
170
+ }
171
+
172
+ /**
173
+ * Writes `data-row-id` onto the elements the locators find, so the original
174
+ * product can be captured with the same keys as the part sets. A selector that
175
+ * matches nothing or several elements is reported, never guessed.
176
+ */
177
+ export function applyRowLocators(
178
+ root: ParentNode,
179
+ locators: readonly RowLocator[],
180
+ ): LocatorReport {
181
+ const report: LocatorReport = { tagged: [], missing: [], ambiguous: [] };
182
+ for (const { rowId, selector } of locators) {
183
+ const matches = root.querySelectorAll(selector);
184
+ if (matches.length === 0) report.missing.push(rowId);
185
+ else if (matches.length > 1) report.ambiguous.push(rowId);
186
+ else {
187
+ matches[0]?.setAttribute("data-row-id", rowId);
188
+ report.tagged.push(rowId);
189
+ }
190
+ }
191
+ return report;
192
+ }
193
+
194
+ /** Painted styles of every row under `root`, keyed by row id. */
195
+ export function captureRowStyles(
196
+ root: ParentNode,
197
+ ): Record<string, CapturedStyles> {
198
+ const captured: Record<string, CapturedStyles> = {};
199
+ for (const element of root.querySelectorAll("[data-row-id]")) {
200
+ const rowId = element.getAttribute("data-row-id");
201
+ if (!rowId) continue;
202
+ if (rowId in captured) {
203
+ throw new Error(`Row ${rowId} is rendered twice`);
204
+ }
205
+ captured[rowId] = captureStyles(element);
206
+ }
207
+ return captured;
208
+ }
209
+
210
+ export interface FidelityReport {
211
+ compared: number;
212
+ /** Rows the original has and the reference part set does not render. */
213
+ missing: string[];
214
+ differences: Array<{
215
+ rowId: string;
216
+ property: string;
217
+ original: string;
218
+ reference: string;
219
+ }>;
220
+ }
221
+
222
+ /**
223
+ * The fidelity check: a reference part set's painted styles against captures
224
+ * of the original product, row by row. `properties` narrows the comparison to
225
+ * what the reference is meant to reproduce.
226
+ */
227
+ export function compareRowStyles(
228
+ original: Readonly<Record<string, CapturedStyles>>,
229
+ reference: Readonly<Partial<Record<string, CapturedStyles>>>,
230
+ properties?: readonly string[],
231
+ ): FidelityReport {
232
+ const report: FidelityReport = { compared: 0, missing: [], differences: [] };
233
+ for (const [rowId, originalStyles] of Object.entries(original)) {
234
+ const referenceStyles = reference[rowId];
235
+ if (!referenceStyles) {
236
+ report.missing.push(rowId);
237
+ continue;
238
+ }
239
+ report.compared++;
240
+ for (const property of properties ?? Object.keys(originalStyles)) {
241
+ const originalValue = originalStyles[property] ?? "(missing)";
242
+ const referenceValue = referenceStyles[property] ?? "(missing)";
243
+ if (originalValue !== referenceValue) {
244
+ report.differences.push({
245
+ rowId,
246
+ property,
247
+ original: originalValue,
248
+ reference: referenceValue,
249
+ });
250
+ }
251
+ }
252
+ }
253
+ return report;
254
+ }
@@ -0,0 +1,97 @@
1
+ /** Dotted, lowercase, at least two segments: `sidebar.nav.inbox`. */
2
+ export type RowId = string;
3
+
4
+ /** A catalog part, or a part and sub-part: `settings`, `settings.row`. */
5
+ export type PartRef = string;
6
+
7
+ export type RowKind =
8
+ | "section"
9
+ | "destination"
10
+ | "action"
11
+ | "toggle"
12
+ | "choice"
13
+ | "field"
14
+ | "info";
15
+
16
+ export type RowIcon =
17
+ | { kind: "symbol"; key: string }
18
+ | { kind: "image"; src: string; alt?: string | undefined };
19
+
20
+ export type RowBadge =
21
+ | { kind: "count"; value: number }
22
+ | { kind: "dot" }
23
+ | {
24
+ kind: "label";
25
+ value: string;
26
+ /** It asks for the person's attention (a required setup step). */
27
+ tone?: "attention" | undefined;
28
+ };
29
+
30
+ export interface RowState {
31
+ /** The current destination or the chosen option. */
32
+ selected?: boolean | undefined;
33
+ checked?: boolean | "mixed" | undefined;
34
+ expanded?: boolean | undefined;
35
+ disabled?: boolean | undefined;
36
+ /** Work for this row is in flight. */
37
+ pending?: boolean | undefined;
38
+ /** What the row sets up is done: connected, configured. */
39
+ complete?: boolean | undefined;
40
+ /** A field's or choice's current value, as text. */
41
+ value?: string | undefined;
42
+ badge?: RowBadge | undefined;
43
+ }
44
+
45
+ export type RowEffect =
46
+ | { kind: "navigate"; to: string }
47
+ | { kind: "action"; effectId: string }
48
+ | { kind: "open"; target: RowId };
49
+
50
+ export interface ModelRow {
51
+ id: RowId;
52
+ part: PartRef;
53
+ /** The row's accessible name. Both part sets must expose exactly this text. */
54
+ label: string;
55
+ kind?: RowKind | undefined;
56
+ description?: string | undefined;
57
+ icon?: RowIcon | undefined;
58
+ state?: RowState | undefined;
59
+ effect?: RowEffect | undefined;
60
+ /** Nested rows: a section's entries, a menu's items. */
61
+ rows?: readonly ModelRow[] | undefined;
62
+ /**
63
+ * Content that belongs to one product (a canvas, a custom control), drawn
64
+ * by the app beside the parts. The key names it for both part sets.
65
+ */
66
+ slot?: string | undefined;
67
+ /**
68
+ * App content for the part's other named regions, by region (a settings
69
+ * row's `status` beside its label, or its `detail` below it). Each value is
70
+ * a slot key the app fills, as with `slot`. A part throws on a region it
71
+ * does not draw.
72
+ */
73
+ regions?: Readonly<Record<string, string>> | undefined;
74
+ /**
75
+ * The label names the row for assistive technology only: a group with no
76
+ * visible title still has a name, drawn visually hidden.
77
+ */
78
+ labelHidden?: boolean | undefined;
79
+ /**
80
+ * Rows drawn inside this row's slot or regions by their own surfaces, by id: a section
81
+ * whose component mounts only when shown and owns its rows' state. `"any"`
82
+ * when the slot holds content the app passed in, whose rows it cannot
83
+ * know. A surface nested in a slot that does not host its rows throws.
84
+ */
85
+ hosts?: readonly RowId[] | "any" | undefined;
86
+ /**
87
+ * The DOM id the row's element carries, for deep links and search results
88
+ * that scroll to it (`#bell-sound`). Unique on the page.
89
+ */
90
+ anchor?: string | undefined;
91
+ }
92
+
93
+ export interface ScreenModel {
94
+ /** The surface's own id, the first segment its rows usually share. */
95
+ id: string;
96
+ rows: readonly ModelRow[];
97
+ }