@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,7 +1,8 @@
1
- import {afterEach, beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
1
+ import {afterEach, beforeAll, beforeEach, describe, expect, it, mock, spyOn} from "bun:test";
2
2
  import {act} from "@testing-library/react-native";
3
3
  import React from "react";
4
4
 
5
+ import * as webPortalModule from "./createWebPortal";
5
6
  import {Text} from "./Text";
6
7
  import {Arrow, getTooltipPosition, Tooltip} from "./Tooltip";
7
8
  import {renderWithTheme} from "./test-utils";
@@ -273,6 +274,7 @@ describe("Tooltip", () => {
273
274
  },
274
275
  });
275
276
  });
277
+ break;
276
278
  }
277
279
  }
278
280
  });
@@ -341,6 +343,169 @@ describe("Tooltip", () => {
341
343
  expect(getByTestId("tooltip-container")).toBeTruthy();
342
344
  });
343
345
 
346
+ it("logs an error and hides the tooltip when the trigger measures as zero-sized", async () => {
347
+ const consoleError = mock(() => {});
348
+ const originalError = console.error;
349
+ console.error = consoleError;
350
+ try {
351
+ const {queryByTestId, UNSAFE_getAllByType, toJSON} = renderWithTheme(
352
+ <Tooltip idealPosition="top" text="Bad measurement">
353
+ <Text>Trigger</Text>
354
+ </Tooltip>
355
+ );
356
+
357
+ const root = (toJSON() as TestNode).children?.[0] as TestNode;
358
+ await act(async () => {
359
+ root.props.onTouchStart?.({nativeEvent: {}});
360
+ });
361
+ await act(async () => {
362
+ await new Promise((resolve) => setTimeout(resolve, 150));
363
+ });
364
+ expect(queryByTestId("tooltip-container")).toBeTruthy();
365
+
366
+ const {View: ViewComp} = await import("react-native");
367
+ const wrapper = UNSAFE_getAllByType(ViewComp).find((node: {props: {hitSlop?: object}}) =>
368
+ Boolean(node.props.hitSlop)
369
+ );
370
+ const fiber = (wrapper as unknown as {_fiber?: {ref?: {current: unknown}}})?._fiber;
371
+ if (fiber?.ref && typeof fiber.ref === "object") {
372
+ (fiber.ref as {current: unknown}).current = {
373
+ measure: (callback: MeasureCallback) => {
374
+ callback(0, 0, 0, 0, 0, 0);
375
+ },
376
+ };
377
+ }
378
+ const positionedView = UNSAFE_getAllByType(ViewComp).find(
379
+ (node: {props: {onLayout?: unknown}}) => Boolean(node.props.onLayout)
380
+ ) as {props: TestNode["props"]} | undefined;
381
+
382
+ await act(async () => {
383
+ positionedView?.props.onLayout?.({
384
+ nativeEvent: {layout: {height: 30, width: 150, x: 0, y: 0}},
385
+ });
386
+ });
387
+
388
+ expect(queryByTestId("tooltip-container")).toBeNull();
389
+ expect(consoleError).toHaveBeenCalled();
390
+ } finally {
391
+ console.error = originalError;
392
+ }
393
+ });
394
+
395
+ describe("measurement lifecycle", () => {
396
+ const showByTouch = async (root: TestNode): Promise<void> => {
397
+ await act(async () => {
398
+ root.props.onTouchStart?.({nativeEvent: {}});
399
+ });
400
+ await act(async () => {
401
+ await new Promise((resolve) => setTimeout(resolve, 150));
402
+ });
403
+ };
404
+
405
+ const setup = async (measure: (callback: MeasureCallback) => void) => {
406
+ const utils = renderWithTheme(
407
+ <Tooltip idealPosition="top" text="Lifecycle">
408
+ <Text>Trigger</Text>
409
+ </Tooltip>
410
+ );
411
+ const {View: ViewComp} = await import("react-native");
412
+ const getWrapper = () =>
413
+ utils
414
+ .UNSAFE_getAllByType(ViewComp)
415
+ .find((node: {props: {hitSlop?: object}}) => Boolean(node.props.hitSlop)) as unknown as {
416
+ _fiber?: {ref?: {current: unknown}};
417
+ props: TestNode["props"];
418
+ };
419
+ const fiber = getWrapper()._fiber;
420
+ if (fiber?.ref && typeof fiber.ref === "object") {
421
+ (fiber.ref as {current: unknown}).current = {measure};
422
+ }
423
+ const layoutTooltip = async (width: number, height: number): Promise<void> => {
424
+ const positionedView = utils
425
+ .UNSAFE_getAllByType(ViewComp)
426
+ .find((node: {props: {onLayout?: unknown}}) => Boolean(node.props.onLayout)) as
427
+ | {props: TestNode["props"]}
428
+ | undefined;
429
+ await act(async () => {
430
+ positionedView?.props.onLayout?.({nativeEvent: {layout: {height, width, x: 0, y: 0}}});
431
+ });
432
+ };
433
+ return {...utils, getWrapper, layoutTooltip};
434
+ };
435
+
436
+ it("waits for a sized layout instead of hiding on an empty first layout", async () => {
437
+ const {getWrapper, layoutTooltip, queryByTestId} = await setup((callback) => {
438
+ callback(0, 0, 100, 40, 200, 300);
439
+ });
440
+
441
+ await showByTouch(getWrapper() as unknown as TestNode);
442
+ await layoutTooltip(0, 0);
443
+ expect(queryByTestId("tooltip-container")).toBeTruthy();
444
+
445
+ await layoutTooltip(150, 30);
446
+ expect(queryByTestId("tooltip-container")).toBeTruthy();
447
+ });
448
+
449
+ it("ignores a stale invalid measure result from an earlier show", async () => {
450
+ let pendingCallback: MeasureCallback | undefined;
451
+ const consoleError = mock(() => {});
452
+ const originalError = console.error;
453
+ console.error = consoleError;
454
+ try {
455
+ const {getWrapper, layoutTooltip, queryByTestId} = await setup((callback) => {
456
+ pendingCallback = callback;
457
+ });
458
+
459
+ await showByTouch(getWrapper() as unknown as TestNode);
460
+ await layoutTooltip(150, 30);
461
+ const staleCallback = pendingCallback;
462
+
463
+ // Hide, then show again before the first measure resolves.
464
+ await act(async () => {
465
+ getWrapper().props.onTouchStart?.({nativeEvent: {}});
466
+ });
467
+ expect(queryByTestId("tooltip-container")).toBeNull();
468
+ await showByTouch(getWrapper() as unknown as TestNode);
469
+ expect(queryByTestId("tooltip-container")).toBeTruthy();
470
+
471
+ await act(async () => {
472
+ staleCallback?.(0, 0, 0, 0, 0, 0);
473
+ });
474
+ expect(queryByTestId("tooltip-container")).toBeTruthy();
475
+ const tooltipErrors = (consoleError.mock.calls as unknown[][]).filter((args) =>
476
+ String(args[0]).startsWith("Tooltip:")
477
+ );
478
+ expect(tooltipErrors).toHaveLength(0);
479
+ } finally {
480
+ console.error = originalError;
481
+ }
482
+ });
483
+ });
484
+
485
+ it("getTooltipPosition returns empty for zero-sized or non-finite measurements", () => {
486
+ expect(
487
+ getTooltipPosition({
488
+ children: {height: 0, pageX: 0, pageY: 0, width: 0},
489
+ measured: true,
490
+ tooltip: {height: 30, width: 150, x: 0, y: 0},
491
+ })
492
+ ).toEqual({});
493
+ expect(
494
+ getTooltipPosition({
495
+ children: {height: 40, pageX: Number.NaN, pageY: 100, width: 100},
496
+ measured: true,
497
+ tooltip: {height: 30, width: 150, x: 0, y: 0},
498
+ })
499
+ ).toEqual({});
500
+ expect(
501
+ getTooltipPosition({
502
+ children: {height: 40, pageX: 100, pageY: 100, width: 100},
503
+ measured: true,
504
+ tooltip: {height: 0, width: 0, x: 0, y: 0},
505
+ })
506
+ ).toEqual({});
507
+ });
508
+
344
509
  it("getTooltipPosition returns empty when not measured", () => {
345
510
  const result = getTooltipPosition({
346
511
  children: {},
@@ -640,6 +805,7 @@ describe("Tooltip", () => {
640
805
  },
641
806
  });
642
807
  });
808
+ break;
643
809
  }
