@terreno/ui 57.12.0 → 57.14.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 (164) hide show
  1. package/dist/AiSuggestionBox.js +9 -9
  2. package/dist/AiSuggestionBox.js.map +1 -1
  3. package/dist/Banner.js +1 -1
  4. package/dist/Banner.js.map +1 -1
  5. package/dist/Box.d.ts.map +1 -1
  6. package/dist/Box.js +36 -3
  7. package/dist/Box.js.map +1 -1
  8. package/dist/Button.d.ts.map +1 -1
  9. package/dist/Button.js +7 -0
  10. package/dist/Button.js.map +1 -1
  11. package/dist/Common.d.ts +31 -2
  12. package/dist/Common.d.ts.map +1 -1
  13. package/dist/Common.js.map +1 -1
  14. package/dist/DataTable.d.ts.map +1 -1
  15. package/dist/DataTable.js +50 -3
  16. package/dist/DataTable.js.map +1 -1
  17. package/dist/Image.d.ts.map +1 -1
  18. package/dist/Image.js +1 -1
  19. package/dist/Image.js.map +1 -1
  20. package/dist/MarkdownView.d.ts.map +1 -1
  21. package/dist/MarkdownView.js +67 -5
  22. package/dist/MarkdownView.js.map +1 -1
  23. package/dist/Modal.js +1 -1
  24. package/dist/SegmentedControl.d.ts.map +1 -1
  25. package/dist/SegmentedControl.js +15 -11
  26. package/dist/SegmentedControl.js.map +1 -1
  27. package/dist/SelectField.d.ts.map +1 -1
  28. package/dist/SelectField.js +4 -1
  29. package/dist/SelectField.js.map +1 -1
  30. package/dist/SplitPage.js +1 -1
  31. package/dist/SplitPage.js.map +1 -1
  32. package/dist/SplitPage.native.js +1 -1
  33. package/dist/SplitPage.native.js.map +1 -1
  34. package/dist/TerrenoProvider.d.ts +7 -3
  35. package/dist/TerrenoProvider.d.ts.map +1 -1
  36. package/dist/TerrenoProvider.js +2 -2
  37. package/dist/TerrenoProvider.js.map +1 -1
  38. package/dist/Theme.d.ts +19 -85
  39. package/dist/Theme.d.ts.map +1 -1
  40. package/dist/Theme.js +149 -33
  41. package/dist/Theme.js.map +1 -1
  42. package/dist/Toast.d.ts.map +1 -1
  43. package/dist/Toast.js +21 -12
  44. package/dist/Toast.js.map +1 -1
  45. package/dist/ToastNotifications.d.ts.map +1 -1
  46. package/dist/ToastNotifications.js +5 -2
  47. package/dist/ToastNotifications.js.map +1 -1
  48. package/dist/Tooltip.d.ts +1 -0
  49. package/dist/Tooltip.d.ts.map +1 -1
  50. package/dist/Tooltip.js +84 -37
  51. package/dist/Tooltip.js.map +1 -1
  52. package/dist/blocks/BlocksView.d.ts.map +1 -1
  53. package/dist/blocks/BlocksView.js +5 -1
  54. package/dist/blocks/BlocksView.js.map +1 -1
  55. package/dist/blocks/blockRenderers.d.ts +10 -1
  56. package/dist/blocks/blockRenderers.d.ts.map +1 -1
  57. package/dist/blocks/blockRenderers.js +330 -9
  58. package/dist/blocks/blockRenderers.js.map +1 -1
  59. package/dist/blocks/useChecklistState.d.ts +18 -0
  60. package/dist/blocks/useChecklistState.d.ts.map +1 -0
  61. package/dist/blocks/useChecklistState.js +29 -0
  62. package/dist/blocks/useChecklistState.js.map +1 -0
  63. package/dist/blocks/useResolvedImages.d.ts.map +1 -1
  64. package/dist/blocks/useResolvedImages.js +16 -0
  65. package/dist/blocks/useResolvedImages.js.map +1 -1
  66. package/dist/colorContrast.d.ts +9 -0
  67. package/dist/colorContrast.d.ts.map +1 -0
  68. package/dist/colorContrast.js +47 -0
  69. package/dist/colorContrast.js.map +1 -0
  70. package/dist/icons/OfflineIcon.js +1 -1
  71. package/dist/table/TableDate.d.ts.map +1 -1
  72. package/dist/table/TableDate.js +3 -1
  73. package/dist/table/TableDate.js.map +1 -1
  74. package/package.json +2 -2
  75. package/src/AiSuggestionBox.tsx +10 -10
  76. package/src/Banner.tsx +1 -1
  77. package/src/Box.test.tsx +80 -0
  78. package/src/Box.tsx +52 -0
  79. package/src/Button.test.tsx +39 -1
  80. package/src/Button.tsx +9 -0
  81. package/src/Common.ts +32 -2
  82. package/src/DataTable.test.tsx +133 -1
  83. package/src/DataTable.tsx +78 -3
  84. package/src/Image.tsx +1 -0
  85. package/src/MarkdownView.test.tsx +165 -2
  86. package/src/MarkdownView.tsx +100 -5
  87. package/src/Modal.tsx +1 -1
  88. package/src/SegmentedControl.test.tsx +7 -0
  89. package/src/SegmentedControl.tsx +22 -11
  90. package/src/SelectField.tsx +4 -1
  91. package/src/SplitPage.native.tsx +1 -1
  92. package/src/SplitPage.tsx +1 -1
  93. package/src/TerrenoProvider.tsx +15 -5
  94. package/src/Theme.test.tsx +212 -3
  95. package/src/Theme.tsx +184 -39
  96. package/src/Toast.test.tsx +7 -0
  97. package/src/Toast.tsx +22 -12
  98. package/src/ToastNotifications.test.tsx +36 -0
  99. package/src/ToastNotifications.tsx +5 -2
  100. package/src/Tooltip.test.tsx +232 -1
  101. package/src/Tooltip.tsx +128 -67
  102. package/src/__snapshots__/AddressField.test.tsx.snap +7 -7
  103. package/src/__snapshots__/AiSuggestionBox.test.tsx.snap +23 -23
  104. package/src/__snapshots__/Banner.test.tsx.snap +3 -0
  105. package/src/__snapshots__/BooleanField.test.tsx.snap +3 -3
  106. package/src/__snapshots__/Button.test.tsx.snap +16 -16
  107. package/src/__snapshots__/Card.test.tsx.snap +0 -1
  108. package/src/__snapshots__/CustomSelectField.test.tsx.snap +23 -23
  109. package/src/__snapshots__/DataTable.test.tsx.snap +30 -30
  110. package/src/__snapshots__/DecimalRangeActionSheet.test.tsx.snap +2 -2
  111. package/src/__snapshots__/DropdownPanel.test.tsx.snap +2 -2
  112. package/src/__snapshots__/EmailField.test.tsx.snap +2 -2
  113. package/src/__snapshots__/ErrorPage.test.tsx.snap +1 -1
  114. package/src/__snapshots__/Field.test.tsx.snap +37 -37
  115. package/src/__snapshots__/FilterSelectMenu.test.tsx.snap +2 -1
  116. package/src/__snapshots__/HeightActionSheet.test.tsx.snap +2 -2
  117. package/src/__snapshots__/HeightField.test.tsx.snap +8 -8
  118. package/src/__snapshots__/HeightFieldDesktop.test.tsx.snap +6 -6
  119. package/src/__snapshots__/InfoModalIcon.test.tsx.snap +2 -2
  120. package/src/__snapshots__/MarkdownView.test.tsx.snap +2 -2
  121. package/src/__snapshots__/MobileAddressAutoComplete.test.tsx.snap +1 -1
  122. package/src/__snapshots__/Modal.test.tsx.snap +10 -10
  123. package/src/__snapshots__/NumberField.test.tsx.snap +1 -1
  124. package/src/__snapshots__/NumberPickerActionSheet.test.tsx.snap +2 -2
  125. package/src/__snapshots__/Page.test.tsx.snap +1 -1
  126. package/src/__snapshots__/PhoneNumberField.test.tsx.snap +4 -4
  127. package/src/__snapshots__/PickerSelect.test.tsx.snap +8 -8
  128. package/src/__snapshots__/SectionDivider.test.tsx.snap +2 -2
  129. package/src/__snapshots__/SegmentedControl.test.tsx.snap +183 -0
  130. package/src/__snapshots__/SelectBadge.test.tsx.snap +9 -9
  131. package/src/__snapshots__/SelectField.test.tsx.snap +19 -19
  132. package/src/__snapshots__/Signature.test.tsx.snap +1 -1
  133. package/src/__snapshots__/SignatureCaptureField.test.tsx.snap +2 -2
  134. package/src/__snapshots__/SignatureField.test.tsx.snap +2 -2
  135. package/src/__snapshots__/SplitPage.test.tsx.snap +11 -11
  136. package/src/__snapshots__/TapToEdit.test.tsx.snap +1 -1
  137. package/src/__snapshots__/TerrenoProvider.test.tsx.snap +20 -4
  138. package/src/__snapshots__/TextArea.test.tsx.snap +4 -4
  139. package/src/__snapshots__/TextField.test.tsx.snap +3 -3
  140. package/src/__snapshots__/TimezonePicker.test.tsx.snap +18 -18
  141. package/src/__snapshots__/Toast.test.tsx.snap +224 -154
  142. package/src/__snapshots__/TypedSignatureField.test.tsx.snap +3 -3
  143. package/src/__snapshots__/UnifiedAddressAutoComplete.test.tsx.snap +6 -6
  144. package/src/__snapshots__/WebAddressAutocomplete.test.tsx.snap +4 -4
  145. package/src/blocks/BlocksView.test.tsx +1101 -2
  146. package/src/blocks/BlocksView.tsx +5 -1
  147. package/src/blocks/blockRenderers.tsx +730 -22
  148. package/src/blocks/useChecklistState.ts +46 -0
  149. package/src/blocks/useResolvedImages.ts +16 -0
  150. package/src/bunSetup.ts +3 -0
  151. package/src/colorContrast.test.ts +27 -0
  152. package/src/colorContrast.ts +55 -0
  153. package/src/icons/OfflineIcon.tsx +2 -2
  154. package/src/icons/__snapshots__/OfflineIcon.test.tsx.snap +4 -4
  155. package/src/login/__snapshots__/LoginScreen.test.tsx.snap +7 -7
  156. package/src/signUp/__snapshots__/OAuthButtons.test.tsx.snap +3 -3
  157. package/src/signUp/__snapshots__/SignUpScreen.test.tsx.snap +7 -7
  158. package/src/table/TableDate.tsx +3 -1
  159. package/src/table/__snapshots__/Table.test.tsx.snap +5 -5
  160. package/src/table/__snapshots__/TableBadge.test.tsx.snap +3 -3
  161. package/src/table/__snapshots__/TableDate.test.tsx.snap +4 -0
  162. package/src/table/__snapshots__/TableHeader.test.tsx.snap +4 -4
  163. package/src/table/__snapshots__/TableHeaderCell.test.tsx.snap +7 -7
  164. package/src/table/__snapshots__/TableRow.test.tsx.snap +6 -6
