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