@tamagui/floating 2.7.6 → 3.0.0-beta.637.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 (151) hide show
  1. package/dist/cjs/Floating.cjs +10 -11
  2. package/dist/cjs/Floating.native.cjs +37 -0
  3. package/dist/cjs/Floating.native.js +20 -20
  4. package/dist/cjs/Floating.native.js.map +1 -1
  5. package/dist/cjs/index.cjs +45 -46
  6. package/dist/cjs/index.native.cjs +74 -0
  7. package/dist/cjs/index.native.js +45 -45
  8. package/dist/cjs/index.native.js.map +1 -1
  9. package/dist/cjs/interactions/PopupTriggerMap.cjs +36 -39
  10. package/dist/cjs/interactions/PopupTriggerMap.native.cjs +96 -0
  11. package/dist/cjs/interactions/PopupTriggerMap.native.js +76 -75
  12. package/dist/cjs/interactions/PopupTriggerMap.native.js.map +1 -1
  13. package/dist/cjs/interactions/createFloatingEvents.cjs +36 -39
  14. package/dist/cjs/interactions/createFloatingEvents.native.cjs +52 -0
  15. package/dist/cjs/interactions/createFloatingEvents.native.js +39 -41
  16. package/dist/cjs/interactions/createFloatingEvents.native.js.map +1 -1
  17. package/dist/cjs/interactions/safePolygon.cjs +263 -252
  18. package/dist/cjs/interactions/safePolygon.native.cjs +294 -0
  19. package/dist/cjs/interactions/safePolygon.native.js +271 -259
  20. package/dist/cjs/interactions/safePolygon.native.js.map +1 -1
  21. package/dist/cjs/interactions/types.cjs +10 -11
  22. package/dist/cjs/interactions/types.native.cjs +19 -0
  23. package/dist/cjs/interactions/types.native.js +10 -10
  24. package/dist/cjs/interactions/types.native.js.map +1 -1
  25. package/dist/cjs/interactions/useClick.cjs +96 -105
  26. package/dist/cjs/interactions/useClick.native.cjs +122 -0
  27. package/dist/cjs/interactions/useClick.native.js +101 -109
  28. package/dist/cjs/interactions/useClick.native.js.map +1 -1
  29. package/dist/cjs/interactions/useDelayGroup.cjs +86 -91
  30. package/dist/cjs/interactions/useDelayGroup.native.cjs +119 -0
  31. package/dist/cjs/interactions/useDelayGroup.native.js +92 -96
  32. package/dist/cjs/interactions/useDelayGroup.native.js.map +1 -1
  33. package/dist/cjs/interactions/useFocus.cjs +108 -115
  34. package/dist/cjs/interactions/useFocus.native.cjs +131 -0
  35. package/dist/cjs/interactions/useFocus.native.js +114 -120
  36. package/dist/cjs/interactions/useFocus.native.js.map +1 -1
  37. package/dist/cjs/interactions/useHover.cjs +321 -335
  38. package/dist/cjs/interactions/useHover.native.cjs +357 -0
  39. package/dist/cjs/interactions/useHover.native.js +333 -347
  40. package/dist/cjs/interactions/useHover.native.js.map +1 -1
  41. package/dist/cjs/interactions/useInnerOffset.cjs +103 -111
  42. package/dist/cjs/interactions/useInnerOffset.native.cjs +132 -0
  43. package/dist/cjs/interactions/useInnerOffset.native.js +113 -120
  44. package/dist/cjs/interactions/useInnerOffset.native.js.map +1 -1
  45. package/dist/cjs/interactions/useInteractions.cjs +85 -90
  46. package/dist/cjs/interactions/useInteractions.native.cjs +199 -0
  47. package/dist/cjs/interactions/useInteractions.native.js +180 -195
  48. package/dist/cjs/interactions/useInteractions.native.js.map +1 -1
  49. package/dist/cjs/interactions/useListNavigation.cjs +404 -390
  50. package/dist/cjs/interactions/useListNavigation.native.cjs +446 -0
  51. package/dist/cjs/interactions/useListNavigation.native.js +416 -401
  52. package/dist/cjs/interactions/useListNavigation.native.js.map +1 -1
  53. package/dist/cjs/interactions/useRole.cjs +92 -106
  54. package/dist/cjs/interactions/useRole.native.cjs +121 -0
  55. package/dist/cjs/interactions/useRole.native.js +103 -116
  56. package/dist/cjs/interactions/useRole.native.js.map +1 -1
  57. package/dist/cjs/interactions/useTypeahead.cjs +115 -129
  58. package/dist/cjs/interactions/useTypeahead.native.cjs +144 -0
  59. package/dist/cjs/interactions/useTypeahead.native.js +128 -141
  60. package/dist/cjs/interactions/useTypeahead.native.js.map +1 -1
  61. package/dist/cjs/interactions/utils.cjs +126 -143
  62. package/dist/cjs/interactions/utils.native.cjs +209 -0
  63. package/dist/cjs/interactions/utils.native.js +138 -154
  64. package/dist/cjs/interactions/utils.native.js.map +1 -1
  65. package/dist/cjs/middleware/inner.cjs +74 -101
  66. package/dist/cjs/middleware/inner.native.cjs +102 -0
  67. package/dist/cjs/middleware/inner.native.js +83 -109
  68. package/dist/cjs/middleware/inner.native.js.map +1 -1
  69. package/dist/cjs/useFloating.cjs +34 -46
  70. package/dist/cjs/useFloating.native.cjs +56 -0
  71. package/dist/cjs/useFloating.native.js +37 -48
  72. package/dist/cjs/useFloating.native.js.map +1 -1
  73. package/dist/esm/Floating.mjs +1 -2
  74. package/dist/esm/Floating.native.js +6 -4
  75. package/dist/esm/Floating.native.js.map +1 -1
  76. package/dist/esm/index.js +4 -5
  77. package/dist/esm/index.mjs +4 -5
  78. package/dist/esm/index.native.js +4 -5
  79. package/dist/esm/interactions/PopupTriggerMap.mjs +23 -22
  80. package/dist/esm/interactions/PopupTriggerMap.mjs.map +1 -1
  81. package/dist/esm/interactions/PopupTriggerMap.native.js +62 -58
  82. package/dist/esm/interactions/PopupTriggerMap.native.js.map +1 -1
  83. package/dist/esm/interactions/createFloatingEvents.mjs +22 -21
  84. package/dist/esm/interactions/createFloatingEvents.mjs.map +1 -1
  85. package/dist/esm/interactions/createFloatingEvents.native.js +25 -24
  86. package/dist/esm/interactions/createFloatingEvents.native.js.map +1 -1
  87. package/dist/esm/interactions/safePolygon.mjs +250 -234
  88. package/dist/esm/interactions/safePolygon.mjs.map +1 -1
  89. package/dist/esm/interactions/safePolygon.native.js +258 -242
  90. package/dist/esm/interactions/safePolygon.native.js.map +1 -1
  91. package/dist/esm/interactions/types.mjs +0 -2
  92. package/dist/esm/interactions/types.native.js +0 -2
  93. package/dist/esm/interactions/useClick.mjs +83 -87
  94. package/dist/esm/interactions/useClick.mjs.map +1 -1
  95. package/dist/esm/interactions/useClick.native.js +88 -92
  96. package/dist/esm/interactions/useClick.native.js.map +1 -1
  97. package/dist/esm/interactions/useDelayGroup.mjs +68 -65
  98. package/dist/esm/interactions/useDelayGroup.mjs.map +1 -1
  99. package/dist/esm/interactions/useDelayGroup.native.js +74 -71
  100. package/dist/esm/interactions/useDelayGroup.native.js.map +1 -1
  101. package/dist/esm/interactions/useFocus.mjs +95 -97
  102. package/dist/esm/interactions/useFocus.mjs.map +1 -1
  103. package/dist/esm/interactions/useFocus.native.js +101 -103
  104. package/dist/esm/interactions/useFocus.native.js.map +1 -1
  105. package/dist/esm/interactions/useHover.mjs +304 -310
  106. package/dist/esm/interactions/useHover.mjs.map +1 -1
  107. package/dist/esm/interactions/useHover.native.js +316 -323
  108. package/dist/esm/interactions/useHover.native.js.map +1 -1
  109. package/dist/esm/interactions/useInnerOffset.mjs +87 -85
  110. package/dist/esm/interactions/useInnerOffset.mjs.map +1 -1
  111. package/dist/esm/interactions/useInnerOffset.native.js +97 -95
  112. package/dist/esm/interactions/useInnerOffset.native.js.map +1 -1
  113. package/dist/esm/interactions/useInteractions.mjs +71 -72
  114. package/dist/esm/interactions/useInteractions.mjs.map +1 -1
  115. package/dist/esm/interactions/useInteractions.native.js +166 -178
  116. package/dist/esm/interactions/useInteractions.native.js.map +1 -1
  117. package/dist/esm/interactions/useListNavigation.mjs +392 -373
  118. package/dist/esm/interactions/useListNavigation.mjs.map +1 -1
  119. package/dist/esm/interactions/useListNavigation.native.js +404 -385
  120. package/dist/esm/interactions/useListNavigation.native.js.map +1 -1
  121. package/dist/esm/interactions/useRole.mjs +76 -80
  122. package/dist/esm/interactions/useRole.mjs.map +1 -1
  123. package/dist/esm/interactions/useRole.native.js +87 -91
  124. package/dist/esm/interactions/useRole.native.js.map +1 -1
  125. package/dist/esm/interactions/useTypeahead.mjs +102 -111
  126. package/dist/esm/interactions/useTypeahead.mjs.map +1 -1
  127. package/dist/esm/interactions/useTypeahead.native.js +115 -124
  128. package/dist/esm/interactions/useTypeahead.native.js.map +1 -1
  129. package/dist/esm/interactions/utils.mjs +91 -106
  130. package/dist/esm/interactions/utils.mjs.map +1 -1
  131. package/dist/esm/interactions/utils.native.js +103 -118
  132. package/dist/esm/interactions/utils.native.js.map +1 -1
  133. package/dist/esm/middleware/inner.mjs +58 -75
  134. package/dist/esm/middleware/inner.mjs.map +1 -1
  135. package/dist/esm/middleware/inner.native.js +67 -84
  136. package/dist/esm/middleware/inner.native.js.map +1 -1
  137. package/dist/esm/useFloating.mjs +17 -21
  138. package/dist/esm/useFloating.mjs.map +1 -1
  139. package/dist/esm/useFloating.native.js +19 -23
  140. package/dist/esm/useFloating.native.js.map +1 -1
  141. package/package.json +6 -5
  142. package/types/interactions/PopupTriggerMap.d.ts.map +1 -1
  143. package/types/interactions/useInteractions.d.ts.map +1 -1
  144. package/types/middleware/inner.d.ts.map +1 -1
  145. package/types/useFloating.d.ts.map +1 -1
  146. package/dist/esm/Floating.mjs.map +0 -1
  147. package/dist/esm/index.js.map +0 -1
  148. package/dist/esm/index.mjs.map +0 -1
  149. package/dist/esm/index.native.js.map +0 -1
  150. package/dist/esm/interactions/types.mjs.map +0 -1
  151. package/dist/esm/interactions/types.native.js.map +0 -1
