@terreno/ui 57.8.0 → 57.9.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 (134) hide show
  1. package/dist/AreaChart.d.ts +1 -1
  2. package/dist/AreaChart.d.ts.map +1 -1
  3. package/dist/AreaChart.js +70 -22
  4. package/dist/AreaChart.js.map +1 -1
  5. package/dist/BarChart.d.ts.map +1 -1
  6. package/dist/BarChart.js +55 -13
  7. package/dist/BarChart.js.map +1 -1
  8. package/dist/ChartCard.d.ts +5 -0
  9. package/dist/ChartCard.d.ts.map +1 -0
  10. package/dist/ChartCard.js +18 -0
  11. package/dist/ChartCard.js.map +1 -0
  12. package/dist/Common.d.ts +79 -5
  13. package/dist/Common.d.ts.map +1 -1
  14. package/dist/Common.js.map +1 -1
  15. package/dist/DashboardGrid.d.ts +6 -2
  16. package/dist/DashboardGrid.d.ts.map +1 -1
  17. package/dist/DashboardGrid.js +43 -10
  18. package/dist/DashboardGrid.js.map +1 -1
  19. package/dist/DonutChart.d.ts.map +1 -1
  20. package/dist/DonutChart.js +20 -10
  21. package/dist/DonutChart.js.map +1 -1
  22. package/dist/DropdownMenuItem.d.ts +16 -0
  23. package/dist/DropdownMenuItem.d.ts.map +1 -0
  24. package/dist/DropdownMenuItem.js +11 -0
  25. package/dist/DropdownMenuItem.js.map +1 -0
  26. package/dist/FilePickerButton.d.ts.map +1 -1
  27. package/dist/FilePickerButton.js +70 -10
  28. package/dist/FilePickerButton.js.map +1 -1
  29. package/dist/GPTChat.d.ts.map +1 -1
  30. package/dist/GPTChat.js +103 -14
  31. package/dist/GPTChat.js.map +1 -1
  32. package/dist/LineChart.d.ts +1 -1
  33. package/dist/LineChart.d.ts.map +1 -1
  34. package/dist/LineChart.js +62 -21
  35. package/dist/LineChart.js.map +1 -1
  36. package/dist/Scorecard.d.ts +5 -0
  37. package/dist/Scorecard.d.ts.map +1 -0
  38. package/dist/Scorecard.js +11 -0
  39. package/dist/Scorecard.js.map +1 -0
  40. package/dist/SparklineChart.d.ts +5 -0
  41. package/dist/SparklineChart.d.ts.map +1 -0
  42. package/dist/SparklineChart.js +35 -0
  43. package/dist/SparklineChart.js.map +1 -0
  44. package/dist/TextField.d.ts.map +1 -1
  45. package/dist/TextField.js +12 -4
  46. package/dist/TextField.js.map +1 -1
  47. package/dist/ToastNotifications.d.ts.map +1 -1
  48. package/dist/ToastNotifications.js +29 -6
  49. package/dist/ToastNotifications.js.map +1 -1
  50. package/dist/WebDropdownMenu.d.ts +2 -1
  51. package/dist/WebDropdownMenu.d.ts.map +1 -1
  52. package/dist/WebDropdownMenu.js +2 -6
  53. package/dist/WebDropdownMenu.js.map +1 -1
  54. package/dist/charts/ChartFacadeContainer.d.ts +10 -0
  55. package/dist/charts/ChartFacadeContainer.d.ts.map +1 -0
  56. package/dist/charts/ChartFacadeContainer.js +10 -0
  57. package/dist/charts/ChartFacadeContainer.js.map +1 -0
  58. package/dist/charts/ChartFrame.d.ts +4 -0
  59. package/dist/charts/ChartFrame.d.ts.map +1 -1
  60. package/dist/charts/ChartFrame.js +4 -2
  61. package/dist/charts/ChartFrame.js.map +1 -1
  62. package/dist/charts/layout.d.ts +9 -2
  63. package/dist/charts/layout.d.ts.map +1 -1
  64. package/dist/charts/layout.js +28 -6
  65. package/dist/charts/layout.js.map +1 -1
  66. package/dist/charts/scales.d.ts +3 -1
  67. package/dist/charts/scales.d.ts.map +1 -1
  68. package/dist/charts/scales.js +7 -7
  69. package/dist/charts/scales.js.map +1 -1
  70. package/dist/charts/series.d.ts +15 -0
  71. package/dist/charts/series.d.ts.map +1 -0
  72. package/dist/charts/series.js +19 -0
  73. package/dist/charts/series.js.map +1 -0
  74. package/dist/charts/types/chartTypes.d.ts +7 -0
  75. package/dist/charts/types/chartTypes.d.ts.map +1 -1
  76. package/dist/dashboardGridLayout.d.ts +6 -0
  77. package/dist/dashboardGridLayout.d.ts.map +1 -1
  78. package/dist/dashboardGridLayout.js +19 -3
  79. package/dist/dashboardGridLayout.js.map +1 -1
  80. package/dist/index.d.ts +4 -1
  81. package/dist/index.d.ts.map +1 -1
  82. package/dist/index.js +1 -1
  83. package/dist/index.js.map +1 -1
  84. package/dist/lazyBoundaries/heavyOptionalExports.d.ts +9 -0
  85. package/dist/lazyBoundaries/heavyOptionalExports.d.ts.map +1 -1
  86. package/dist/lazyBoundaries/heavyOptionalExports.js +6 -0
  87. package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -1
  88. package/dist/resolveDocumentBodyPortalTarget.d.ts +7 -0
  89. package/dist/resolveDocumentBodyPortalTarget.d.ts.map +1 -0
  90. package/dist/resolveDocumentBodyPortalTarget.js +12 -0
  91. package/dist/resolveDocumentBodyPortalTarget.js.map +1 -0
  92. package/package.json +1 -1
  93. package/src/AIRequestExplorer.test.tsx +2 -2
  94. package/src/AreaChart.test.tsx +81 -0
  95. package/src/AreaChart.tsx +171 -62
  96. package/src/BarChart.test.tsx +119 -0
  97. package/src/BarChart.tsx +94 -20
  98. package/src/ChartCard.test.tsx +79 -0
  99. package/src/ChartCard.tsx +80 -0
  100. package/src/Common.ts +69 -2
  101. package/src/DashboardGrid.test.tsx +98 -1
  102. package/src/DashboardGrid.tsx +71 -20
  103. package/src/DonutChart.test.tsx +73 -0
  104. package/src/DonutChart.tsx +87 -14
  105. package/src/DropdownMenuItem.tsx +48 -0
  106. package/src/FilePickerButton.test.tsx +60 -6
  107. package/src/FilePickerButton.tsx +116 -28
  108. package/src/GPTChat.test.tsx +133 -6
  109. package/src/GPTChat.tsx +196 -38
  110. package/src/LineChart.test.tsx +104 -0
  111. package/src/LineChart.tsx +154 -58
  112. package/src/Scorecard.test.tsx +65 -0
  113. package/src/Scorecard.tsx +45 -0
  114. package/src/SparklineChart.test.tsx +69 -0
  115. package/src/SparklineChart.tsx +85 -0
  116. package/src/TextField.test.tsx +16 -0
  117. package/src/TextField.tsx +13 -3
  118. package/src/ToastNotifications.tsx +33 -6
  119. package/src/ToastNotifications.webModal.test.tsx +285 -0
  120. package/src/WebDropdownMenu.tsx +3 -7
  121. package/src/bunSetup.ts +1 -0
  122. package/src/charts/ChartFacadeContainer.tsx +35 -0
  123. package/src/charts/ChartFrame.tsx +39 -1
  124. package/src/charts/layout.test.ts +22 -1
  125. package/src/charts/layout.ts +37 -4
  126. package/src/charts/scales.test.ts +33 -0
  127. package/src/charts/scales.ts +10 -6
  128. package/src/charts/series.ts +43 -0
  129. package/src/charts/types/chartTypes.ts +8 -0
  130. package/src/dashboardGridLayout.test.ts +59 -1
  131. package/src/dashboardGridLayout.ts +30 -3
  132. package/src/index.tsx +6 -0
  133. package/src/lazyBoundaries/heavyOptionalExports.tsx +21 -0
  134. package/src/resolveDocumentBodyPortalTarget.ts +11 -0
