@terreno/ui 56.0.0-beta.2 → 57.0.0-beta.1

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 (69) hide show
  1. package/dist/Badge.d.ts +1 -1
  2. package/dist/Badge.js +37 -1
  3. package/dist/Badge.js.map +1 -1
  4. package/dist/Common.d.ts +110 -3
  5. package/dist/Common.js.map +1 -1
  6. package/dist/Filter.d.ts +13 -0
  7. package/dist/Filter.js +120 -0
  8. package/dist/Filter.js.map +1 -0
  9. package/dist/FilterAccordion.d.ts +8 -0
  10. package/dist/FilterAccordion.js +59 -0
  11. package/dist/FilterAccordion.js.map +1 -0
  12. package/dist/FilterBoolean.d.ts +8 -0
  13. package/dist/FilterBoolean.js +74 -0
  14. package/dist/FilterBoolean.js.map +1 -0
  15. package/dist/FilterChangesBadge.d.ts +8 -0
  16. package/dist/FilterChangesBadge.js +11 -0
  17. package/dist/FilterChangesBadge.js.map +1 -0
  18. package/dist/FilterSelectMenu.d.ts +8 -0
  19. package/dist/FilterSelectMenu.js +37 -0
  20. package/dist/FilterSelectMenu.js.map +1 -0
  21. package/dist/IconRegistry.js +11 -6
  22. package/dist/IconRegistry.js.map +1 -1
  23. package/dist/PickerSelect.d.ts +8 -1
  24. package/dist/PickerSelect.js +2 -2
  25. package/dist/PickerSelect.js.map +1 -1
  26. package/dist/SelectField.js +2 -2
  27. package/dist/SelectField.js.map +1 -1
  28. package/dist/Theme.js +2 -0
  29. package/dist/Theme.js.map +1 -1
  30. package/dist/WebDropdownMenu.d.ts +5 -1
  31. package/dist/WebDropdownMenu.js +24 -10
  32. package/dist/WebDropdownMenu.js.map +1 -1
  33. package/dist/icons/BarsFilterIcon.d.ts +9 -0
  34. package/dist/icons/BarsFilterIcon.js +12 -0
  35. package/dist/icons/BarsFilterIcon.js.map +1 -0
  36. package/dist/index.d.ts +6 -0
  37. package/dist/index.js +6 -0
  38. package/dist/index.js.map +1 -1
  39. package/package.json +23 -23
  40. package/src/Badge.test.tsx +22 -1
  41. package/src/Badge.tsx +43 -0
  42. package/src/Common.ts +120 -5
  43. package/src/DateTimeField.test.tsx +16 -18
  44. package/src/Filter.test.tsx +94 -0
  45. package/src/Filter.tsx +257 -0
  46. package/src/FilterAccordion.test.tsx +58 -0
  47. package/src/FilterAccordion.tsx +113 -0
  48. package/src/FilterBoolean.test.tsx +65 -0
  49. package/src/FilterBoolean.tsx +130 -0
  50. package/src/FilterChangesBadge.test.tsx +16 -0
  51. package/src/FilterChangesBadge.tsx +13 -0
  52. package/src/FilterSelectMenu.test.tsx +56 -0
  53. package/src/FilterSelectMenu.tsx +76 -0
  54. package/src/IconRegistry.test.tsx +6 -4
  55. package/src/IconRegistry.tsx +13 -8
  56. package/src/PickerSelect.test.tsx +20 -0
  57. package/src/PickerSelect.tsx +10 -0
  58. package/src/SelectField.tsx +2 -0
  59. package/src/Theme.tsx +2 -0
  60. package/src/WebDropdownMenu.test.tsx +86 -1
  61. package/src/WebDropdownMenu.tsx +43 -21
  62. package/src/__snapshots__/Filter.test.tsx.snap +161 -0
  63. package/src/__snapshots__/FilterAccordion.test.tsx.snap +83 -0
  64. package/src/__snapshots__/FilterBoolean.test.tsx.snap +109 -0
  65. package/src/__snapshots__/FilterChangesBadge.test.tsx.snap +18 -0
  66. package/src/__snapshots__/FilterSelectMenu.test.tsx.snap +359 -0
  67. package/src/conflictFieldDiff.test.ts +43 -0
  68. package/src/icons/BarsFilterIcon.tsx +18 -0
  69. package/src/index.tsx +6 -0