644
810
  }
645
811
  });
@@ -682,6 +848,7 @@ describe("Tooltip", () => {
682
848
  },
683
849
  });
684
850
  });
851
+ break;
685
852
  }
686
853
  }
687
854
  unmount();
@@ -899,6 +1066,7 @@ describe("Tooltip", () => {
899
1066
  nativeEvent: {layout: {height: 30, width: 80, x: 0, y: 0}},
900
1067
  });
901
1068
  });
1069
+ break;
902
1070
  }
903
1071
  }
904
1072
  unmount();
@@ -946,6 +1114,7 @@ describe("Tooltip", () => {
946
1114
  nativeEvent: {layout: {height: 900, width: 900, x: 0, y: 0}},
947
1115
  });
948
1116
  });
1117
+ break;
949
1118
  }
950
1119
  }
951
1120
  unmount();
@@ -1092,6 +1261,7 @@ describe("Tooltip", () => {
1092
1261
  nativeEvent: {layout: {height: 40, width: 200, x: 0, y: 0}},
1093
1262
  });
1094
1263
  });
1264
+ break;
1095
1265
  }
1096
1266
  }
1097
1267
  });
@@ -1145,6 +1315,7 @@ describe("Tooltip", () => {
1145
1315
  nativeEvent: {layout: {height: 40, width: 200, x: 0, y: 0}},
1146
1316
  });