@@ -1,5 +1,26 @@
1
- import type {Block, BlockAction, InlineDataset} from "@terreno/blocks";
1
+ import {
2
+ type Block,
3
+ type BlockAction,
4
+ type ButtonElement,
5
+ blockPlainText,
6
+ type ChecklistBlock,
7
+ type ChecklistItem,
8
+ type CopyAction,
9
+ checklistElementId,
10
+ type Dataset,
11
+ type DatasetColumn,
12
+ type GalleryBlock,
13
+ type GalleryImage,
14
+ type InlineDataset,
15
+ type ListBlock,
16
+ type ListItem,
17
+ type StepperBlock,
18
+ stepperElementIds,
19
+ type TableBlock,
20
+ } from "@terreno/blocks";
21
+ import * as Clipboard from "expo-clipboard";
2
22
  import type React from "react";
23
+ import {useCallback, useEffect, useRef, useState} from "react";
3
24
 
4
25
  import {Accordion} from "../Accordion";
5
26
  import {AreaChart} from "../AreaChart";
@@ -9,10 +30,13 @@ import {BarChart} from "../BarChart";
9
30
  import {Box} from "../Box";
10
31
  import {Button} from "../Button";
11
32
  import {Card} from "../Card";
33
+ import {CheckBox} from "../CheckBox";
34
+ import {type DataTableColumn, getSpacing, type LayoutChangeEvent} from "../Common";
12
35
  import {DataTable} from "../DataTable";