@@ -0,0 +1,56 @@
1
+ import {describe, expect, it, mock} from "bun:test";
2
+ import {assert} from "chai";
3
+ import {StyleSheet} from "react-native";
4
+
5
+ import {FilterSelectMenu} from "./FilterSelectMenu";
6
+ import {renderWithTheme} from "./test-utils";
7
+
8
+ const OPTIONS = [
9
+ {label: "All", value: "all"},
10
+ {label: "Today", value: "today"},
11
+ ];
12
+
13
+ describe("FilterSelectMenu", () => {
14
+ const defaultProps = {
15
+ onChange: () => {},
16
+ options: OPTIONS,
17
+ title: "Due date",
18
+ };
19
+
20
+ it("renders correctly with default props", () => {
21
+ const {toJSON} = renderWithTheme(<FilterSelectMenu {...defaultProps} />);
22
+ expect(toJSON()).toMatchSnapshot();
23
+ });
24
+
25
+ it("renders the title", () => {
26
+ const {getByText} = renderWithTheme(<FilterSelectMenu {...defaultProps} />);
27
+ expect(getByText("Due date")).toBeTruthy();
28
+ });
29
+
30
+ it("shows the changes badge only when enabled", () => {
31
+ const {queryByTestId, rerender} = renderWithTheme(
32
+ <FilterSelectMenu {...defaultProps} testID="filter" />
33
+ );
34
+ expect(queryByTestId("filter.badge")).toBeNull();
35
+
36
+ rerender(<FilterSelectMenu {...defaultProps} showChangesBadge testID="filter" />);
37
+ expect(queryByTestId("filter.badge")).toBeTruthy();
38
+ });
39
+
40
+ it("renders the select control", () => {
41
+ const {getByTestId} = renderWithTheme(
42
+ <FilterSelectMenu {...defaultProps} onChange={mock()} testID="filter" value="all" />
43
+ );
44
+ expect(getByTestId("filter")).toBeTruthy();
45
+ });
46
+
47
+ it("clips the compact select control to its fixed width", () => {
48
+ const {getByTestId} = renderWithTheme(
49
+ <FilterSelectMenu {...defaultProps} testID="filter" value="all" />
50
+ );
51
+ const style = StyleSheet.flatten(getByTestId("filter.selectContainer").props.style);
52
+
53
+ assert.equal(style.width, 138);
54
+ assert.equal(style.overflow, "hidden");
55
+ });
56
+ });
@@ -0,0 +1,76 @@
1
+ import {type FC, useState} from "react";
2
+ import {Pressable, View} from "react-native";
3
+
4
+ import type {FilterSelectMenuProps} from "./Common";
5
+ import {FilterChangesBadge} from "./FilterChangesBadge";
6
+ import {SelectField} from "./SelectField";
7
+ import {Text} from "./Text";
8
+ import {useTheme} from "./Theme";
9
+ import {resolveTestID} from "./testing/resolveTestId";
10
+
11
+ const SELECT_WIDTH = 138;
12
+
13
+ /**
14
+ * A single-select filter row: a title on the left and a compact select control
15
+ * on the right. Only the select is the click zone. Hovering the select tints
16
+ * the row with the `surface-base-hover` token.
17
+ */
18
+ export const FilterSelectMenu: FC<FilterSelectMenuProps> = ({
19
+ title,
20
+ options,
21
+ value,
22
+ onChange,
23
+ placeholder,
24
+ showChangesBadge = false,
25
+ disabled = false,
26
+ testID,
27
+ }) => {
28
+ const {theme} = useTheme();
29
+ const [isHovered, setIsHovered] = useState(false);
30
+
31
+ return (
32
+ <View
33
+ style={{
34
+ alignItems: "center",
35
+ backgroundColor: isHovered ? theme.surface.baseHover : theme.surface.base,
36
+ borderRadius: theme.radius.default,
37
+ flexDirection: "row",
38
+ gap: theme.spacing.sm,
39
+ paddingHorizontal: theme.spacing.sm,
40
+ paddingVertical: theme.primitives.spacing3,
41
+ }}
42
+ testID={testID}
43
+ >
44
+ <View style={{alignItems: "center", flex: 1, flexDirection: "row", gap: 6}}>
45
+ <Text size="md">{title}</Text>
46
+ {showChangesBadge && (
47
+ <FilterChangesBadge testID={testID ? resolveTestID(testID, "badge") : undefined} />
48
+ )}
49
+ </View>
50
+ <Pressable
51
+ onHoverIn={() => setIsHovered(true)}
52
+ onHoverOut={() => setIsHovered(false)}
53
+ style={{overflow: "hidden", width: SELECT_WIDTH}}
54
+ testID={testID ? resolveTestID(testID, "selectContainer") : undefined}
55
+ >
56
+ <SelectField
57
+ disabled={disabled}
58
+ // The compact filter select is fixed at SELECT_WIDTH. Search mode renders
59
+ // the trigger as an <input> whose intrinsic min-width exceeds that width,
60
+ // overflowing the row and pushing the chevron outside the control's border.
61
+ // Filter option lists are short, so a plain (non-search) dropdown is correct.
62
+ disableSearch
63
+ onChange={onChange}
64
+ options={options}
65
+ placeholder={placeholder}
66
+ // The Filter panel is a fixed portal on document.body (zIndex 9999).
67
+ // Render the menu in a matching body portal so it floats above the
68
+ // panel instead of a React Native Modal that would stack behind it.
69
+ renderMenuInBodyPortal
70
+ testID={testID ? resolveTestID(testID, "select") : undefined}
71
+ value={value}
72
+ />
73
+ </Pressable>
74
+ </View>
75
+ );
76
+ };
@@ -86,25 +86,27 @@ describe("IconRegistry", () => {
86
86
  ).not.toThrow();
87
87
  });