@@ -57,6 +57,8 @@ import {
57
57
  type ViewStyle,
58
58
  } from "react-native";
59
59
  import type {ToastProps as TerrenoToastPayload} from "./Common";
60
+ import {createWebPortal} from "./createWebPortal";
61
+ import {resolveDocumentBodyPortalTarget} from "./resolveDocumentBodyPortalTarget";
60
62
 
61
63
  // ============================================================================
62
64
  // useDimensions hook
@@ -451,7 +453,16 @@ const toastStyles = StyleSheet.create({
451
453
  // Toast Container
452
454
  // ============================================================================
453
455
 
454
- const {height, width} = Dimensions.get("window");
456
+ const {height: windowHeight, width: windowWidth} = Dimensions.get("window");
457
+
458
+ // Static web export renders in Node, where Dimensions reports 0x0 and that size is baked into
459
+ // the HTML. The web container is position:fixed, so percentages track the viewport instead.
460
+ const getContainerSize = (): {height: ViewStyle["height"]; width: ViewStyle["width"]} => {
461
+ if (Platform.OS === "web") {
462
+ return {height: "100%", width: "100%"};
463
+ }
464
+ return {height: windowHeight, width: windowWidth};
465
+ };
455
466
 
456
467
  export interface ToastContainerProps extends ToastOptions {
457
468
  renderToast?(toast: ToastProps): ReactElement;
@@ -634,7 +645,7 @@ const ToastContainer = forwardRef<ToastContainerRef, ToastContainerProps>((props
634
645
  bottom: offsetBottom || offset,
635
646
  flexDirection: "column",
636
647
  justifyContent: "flex-end",
637
- width: width,
648
+ width: getContainerSize().width,
638
649
  };
639
650
  return (
640
651
  <KeyboardAvoidingView
@@ -657,7 +668,7 @@ const ToastContainer = forwardRef<ToastContainerRef, ToastContainerProps>((props
657
668
  flexDirection: "column-reverse",
658
669
  justifyContent: "flex-start",
659
670
  top: offsetTop || offset,
660
- width: width,
671
+ width: getContainerSize().width,
661
672
  };
662
673
  return (
663
674
  <KeyboardAvoidingView
@@ -676,12 +687,13 @@ const ToastContainer = forwardRef<ToastContainerRef, ToastContainerProps>((props
676
687
  }, [toasts, offset, offsetTop]);
677
688
 
678
689
  const renderCenterToasts = useCallback(() => {
690
+ const {height, width} = getContainerSize();
679
691
  const style: ViewStyle = {
680
692
  flexDirection: "column-reverse",
681
- height: height,
693
+ height,
682
694
  justifyContent: "center",
683
695
  top: offsetTop || offset,
684
- width: width,
696
+ width,
685
697
  };
686
698
 
687
699
  const data = toasts.filter((t) => t.placement === "center");
@@ -703,13 +715,28 @@ const ToastContainer = forwardRef<ToastContainerRef, ToastContainerProps>((props
703
715
  );
704
716
  }, [toasts, offset, offsetTop]);
705
717
 
706
- return (
718
+ const toastStack = (
707
719
  <>
708
720
  {renderTopToasts()}
709
721
  {renderBottomToasts()}
710
722
  {renderCenterToasts()}
711
723
  </>
712
724
  );
725
+
726
+ // react-native-web Modal appends its own node to document.body (z-index 9999).
727
+ // A position:fixed toast left in the app tree stays behind that layer and inherits
728
+ // the backdrop dim. Portal the same container to document.body so its z-index wins.
729
+ // Native keeps the in-tree absolute container.
730
+ if (Platform.OS !== "web") {
731
+ return toastStack;
732
+ }
733
+
734
+ const portalTarget = resolveDocumentBodyPortalTarget();
735
+ if (!portalTarget) {
736
+ return toastStack;
737
+ }
738
+
739
+ return createWebPortal({children: toastStack, container: portalTarget});
713
740
  });
714
741
 
715
742
  ToastContainer.displayName = "ToastContainer";
@@ -0,0 +1,285 @@
1
+ import {afterEach, beforeAll, beforeEach, describe, expect, it, spyOn} from "bun:test";
2
+ import {act, render, waitFor} from "@testing-library/react-native";
3
+ import type {ReactElement} from "react";
4
+ import {
5
+ Platform as ImportedPlatform,
6
+ KeyboardAvoidingView,
7
+ Pressable,
8
+ StyleSheet,
9
+ Text,
10
+ } from "react-native";
11
+ import type {ReactTestInstance} from "react-test-renderer";
12
+
13
+ import {Modal} from "./Modal";
14
+ import {ToastProvider, type ToastType, useToastNotifications} from "./ToastNotifications";
15
+ import {renderWithTheme} from "./test-utils";
16
+ import * as Utilities from "./Utilities";
17
+
18
+ // react-native-web ModalAnimation paints the dialog layer at this z-index on document.body.
19
+ const REACT_NATIVE_WEB_MODAL_Z_INDEX = 9999;
20
+
21
+ const TOAST_MESSAGE = "Saved above the modal";
22
+
23
+ interface PortalFiber {
24
+ return: PortalFiber | null;
25
+ stateNode: {containerInfo?: unknown} | null;
26
+ tag: number;
27
+ }
28
+
29
+ const HOST_PORTAL_TAG = 4;
30
+
31
+ const collectPlatformObjects = (): {OS: string}[] => {
32
+ const objects: {OS: string}[] = [];
33
+ const push = (candidate: unknown): void => {
34
+ if (
35
+ candidate &&
36
+ typeof candidate === "object" &&
37
+ "OS" in (candidate as Record<string, unknown>) &&
38
+ !objects.includes(candidate as {OS: string})
39
+ ) {
40
+ objects.push(candidate as {OS: string});
41
+ }
42
+ };
43
+ push(ImportedPlatform);
44
+ const rn = require("react-native") as {Platform?: unknown; default?: {Platform?: unknown}};
45
+ push(rn.Platform);
46
+ push(rn.default?.Platform);
47
+ try {
48
+ const libPlatform = require("react-native/Libraries/Utilities/Platform") as {
49
+ default?: unknown;
50
+ };
51
+ push(libPlatform.default);
52
+ } catch {
53
+ // The internal Platform module may not be resolvable; the top-level mock still covers it.
54
+ }
55
+ return objects;
56
+ };
57
+
58
+ const platformObjects = collectPlatformObjects();
59
+ const originalPlatformOS = platformObjects.map((platform) => platform.OS);
60
+
61
+ const setPlatformOS = (os: string): void => {
62
+ for (const platform of platformObjects) {
63
+ try {
64
+ platform.OS = os;
65
+ } catch {
66
+ // Skip any binding that is not writable.
67
+ }
68
+ }
69
+ };
70
+
71
+ const restorePlatformOS = (): void => {
72
+ platformObjects.forEach((platform, index) => {
73
+ try {
74
+ platform.OS = originalPlatformOS[index] ?? "ios";
75
+ } catch {
76
+ // Skip any binding that is not writable.
77
+ }
78
+ });
79
+ };
80
+
81
+ const globalScope = globalThis as {document?: unknown; HTMLElement?: unknown};
82
+ const originalDocument = globalScope.document;
83
+ const originalHTMLElement = globalScope.HTMLElement;
84
+
85
+ class TestHTMLElement {
86
+ nodeType = 1;
87
+ // react-test-renderer commits a portal by appending host nodes onto `container.children`
88
+ // and reads `createNodeMock` from that container when attaching refs.
89
+ children: unknown[] = [];
90
+ createNodeMock = (): Record<string, never> => ({});
91
+ }
92
+
93
+ const installDocumentBody = (): TestHTMLElement => {
94
+ const body = new TestHTMLElement();
95
+ globalScope.HTMLElement = TestHTMLElement;
96
+ globalScope.document = {activeElement: null, body};
97
+ return body;
98
+ };
99
+
100
+ const restoreDocument = (): void => {
101
+ globalScope.document = originalDocument;
102
+ globalScope.HTMLElement = originalHTMLElement;
103
+ };
104
+
105
+ const flattenStyle = (style: unknown): {pointerEvents?: string; zIndex?: number} => {
106
+ return StyleSheet.flatten(style) as {pointerEvents?: string; zIndex?: number};
107
+ };
108
+
109
+ const portalContainerFor = (node: ReactTestInstance): unknown => {
110
+ let fiber = (node as unknown as {_fiber?: PortalFiber})._fiber ?? null;
111
+ while (fiber) {
112
+ if (fiber.tag === HOST_PORTAL_TAG) {
113
+ return fiber.stateNode?.containerInfo;
114
+ }
115
+ fiber = fiber.return;
116
+ }
117
+ return undefined;
118
+ };
119
+
120
+ const isInside = (node: ReactTestInstance, ancestor: ReactTestInstance): boolean => {
121
+ let current: ReactTestInstance | null = node;
122
+ while (current) {
123
+ if (current === ancestor) {
124
+ return true;
125
+ }
126
+ current = current.parent;
127
+ }
128
+ return false;
129
+ };
130
+
131
+ const findBackdrop = (pressables: ReactTestInstance[]): ReactTestInstance | undefined => {
132
+ return pressables.find((node) => {
133
+ const style = node.props.style as {backgroundColor?: string} | {backgroundColor?: string}[];
134
+ if (Array.isArray(style)) {
135
+ return style.some((entry) => entry?.backgroundColor?.includes("rgba"));
136
+ }
137
+ return style?.backgroundColor?.includes("rgba");
138
+ });
139
+ };
140
+
141
+ const flushFrames = async (): Promise<void> => {
142
+ for (let i = 0; i < 5; i++) {
143
+ await act(async () => {
144
+ await new Promise((resolve) => setTimeout(resolve, 20));
145
+ });
146
+ }
147
+ };
148
+
149
+ beforeAll(() => {
150
+ global.requestAnimationFrame = (callback: FrameRequestCallback) => {
151
+ return setTimeout(() => callback(Date.now()), 0) as unknown as number;
152
+ };
153
+ global.cancelAnimationFrame = (id: number) => {
154
+ clearTimeout(id);
155
+ };
156
+ });
157
+
158
+ describe("Toast above an open web modal", () => {
159
+ let isNativeSpy: ReturnType<typeof spyOn> | undefined;
160
+
161
+ beforeEach(() => {
162
+ setPlatformOS("web");
163
+ isNativeSpy = spyOn(Utilities, "isNative").mockReturnValue(false);
164
+ installDocumentBody();
165
+ });
166
+
167
+ afterEach(() => {
168
+ isNativeSpy?.mockRestore();
169
+ restorePlatformOS();
170
+ restoreDocument();
171
+ });
172
+
173
+ it("portals the toast onto document.body above the modal backdrop", async () => {
174
+ const body = globalScope.document
175
+ ? (globalScope.document as {body: TestHTMLElement}).body
176
+ : undefined;
177
+ let toastRef: ToastType | null = null;
178
+
179
+ const Harness = (): ReactElement => {
180
+ toastRef = useToastNotifications();
181
+ return (
182
+ <Modal onDismiss={() => {}} title="Confirm" visible>
183
+ <Text>Modal body</Text>
184
+ </Modal>
185
+ );
186
+ };
187
+
188
+ const {getByText, UNSAFE_getAllByType} = renderWithTheme(
189
+ <ToastProvider swipeEnabled={false}>
190
+ <Harness />
191
+ </ToastProvider>
192
+ );
193
+
194
+ await waitFor(() => {
195
+ expect(toastRef?.show).toBeDefined();
196
+ });
197
+
198
+ await act(async () => {
199
+ toastRef?.show(TOAST_MESSAGE, {duration: 0, id: "above-modal"});
200
+ });
201
+ await flushFrames();
202
+
203
+ const toastText = getByText(TOAST_MESSAGE);
204
+ const toastHost = UNSAFE_getAllByType(KeyboardAvoidingView).find((node) =>
205
+ isInside(toastText, node)
206
+ );
207
+ expect(toastHost).toBeTruthy();
208
+
209
+ const hostStyle = flattenStyle(toastHost?.props.style);
210
+ expect(hostStyle.pointerEvents).toBe("box-none");
211
+ expect(hostStyle.zIndex).toBeGreaterThan(REACT_NATIVE_WEB_MODAL_Z_INDEX);
212
+
213
+ expect(portalContainerFor(toastText)).toBe(body);
214
+
215
+ const backdrop = findBackdrop(UNSAFE_getAllByType(Pressable));
216
+ expect(backdrop).toBeTruthy();
217
+ expect(isInside(toastText, backdrop as ReactTestInstance)).toBe(false);
218
+ });
219
+
220
+ // Static export pre-renders in Node, where Dimensions reports a 0x0 window; a measured
221
+ // pixel width would bake a 0px container into the HTML and push the toast off-screen.
222
+ it("sizes the fixed container to the viewport instead of the measured window", async () => {
223
+ let toastRef: ToastType | null = null;
224
+ const Harness = (): ReactElement => {
225
+ toastRef = useToastNotifications();
226
+ return <Text>App</Text>;
227
+ };
228
+
229
+ const {getByText, UNSAFE_getAllByType} = renderWithTheme(
230
+ <ToastProvider swipeEnabled={false}>
231
+ <Harness />
232
+ </ToastProvider>
233
+ );
234
+
235
+ await waitFor(() => {
236
+ expect(toastRef?.show).toBeDefined();
237
+ });
238
+ await act(async () => {
239
+ toastRef?.show(TOAST_MESSAGE, {duration: 0, id: "static-export"});
240
+ });
241
+ await flushFrames();
242
+
243
+ const toastText = getByText(TOAST_MESSAGE);
244
+ const toastHost = UNSAFE_getAllByType(KeyboardAvoidingView).find((node) =>
245
+ isInside(toastText, node)
246
+ );
247
+ expect((StyleSheet.flatten(toastHost?.props.style) as {width?: unknown}).width).toBe("100%");
248
+ });
249
+ });
250
+
251
+ describe("Toast container on native", () => {
252
+ afterEach(() => {
253
+ restorePlatformOS();
254
+ restoreDocument();
255
+ });
256
+
257
+ it("keeps the toast in the tree when a document body exists", async () => {
258
+ setPlatformOS("ios");
259
+ installDocumentBody();
260
+
261
+ let toastRef: ToastType | null = null;
262
+ const Harness = (): ReactElement => {
263
+ toastRef = useToastNotifications();
264
+ return <Text>App</Text>;
265
+ };
266
+
267
+ const {getByText} = render(
268
+ <ToastProvider swipeEnabled={false}>
269
+ <Harness />
270
+ </ToastProvider>
271
+ );
272
+
273
+ await waitFor(() => {
274
+ expect(toastRef?.show).toBeDefined();
275
+ });
276
+
277
+ await act(async () => {
278
+ toastRef?.show(TOAST_MESSAGE, {duration: 0, id: "native-toast"});
279
+ });
280
+ await flushFrames();
281
+
282
+ const toastText = getByText(TOAST_MESSAGE);
283
+ expect(portalContainerFor(toastText)).toBeUndefined();
284
+ });
285
+ });
@@ -14,9 +14,12 @@ import {
14
14
  } from "react-native";
15
15
 
16
16
  import {createWebPortal} from "./createWebPortal";
17
+ import {resolveDocumentBodyPortalTarget} from "./resolveDocumentBodyPortalTarget";
17
18
  import {useTheme} from "./Theme";
18
19
  import {createBoxShadow} from "./Utilities";
19
20
 
21
+ export {resolveDocumentBodyPortalTarget};
22
+
20
23
  export const scheduleAfterPaint = (callback: () => void): void => {
21
24
  if (typeof requestAnimationFrame === "function") {
22
25
  requestAnimationFrame(callback);
@@ -25,13 +28,6 @@ export const scheduleAfterPaint = (callback: () => void): void => {
25
28
  }
26
29
  };
27
30
 
28
- export const resolveDocumentBodyPortalTarget = (): HTMLElement | null => {
29
- if (typeof document === "undefined" || typeof HTMLElement === "undefined") {
30
- return null;
31
- }
32
- return document.body instanceof HTMLElement ? document.body : null;
33
- };
34
-
35
31
  export interface WebDropdownMenuOption {
36
32
  key?: string | number;
37
33
  label: string;
package/src/bunSetup.ts CHANGED
@@ -896,6 +896,7 @@ mock.module("expo-haptics", () => ({
896
896
  mock.module("expo-clipboard", () => ({
897
897
  getStringAsync: mock(() => Promise.resolve("")),
898
898
  hasStringAsync: mock(() => Promise.resolve(false)),
899
+ setImageAsync: mock(() => Promise.resolve(undefined)),
899
900
  setStringAsync: mock(() => Promise.resolve(undefined)),
900
901
  }));
901
902
 
@@ -0,0 +1,35 @@
1
+ import type {FC, ReactNode} from "react";
2
+
3
+ import {ChartCard} from "../ChartCard";
4
+ import {resolveTestID} from "../testing/resolveTestId";
5
+
6
+ export interface ChartFacadeContainerProps {
7
+ children: ReactNode;
8
+ onPeriodPress?: () => void;
9
+ periodLabel?: string;
10
+ testID?: string;
11
+ title?: string;
12
+ }
13
+
14
+ export const ChartFacadeContainer: FC<ChartFacadeContainerProps> = ({
15
+ children,
16
+ onPeriodPress,
17
+ periodLabel,
18
+ testID,
19
+ title,
20
+ }) => {
21
+ if (!title) {
22
+ return children;
23
+ }
24
+
25
+ return (
26
+ <ChartCard
27
+ onPeriodPress={onPeriodPress}
28
+ periodLabel={periodLabel}
29
+ testID={resolveTestID(testID, "card")}
30
+ title={title}
31
+ >
32
+ {children}
33
+ </ChartCard>
34
+ );
35
+ };
@@ -11,6 +11,7 @@ export interface ChartFrameProps {
11
11
  children: ReactNode;
12
12
  emptyText: string;
13
13
  isEmpty: boolean;
14
+ legendItems?: Array<{color: string; label: string}>;
14
15
  legendLabel?: string;
15
16
  loading?: boolean;
16
17
  testID?: string;
@@ -22,6 +23,7 @@ export const ChartFrame: FC<ChartFrameProps> = ({
22
23
  children,
23
24
  emptyText,
24
25
  isEmpty,
26
+ legendItems,
25
27
  legendLabel,
26
28
  loading = false,
27
29
  testID,
@@ -55,7 +57,43 @@ export const ChartFrame: FC<ChartFrameProps> = ({
55
57
  ) : null}
56
58
  </Box>
57
59
  ) : null}
58
- {showPlot && legendLabel ? (
60
+ {showPlot && legendItems && legendItems.length > 0 ? (
61
+ <Box
62
+ direction="row"
63
+ gap={3}
64
+ height={CHART_FOOTER_ROW_HEIGHT}
65
+ minWidth={0}
66
+ overflow="hidden"
67
+ >
68
+ {legendItems.map((item, index) => (
69
+ <Box
70
+ alignItems="center"
71
+ dangerouslySetInlineStyle={{__style: {flexBasis: 0, flexGrow: 1, flexShrink: 1}}}
72
+ direction="row"
73
+ gap={1}
74
+ key={`${item.label}-${index}`}
75
+ minWidth={0}
76
+ testID={resolveTestID(testID, `legend.${index}.item`)}
77
+ >
78
+ <Box
79
+ dangerouslySetInlineStyle={{
80
+ __style: {backgroundColor: item.color, height: 8, width: 8},
81
+ }}
82
+ testID={resolveTestID(testID, `legend.${index}.swatch`)}
83
+ />
84
+ <Text
85
+ size="sm"
86
+ skipLinking
87
+ testID={resolveTestID(testID, `legend.${index}`)}
88
+ truncate
89
+ >
90
+ {item.label}
91
+ </Text>
92
+ </Box>
93
+ ))}
94
+ </Box>
95
+ ) : null}
96
+ {showPlot && (!legendItems || legendItems.length === 0) && legendLabel ? (
59
97
  <Box height={CHART_FOOTER_ROW_HEIGHT} minWidth={0} overflow="hidden">
60
98
  <Text size="sm" skipLinking testID={resolveTestID(testID, "legend")} truncate>
61
99
  {legendLabel}
@@ -1,4 +1,5 @@
1
1
  import {describe, expect, it} from "bun:test";
2
+ import {assert} from "chai";
2
3
 
3
4
  import {
4
5
  CHART_Y_AXIS_MAX_WIDTH,
@@ -8,6 +9,7 @@ import {
8
9
  getPlotHeight,
9
10
  getXTickStyle,
10
11
  getYTickStyle,
12
+ shouldRotateChartXTicks,
11
13
  } from "./layout";
12
14
 
13
15
  describe("chart layout", () => {
@@ -19,8 +21,16 @@ describe("chart layout", () => {
19
21
  expect(getPlotHeight({hasLegend: true, height: 200})).toBe(146);
20
22
  });
21
23
 
24
+ it("reserves a custom x-axis height for rotated labels", () => {
25
+ assert.equal(getPlotHeight({hasLegend: false, height: 200, xAxisHeight: 72}), 110);
26
+ });
27
+
22
28
  it("keeps a drawable plot when the requested height is smaller than the rows", () => {
23
- expect(getPlotHeight({hasLegend: true, height: 20})).toBe(40);
29
+ expect(getPlotHeight({hasLegend: true, height: 20})).toBe(1);
30
+ });
31
+
32
+ it("keeps rotated charts inside the requested height", () => {
33
+ assert.equal(getPlotHeight({hasLegend: true, height: 140, xAxisHeight: 72}), 32);
24
34
  });
25
35
 
26
36
  it("fits a donut inside the height left by tooltip and padded, gapped legend rows", () => {
@@ -69,6 +79,17 @@ describe("chart layout", () => {
69
79
  expect(getXTickStyle({bandwidth: 0, xCenter: 10}).width).toBe(1);
70
80
  });
71
81
 
82
+ it("rotates dense auto ticks but respects explicit policies", () => {
83
+ assert.isFalse(shouldRotateChartXTicks({labelCount: 7, policy: "auto"}));
84
+ assert.isTrue(shouldRotateChartXTicks({labelCount: 8, policy: "auto"}));
85
+ assert.isTrue(shouldRotateChartXTicks({labelCount: 2, policy: "rotate"}));
86
+ assert.isFalse(shouldRotateChartXTicks({labelCount: 20, policy: "truncate"}));
87
+ assert.deepInclude(getXTickStyle({bandwidth: 10, isRotated: true, xCenter: 20}), {
88
+ transform: [{rotate: "45deg"}],
89
+ transformOrigin: "left top",
90
+ });
91
+ });
92
+
72
93
  it("centers a y tick label on its value and spans the axis gutter", () => {
73
94
  expect(getYTickStyle({axisWidth: 40, y: 50})).toEqual({
74
95
  left: 0,
@@ -2,6 +2,7 @@ import type {ChartPlot} from "./types/chartTypes";
2
2
 
3
3
  export const CHART_Y_AXIS_MAX_WIDTH = 40;
4
4
  export const CHART_X_AXIS_HEIGHT = 18;
5
+ export const CHART_ROTATED_X_AXIS_HEIGHT = 72;
5
6
  export const CHART_FOOTER_ROW_HEIGHT = 18;
6
7
 
7
8
  const Y_AXIS_WIDTH_RATIO = 0.3;
@@ -39,12 +40,18 @@ export const getChartAxisWidth = (chartWidth: number): number => {
39
40
  export const getPlotHeight = ({
40
41
  hasLegend,
41
42
  height,
43
+ xAxisHeight = CHART_X_AXIS_HEIGHT,
42
44
  }: {
43
45
  hasLegend: boolean;
44
46
  height: number;
47
+ xAxisHeight?: number;
45
48
  }): number => {
46
- const footer = CHART_X_AXIS_HEIGHT + CHART_FOOTER_ROW_HEIGHT * (hasLegend ? 2 : 1);
47
- return Math.max(height - footer, MIN_PLOT_HEIGHT);
49
+ const footer = xAxisHeight + CHART_FOOTER_ROW_HEIGHT * (hasLegend ? 2 : 1);
50
+ const remaining = height - footer;
51
+ if (remaining >= MIN_PLOT_HEIGHT) {
52
+ return remaining;
53
+ }
54
+ return Math.max(remaining, 1);
48
55
  };
49
56
 
50
57
  export const getDonutSize = ({
@@ -85,13 +92,39 @@ export const getChartPlot = ({
85
92
  */
86
93
  export const getXTickStyle = ({
87
94
  bandwidth,
95
+ isRotated = false,
88
96
  xCenter,
89
97
  }: {
90
98
  bandwidth: number;
99
+ isRotated?: boolean;
91
100
  xCenter: number;
92
101
  }): ChartTickStyle => {
93
- const width = Math.max(bandwidth, 1);
94
- return {left: xCenter - width / 2, position: "absolute", top: 0, width};
102
+ const width = isRotated
103
+ ? Math.max(bandwidth * 1.5, CHART_ROTATED_X_AXIS_HEIGHT)
104
+ : Math.max(bandwidth, 1);
105
+ return {
106
+ left: isRotated ? xCenter - bandwidth / 4 : xCenter - width / 2,
107
+ position: "absolute",
108
+ top: 0,
109
+ ...(isRotated ? {transform: [{rotate: "45deg"}], transformOrigin: "left top"} : {}),
110
+ width,
111
+ };
112
+ };
113
+
114
+ export const shouldRotateChartXTicks = ({
115
+ labelCount,
116
+ policy = "auto",
117
+ }: {
118
+ labelCount: number;
119
+ policy?: "auto" | "rotate" | "truncate";
120
+ }): boolean => {
121
+ if (policy === "rotate") {
122
+ return true;
123
+ }
124
+ if (policy === "truncate") {
125
+ return false;
126
+ }
127
+ return labelCount > 7;
95
128
  };
96
129
 
97
130
  export const getYTickStyle = ({axisWidth, y}: {axisWidth: number; y: number}): ChartTickStyle => {
@@ -1,4 +1,5 @@
1
1
  import {describe, expect, it} from "bun:test";
2
+ import {assert} from "chai";
2
3
 
3
4
  import {createCartesianScales, getYTickValues} from "./scales";
4
5
  import type {ChartPoint} from "./types/chartTypes";
@@ -27,6 +28,38 @@ describe("createCartesianScales", () => {
27
28
  expect(scales.y(50)).toBe(50);
28
29
  });
29
30
 
31
+ it("scales a sparkline to the data range instead of forcing zero", () => {
32
+ const points = [
33
+ {label: "A", value: 100},
34
+ {label: "B", value: 110},
35
+ ];
36
+ const scales = createCartesianScales({
37
+ includeZero: false,
38
+ plot: PLOT,
39
+ points,
40
+ });
41
+
42
+ assert.equal(scales.y(100), 100);
43
+ assert.equal(scales.y(110), 0);
44
+ });
45
+
46
+ it("uses the explicit shared x labels while scaling all series values on y", () => {
47
+ const points = [
48
+ ...FIXTURE_POINTS,
49
+ ...FIXTURE_POINTS.map((point) => ({...point, value: point.value / 2})),
50
+ ];
51
+ const scales = createCartesianScales({
52
+ plot: PLOT,
53
+ points,
54
+ xLabels: FIXTURE_POINTS.map((point) => point.label),
55
+ });
56
+
57
+ assert.equal(scales.bandwidth, 30);
58
+ assert.equal(scales.xCenter("C"), 85);
59
+ assert.equal(scales.y(100), 0);
60
+ assert.equal(scales.y(25), 75);
61
+ });
62
+
30
63
  it("returns three ticks from the y domain including zero", () => {
31
64
  expect(getYTickValues(FIXTURE_POINTS)).toEqual([0, 50, 100]);
32
65
  });