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