88
88
 
89
- it("useIconRegistry returns the registered icon map", () => {
89
+ it("useIconRegistry merges consumer icons over the built-in icons", () => {
90
90
  const wrapper = ({children}: {children: React.ReactNode}) => (
91
91
  <ThemeProvider>
92
92
  <IconRegistryProvider icons={ICONS}>{children}</IconRegistryProvider>
93
93
  </ThemeProvider>
94
94
  );
95
95
  const {result} = renderHook(() => useIconRegistry(), {wrapper});
96
- expect(result.current).toBe(ICONS);
97
96
  expect(result.current.customStar).toBe(CustomStar);
97
+ // Built-in icons remain available alongside consumer icons.
98
+ expect(result.current["bars-filter"]).toBeDefined();
98
99
  });
99
100
 
100
- it("useIconRegistry returns empty registry when no icons are provided", () => {
101
+ it("useIconRegistry exposes only the built-in icons when none are provided", () => {
101
102
  const wrapper = ({children}: {children: React.ReactNode}) => (
102
103
  <ThemeProvider>
103
104
  <IconRegistryProvider>{children}</IconRegistryProvider>
104
105
  </ThemeProvider>
105
106
  );
106
107
  const {result} = renderHook(() => useIconRegistry(), {wrapper});
107
- expect(Object.keys(result.current)).toHaveLength(0);
108
+ expect(result.current["bars-filter"]).toBeDefined();
109
+ expect(result.current.customStar).toBeUndefined();
108
110
  });
109
111
 
110
112
  it("useCustomIcon resolves registered names and ignores everything else", () => {
@@ -1,12 +1,17 @@
1
- import {createContext, type FC, type ReactNode, useContext} from "react";
1
+ import {createContext, type FC, type ReactNode, useContext, useMemo} from "react";
2
2
 
3
3
  import type {CustomIconComponent, IconName, IconRegistryMap} from "./Common";
4
+ import {BarsFilterIcon} from "./icons/BarsFilterIcon";
4
5
 
5
- // Stable empty registry so consumers that don't register icons don't trigger
6
- // unnecessary context re-renders.
7
- const EMPTY_REGISTRY: IconRegistryMap = {};
6
+ // Icons shipped with @terreno/ui and always available via `iconName`. Consumer
7
+ // icons passed to the provider override these on name collision.
8
+ const BUILT_IN_ICONS: IconRegistryMap = {
9
+ "bars-filter": ({color, size, testID}) => (
10
+ <BarsFilterIcon fill={color} height={size} testID={testID} width={size} />
11
+ ),
12
+ };
8
13
 
9
- const IconRegistryContext = createContext<IconRegistryMap>(EMPTY_REGISTRY);
14
+ const IconRegistryContext = createContext<IconRegistryMap>(BUILT_IN_ICONS);
10
15
 
11
16
  interface IconRegistryProviderProps {
12
17
  /** Map of custom icon name to the component that renders it. */
@@ -15,10 +20,10 @@ interface IconRegistryProviderProps {
15
20
  }
16
21
 
17
22
  export const IconRegistryProvider: FC<IconRegistryProviderProps> = ({icons, children}) => {
23
+ // Built-in icons are always available; consumer icons override them by name.
24
+ const mergedIcons = useMemo(() => ({...BUILT_IN_ICONS, ...icons}), [icons]);
18
25
  return (
19
- <IconRegistryContext.Provider value={icons ?? EMPTY_REGISTRY}>
20
- {children}
21
- </IconRegistryContext.Provider>
26
+ <IconRegistryContext.Provider value={mergedIcons}>{children}</IconRegistryContext.Provider>
22
27
  );
23
28
  };
24
29
 
@@ -1,9 +1,11 @@
1
1
  import {describe, expect, it, mock} from "bun:test";
2
2
  import {act, fireEvent} from "@testing-library/react-native";
3
+ import {assert} from "chai";
3
4
  import type {ReactTestInstance} from "react-test-renderer";
4
5
 
5
6
  import {RNPickerSelect} from "./PickerSelect";
6
7
  import {renderWithTheme} from "./test-utils";
8
+ import {WebDropdownMenu} from "./WebDropdownMenu";
7
9
 
8
10
  /** Minimal element stub used to fake `document.activeElement` on web. */
9
11
  interface MockElement {
@@ -232,6 +234,24 @@ describe("PickerSelect", () => {
232
234
  }
233
235
  });
234
236
 
237
+ it("requests body-portal rendering independently of trigger focus", () => {
238
+ ensureDocument();
239
+ savedOS = PlatformModule.OS;
240
+ try {
241
+ PlatformModule.OS = "web";
242
+ const {root} = renderWithTheme(
243
+ <RNPickerSelect {...defaultProps} renderMenuInBodyPortal value="2" />
244
+ );
245
+ const menu = root.findByType(WebDropdownMenu);
246
+
247
+ assert.isTrue(menu.props.renderInBodyPortal);
248
+ assert.isFalse(menu.props.keepTriggerFocus);
249
+ } finally {
250
+ PlatformModule.OS = savedOS;
251
+ restoreDocument();
252
+ }
253
+ });
254
+
235
255
  it("renders web dropdown and opens on focus", async () => {
236
256
  ensureDocument();
237
257
  savedOS = PlatformModule.OS;
@@ -133,6 +133,14 @@ export interface RNPickerSelectProps {
133
133
  * @default false
134
134
  */
135
135
  disableSearch?: boolean;
136
+
137
+ /**
138
+ * Web only. When true, the dropdown menu renders in a fixed portal on
139
+ * `document.body` instead of a React Native `Modal`, so it floats above
140
+ * ancestor portals / high-zIndex overlays (e.g. the `Filter` panel).
141
+ * @default false
142
+ */
143
+ renderMenuInBodyPortal?: boolean;
136
144
  }
137
145
 
138
146
  export const RNPickerSelect = ({
@@ -158,6 +166,7 @@ export const RNPickerSelect = ({
158
166
 
159
167
  InputAccessoryView,
160
168
  disableSearch = false,
169
+ renderMenuInBodyPortal = false,
161
170
  }: RNPickerSelectProps) => {
162
171
  const searchable = !disableSearch;
163
172
  const [showPicker, setShowPicker] = useState<boolean>(false);
@@ -824,6 +833,7 @@ export const RNPickerSelect = ({
824
833
  }}
825
834
  options={menuOptions}
826
835
  presentation={Platform.OS === "android" ? "centered" : "anchored"}
836
+ renderInBodyPortal={renderMenuInBodyPortal}
827
837
  selectedIndex={menuSelectedIndex >= 0 ? menuSelectedIndex : undefined}
828
838
  showEmptyStateWhenNoOptions={searchInTrigger && webSearchQuery.trim().length > 0}
829
839
  testIDPrefix="web_dropdown"
@@ -16,6 +16,7 @@ export const SelectField: FC<SelectFieldProps> = ({
16
16
  requireValue = false,
17
17
  placeholder = "Please select an option.",
18
18
  disableSearch = false,
19
+ renderMenuInBodyPortal = false,
19
20
  title,
20
21
  value,
21
22
  onChange,
@@ -42,6 +43,7 @@ export const SelectField: FC<SelectFieldProps> = ({
42
43
  }
43
44
  }}
44
45
  placeholder={!requireValue ? clearOption : {}}
46
+ renderMenuInBodyPortal={renderMenuInBodyPortal}
45
47
  textInputProps={{testID: fieldTestIDs.input}}
46
48
  value={value ?? ""}
47
49
  />
package/src/Theme.tsx CHANGED
@@ -128,6 +128,8 @@ const defaultTheme: TerrenoThemeConfig = {
128
128
  },
129
129
  surface: {
130
130
  base: "neutral000",
131
+ baseAlternate: "neutral050",
132
+ baseHover: "secondary000",
131
133
  disabled: "neutral500",
132
134
  error: "error200",
133
135
  errorLight: "error000",
@@ -5,7 +5,12 @@ import type {ComponentProps} from "react";
5
5
  import {Dimensions} from "react-native";
6
6
 
7
7
  import {renderWithTheme} from "./test-utils";
8
- import {scheduleAfterPaint, useWebDropdownAnchor, WebDropdownMenu} from "./WebDropdownMenu";
8
+ import {
9
+ resolveDocumentBodyPortalTarget,
10
+ scheduleAfterPaint,
11
+ useWebDropdownAnchor,
12
+ WebDropdownMenu,
13
+ } from "./WebDropdownMenu";
9
14
 
10
15
  describe("WebDropdownMenu", () => {
11
16
  const anchor = {height: 40, width: 200, x: 16, y: 32};
@@ -506,6 +511,20 @@ describe("WebDropdownMenu keepTriggerFocus portal overlay", () => {
506
511
  );
507
512
  };
508
513
 
514
+ it("resolves document.body as the portal target when it is an HTMLElement", () => {
515
+ ensureWebDocument();
516
+ const body = new (globalThis as {HTMLElement: typeof HTMLElement}).HTMLElement();
517
+ Object.defineProperty((globalThis as {document: Document}).document, "body", {
518
+ configurable: true,
519
+ value: body,
520
+ });
521
+ try {
522
+ assert.strictEqual(resolveDocumentBodyPortalTarget(), body);
523
+ } finally {
524
+ restoreWebDocument();
525
+ }
526
+ });
527
+
509
528
  it("renders fixed-position overlay instead of Modal when keepTriggerFocus is true on web", () => {
510
529
  ensureWebDocument();
511
530
  savedOS = PlatformModule.OS;
@@ -531,6 +550,25 @@ describe("WebDropdownMenu keepTriggerFocus portal overlay", () => {
531
550
  }
532
551
  });
533
552
 
553
+ it("renders in the body portal without requiring trigger focus retention", () => {
554
+ ensureWebDocument();
555
+ savedOS = PlatformModule.OS;
556
+ try {
557
+ PlatformModule.OS = "web";
558
+ const {getByTestId, queryByTestId, root} = renderKeepFocusMenu({
559
+ keepTriggerFocus: false,
560
+ renderInBodyPortal: true,
561
+ });
562
+
563
+ assert.isOk(getByTestId("web_dropdown_menu"));
564
+ assert.isNull(queryByTestId("web_dropdown_modal"));
565
+ assert.equal(root.findAll((node) => node.type === "Modal").length, 0);
566
+ } finally {
567
+ PlatformModule.OS = savedOS;
568
+ restoreWebDocument();
569
+ }
570
+ });
571
+
534
572
  it("renders a placeholder view when keepTriggerFocus menu is closed on web", () => {
535
573
  ensureWebDocument();
536
574
  savedOS = PlatformModule.OS;
@@ -644,6 +682,53 @@ describe("useWebDropdownAnchor", () => {
644
682
  expect(result.current.anchor).toEqual({height: 40, width: 100, x: 10, y: 20});
645
683
  });
646
684
 
685
+ it("ignores an older measurement that completes after a newer request", () => {
686
+ const {result} = renderHook(() => useWebDropdownAnchor());
687
+ const callbacks: Array<(x: number, y: number, width: number, height: number) => void> = [];
688
+ const measureInWindow = mock(
689
+ (callback: (x: number, y: number, width: number, height: number) => void): void => {
690
+ callbacks.push(callback);
691
+ }
692
+ );
693
+ (result.current.triggerRef as {current: unknown}).current = {measureInWindow};
694
+ const onFirstMeasured = mock(() => {});
695
+ const onSecondMeasured = mock(() => {});
696
+
697
+ act(() => {
698
+ result.current.measure(onFirstMeasured);
699
+ result.current.measure(onSecondMeasured);
700
+ });
701
+ act(() => {
702
+ callbacks[1](20, 30, 120, 50);
703
+ callbacks[0](10, 15, 100, 40);
704
+ });
705
+
706
+ assert.deepEqual(result.current.anchor, {height: 50, width: 120, x: 20, y: 30});
707
+ assert.equal(onFirstMeasured.mock.calls.length, 0);
708
+ assert.equal(onSecondMeasured.mock.calls.length, 1);
709
+ });
710
+
711
+ it("ignores a pending measurement after cancellation", () => {
712
+ const {result} = renderHook(() => useWebDropdownAnchor());
713
+ let completeMeasurement = (_x: number, _y: number, _width: number, _height: number): void => {};
714
+ const measureInWindow = mock(
715
+ (callback: (x: number, y: number, width: number, height: number) => void): void => {
716
+ completeMeasurement = callback;
717
+ }
718
+ );
719
+ (result.current.triggerRef as {current: unknown}).current = {measureInWindow};
720
+ const onMeasured = mock(() => {});
721
+
722
+ act(() => {
723
+ result.current.measure(onMeasured);
724
+ result.current.cancelPendingMeasurement();
725
+ completeMeasurement(10, 20, 100, 40);
726
+ });
727
+
728
+ assert.deepEqual(result.current.anchor, {height: 0, width: 0, x: 0, y: 0});
729
+ assert.equal(onMeasured.mock.calls.length, 0);
730
+ });
731
+
647
732
  it("exercises the Pressable style callback for hover/pressed states", () => {
648
733
  const {root} = renderWithTheme(
649
734
  <WebDropdownMenu
@@ -24,6 +24,13 @@ export const scheduleAfterPaint = (callback: () => void): void => {
24
24
  }
25
25
  };
26
26
 
27
+ export const resolveDocumentBodyPortalTarget = (): HTMLElement | null => {
28
+ if (typeof document === "undefined" || typeof HTMLElement === "undefined") {
29
+ return null;
30
+ }
31
+ return document.body instanceof HTMLElement ? document.body : null;
32
+ };
33
+
27
34
  export interface WebDropdownMenuOption {
28
35
  key?: string | number;
29
36
  label: string;
@@ -85,6 +92,8 @@ export interface WebDropdownMenuProps {
85
92
  * Web only.
86
93
  */
87
94
  keepTriggerFocus?: boolean;
95
+ /** Web only. Renders the menu in a fixed portal attached to `document.body`. */
96
+ renderInBodyPortal?: boolean;
88
97
  /**
89
98
  * `anchored` positions the menu below/above the trigger (web-style).
90
99
  * `centered` shows a centered dialog, matching Android's native picker modal.
@@ -124,6 +133,7 @@ export const WebDropdownMenu = ({
124
133
  disableSearch = false,
125
134
  showEmptyStateWhenNoOptions = false,
126
135
  keepTriggerFocus = false,
136
+ renderInBodyPortal = false,
127
137
  presentation = "anchored",
128
138
  }: WebDropdownMenuProps): ReactElement => {
129
139
  const searchable = !disableSearch;
@@ -264,6 +274,7 @@ export const WebDropdownMenu = ({
264
274
  const menuLayoutStyle = isCenteredPresentation
265
275
  ? centeredMenuLayoutStyle
266
276
  : anchoredMenuLayoutStyle;
277
+ const usesWebPortal = keepTriggerFocus || renderInBodyPortal;
267
278
 
268
279
  const menuContent = (
269
280
  <>
@@ -375,7 +386,7 @@ export const WebDropdownMenu = ({
375
386
 
376
387
  // Close on Escape when using the portal overlay (Modal handles this itself).
377
388
  useEffect(() => {
378
- if (!visible || !keepTriggerFocus || Platform.OS !== "web" || typeof document === "undefined") {
389
+ if (!visible || !usesWebPortal || Platform.OS !== "web" || typeof document === "undefined") {
379
390
  return;
380
391
  }
381
392
  const closeOnEscape = (event: KeyboardEvent): void => {
@@ -390,9 +401,9 @@ export const WebDropdownMenu = ({
390
401
  document.removeEventListener("keyup", closeOnEscape, false);
391
402
  }
392
403
  };
393
- }, [keepTriggerFocus, onClose, visible]);
404
+ }, [onClose, usesWebPortal, visible]);
394
405
 
395
- if (Platform.OS === "web" && keepTriggerFocus) {
406
+ if (Platform.OS === "web" && usesWebPortal) {
396
407
  if (!visible) {
397
408
  return <View testID={`${testIDPrefix}_modal`} />;
398
409
  }
@@ -428,10 +439,7 @@ export const WebDropdownMenu = ({
428
439
  </View>
429
440
  );
430
441
 
431
- const portalTarget =
432
- typeof document !== "undefined" && document.body instanceof HTMLElement
433
- ? document.body
434
- : null;
442
+ const portalTarget = resolveDocumentBodyPortalTarget();
435
443
 
436
444
  if (portalTarget) {
437
445
  return createWebPortal({children: overlay, container: portalTarget});
@@ -506,23 +514,37 @@ export const WebDropdownMenu = ({
506
514
  export const useWebDropdownAnchor = (): {
507
515
  triggerRef: React.RefObject<View | null>;
508
516
  anchor: WebDropdownAnchor;
517
+ cancelPendingMeasurement: () => void;
509
518
  measure: (onMeasured: (anchor: WebDropdownAnchor) => void) => void;
510
519
  } => {
511
520
  const triggerRef = useRef<View>(null);
512
521
  const [anchor, setAnchor] = useState<WebDropdownAnchor>({height: 0, width: 0, x: 0, y: 0});
522
+ const measurementRequestRef = useRef(0);
523
+
524
+ const cancelPendingMeasurement = useCallback((): void => {
525
+ measurementRequestRef.current += 1;
526
+ }, []);
527
+
528
+ const measure = useCallback(
529
+ (onMeasured: (next: WebDropdownAnchor) => void): void => {
530
+ const requestId = measurementRequestRef.current + 1;
531
+ measurementRequestRef.current = requestId;
532
+ const node = triggerRef.current;
533
+ if (node && typeof node.measureInWindow === "function") {
534
+ node.measureInWindow((x, y, w, h) => {
535
+ if (requestId !== measurementRequestRef.current) {
536
+ return;
537
+ }
538
+ const next = {height: h, width: w, x, y};
539
+ setAnchor(next);
540
+ onMeasured(next);
541
+ });
542
+ return;
543
+ }
544
+ onMeasured(anchor);
545
+ },
546
+ [anchor]
547
+ );
513
548
 
514
- const measure = (onMeasured: (next: WebDropdownAnchor) => void): void => {
515
- const node = triggerRef.current;
516
- if (node && typeof node.measureInWindow === "function") {
517
- node.measureInWindow((x, y, w, h) => {
518
- const next = {height: h, width: w, x, y};
519
- setAnchor(next);
520
- onMeasured(next);
521
- });
522
- return;
523
- }
524
- onMeasured(anchor);
525
- };
526
-
527
- return {anchor, measure, triggerRef};
549
+ return {anchor, cancelPendingMeasurement, measure, triggerRef};
528
550
  };