13
36
  import {DonutChart} from "../DonutChart";
14
37
  import {Heading} from "../Heading";
15
38
  import {HtmlFrame} from "../HtmlFrame";
39
+ import {IconButton} from "../IconButton";
16
40
  import {Image} from "../Image";
17
41
  import {LineChart} from "../LineChart";
18
42
  import {MarkdownView} from "../MarkdownView";
@@ -24,6 +48,8 @@ import {fileRefId} from "./useResolvedImages";
24
48
 
25
49
  export interface BlockRenderContext {
26
50
  allowHtml?: boolean;
51
+ /** The document's top-level blocks, where a copy action looks up its `target`. */
52
+ blocks?: readonly Block[];
27
53
  hostActions?: readonly string[];
28
54
  streaming?: boolean;
29
55
  loadingIds: ReadonlySet<string>;
@@ -35,6 +61,9 @@ export interface BlockRenderContext {
35
61
  resolvedImages?: Record<string, string | undefined>;
36
62
  /** Dataset name currently bound on a chart or table id, before any local selection. */
37
63
  boundData?: Record<string, string>;
64
+ /** Local ticks by checklist id, then item id, for checklists that tick on the device. */
65
+ checklistTicks?: Record<string, Record<string, boolean>>;
66
+ setChecklistTick?: (target: {checked: boolean; checklistId: string; itemId: string}) => void;
38
67
  selections: Record<string, string>;
39
68
  setSelection: (target: string, data: string) => void;
40
69
  }
@@ -66,6 +95,679 @@ const chartFor = {
66
95
  line: LineChart,
67
96
  } as const;
68
97
 
98
+ /** Drops float noise such as 0.30000000000000004 from value ± step. */
99
+ const stepValue = (value: number, delta: number): number => Number((value + delta).toFixed(10));
100
+
101
+ const formatAmount = ({
102
+ amount,
103
+ decimals,
104
+ unit,
105
+ }: {
106
+ amount: number;
107
+ decimals?: number;
108
+ unit?: string;
109
+ }): string => {
110
+ const formatted = amount.toFixed(decimals ?? 0);
111
+ return unit ? `${formatted} ${unit}` : formatted;
112
+ };
113
+
114
+ const renderStepper = (
115
+ block: StepperBlock,
116
+ path: string,
117
+ context: BlockRenderContext
118
+ ): React.ReactElement => {
119
+ const ids = stepperElementIds(block.id);
120
+ const step = block.step ?? 1;
121
+ const isPending =
122
+ context.pendingElementIds?.has(ids.decrease) === true ||
123
+ context.pendingElementIds?.has(ids.increase) === true;
124
+ const isUnregistered =
125
+ context.hostActions !== undefined && !context.hostActions.includes(block.callback.name);
126
+ const isLocked = isPending || isUnregistered;
127
+ const send = (elementId: string, value: number): void => {
128
+ context.onAction?.({
129
+ action: {
130
+ kind: "callback",
131
+ name: block.callback.name,
132
+ payload: {...(block.callback.payload ?? {}), value},
133
+ },
134
+ blockId: block.id,
135
+ elementId,
136
+ });
137
+ };
138
+ // A button that would step past a bound is off, since the server rejects a value outside it.
139
+ const decreaseDisabled = isLocked || stepValue(block.value, -step) < block.min;
140
+ const increaseDisabled = isLocked || stepValue(block.value, step) > block.max;
141
+ return (
142
+ <Box gap={3} key={path} testID={path}>
143
+ <Text color="secondaryLight" size="sm">
144
+ {block.label}
145
+ </Text>
146
+ <Box alignItems="center" direction="row" gap={4}>
147
+ <IconButton
148
+ accessibilityLabel={`Decrease ${block.label}`}
149
+ disabled={decreaseDisabled}
150
+ iconName="minus"
151
+ onClick={() => {
152
+ if (decreaseDisabled) {
153
+ return;
154
+ }
155
+ send(ids.decrease, stepValue(block.value, -step));
156
+ }}
157
+ testID={`${path}-${ids.decrease}`}
158
+ variant="secondary"
159
+ />
160
+ <Box alignItems="center">
161
+ <Text bold size="xl" testID={`${path}-value`}>
162
+ {String(block.value)}
163
+ </Text>
164
+ {block.unit ? (
165
+ <Text color="secondaryLight" size="sm">
166
+ {block.unit}
167
+ </Text>
168
+ ) : null}
169
+ </Box>
170
+ <IconButton
171
+ accessibilityLabel={`Increase ${block.label}`}
172
+ disabled={increaseDisabled}
173
+ iconName="plus"
174
+ onClick={() => {
175
+ if (increaseDisabled) {
176
+ return;
177
+ }
178
+ send(ids.increase, stepValue(block.value, step));
179
+ }}
180
+ testID={`${path}-${ids.increase}`}
181
+ variant="secondary"
182
+ />
183
+ </Box>
184
+ {block.itemsTitle ? <Heading size="sm">{block.itemsTitle}</Heading> : null}
185
+ {block.items && block.items.length > 0 ? (
186
+ <Box gap={1} testID={`${path}-items`}>
187
+ {block.items.map((item, index) => (
188
+ <Box direction="row" gap={2} justifyContent="between" key={`${path}-item-${index}`}>
189
+ <Text>{item.label}</Text>
190
+ <Text bold>{formatAmount(item)}</Text>
191
+ </Box>
192
+ ))}
193
+ </Box>
194
+ ) : null}
195
+ {block.note ? (
196
+ <Text color="secondaryLight" size="sm">
197
+ {block.note}
198
+ </Text>
199
+ ) : null}
200
+ </Box>
201
+ );
202
+ };
203
+
204
+ /** One row: the tick, then the bold text, the small muted meta (such as a time), and the detail. */
205
+ const ChecklistRowContent: React.FC<{
206
+ isChecked: boolean;
207
+ item: ChecklistItem;
208
+ itemPath: string;
209
+ }> = ({isChecked, item, itemPath}) => (
210
+ <>
211
+ <Box paddingY={1}>
212
+ <CheckBox selected={isChecked} testID={`${itemPath}-checkbox`} />
213
+ </Box>
214
+ <Box flex="grow" gap={1}>
215
+ <Text bold testID={`${itemPath}-text`}>
216
+ {item.text}
217
+ </Text>
218
+ {item.meta ? (
219
+ <Text color="secondaryLight" size="sm" testID={`${itemPath}-meta`}>
220
+ {item.meta}
221
+ </Text>
222
+ ) : null}
223
+ {item.detail ? (
224
+ <Text color="secondaryLight" size="sm" testID={`${itemPath}-detail`}>
225
+ {item.detail}
226
+ </Text>
227
+ ) : null}
228
+ </Box>
229
+ </>
230
+ );
231
+
232
+ /**
233
+ * Ticks go to the host when the checklist names a callback, the host listens with `onAction`,
234
+ * and `hostActions` is omitted (as for the stepper) or lists that callback. Otherwise they stay
235
+ * on the device.
236
+ */
237
+ const isChecklistCallbackMode = (block: ChecklistBlock, context: BlockRenderContext): boolean =>
238
+ block.callback !== undefined &&
239
+ context.onAction !== undefined &&
240
+ (context.hostActions === undefined || context.hostActions.includes(block.callback.name));
241
+
242
+ const renderChecklist = (
243
+ block: ChecklistBlock,
244
+ path: string,
245
+ context: BlockRenderContext
246
+ ): React.ReactElement => {
247
+ const isCallbackMode = isChecklistCallbackMode(block, context);
248
+ // One pending tick locks every item, so a second tick cannot send a `state` that misses it.
249
+ const isAnyPending = block.items.some(
250
+ (item) => context.pendingElementIds?.has(checklistElementId(block.id, item.id)) === true
251
+ );
252
+ const localTicks = isCallbackMode ? undefined : context.checklistTicks?.[block.id];
253
+ const state: Record<string, boolean> = {};
254
+ for (const item of block.items) {
255
+ state[item.id] = localTicks?.[item.id] ?? item.checked === true;
256
+ }
257
+ const checkedCount = Object.values(state).filter(Boolean).length;
258
+ const toggle = (item: ChecklistItem, elementId: string): void => {
259
+ const checked = !state[item.id];
260
+ if (!isCallbackMode || block.callback === undefined) {
261
+ context.setChecklistTick?.({checked, checklistId: block.id, itemId: item.id});
262
+ return;
263
+ }
264
+ context.onAction?.({
265
+ action: {
266
+ kind: "callback",
267
+ name: block.callback.name,
268
+ payload: {
269
+ ...(block.callback.payload ?? {}),
270
+ checked,
271
+ itemId: item.id,
272
+ state: {...state, [item.id]: checked},
273
+ },
274
+ },
275
+ blockId: block.id,
276
+ elementId,
277
+ });
278
+ };
279
+ return (
280
+ <Box gap={3} key={path} testID={path}>
281
+ <Box alignItems="center" direction="row" gap={2} justifyContent="between">
282
+ <Box flex="shrink">{block.title ? <Heading size="sm">{block.title}</Heading> : null}</Box>
283
+ <Text color="secondaryLight" size="sm" testID={`${path}-counter`}>
284
+ {`${checkedCount} of ${block.items.length}`}
285
+ </Text>
286
+ </Box>
287
+ <Box gap={2}>
288
+ {block.items.map((item) => {
289
+ const elementId = checklistElementId(block.id, item.id);
290
+ const itemPath = `${path}-${elementId}`;
291
+ const isChecked = state[item.id] === true;
292
+ const isLocked = isCallbackMode
293
+ ? isAnyPending
294
+ : context.pendingElementIds?.has(elementId) === true;
295
+ return (
296
+ <Box key={itemPath} testID={itemPath}>
297
+ <Box
298
+ accessibilityHint={
299
+ isChecked ? "Marks this item as not done" : "Marks this item as done"
300
+ }
301
+ accessibilityLabel={item.text}
302
+ accessibilityRole="checkbox"
303
+ accessibilityState={{checked: isChecked, disabled: isLocked}}
304
+ direction="row"
305
+ gap={3}
306
+ onClick={() => {
307
+ if (isLocked) {
308
+ return;
309
+ }
310
+ toggle(item, elementId);
311
+ }}
312
+ testID={`${itemPath}-row`}
313
+ {...(isLocked ? {dangerouslySetInlineStyle: {__style: {opacity: 0.5}}} : {})}
314
+ >
315
+ <ChecklistRowContent isChecked={isChecked} item={item} itemPath={itemPath} />
316
+ </Box>
317
+ </Box>
318
+ );
319
+ })}
320
+ </Box>
321
+ </Box>
322
+ );
323
+ };
324
+
325
+ /** Column width before the first layout pass reports the container width. */
326
+ const TABLE_DEFAULT_COLUMN_WIDTH = 120;
327
+ /** Narrowest column; past this the table scrolls sideways. */
328
+ const TABLE_MIN_COLUMN_WIDTH = 96;
329
+
330
+ const DATASET_TO_COLUMN_TYPE: Record<DatasetColumn["type"], DataTableColumn["columnType"]> = {
331
+ date: "date",
332
+ number: "number",
333
+ string: "text",
334
+ };
335
+
336
+ const tableColumnWidth = ({
337
+ columnCount,
338
+ containerWidth,
339
+ }: {
340
+ columnCount: number;
341
+ containerWidth?: number;
342
+ }): number => {
343
+ if (containerWidth === undefined || columnCount === 0) {
344
+ return TABLE_DEFAULT_COLUMN_WIDTH;
345
+ }
346
+ return Math.max(TABLE_MIN_COLUMN_WIDTH, Math.floor(containerWidth / columnCount));
347
+ };
348
+
349
+ /** DataTable's default header and row height. */
350
+ const TABLE_ROW_HEIGHT = 54;
351
+ /** Rows a table block shows before its body scrolls. */
352
+ const TABLE_MAX_VISIBLE_ROWS = 10;
353
+ /** Top and bottom border of the DataTable frame. */
354
+ const TABLE_BORDER_HEIGHT = 2;
355
+
356
+ /**
357
+ * DataTable fills its parent's height, so the block gives it a frame sized to the header plus its
358
+ * rows; otherwise the table also counts the title and spills over the next block.
359
+ */
360
+ const tableFrameHeight = (rowCount: number): number =>
361
+ (Math.min(rowCount, TABLE_MAX_VISIBLE_ROWS) + 1) * TABLE_ROW_HEIGHT + TABLE_BORDER_HEIGHT;
362
+
363
+ const TableBlockView: React.FC<{
364
+ block: TableBlock;
365
+ context: BlockRenderContext;
366
+ path: string;
367
+ }> = ({block, context, path}) => {
368
+ const [containerWidth, setContainerWidth] = useState<number | undefined>(undefined);
369
+ const handleLayout = useCallback((event: LayoutChangeEvent): void => {
370
+ const {width} = event.nativeEvent.layout;
371
+ setContainerWidth((previous) => (previous === width ? previous : width));
372
+ }, []);
373
+ const dataName =
374
+ (block.id !== undefined ? context.selections[block.id] : undefined) ?? block.data;
375
+ const dataset = dataName === undefined ? undefined : context.resolved[dataName];
376
+ const names = block.columns ?? dataset?.columns.map((column) => column.name) ?? [];
377
+ const indexes = names.map(
378
+ (name) => dataset?.columns.findIndex((column) => column.name === name) ?? -1
379
+ );
380
+ const width = tableColumnWidth({columnCount: names.length, containerWidth});
381
+ const columns: DataTableColumn[] = names.map((name, position) => {
382
+ const datasetType = dataset?.columns[indexes[position] ?? -1]?.type;
383
+ return {
384
+ columnType: datasetType === undefined ? "text" : DATASET_TO_COLUMN_TYPE[datasetType],
385
+ title: name.replaceAll("_", " "),
386
+ width,
387
+ };
388
+ });
389
+ return (
390
+ <Box gap={2} onLayout={handleLayout} testID={path}>
391
+ {block.title ? <Heading size="sm">{block.title}</Heading> : null}
392
+ <Box height={tableFrameHeight(dataset?.rows.length ?? 0)} testID={`${path}-table-frame`}>
393
+ <DataTable
394
+ columns={columns}
395
+ data={(dataset?.rows ?? []).map((row) =>
396
+ indexes.map((index) => ({value: index < 0 ? "" : row[index]}))
397
+ )}
398
+ testID={`${path}-table`}
399
+ />
400
+ </Box>
401
+ </Box>
402
+ );
403
+ };
404
+
405
+ /** Most tiles in one gallery row; more images wrap into further rows. */
406
+ const GALLERY_COLUMNS = 3;
407
+ /** Narrowest tile, and the tile width before the first layout pass; past this the row scrolls sideways. */
408
+ const GALLERY_MIN_TILE_WIDTH = 160;
409
+ /** Box `gap` step between tiles. */
410
+ const GALLERY_GAP = 2;
411
+
412
+ interface GalleryLayout {
413
+ isScrolling: boolean;
414
+ rows: GalleryImage[][];
415
+ tileHeight: number;
416
+ tileWidth: number;
417
+ }
418
+
419
+ const chunk = <T,>(items: readonly T[], size: number): T[][] => {
420
+ const rows: T[][] = [];
421
+ for (let start = 0; start < items.length; start += size) {
422
+ rows.push(items.slice(start, start + size));
423
+ }
424
+ return rows;
425
+ };
426
+
427
+ const galleryLayout = ({
428
+ containerWidth,
429
+ images,
430
+ }: {
431
+ containerWidth?: number;
432
+ images: readonly GalleryImage[];
433
+ }): GalleryLayout => {
434
+ const columns = Math.min(GALLERY_COLUMNS, images.length);
435
+ const gap = getSpacing(GALLERY_GAP);
436
+ const fitWidth =
437
+ containerWidth === undefined || columns === 0
438
+ ? GALLERY_MIN_TILE_WIDTH
439
+ : Math.floor((containerWidth - gap * (columns - 1)) / columns);
440
+ const isScrolling = fitWidth < GALLERY_MIN_TILE_WIDTH;
441
+ const tileWidth = isScrolling ? GALLERY_MIN_TILE_WIDTH : fitWidth;
442
+ return {
443
+ isScrolling,
444
+ rows: isScrolling ? [[...images]] : chunk(images, GALLERY_COLUMNS),
445
+ tileHeight: Math.round((tileWidth * 3) / 4),
446
+ tileWidth,
447
+ };
448
+ };
449
+
450
+ const GalleryTile: React.FC<{
451
+ height: number;
452
+ image: GalleryImage;
453
+ testID: string;
454
+ url?: string;
455
+ width: number;
456
+ }> = ({height, image, testID, url, width}) => (
457
+ <Box gap={1} testID={testID} width={width}>
458
+ {url ? (
459
+ <Box overflow="hidden" rounding="md" testID={`${testID}-image`}>
460
+ <Image
461
+ alt={image.alt}
462
+ color="transparent"
463
+ fit="cover"
464
+ naturalWidth={width}
465
+ src={url}
466
+ style={{height, width}}
467
+ />
468
+ </Box>
469
+ ) : (
470
+ <Box
471
+ accessibilityLabel={image.alt}
472
+ alignItems="center"
473
+ color="neutralLight"
474
+ height={height}
475
+ justifyContent="center"
476
+ padding={2}
477
+ rounding="md"
478
+ testID={`${testID}-placeholder`}
479
+ >
480
+ <Text align="center" color="secondaryLight" size="sm">
481
+ {image.alt}
482
+ </Text>
483
+ </Box>
484
+ )}
485
+ {image.caption ? (
486
+ <Text color="secondaryLight" size="sm" testID={`${testID}-caption`}>
487
+ {image.caption}
488
+ </Text>
489
+ ) : null}
490
+ </Box>
491
+ );
492
+
493
+ /**
494
+ * Up to three tiles share one row; more wrap into a three-column grid. When the measured width
495
+ * would make a tile narrower than GALLERY_MIN_TILE_WIDTH, every tile sits in one row that
496
+ * scrolls sideways instead.
497
+ */
498
+ const GalleryBlockView: React.FC<{
499
+ block: GalleryBlock;
500
+ context: BlockRenderContext;
501
+ path: string;
502
+ }> = ({block, context, path}) => {
503
+ const [containerWidth, setContainerWidth] = useState<number | undefined>(undefined);
504
+ const handleLayout = useCallback((event: LayoutChangeEvent): void => {
505
+ const {width} = event.nativeEvent.layout;
506
+ setContainerWidth((previous) => (previous === width ? previous : width));
507
+ }, []);
508
+ const layout = galleryLayout({containerWidth, images: block.images});
509
+ const indexOf = new Map(block.images.map((image, index) => [image, index]));
510
+ const renderTile = (image: GalleryImage): React.ReactElement => {
511
+ const index = indexOf.get(image) ?? 0;
512
+ const fileId = fileRefId(image.src);
513
+ const url = fileId === undefined ? image.src : context.resolvedImages?.[fileId];
514
+ return (
515
+ <GalleryTile
516
+ height={layout.tileHeight}
517
+ image={image}
518
+ key={`${path}-image-${index}`}
519
+ testID={`${path}-image-${index}`}
520
+ url={url}
521
+ width={layout.tileWidth}
522
+ />
523
+ );
524
+ };
525
+ return (
526
+ <Box onLayout={handleLayout} testID={path}>
527
+ {layout.isScrolling ? (
528
+ <Box direction="row" gap={GALLERY_GAP} overflow="scrollX" scroll testID={`${path}-scroll`}>
529
+ {block.images.map(renderTile)}
530
+ </Box>
531
+ ) : (
532
+ <Box gap={GALLERY_GAP}>
533
+ {layout.rows.map((row, rowIndex) => (
534
+ <Box
535
+ direction="row"
536
+ gap={GALLERY_GAP}
537
+ key={`${path}-row-${rowIndex}`}
538
+ testID={`${path}-row-${rowIndex}`}
539
+ >
540
+ {row.map(renderTile)}
541
+ </Box>
542
+ ))}
543
+ </Box>
544
+ )}
545
+ </Box>
546
+ );
547
+ };
548
+
549
+ /** Fixed thumbnail width; also the gutter an imageless item keeps when another item has a thumbnail. */
550
+ const LIST_THUMBNAIL_WIDTH = 112;
551
+ /** 3:4 portrait: 112 * 4 / 3 = 149.3, rounded. */
552
+ const LIST_THUMBNAIL_HEIGHT = Math.round((LIST_THUMBNAIL_WIDTH * 4) / 3);
553
+
554
+ const ListThumbnail: React.FC<{
555
+ image: NonNullable<ListItem["image"]>;
556
+ testID: string;
557
+ url?: string;
558
+ }> = ({image, testID, url}) => {
559
+ if (!url) {
560
+ return (
561
+ <Box
562
+ accessibilityLabel={image.alt}
563
+ alignItems="center"
564
+ color="neutralLight"
565
+ height={LIST_THUMBNAIL_HEIGHT}
566
+ justifyContent="center"
567
+ padding={2}
568
+ rounding="md"
569
+ testID={`${testID}-placeholder`}
570
+ width={LIST_THUMBNAIL_WIDTH}
571
+ >
572
+ <Text align="center" color="secondaryLight" size="sm">
573
+ {image.alt}
574
+ </Text>
575
+ </Box>
576
+ );
577
+ }
578
+ return (
579
+ <Box overflow="hidden" rounding="md" testID={`${testID}-image`}>
580
+ <Image
581
+ alt={image.alt}
582
+ color="transparent"
583
+ fit="cover"
584
+ naturalWidth={LIST_THUMBNAIL_WIDTH}
585
+ src={url}
586
+ style={{height: LIST_THUMBNAIL_HEIGHT, width: LIST_THUMBNAIL_WIDTH}}
587
+ />
588
+ </Box>
589
+ );
590
+ };
591
+
592
+ /**
593
+ * One row per item: a 3:4 thumbnail on the left, then the small muted meta, the bold title,
594
+ * and the muted plain text. When any item has a thumbnail, an item without one keeps an empty
595
+ * gutter of the same width so every title starts at the same x.
596
+ */
597
+ const renderList = (
598
+ block: ListBlock,
599
+ path: string,
600
+ context: BlockRenderContext
601
+ ): React.ReactElement => {
602
+ const hasThumbnails = block.items.some((item) => item.image !== undefined);
603
+ return (
604
+ <Box gap={3} key={path} testID={path}>
605
+ {block.items.map((item, index) => {
606
+ const itemPath = `${path}-item-${index}`;
607
+ const fileId = item.image === undefined ? undefined : fileRefId(item.image.src);
608
+ const url = fileId === undefined ? item.image?.src : context.resolvedImages?.[fileId];
609
+ return (
610
+ <Box direction="row" gap={3} key={itemPath} testID={itemPath}>
611
+ {item.image !== undefined ? (
612
+ <ListThumbnail image={item.image} testID={itemPath} url={url} />
613
+ ) : hasThumbnails ? (
614
+ <Box testID={`${itemPath}-gutter`} width={LIST_THUMBNAIL_WIDTH} />
615
+ ) : null}
616
+ <Box flex="grow" gap={1} testID={`${itemPath}-body`}>
617
+ {item.meta ? (
618
+ <Text color="secondaryLight" size="sm" testID={`${itemPath}-meta`}>
619
+ {item.meta}
620
+ </Text>
621
+ ) : null}
622
+ <Text bold testID={`${itemPath}-title`}>
623
+ {item.title}
624
+ </Text>
625
+ {item.text ? (
626
+ <Text color="secondaryLight" testID={`${itemPath}-text`}>
627
+ {item.text}
628
+ </Text>
629
+ ) : null}
630
+ </Box>
631
+ </Box>
632
+ );
633
+ })}
634
+ </Box>
635
+ );
636
+ };
637
+
638
+ /** How long "Copied" or "Couldn't copy" stays next to a copy button. */
639
+ const COPY_STATUS_MS = 2000;
640
+
641
+ const COPY_STATUS_TEXT = {
642
+ copied: "Copied",
643
+ failed: "Couldn't copy",
644
+ } as const;
645
+
646
+ /** Finds a block by id as it is shown now, with any override applied. */
647
+ const findShownBlock = (
648
+ blocks: readonly Block[],
649
+ id: string,
650
+ overrides: Record<string, Block> | undefined
651
+ ): Block | undefined => {
652
+ for (const original of blocks) {
653
+ const shown =
654
+ original.id !== undefined && overrides?.[original.id] !== undefined
655
+ ? overrides[original.id]
656
+ : original;
657
+ if (original.id === id || shown.id === id) {
658
+ return shown;
659
+ }
660
+ if (shown.type === "columns" || shown.type === "card") {
661
+ const found = findShownBlock(shown.children, id, overrides);
662
+ if (found !== undefined) {
663
+ return found;
664
+ }
665
+ }
666
+ }
667
+ return undefined;
668
+ };
669
+
670
+ /**
671
+ * The text a copy action writes: its literal `text`, or its target block as shown now (override,
672
+ * local checklist ticks, and the selected, resolved table dataset applied).
673
+ */
674
+ const copyActionText = (action: CopyAction, context: BlockRenderContext): string => {
675
+ if (action.text !== undefined) {
676
+ return action.text;
677
+ }
678
+ if (action.target === undefined) {
679
+ return "";
680
+ }
681
+ const block = findShownBlock(context.blocks ?? [], action.target, context.overrides);
682
+ if (block === undefined) {
683
+ return "";
684
+ }
685
+ if (block.type === "checklist") {
686
+ const checked = isChecklistCallbackMode(block, context)
687
+ ? undefined
688
+ : context.checklistTicks?.[block.id];
689
+ return blockPlainText(block, {checked});
690
+ }
691
+ if (block.type === "table") {
692
+ const dataName =
693
+ (block.id !== undefined ? context.selections[block.id] : undefined) ?? block.data;
694
+ const dataset = context.resolved[dataName];
695
+ const datasets: Record<string, Dataset> = dataset === undefined ? {} : {[dataName]: dataset};
696
+ return blockPlainText({...block, data: dataName}, {datasets});
697
+ }
698
+ return blockPlainText(block);
699
+ };
700
+
701
+ type CopyStatus = keyof typeof COPY_STATUS_TEXT;
702
+
703
+ /** Writes the text and reports the outcome; empty text counts as a failed copy. */
704
+ const writeClipboard = async (text: string): Promise<CopyStatus> => {
705
+ if (text === "") {
706
+ console.warn("Failed to copy block text: the copy target has no text yet");
707
+ return "failed";
708
+ }
709
+ try {
710
+ await Clipboard.setStringAsync(text);
711
+ return "copied";
712
+ } catch (error) {
713
+ console.warn("Failed to copy block text", error);
714
+ return "failed";
715
+ }
716
+ };
717
+
718
+ /**
719
+ * Copies on the device and never calls `onAction`. The status sits in a polite live region that
720
+ * stays mounted, so screen readers announce "Copied" when the text appears.
721
+ */
722
+ const CopyButton: React.FC<{
723
+ action: CopyAction;
724
+ context: BlockRenderContext;
725
+ testID: string;
726
+ text: string;
727
+ variant: ButtonElement["variant"];
728
+ }> = ({action, context, testID, text, variant}) => {
729
+ // An object, so a repeat copy with the same status still restarts the clear timer.
730
+ const [feedback, setFeedback] = useState<{status: CopyStatus} | undefined>(undefined);
731
+
732
+ // Clears the status 2 s after each copy; a new copy or unmounting cancels the pending clear.
733
+ useEffect(() => {
734
+ if (feedback === undefined) {
735
+ return;
736
+ }
737
+ const timer = setTimeout(() => setFeedback(undefined), COPY_STATUS_MS);
738
+ return (): void => clearTimeout(timer);
739
+ }, [feedback]);
740
+
741
+ // BlocksView rebuilds the context every render, so the press reads the latest one from a ref
742
+ // and the callback keeps one identity.
743
+ const latest = useRef({action, context});
744
+ latest.current = {action, context};
745
+
746
+ const handleCopy = useCallback(async (): Promise<void> => {
747
+ const status = await writeClipboard(
748
+ copyActionText(latest.current.action, latest.current.context)
749
+ );
750
+ setFeedback({status});
751
+ }, []);
752
+
753
+ return (
754
+ <Box alignItems="center" direction="row" gap={2}>
755
+ <Button onClick={handleCopy} testID={testID} text={text} variant={variant ?? "primary"} />
756
+ <Box accessibilityLiveRegion="polite" testID={`${testID}-status`}>
757
+ {feedback ? (
758
+ <Text
759
+ color={feedback.status === "failed" ? "error" : "secondaryLight"}
760
+ size="sm"
761
+ testID={`${testID}-status-text`}
762
+ >
763
+ {COPY_STATUS_TEXT[feedback.status]}
764
+ </Text>
765
+ ) : null}
766
+ </Box>
767
+ </Box>
768
+ );
769
+ };
770
+
69
771
  /** Renders one catalog block with @terreno/ui components. */
70
772
  export const renderBlock = (
71
773
  block: Block,
@@ -169,6 +871,11 @@ export const renderBlock = (
169
871
  case "card":
170
872
  return (
171
873
  <Card key={path} testID={path}>
874
+ {block.eyebrow ? (
875
+ <Text color="secondaryLight" size="sm" testID={`${path}-eyebrow`}>
876
+ {block.eyebrow}
877
+ </Text>
878
+ ) : null}
172
879
  {block.title ? <Heading size="sm">{block.title}</Heading> : null}
173
880
  {renderChildren(block.children, path, context)}
174
881
  </Card>
@@ -198,27 +905,16 @@ export const renderBlock = (
198
905
  </Box>
199
906
  );
200
907
  }
201
- case "table": {
202
- const dataName =
203
- (block.id !== undefined ? context.selections[block.id] : undefined) ?? block.data;
204
- const dataset = dataName === undefined ? undefined : context.resolved[dataName];
205
- const names = block.columns ?? dataset?.columns.map((column) => column.name) ?? [];
206
- const indexes = names.map(
207
- (name) => dataset?.columns.findIndex((column) => column.name === name) ?? -1
208
- );
209
- return (
210
- <Box gap={2} key={path} testID={path}>
211
- {block.title ? <Heading size="sm">{block.title}</Heading> : null}
212
- <DataTable
213
- columns={names.map((name) => ({columnType: "text", title: name, width: 120}))}
214
- data={(dataset?.rows ?? []).map((row) =>
215
- indexes.map((index) => ({value: index < 0 ? "" : row[index]}))
216
- )}
217
- testID={`${path}-table`}
218
- />
219
- </Box>
220
- );
221
- }
908
+ case "table":
909
+ return <TableBlockView block={block} context={context} key={path} path={path} />;
910
+ case "gallery":
911
+ return <GalleryBlockView block={block} context={context} key={path} path={path} />;
912
+ case "list":
913
+ return renderList(block, path, context);
914
+ case "stepper":
915
+ return renderStepper(block, path, context);
916
+ case "checklist":
917
+ return renderChecklist(block, path, context);
222
918
  case "actions":
223
919
  return (
224
920
  <Box direction="row" gap={2} key={path} testID={path} wrap>
@@ -252,6 +948,18 @@ export const renderBlock = (
252
948
  );
253
949
  }
254
950
  const action = element.action;
951
+ if (action.kind === "copy") {
952
+ return (
953
+ <CopyButton
954
+ action={action}
955
+ context={context}
956
+ key={element.id}
957
+ testID={`${path}-${element.id}`}
958
+ text={element.text}
959
+ variant={element.variant}
960
+ />
961
+ );
962
+ }
255
963
  const disabled =
256
964
  action.kind === "callback" &&
257
965
  context.hostActions !== undefined &&