@@ -1,29 +1,26 @@
1
+
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
6
  var __export = (target, all) => {
6
- for (var name in all) __defProp(target, name, {
7
- get: all[name],
8
- enumerable: true
9
- });
7
+ for (var name in all) __defProp(target, name, {
8
+ get: all[name],
9
+ enumerable: true
10
+ });
10
11
  };
11
12
  var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
14
- get: () => from[key],
15
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
- });
17
- }
18
- return to;
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
15
+ get: () => from[key],
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ }
19
+ return to;
19
20
  };
20
- var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
21
- value: true
22
- }), mod);
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
22
  var useListNavigation_exports = {};
24
- __export(useListNavigation_exports, {
25
- useListNavigation: () => useListNavigation
26
- });
23
+ __export(useListNavigation_exports, { useListNavigation: () => useListNavigation });
27
24
  module.exports = __toCommonJS(useListNavigation_exports);
28
25
  var import_react = require("react");
29
26
  var import_use_event = require("@tamagui/use-event");
@@ -33,386 +30,403 @@ const ARROW_DOWN = "ArrowDown";
33
30
  const ARROW_LEFT = "ArrowLeft";
34
31
  const ARROW_RIGHT = "ArrowRight";
35
32
  function doSwitch(orientation, vertical, horizontal) {
36
- switch (orientation) {
37
- case "vertical":
38
- return vertical;
39
- case "horizontal":
40
- return horizontal;
41
- default:
42
- return vertical || horizontal;
43
- }
33
+ switch (orientation) {
34
+ case "vertical": return vertical;
35
+ case "horizontal": return horizontal;
36
+ default: return vertical || horizontal;
37
+ }
44
38
  }