1147
1317
  });
1318
+ break;
1148
1319
  }
1149
1320
  }
1150
1321
 
@@ -1202,6 +1373,7 @@ describe("Tooltip", () => {
1202
1373
  nativeEvent: {layout: {height: 40, width: 200, x: 0, y: 0}},
1203
1374
  });
1204
1375
  });
1376
+ break;
1205
1377
  }
1206
1378
  }
1207
1379
 
@@ -1657,4 +1829,63 @@ describe("Tooltip", () => {
1657
1829
  }
1658
1830
  });
1659
1831
  });
1832
+
1833
+ describe("web body portal", () => {
1834
+ const globalScope = globalThis as {document?: unknown; HTMLElement?: unknown};
1835
+ const originalDocument = globalScope.document;
1836
+ const originalHTMLElement = globalScope.HTMLElement;
1837
+ let Platform: {OS: string};
1838
+ let portalSpy: ReturnType<typeof spyOn>;
1839
+
1840
+ // A stub body that passes `instanceof HTMLElement` routes the tooltip to the web portal.
1841
+ // The spy renders the portal children inline, since react-test-renderer cannot host
1842
+ // a react-dom portal.
1843
+ beforeEach(async () => {
1844
+ const rn = await import("react-native");
1845
+ Platform = rn.Platform;
1846
+ Platform.OS = "web";
1847
+ class FakeHTMLElement {}
1848
+ globalScope.HTMLElement = FakeHTMLElement;
1849
+ globalScope.document = {body: new FakeHTMLElement()};
1850
+ portalSpy = spyOn(webPortalModule, "createWebPortal").mockImplementation(
1851
+ ({children}) => children
1852
+ );
1853
+ });
1854
+
1855
+ afterEach(() => {
1856
+ Platform.OS = "ios";
1857
+ globalScope.document = originalDocument;
1858
+ globalScope.HTMLElement = originalHTMLElement;
1859
+ portalSpy.mockRestore();
1860
+ });
1861
+
1862
+ it("renders the bubble into document.body with fixed positioning", async () => {
1863
+ const {queryByTestId, toJSON} = renderWithTheme(
1864
+ <Tooltip text="Body portal">
1865
+ <Text>Hover me</Text>
1866
+ </Tooltip>
1867
+ );
1868
+
1869
+ const root = (toJSON() as TestNode).children?.[0] as TestNode;
1870
+ await act(async () => {
1871
+ root.props.onPointerEnter?.();
1872
+ });
1873
+ await act(async () => {
1874
+ await new Promise((resolve) => setTimeout(resolve, 900));
1875
+ });
1876
+
1877
+ expect(queryByTestId("tooltip-container")).toBeTruthy();
1878
+ expect(queryByTestId("portal")).toBeNull();
1879
+ expect(portalSpy).toHaveBeenCalled();
1880
+ const [{children, container}] = portalSpy.mock.calls.at(-1) as [
1881
+ {
1882
+ children: React.ReactElement<{style: {position: string; zIndex: number}}>;
1883
+ container: unknown;
1884
+ },
1885
+ ];
1886
+ expect(container).toBe((globalScope.document as {body: unknown}).body);
1887
+ expect(children.props.style.position).toBe("fixed");
1888
+ expect(children.props.style.zIndex).toBe(9999);
1889
+ });
1890
+ });
1660
1891
  });
package/src/Tooltip.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import {type FC, useCallback, useEffect, useRef, useState} from "react";
1
+ import {type FC, type ReactNode, useCallback, useEffect, useRef, useState} from "react";
2
2
  import {
3
3
  Dimensions,
4
4
  type LayoutChangeEvent,
@@ -10,7 +10,9 @@ import {
10
10
  } from "react-native";
11
11
 
12
12
  import type {TooltipPosition, TooltipProps} from "./Common";
13
+ import {createWebPortal} from "./createWebPortal";
13
14
  import {Portal} from "./PortalHost";
15
+ import {resolveDocumentBodyPortalTarget} from "./resolveDocumentBodyPortalTarget";
14
16
  import {Text} from "./Text";
15
17
  import {useTheme} from "./Theme";
16
18
 
@@ -45,6 +47,30 @@ interface ChildrenProps {
45
47
  onHoverOut?: () => void;
46
48
  }
47
49
 
50
+ const isPositiveFinite = (value: unknown): boolean =>
51
+ typeof value === "number" && Number.isFinite(value) && value > 0;
52
+
53
+ const isFiniteNumber = (value: unknown): boolean =>
54
+ typeof value === "number" && Number.isFinite(value);
55
+
56
+ // A detached or collapsed trigger measures as zero-sized at (0, 0), which would place the
57
+ // tooltip in the top left corner of the screen on top of whatever is there.
58
+ export const isValidTooltipMeasurement = ({
59
+ children,
60
+ tooltip,
61
+ }: Pick<Measurement, "children" | "tooltip">): boolean => {
62
+ const {width, height, pageX, pageY} = children as Partial<ChildrenMeasurement>;
63
+ const {width: tooltipWidth, height: tooltipHeight} = tooltip as Partial<LayoutRectangle>;
64
+ return (
65
+ isPositiveFinite(width) &&
66
+ isPositiveFinite(height) &&
67
+ isFiniteNumber(pageX) &&
68
+ isFiniteNumber(pageY) &&
69
+ isPositiveFinite(tooltipWidth) &&
70
+ isPositiveFinite(tooltipHeight)
71
+ );
72
+ };
73
+
48
74
  export const getTooltipPosition = ({
49
75
  children,
50
76
  tooltip,
@@ -55,6 +81,9 @@ export const getTooltipPosition = ({
55
81
  console.debug("No measurements for child yet, cannot show tooltip yet.");
56
82
  return {};
57
83
  }
84
+ if (!isValidTooltipMeasurement({children, tooltip})) {
85
+ return {};
86
+ }
58
87
 
59
88
  const {
60
89
  pageY: childrenY,
@@ -193,6 +222,8 @@ export const Tooltip: FC<TooltipProps> = ({text, children, idealPosition, includ
193
222
  const hideTooltipTimer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
194
223
  const childrenWrapperRef = useRef<View>(null);
195
224
  const touched = useRef(false);
225
+ // Bumped on every hide so async measure callbacks from an earlier show are ignored.
226
+ const showGeneration = useRef(0);
196
227
  const isWeb = Platform.OS === "web";
197
228
  const resetMeasurement = useCallback(() => {
198
229
  setMeasurement({
@@ -209,6 +240,7 @@ export const Tooltip: FC<TooltipProps> = ({text, children, idealPosition, includ
209
240
  clearTimeout(hideTooltipTimer.current);
210
241
  }
211
242
 
243
+ showGeneration.current += 1;
212
244
  touched.current = false;
213
245
  setVisible(false);
214
246
  resetMeasurement();
@@ -255,31 +287,44 @@ export const Tooltip: FC<TooltipProps> = ({text, children, idealPosition, includ
255
287
 
256
288
  const handleOnLayout = useCallback(
257
289
  ({nativeEvent: {layout}}: LayoutChangeEvent) => {
258
- if (childrenWrapperRef?.current && !childrenWrapperRef?.current?.measure) {
259
- console.error("Tooltip: childrenWrapperRef does not have a measure method.");
290
+ // An empty first layout pass is followed by another onLayout once the content has size.
291
+ if (!isPositiveFinite(layout.width) || !isPositiveFinite(layout.height)) {
292
+ console.debug("Tooltip: tooltip has no size yet, waiting for next layout.");
293
+ return;
294
+ }
295
+ if (!childrenWrapperRef?.current) {
296
+ console.error("Tooltip: childrenWrapperRef is null, hiding tooltip.");
297
+ hideTooltip();
298
+ return;
299
+ }
300
+ if (!childrenWrapperRef.current.measure) {
301
+ console.error(
302
+ "Tooltip: childrenWrapperRef does not have a measure method, hiding tooltip."
303
+ );
304
+ hideTooltip();
260
305
  return;
261
- } else if (!childrenWrapperRef?.current) {
262
- console.error("Tooltip: childrenWrapperRef is null.");
263
306
  }
264
307
 
265
- childrenWrapperRef?.current?.measure((_x, _y, width, height, pageX, pageY) => {
266
- setMeasurement({
267
- children: {height, pageX, pageY, width},
268
- measured: true,
269
- tooltip: {...layout},
270
- });
271
- const position = getTooltipPosition({
272
- children: {height, pageX, pageY, width},
273
- idealPosition,
274
- measured: true,
275
- tooltip: {...layout},
276
- });
308
+ const generation = showGeneration.current;
309
+ childrenWrapperRef.current.measure((_x, _y, width, height, pageX, pageY) => {
310
+ if (generation !== showGeneration.current) {
311
+ return;
312
+ }
313
+ const children = {height, pageX, pageY, width};
314
+ const tooltip = {...layout};
315
+ if (!isValidTooltipMeasurement({children, tooltip})) {
316
+ console.error("Tooltip: invalid measurements, hiding tooltip.", {children, tooltip});
317
+ hideTooltip();
318
+ return;
319
+ }
320
+ setMeasurement({children, measured: true, tooltip});
321
+ const position = getTooltipPosition({children, idealPosition, measured: true, tooltip});
277
322
  if (position.finalPosition) {
278
323
  setFinalPosition(position.finalPosition);
279
324
  }
280
325
  });
281
326
  },
282
- [idealPosition]
327
+ [hideTooltip, idealPosition]
283
328
  );
284
329
 
285
330
  const handleTouchStart = useCallback(() => {
@@ -348,57 +393,73 @@ export const Tooltip: FC<TooltipProps> = ({text, children, idealPosition, includ
348
393
  : {};
349
394
  const isPositioned = placement.left !== undefined && placement.top !== undefined;
350
395
 
396
+ // On web the bubble attaches to document.body with fixed positioning, so no
397
+ // full-screen portal layer sits over the page while the tooltip is open.
398
+ const webPortalTarget = isWeb ? resolveDocumentBodyPortalTarget() : null;
399
+
400
+ const tooltipBubble = (
401
+ <View
402
+ onLayout={handleOnLayout}
403
+ style={{
404
+ // The trigger is measured on the first layout pass, so keep the tooltip
405
+ // off screen until then instead of flashing it in the top left corner.
406
+ left: isPositioned ? placement.left : -9999,
407
+ opacity: isPositioned ? 1 : 0,
408
+ position: (webPortalTarget ? "fixed" : "absolute") as ViewStyle["position"],
409
+ top: isPositioned ? placement.top : -9999,
410
+ // Match the body-level web overlays (RNW Modal, DropdownPanel) so tooltips
411
+ // inside them stay on top.
412
+ zIndex: webPortalTarget ? 9999 : 999,
413
+ }}
414
+ >
415
+ {includeArrow && isWeb && (
416
+ <View style={arrowContainerStyles as ViewStyle}>
417
+ <Arrow color={theme.surface.secondaryExtraDark} position={finalPosition} />
418
+ </View>
419
+ )}
420
+ <View
421
+ style={{
422
+ backgroundColor: theme.surface.secondaryExtraDark,
423
+ borderRadius: theme.radius.default,
424
+ display: "flex",
425
+ flexShrink: 1,
426
+ maxWidth: 320,
427
+ paddingHorizontal: 8,
428
+ paddingVertical: 2,
429
+ }}
430
+ >
431
+ <Pressable
432
+ accessibilityHint="Tooltip information"
433
+ aria-label={text}
434
+ aria-role="button"
435
+ onPress={hideTooltip}
436
+ style={{
437
+ backgroundColor: theme.surface.secondaryExtraDark,
438
+ borderRadius: theme.radius.default,
439
+ }}
440
+ testID="tooltip-container"
441
+ >
442
+ <Text color="inverted" size="sm">
443
+ {text}
444
+ </Text>
445
+ </Pressable>
446
+ </View>
447
+ </View>
448
+ );
449
+
450
+ const renderTooltip = (): ReactNode => {
451
+ if (!visible) {
452
+ return null;
453
+ }
454
+ if (webPortalTarget) {
455
+ return createWebPortal({children: tooltipBubble, container: webPortalTarget});
456
+ }
457
+ return <Portal>{tooltipBubble}</Portal>;
458
+ };
459
+
351
460
  return (
352
461
  <View>
353
- {visible && (
354
- <Portal>
355
- <View
356
- onLayout={handleOnLayout}
357
- style={{
358
- // The trigger is measured on the first layout pass, so keep the tooltip
359
- // off screen until then instead of flashing it in the top left corner.
360
- left: isPositioned ? placement.left : -9999,
361
- opacity: isPositioned ? 1 : 0,
362
- position: "absolute",
363
- top: isPositioned ? placement.top : -9999,
364
- zIndex: 999,
365
- }}
366
- >
367
- {includeArrow && isWeb && (
368
- <View style={arrowContainerStyles as ViewStyle}>
369
- <Arrow color={theme.surface.secondaryExtraDark} position={finalPosition} />
370
- </View>
371
- )}
372
- <View
373
- style={{
374
- backgroundColor: theme.surface.secondaryExtraDark,
375
- borderRadius: theme.radius.default,
376
- display: "flex",
377
- flexShrink: 1,
378
- maxWidth: 320,
379
- paddingHorizontal: 8,
380
- paddingVertical: 2,
381
- }}
382
- >
383
- <Pressable
384
- accessibilityHint="Tooltip information"
385
- aria-label={text}
386
- aria-role="button"
387
- onPress={hideTooltip}
388
- style={{
389
- backgroundColor: theme.surface.secondaryExtraDark,
390
- borderRadius: theme.radius.default,
391
- }}
392
- testID="tooltip-container"
393
- >
394
- <Text color="inverted" size="sm">
395
- {text}
396
- </Text>
397
- </Pressable>
398
- </View>
399
- </View>
400
- </Portal>
401
- )}
462
+ {renderTooltip()}
402
463
  <View
403
464
  hitSlop={{bottom: 10, left: 15, right: 15, top: 10}}
404
465
  onPointerEnter={() => {
@@ -88,7 +88,7 @@ exports[`AddressField renders correctly with default props 1`] = `
88
88
  "props": {
89
89
  "style": {
90
90
  "backgroundColor": "#FFFFFF",
91
- "borderColor": "#9A9A9A",
91
+ "borderColor": "#949494",
92
92
  "borderRadius": 4,
93
93
  "borderWidth": 1,
94
94
  "flexDirection": "column",
@@ -206,7 +206,7 @@ exports[`AddressField renders correctly with default props 1`] = `
206
206
  "props": {
207
207
  "style": {
208
208
  "backgroundColor": "#FFFFFF",
209
- "borderColor": "#9A9A9A",
209
+ "borderColor": "#949494",
210
210
  "borderRadius": 4,
211
211
  "borderWidth": 1,
212
212
  "flexDirection": "column",
@@ -324,7 +324,7 @@ exports[`AddressField renders correctly with default props 1`] = `
324
324
  "props": {
325
325
  "style": {
326
326
  "backgroundColor": "#FFFFFF",
327
- "borderColor": "#9A9A9A",
327
+ "borderColor": "#949494",
328
328
  "borderRadius": 4,
329
329
  "borderWidth": 1,
330
330
  "flexDirection": "column",
@@ -457,7 +457,7 @@ exports[`AddressField renders correctly with default props 1`] = `
457
457
  "current": null,
458
458
  },
459
459
  "style": {
460
- "borderColor": "#9A9A9A",
460
+ "borderColor": "#949494",
461
461
  "borderRadius": 4,
462
462
  "borderWidth": 1,
463
463
  "color": "#1C1C1C",
@@ -2475,7 +2475,7 @@ exports[`AddressField renders correctly with default props 1`] = `
2475
2475
  "props": {
2476
2476
  "style": {
2477
2477
  "backgroundColor": "#FFFFFF",
2478
- "borderColor": "#9A9A9A",
2478
+ "borderColor": "#949494",
2479
2479
  "borderRadius": 4,
2480
2480
  "borderWidth": 1,
2481
2481
  "boxShadow": "0px 2px 8px rgba(0, 0, 0, 0.15)",
@@ -2520,7 +2520,7 @@ exports[`AddressField renders correctly with default props 1`] = `
2520
2520
  },
2521
2521
  {
2522
2522
  "backgroundColor": "#FFFFFF",
2523
- "borderColor": "#9A9A9A",
2523
+ "borderColor": "#949494",
2524
2524
  },
2525
2525
  false,
2526
2526
  ],
@@ -2644,7 +2644,7 @@ exports[`AddressField renders correctly with default props 1`] = `
2644
2644
  "props": {
2645
2645
  "style": {
2646
2646
  "backgroundColor": "#FFFFFF",
2647
- "borderColor": "#9A9A9A",
2647
+ "borderColor": "#949494",
2648
2648
  "borderRadius": 4,
2649
2649
  "borderWidth": 1,
2650
2650
  "flexDirection": "column",