45
39
  function isMainOrientationKey(key, orientation) {
46
- const vertical = key === ARROW_UP || key === ARROW_DOWN;
47
- const horizontal = key === ARROW_LEFT || key === ARROW_RIGHT;
48
- return doSwitch(orientation, vertical, horizontal);
40
+ const vertical = key === ARROW_UP || key === ARROW_DOWN;
41
+ const horizontal = key === ARROW_LEFT || key === ARROW_RIGHT;
42
+ return doSwitch(orientation, vertical, horizontal);
49
43
  }
50
44
  function isMainOrientationToEndKey(key, orientation, rtl) {
51
- const vertical = key === ARROW_DOWN;
52
- const horizontal = rtl ? key === ARROW_LEFT : key === ARROW_RIGHT;
53
- return doSwitch(orientation, vertical, horizontal) || key === "Enter" || key === " " || key === "";
45
+ const vertical = key === ARROW_DOWN;
46
+ const horizontal = rtl ? key === ARROW_LEFT : key === ARROW_RIGHT;
47
+ return doSwitch(orientation, vertical, horizontal) || key === "Enter" || key === " " || key === "";
54
48
  }
55
49
  function isCrossOrientationOpenKey(key, orientation, rtl) {
56
- const vertical = rtl ? key === ARROW_LEFT : key === ARROW_RIGHT;
57
- const horizontal = key === ARROW_DOWN;
58
- return doSwitch(orientation, vertical, horizontal);
50
+ const vertical = rtl ? key === ARROW_LEFT : key === ARROW_RIGHT;
51
+ const horizontal = key === ARROW_DOWN;
52
+ return doSwitch(orientation, vertical, horizontal);
59
53
  }
60
54
  function isCrossOrientationCloseKey(key, orientation, rtl) {
61
- const vertical = rtl ? key === ARROW_RIGHT : key === ARROW_LEFT;
62
- const horizontal = key === ARROW_UP;
63
- return doSwitch(orientation, vertical, horizontal);
55
+ const vertical = rtl ? key === ARROW_RIGHT : key === ARROW_LEFT;
56
+ const horizontal = key === ARROW_UP;
57
+ return doSwitch(orientation, vertical, horizontal);
64
58
  }
65
59
  function useListNavigation(context, props) {
66
- const {
67
- open,
68
- onOpenChange,
69
- elements
70
- } = context;
71
- const {
72
- listRef,
73
- activeIndex,
74
- onNavigate: unstable_onNavigate = () => {},
75
- enabled = true,
76
- selectedIndex = null,
77
- allowEscape = false,
78
- loop = false,
79
- nested = false,
80
- rtl = false,
81
- virtual = false,
82
- focusItemOnOpen = "auto",
83
- focusItemOnHover = true,
84
- openOnArrowKeyDown = true,
85
- disabledIndices = void 0,
86
- orientation = "vertical",
87
- scrollItemIntoView = true
88
- } = props;
89
- const typeableComboboxReference = (0, import_utils.isTypeableCombobox)(elements.domReference);
90
- const focusItemOnOpenRef = (0, import_react.useRef)(focusItemOnOpen);
91
- const indexRef = (0, import_react.useRef)(selectedIndex ?? -1);
92
- const keyRef = (0, import_react.useRef)(null);
93
- const isPointerModalityRef = (0, import_react.useRef)(true);
94
- const previousMountedRef = (0, import_react.useRef)(!!elements.floating);
95
- const previousOpenRef = (0, import_react.useRef)(open);
96
- const forceSyncFocusRef = (0, import_react.useRef)(false);
97
- const forceScrollIntoViewRef = (0, import_react.useRef)(false);
98
- const disabledIndicesRef = (0, import_react.useRef)(disabledIndices);
99
- disabledIndicesRef.current = disabledIndices;
100
- const latestOpenRef = (0, import_react.useRef)(open);
101
- latestOpenRef.current = open;
102
- const scrollItemIntoViewRef = (0, import_react.useRef)(scrollItemIntoView);
103
- scrollItemIntoViewRef.current = scrollItemIntoView;
104
- const selectedIndexRef = (0, import_react.useRef)(selectedIndex);
105
- selectedIndexRef.current = selectedIndex;
106
- const stableOnNavigate = (0, import_use_event.useEvent)(unstable_onNavigate);
107
- const [activeId, setActiveId] = (0, import_react.useState)();
108
- const onNavigate = (0, import_use_event.useEvent)(() => {
109
- stableOnNavigate(indexRef.current === -1 ? null : indexRef.current);
110
- });
111
- const previousOnNavigateRef = (0, import_react.useRef)(onNavigate);
112
- const focusItem = (0, import_use_event.useEvent)(() => {
113
- function runFocus(item2) {
114
- if (virtual) {
115
- setActiveId(item2.id);
116
- } else {
117
- (0, import_utils.enqueueFocus)(item2, {
118
- sync: forceSyncFocusRef.current,
119
- preventScroll: true
120
- });
121
- }
122
- }
123
- const initialItem = listRef.current[indexRef.current];
124
- const forceScrollIntoView = forceScrollIntoViewRef.current;
125
- if (initialItem) {
126
- runFocus(initialItem);
127
- }
128
- const scheduler = forceSyncFocusRef.current ? v => v() : requestAnimationFrame;
129
- scheduler(() => {
130
- const waitedItem = listRef.current[indexRef.current] || initialItem;
131
- if (!waitedItem) return;
132
- if (!initialItem) {
133
- runFocus(waitedItem);
134
- }
135
- const scrollIntoViewOptions = scrollItemIntoViewRef.current;
136
- const shouldScrollIntoView = scrollIntoViewOptions && waitedItem && (forceScrollIntoView || !isPointerModalityRef.current);
137
- if (shouldScrollIntoView) {
138
- waitedItem.scrollIntoView?.(typeof scrollIntoViewOptions === "boolean" ? {
139
- block: "nearest",
140
- inline: "nearest"
141
- } : scrollIntoViewOptions);
142
- }
143
- });
144
- });
145
- (0, import_react.useLayoutEffect)(() => {
146
- if (!enabled) return;
147
- if (open && elements.floating) {
148
- if (focusItemOnOpenRef.current && selectedIndex != null) {
149
- forceScrollIntoViewRef.current = true;
150
- indexRef.current = selectedIndex;
151
- onNavigate();
152
- }
153
- } else if (previousMountedRef.current) {
154
- indexRef.current = -1;
155
- previousOnNavigateRef.current();
156
- }
157
- }, [enabled, open, elements.floating, selectedIndex, onNavigate]);
158
- (0, import_react.useLayoutEffect)(() => {
159
- if (!enabled) return;
160
- if (!open) return;
161
- if (!elements.floating) return;
162
- if (activeIndex == null) {
163
- forceSyncFocusRef.current = false;
164
- if (selectedIndexRef.current != null) {
165
- return;
166
- }
167
- if (previousMountedRef.current) {
168
- indexRef.current = -1;
169
- focusItem();
170
- }
171
- if ((!previousOpenRef.current || !previousMountedRef.current) && focusItemOnOpenRef.current && (keyRef.current != null || focusItemOnOpenRef.current === true && keyRef.current == null)) {
172
- let runs = 0;
173
- const waitForListPopulated = () => {
174
- if (listRef.current[0] == null) {
175
- if (runs < 2) {
176
- const scheduler = runs ? requestAnimationFrame : queueMicrotask;
177
- scheduler(waitForListPopulated);
178
- }
179
- runs++;
180
- } else {
181
- indexRef.current = keyRef.current == null || isMainOrientationToEndKey(keyRef.current, orientation, rtl) || nested ? (0, import_utils.getMinListIndex)(listRef, disabledIndicesRef.current) : (0, import_utils.getMaxListIndex)(listRef, disabledIndicesRef.current);
182
- keyRef.current = null;
183
- onNavigate();
184
- }
185
- };
186
- waitForListPopulated();
187
- }
188
- } else if (!(0, import_utils.isIndexOutOfListBounds)(listRef, activeIndex)) {
189
- indexRef.current = activeIndex;
190
- focusItem();
191
- forceScrollIntoViewRef.current = false;
192
- }
193
- }, [enabled, open, elements.floating, activeIndex, selectedIndexRef, nested, listRef, orientation, rtl, onNavigate, focusItem, disabledIndicesRef]);
194
- (0, import_react.useLayoutEffect)(() => {
195
- previousOnNavigateRef.current = onNavigate;
196
- previousOpenRef.current = open;
197
- previousMountedRef.current = !!elements.floating;
198
- });
199
- (0, import_react.useLayoutEffect)(() => {
200
- if (!open) {
201
- keyRef.current = null;
202
- focusItemOnOpenRef.current = focusItemOnOpen;
203
- }
204
- }, [open, focusItemOnOpen]);
205
- const hasActiveIndex = activeIndex != null;
206
- const commonOnKeyDown = (0, import_use_event.useEvent)(event => {
207
- isPointerModalityRef.current = false;
208
- forceSyncFocusRef.current = true;
209
- if (event.which === 229) {
210
- return;
211
- }
212
- if (!latestOpenRef.current && event.currentTarget === elements.floating) {
213
- return;
214
- }
215
- if (nested && isCrossOrientationCloseKey(event.key, orientation, rtl)) {
216
- (0, import_utils.stopEvent)(event);
217
- onOpenChange(false, event.nativeEvent, "list-navigation");
218
- if ((0, import_utils.isHTMLElement)(elements.domReference)) {
219
- elements.domReference.focus();
220
- }
221
- return;
222
- }
223
- const currentIndex = indexRef.current;
224
- const minIndex = (0, import_utils.getMinListIndex)(listRef, disabledIndices);
225
- const maxIndex = (0, import_utils.getMaxListIndex)(listRef, disabledIndices);
226
- if (!typeableComboboxReference) {
227
- if (event.key === "Home") {
228
- (0, import_utils.stopEvent)(event);
229
- indexRef.current = minIndex;
230
- onNavigate();
231
- }
232
- if (event.key === "End") {
233
- (0, import_utils.stopEvent)(event);
234
- indexRef.current = maxIndex;
235
- onNavigate();
236
- }
237
- }
238
- if (isMainOrientationKey(event.key, orientation)) {
239
- (0, import_utils.stopEvent)(event);
240
- if (open && !virtual && (0, import_utils.activeElement)(event.currentTarget.ownerDocument) === event.currentTarget) {
241
- indexRef.current = isMainOrientationToEndKey(event.key, orientation, rtl) ? minIndex : maxIndex;
242
- onNavigate();
243
- return;
244
- }
245
- if (isMainOrientationToEndKey(event.key, orientation, rtl)) {
246
- if (loop) {
247
- indexRef.current = currentIndex >= maxIndex ? allowEscape && currentIndex !== listRef.current.length ? -1 : minIndex : (0, import_utils.findNonDisabledListIndex)(listRef, {
248
- startingIndex: currentIndex,
249
- disabledIndices
250
- });
251
- } else {
252
- indexRef.current = Math.min(maxIndex, (0, import_utils.findNonDisabledListIndex)(listRef, {
253
- startingIndex: currentIndex,
254
- disabledIndices
255
- }));
256
- }
257
- } else {
258
- if (loop) {
259
- indexRef.current = currentIndex <= minIndex ? allowEscape && currentIndex !== -1 ? listRef.current.length : maxIndex : (0, import_utils.findNonDisabledListIndex)(listRef, {
260
- startingIndex: currentIndex,
261
- decrement: true,
262
- disabledIndices
263
- });
264
- } else {
265
- indexRef.current = Math.max(minIndex, (0, import_utils.findNonDisabledListIndex)(listRef, {
266
- startingIndex: currentIndex,
267
- decrement: true,
268
- disabledIndices
269
- }));
270
- }
271
- }
272
- if ((0, import_utils.isIndexOutOfListBounds)(listRef, indexRef.current)) {
273
- indexRef.current = -1;
274
- }
275
- onNavigate();
276
- }
277
- });
278
- const ariaActiveDescendantProp = (0, import_react.useMemo)(() => {
279
- return virtual && open && hasActiveIndex && {
280
- "aria-activedescendant": activeId
281
- };
282
- }, [virtual, open, hasActiveIndex, activeId]);
283
- const floating = (0, import_react.useMemo)(() => {
284
- return {
285
- "aria-orientation": orientation === "both" ? void 0 : orientation,
286
- ...(!typeableComboboxReference ? ariaActiveDescendantProp : {}),
287
- onKeyDown: commonOnKeyDown,
288
- onPointerMove() {
289
- isPointerModalityRef.current = true;
290
- }
291
- };
292
- }, [ariaActiveDescendantProp, commonOnKeyDown, orientation, typeableComboboxReference]);
293
- const reference = (0, import_react.useMemo)(() => {
294
- function checkVirtualMouse(event) {
295
- if (focusItemOnOpen === "auto" && (0, import_utils.isVirtualClick)(event.nativeEvent)) {
296
- focusItemOnOpenRef.current = true;
297
- }
298
- }
299
- function checkVirtualPointer(event) {
300
- focusItemOnOpenRef.current = focusItemOnOpen;
301
- if (focusItemOnOpen === "auto" && (0, import_utils.isVirtualPointerEvent)(event.nativeEvent)) {
302
- focusItemOnOpenRef.current = true;
303
- }
304
- }
305
- return {
306
- ...ariaActiveDescendantProp,
307
- onKeyDown(event) {
308
- isPointerModalityRef.current = false;
309
- const isArrowKey = event.key.startsWith("Arrow");
310
- const isCrossOpenKey = isCrossOrientationOpenKey(event.key, orientation, rtl);
311
- const isMainKey = isMainOrientationKey(event.key, orientation);
312
- const isNavigationKey = (nested ? isCrossOpenKey : isMainKey) || event.key === "Enter" || event.key.trim() === "";
313
- if (virtual && open) {
314
- return commonOnKeyDown(event);
315
- }
316
- if (!open && !openOnArrowKeyDown && isArrowKey) {
317
- return;
318
- }
319
- if (isNavigationKey) {
320
- keyRef.current = event.key;
321
- }
322
- if (nested) {
323
- if (isCrossOpenKey) {
324
- (0, import_utils.stopEvent)(event);
325
- if (open) {
326
- indexRef.current = (0, import_utils.getMinListIndex)(listRef, disabledIndicesRef.current);
327
- onNavigate();
328
- } else {
329
- onOpenChange(true, event.nativeEvent, "list-navigation");
330
- }
331
- }
332
- return;
333
- }
334
- if (isMainKey) {
335
- if (selectedIndex != null) {
336
- indexRef.current = selectedIndex;
337
- }
338
- (0, import_utils.stopEvent)(event);
339
- if (!open && openOnArrowKeyDown) {
340
- onOpenChange(true, event.nativeEvent, "list-navigation");
341
- } else {
342
- commonOnKeyDown(event);
343
- }
344
- if (open) {
345
- onNavigate();
346
- }
347
- }
348
- },
349
- onFocus() {
350
- if (open && !virtual) {
351
- indexRef.current = -1;
352
- onNavigate();
353
- }
354
- },
355
- onPointerDown: checkVirtualPointer,
356
- onPointerEnter: checkVirtualPointer,
357
- onMouseDown: checkVirtualMouse,
358
- onClick: checkVirtualMouse
359
- };
360
- }, [ariaActiveDescendantProp, commonOnKeyDown, disabledIndicesRef, focusItemOnOpen, listRef, nested, onNavigate, onOpenChange, open, openOnArrowKeyDown, orientation, rtl, selectedIndex, virtual]);
361
- const item = (0, import_react.useMemo)(() => {
362
- function syncCurrentTarget(currentTarget) {
363
- if (!latestOpenRef.current) return;
364
- const index = listRef.current.indexOf(currentTarget);
365
- if (index !== -1 && indexRef.current !== index) {
366
- indexRef.current = index;
367
- onNavigate();
368
- }
369
- }
370
- const itemProps = {
371
- onFocus({
372
- currentTarget
373
- }) {
374
- forceSyncFocusRef.current = true;
375
- syncCurrentTarget(currentTarget);
376
- },
377
- onClick: ({
378
- currentTarget
379
- }) => currentTarget.focus({
380
- preventScroll: true
381
- }),
382
- // safari
383
- onMouseMove({
384
- currentTarget
385
- }) {
386
- forceSyncFocusRef.current = true;
387
- forceScrollIntoViewRef.current = false;
388
- if (focusItemOnHover) {
389
- syncCurrentTarget(currentTarget);
390
- }
391
- },
392
- onPointerLeave({
393
- pointerType
394
- }) {
395
- if (!isPointerModalityRef.current || pointerType === "touch") {
396
- return;
397
- }
398
- forceSyncFocusRef.current = true;
399
- if (!focusItemOnHover) {
400
- return;
401
- }
402
- indexRef.current = -1;
403
- onNavigate();
404
- if (!virtual) {
405
- elements.floating?.focus({
406
- preventScroll: true
407
- });
408
- }
409
- }
410
- };
411
- return itemProps;
412
- }, [latestOpenRef, focusItemOnHover, listRef, onNavigate, virtual, elements.floating]);
413
- return (0, import_react.useMemo)(() => enabled ? {
414
- reference,
415
- floating,
416
- item
417
- } : {}, [enabled, reference, floating, item]);
418
- }
60
+ const { open, onOpenChange, elements } = context;
61
+ const { listRef, activeIndex, onNavigate: unstable_onNavigate = () => {}, enabled = true, selectedIndex = null, allowEscape = false, loop = false, nested = false, rtl = false, virtual = false, focusItemOnOpen = "auto", focusItemOnHover = true, openOnArrowKeyDown = true, disabledIndices = void 0, orientation = "vertical", scrollItemIntoView = true } = props;
62
+ const typeableComboboxReference = (0, import_utils.isTypeableCombobox)(elements.domReference);
63
+ const focusItemOnOpenRef = (0, import_react.useRef)(focusItemOnOpen);
64
+ const indexRef = (0, import_react.useRef)(selectedIndex ?? -1);
65
+ const keyRef = (0, import_react.useRef)(null);
66
+ const isPointerModalityRef = (0, import_react.useRef)(true);
67
+ const previousMountedRef = (0, import_react.useRef)(!!elements.floating);
68
+ const previousOpenRef = (0, import_react.useRef)(open);
69
+ const forceSyncFocusRef = (0, import_react.useRef)(false);
70
+ const forceScrollIntoViewRef = (0, import_react.useRef)(false);
71
+ const disabledIndicesRef = (0, import_react.useRef)(disabledIndices);
72
+ disabledIndicesRef.current = disabledIndices;
73
+ const latestOpenRef = (0, import_react.useRef)(open);
74
+ latestOpenRef.current = open;
75
+ const scrollItemIntoViewRef = (0, import_react.useRef)(scrollItemIntoView);
76
+ scrollItemIntoViewRef.current = scrollItemIntoView;
77
+ const selectedIndexRef = (0, import_react.useRef)(selectedIndex);
78
+ selectedIndexRef.current = selectedIndex;
79
+ const stableOnNavigate = (0, import_use_event.useEvent)(unstable_onNavigate);
80
+ const [activeId, setActiveId] = (0, import_react.useState)();
81
+ const onNavigate = (0, import_use_event.useEvent)(() => {
82
+ stableOnNavigate(indexRef.current === -1 ? null : indexRef.current);
83
+ });
84
+ const previousOnNavigateRef = (0, import_react.useRef)(onNavigate);
85
+ const focusItem = (0, import_use_event.useEvent)(() => {
86
+ function runFocus(item2) {
87
+ if (virtual) {
88
+ setActiveId(item2.id);
89
+ } else {
90
+ (0, import_utils.enqueueFocus)(item2, {
91
+ sync: forceSyncFocusRef.current,
92
+ preventScroll: true
93
+ });
94
+ }
95
+ }
96
+ const initialItem = listRef.current[indexRef.current];
97
+ const forceScrollIntoView = forceScrollIntoViewRef.current;
98
+ if (initialItem) {
99
+ runFocus(initialItem);
100
+ }
101
+ const scheduler = forceSyncFocusRef.current ? (v) => v() : requestAnimationFrame;
102
+ scheduler(() => {
103
+ const waitedItem = listRef.current[indexRef.current] || initialItem;
104
+ if (!waitedItem) return;
105
+ if (!initialItem) {
106
+ runFocus(waitedItem);
107
+ }
108
+ const scrollIntoViewOptions = scrollItemIntoViewRef.current;
109
+ const shouldScrollIntoView = scrollIntoViewOptions && waitedItem && (forceScrollIntoView || !isPointerModalityRef.current);
110
+ if (shouldScrollIntoView) {
111
+ waitedItem.scrollIntoView?.(typeof scrollIntoViewOptions === "boolean" ? {
112
+ block: "nearest",
113
+ inline: "nearest"
114
+ } : scrollIntoViewOptions);
115
+ }
116
+ });
117
+ });
118
+ (0, import_react.useLayoutEffect)(() => {
119
+ if (!enabled) return;
120
+ if (open && elements.floating) {
121
+ if (focusItemOnOpenRef.current && selectedIndex != null) {
122
+ forceScrollIntoViewRef.current = true;
123
+ indexRef.current = selectedIndex;
124
+ onNavigate();
125
+ }
126
+ } else if (previousMountedRef.current) {
127
+ indexRef.current = -1;
128
+ previousOnNavigateRef.current();
129
+ }
130
+ }, [
131
+ enabled,
132
+ open,
133
+ elements.floating,
134
+ selectedIndex,
135
+ onNavigate
136
+ ]);
137
+ (0, import_react.useLayoutEffect)(() => {
138
+ if (!enabled) return;
139
+ if (!open) return;
140
+ if (!elements.floating) return;
141
+ if (activeIndex == null) {
142
+ forceSyncFocusRef.current = false;
143
+ if (selectedIndexRef.current != null) {
144
+ return;
145
+ }
146
+ if (previousMountedRef.current) {
147
+ indexRef.current = -1;
148
+ focusItem();
149
+ }
150
+ if ((!previousOpenRef.current || !previousMountedRef.current) && focusItemOnOpenRef.current && (keyRef.current != null || focusItemOnOpenRef.current === true && keyRef.current == null)) {
151
+ let runs = 0;
152
+ const waitForListPopulated = () => {
153
+ if (listRef.current[0] == null) {
154
+ if (runs < 2) {
155
+ const scheduler = runs ? requestAnimationFrame : queueMicrotask;
156
+ scheduler(waitForListPopulated);
157
+ }
158
+ runs++;
159
+ } else {
160
+ indexRef.current = keyRef.current == null || isMainOrientationToEndKey(keyRef.current, orientation, rtl) || nested ? (0, import_utils.getMinListIndex)(listRef, disabledIndicesRef.current) : (0, import_utils.getMaxListIndex)(listRef, disabledIndicesRef.current);
161
+ keyRef.current = null;
162
+ onNavigate();
163
+ }
164
+ };
165
+ waitForListPopulated();
166
+ }
167
+ } else if (!(0, import_utils.isIndexOutOfListBounds)(listRef, activeIndex)) {
168
+ indexRef.current = activeIndex;
169
+ focusItem();
170
+ forceScrollIntoViewRef.current = false;
171
+ }
172
+ }, [
173
+ enabled,
174
+ open,
175
+ elements.floating,
176
+ activeIndex,
177
+ selectedIndexRef,
178
+ nested,
179
+ listRef,
180
+ orientation,
181
+ rtl,
182
+ onNavigate,
183
+ focusItem,
184
+ disabledIndicesRef
185
+ ]);
186
+ (0, import_react.useLayoutEffect)(() => {
187
+ previousOnNavigateRef.current = onNavigate;
188
+ previousOpenRef.current = open;
189
+ previousMountedRef.current = !!elements.floating;
190
+ });
191
+ (0, import_react.useLayoutEffect)(() => {
192
+ if (!open) {
193
+ keyRef.current = null;
194
+ focusItemOnOpenRef.current = focusItemOnOpen;
195
+ }
196
+ }, [open, focusItemOnOpen]);
197
+ const hasActiveIndex = activeIndex != null;
198
+ const commonOnKeyDown = (0, import_use_event.useEvent)((event) => {
199
+ isPointerModalityRef.current = false;
200
+ forceSyncFocusRef.current = true;
201
+ if (event.which === 229) {
202
+ return;
203
+ }
204
+ if (!latestOpenRef.current && event.currentTarget === elements.floating) {
205
+ return;
206
+ }
207
+ if (nested && isCrossOrientationCloseKey(event.key, orientation, rtl)) {
208
+ (0, import_utils.stopEvent)(event);
209
+ onOpenChange(false, event.nativeEvent, "list-navigation");
210
+ if ((0, import_utils.isHTMLElement)(elements.domReference)) {
211
+ elements.domReference.focus();
212
+ }
213
+ return;
214
+ }
215
+ const currentIndex = indexRef.current;
216
+ const minIndex = (0, import_utils.getMinListIndex)(listRef, disabledIndices);
217
+ const maxIndex = (0, import_utils.getMaxListIndex)(listRef, disabledIndices);
218
+ if (!typeableComboboxReference) {
219
+ if (event.key === "Home") {
220
+ (0, import_utils.stopEvent)(event);
221
+ indexRef.current = minIndex;
222
+ onNavigate();
223
+ }
224
+ if (event.key === "End") {
225
+ (0, import_utils.stopEvent)(event);
226
+ indexRef.current = maxIndex;
227
+ onNavigate();
228
+ }
229
+ }
230
+ if (isMainOrientationKey(event.key, orientation)) {
231
+ (0, import_utils.stopEvent)(event);
232
+ if (open && !virtual && (0, import_utils.activeElement)(event.currentTarget.ownerDocument) === event.currentTarget) {
233
+ indexRef.current = isMainOrientationToEndKey(event.key, orientation, rtl) ? minIndex : maxIndex;
234
+ onNavigate();
235
+ return;
236
+ }
237
+ if (isMainOrientationToEndKey(event.key, orientation, rtl)) {
238
+ if (loop) {
239
+ indexRef.current = currentIndex >= maxIndex ? allowEscape && currentIndex !== listRef.current.length ? -1 : minIndex : (0, import_utils.findNonDisabledListIndex)(listRef, {
240
+ startingIndex: currentIndex,
241
+ disabledIndices
242
+ });
243
+ } else {
244
+ indexRef.current = Math.min(maxIndex, (0, import_utils.findNonDisabledListIndex)(listRef, {
245
+ startingIndex: currentIndex,
246
+ disabledIndices
247
+ }));
248
+ }
249
+ } else {
250
+ if (loop) {
251
+ indexRef.current = currentIndex <= minIndex ? allowEscape && currentIndex !== -1 ? listRef.current.length : maxIndex : (0, import_utils.findNonDisabledListIndex)(listRef, {
252
+ startingIndex: currentIndex,
253
+ decrement: true,
254
+ disabledIndices
255
+ });
256
+ } else {
257
+ indexRef.current = Math.max(minIndex, (0, import_utils.findNonDisabledListIndex)(listRef, {
258
+ startingIndex: currentIndex,
259
+ decrement: true,
260
+ disabledIndices
261
+ }));
262
+ }
263
+ }
264
+ if ((0, import_utils.isIndexOutOfListBounds)(listRef, indexRef.current)) {
265
+ indexRef.current = -1;
266
+ }
267
+ onNavigate();
268
+ }
269
+ });
270
+ const ariaActiveDescendantProp = (0, import_react.useMemo)(() => {
271
+ return virtual && open && hasActiveIndex && { "aria-activedescendant": activeId };
272
+ }, [
273
+ virtual,
274
+ open,
275
+ hasActiveIndex,
276
+ activeId
277
+ ]);
278
+ const floating = (0, import_react.useMemo)(() => {
279
+ return {
280
+ "aria-orientation": orientation === "both" ? void 0 : orientation,
281
+ ...!typeableComboboxReference ? ariaActiveDescendantProp : {},
282
+ onKeyDown: commonOnKeyDown,
283
+ onPointerMove() {
284
+ isPointerModalityRef.current = true;
285
+ }
286
+ };
287
+ }, [
288
+ ariaActiveDescendantProp,
289
+ commonOnKeyDown,
290
+ orientation,
291
+ typeableComboboxReference
292
+ ]);
293
+ const reference = (0, import_react.useMemo)(() => {
294
+ function checkVirtualMouse(event) {
295
+ if (focusItemOnOpen === "auto" && (0, import_utils.isVirtualClick)(event.nativeEvent)) {
296
+ focusItemOnOpenRef.current = true;
297
+ }
298
+ }
299
+ function checkVirtualPointer(event) {
300
+ focusItemOnOpenRef.current = focusItemOnOpen;
301
+ if (focusItemOnOpen === "auto" && (0, import_utils.isVirtualPointerEvent)(event.nativeEvent)) {
302
+ focusItemOnOpenRef.current = true;
303
+ }
304
+ }
305
+ return {
306
+ ...ariaActiveDescendantProp,
307
+ onKeyDown(event) {
308
+ isPointerModalityRef.current = false;
309
+ const isArrowKey = event.key.startsWith("Arrow");
310
+ const isCrossOpenKey = isCrossOrientationOpenKey(event.key, orientation, rtl);
311
+ const isMainKey = isMainOrientationKey(event.key, orientation);
312
+ const isNavigationKey = (nested ? isCrossOpenKey : isMainKey) || event.key === "Enter" || event.key.trim() === "";
313
+ if (virtual && open) {
314
+ return commonOnKeyDown(event);
315
+ }
316
+ if (!open && !openOnArrowKeyDown && isArrowKey) {
317
+ return;
318
+ }
319
+ if (isNavigationKey) {
320
+ keyRef.current = event.key;
321
+ }
322
+ if (nested) {
323
+ if (isCrossOpenKey) {
324
+ (0, import_utils.stopEvent)(event);
325
+ if (open) {
326
+ indexRef.current = (0, import_utils.getMinListIndex)(listRef, disabledIndicesRef.current);
327
+ onNavigate();
328
+ } else {
329
+ onOpenChange(true, event.nativeEvent, "list-navigation");
330
+ }
331
+ }
332
+ return;
333
+ }
334
+ if (isMainKey) {
335
+ if (selectedIndex != null) {
336
+ indexRef.current = selectedIndex;
337
+ }
338
+ (0, import_utils.stopEvent)(event);
339
+ if (!open && openOnArrowKeyDown) {
340
+ onOpenChange(true, event.nativeEvent, "list-navigation");
341
+ } else {
342
+ commonOnKeyDown(event);
343
+ }
344
+ if (open) {
345
+ onNavigate();
346
+ }
347
+ }
348
+ },
349
+ onFocus() {
350
+ if (open && !virtual) {
351
+ indexRef.current = -1;
352
+ onNavigate();
353
+ }
354
+ },
355
+ onPointerDown: checkVirtualPointer,
356
+ onPointerEnter: checkVirtualPointer,
357
+ onMouseDown: checkVirtualMouse,
358
+ onClick: checkVirtualMouse
359
+ };
360
+ }, [
361
+ ariaActiveDescendantProp,
362
+ commonOnKeyDown,
363
+ disabledIndicesRef,
364
+ focusItemOnOpen,
365
+ listRef,
366
+ nested,
367
+ onNavigate,
368
+ onOpenChange,
369
+ open,
370
+ openOnArrowKeyDown,
371
+ orientation,
372
+ rtl,
373
+ selectedIndex,
374
+ virtual
375
+ ]);
376
+ const item = (0, import_react.useMemo)(() => {
377
+ function syncCurrentTarget(currentTarget) {
378
+ if (!latestOpenRef.current) return;
379
+ const index = listRef.current.indexOf(currentTarget);
380
+ if (index !== -1 && indexRef.current !== index) {
381
+ indexRef.current = index;
382
+ onNavigate();
383
+ }
384
+ }
385
+ const itemProps = {
386
+ onFocus({ currentTarget }) {
387
+ forceSyncFocusRef.current = true;
388
+ syncCurrentTarget(currentTarget);
389
+ },
390
+ onClick: ({ currentTarget }) => currentTarget.focus({ preventScroll: true }),
391
+ onMouseMove({ currentTarget }) {
392
+ forceSyncFocusRef.current = true;
393
+ forceScrollIntoViewRef.current = false;
394
+ if (focusItemOnHover) {
395
+ syncCurrentTarget(currentTarget);
396
+ }
397
+ },
398
+ onPointerLeave({ pointerType }) {
399
+ if (!isPointerModalityRef.current || pointerType === "touch") {
400
+ return;
401
+ }
402
+ forceSyncFocusRef.current = true;
403
+ if (!focusItemOnHover) {
404
+ return;
405
+ }
406
+ indexRef.current = -1;
407
+ onNavigate();
408
+ if (!virtual) {
409
+ elements.floating?.focus({ preventScroll: true });
410
+ }
411
+ }
412
+ };
413
+ return itemProps;
414
+ }, [
415
+ latestOpenRef,
416
+ focusItemOnHover,
417
+ listRef,
418
+ onNavigate,
419
+ virtual,
420
+ elements.floating
421
+ ]);
422
+ return (0, import_react.useMemo)(() => enabled ? {
423
+ reference,
424
+ floating,
425
+ item
426
+ } : {}, [
427
+ enabled,
428
+ reference,
429
+ floating,
430
+ item
431
+ ]);
432
+ }