@hanzogui/floating 3.0.6 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/dist/cjs/Floating.cjs +7 -5
  2. package/dist/cjs/Floating.native.js +18 -16
  3. package/dist/cjs/Floating.native.js.map +1 -1
  4. package/dist/cjs/index.cjs +28 -26
  5. package/dist/cjs/index.native.js +28 -26
  6. package/dist/cjs/index.native.js.map +1 -1
  7. package/dist/cjs/interactions/PopupTriggerMap.cjs +21 -12
  8. package/dist/cjs/interactions/PopupTriggerMap.native.js +51 -27
  9. package/dist/cjs/interactions/PopupTriggerMap.native.js.map +1 -1
  10. package/dist/cjs/interactions/createFloatingEvents.cjs +21 -12
  11. package/dist/cjs/interactions/createFloatingEvents.native.js +22 -13
  12. package/dist/cjs/interactions/createFloatingEvents.native.js.map +1 -1
  13. package/dist/cjs/interactions/safePolygon.cjs +213 -131
  14. package/dist/cjs/interactions/safePolygon.native.js +182 -103
  15. package/dist/cjs/interactions/safePolygon.native.js.map +1 -1
  16. package/dist/cjs/interactions/types.cjs +7 -5
  17. package/dist/cjs/interactions/types.native.js +7 -5
  18. package/dist/cjs/interactions/types.native.js.map +1 -1
  19. package/dist/cjs/interactions/useClick.cjs +91 -49
  20. package/dist/cjs/interactions/useClick.native.js +96 -54
  21. package/dist/cjs/interactions/useClick.native.js.map +1 -1
  22. package/dist/cjs/interactions/useDelayGroup.cjs +63 -44
  23. package/dist/cjs/interactions/useDelayGroup.native.js +74 -58
  24. package/dist/cjs/interactions/useDelayGroup.native.js.map +1 -1
  25. package/dist/cjs/interactions/useFocus.cjs +73 -44
  26. package/dist/cjs/interactions/useFocus.native.js +75 -49
  27. package/dist/cjs/interactions/useFocus.native.js.map +1 -1
  28. package/dist/cjs/interactions/useHover.cjs +242 -109
  29. package/dist/cjs/interactions/useHover.native.js +268 -143
  30. package/dist/cjs/interactions/useHover.native.js.map +1 -1
  31. package/dist/cjs/interactions/useInnerOffset.cjs +88 -64
  32. package/dist/cjs/interactions/useInnerOffset.native.js +88 -65
  33. package/dist/cjs/interactions/useInnerOffset.native.js.map +1 -1
  34. package/dist/cjs/interactions/useInteractions.cjs +60 -31
  35. package/dist/cjs/interactions/useInteractions.native.js +133 -81
  36. package/dist/cjs/interactions/useInteractions.native.js.map +1 -1
  37. package/dist/cjs/interactions/useListNavigation.cjs +346 -181
  38. package/dist/cjs/interactions/useListNavigation.native.js +350 -214
  39. package/dist/cjs/interactions/useListNavigation.native.js.map +1 -1
  40. package/dist/cjs/interactions/useRole.cjs +83 -73
  41. package/dist/cjs/interactions/useRole.native.js +107 -99
  42. package/dist/cjs/interactions/useRole.native.js.map +1 -1
  43. package/dist/cjs/interactions/useTypeahead.cjs +115 -65
  44. package/dist/cjs/interactions/useTypeahead.native.js +122 -77
  45. package/dist/cjs/interactions/useTypeahead.native.js.map +1 -1
  46. package/dist/cjs/interactions/utils.cjs +68 -38
  47. package/dist/cjs/interactions/utils.native.js +93 -54
  48. package/dist/cjs/interactions/utils.native.js.map +1 -1
  49. package/dist/cjs/middleware/inner.cjs +79 -67
  50. package/dist/cjs/middleware/inner.native.js +86 -76
  51. package/dist/cjs/middleware/inner.native.js.map +1 -1
  52. package/dist/cjs/useFloating.cjs +48 -45
  53. package/dist/cjs/useFloating.native.js +50 -48
  54. package/dist/cjs/useFloating.native.js.map +1 -1
  55. package/dist/esm/Floating.native.js +5 -5
  56. package/dist/esm/Floating.native.js.map +1 -1
  57. package/dist/esm/interactions/PopupTriggerMap.mjs +9 -2
  58. package/dist/esm/interactions/PopupTriggerMap.mjs.map +1 -1
  59. package/dist/esm/interactions/PopupTriggerMap.native.js +39 -17
  60. package/dist/esm/interactions/PopupTriggerMap.native.js.map +1 -1
  61. package/dist/esm/interactions/createFloatingEvents.mjs +9 -2
  62. package/dist/esm/interactions/createFloatingEvents.mjs.map +1 -1
  63. package/dist/esm/interactions/createFloatingEvents.native.js +10 -3
  64. package/dist/esm/interactions/createFloatingEvents.native.js.map +1 -1
  65. package/dist/esm/interactions/safePolygon.mjs +201 -121
  66. package/dist/esm/interactions/safePolygon.mjs.map +1 -1
  67. package/dist/esm/interactions/safePolygon.native.js +170 -93
  68. package/dist/esm/interactions/safePolygon.native.js.map +1 -1
  69. package/dist/esm/interactions/useClick.mjs +79 -39
  70. package/dist/esm/interactions/useClick.mjs.map +1 -1
  71. package/dist/esm/interactions/useClick.native.js +82 -42
  72. package/dist/esm/interactions/useClick.native.js.map +1 -1
  73. package/dist/esm/interactions/useDelayGroup.mjs +39 -22
  74. package/dist/esm/interactions/useDelayGroup.mjs.map +1 -1
  75. package/dist/esm/interactions/useDelayGroup.native.js +42 -28
  76. package/dist/esm/interactions/useDelayGroup.native.js.map +1 -1
  77. package/dist/esm/interactions/useFocus.mjs +59 -32
  78. package/dist/esm/interactions/useFocus.mjs.map +1 -1
  79. package/dist/esm/interactions/useFocus.native.js +61 -37
  80. package/dist/esm/interactions/useFocus.native.js.map +1 -1
  81. package/dist/esm/interactions/useHover.mjs +215 -84
  82. package/dist/esm/interactions/useHover.mjs.map +1 -1
  83. package/dist/esm/interactions/useHover.native.js +240 -117
  84. package/dist/esm/interactions/useHover.native.js.map +1 -1
  85. package/dist/esm/interactions/useInnerOffset.mjs +61 -39
  86. package/dist/esm/interactions/useInnerOffset.mjs.map +1 -1
  87. package/dist/esm/interactions/useInnerOffset.native.js +61 -40
  88. package/dist/esm/interactions/useInnerOffset.native.js.map +1 -1
  89. package/dist/esm/interactions/useInteractions.mjs +48 -21
  90. package/dist/esm/interactions/useInteractions.mjs.map +1 -1
  91. package/dist/esm/interactions/useInteractions.native.js +121 -71
  92. package/dist/esm/interactions/useInteractions.native.js.map +1 -1
  93. package/dist/esm/interactions/useListNavigation.mjs +331 -168
  94. package/dist/esm/interactions/useListNavigation.mjs.map +1 -1
  95. package/dist/esm/interactions/useListNavigation.native.js +335 -201
  96. package/dist/esm/interactions/useListNavigation.native.js.map +1 -1
  97. package/dist/esm/interactions/useRole.mjs +59 -51
  98. package/dist/esm/interactions/useRole.mjs.map +1 -1
  99. package/dist/esm/interactions/useRole.native.js +82 -76
  100. package/dist/esm/interactions/useRole.native.js.map +1 -1
  101. package/dist/esm/interactions/useTypeahead.mjs +100 -52
  102. package/dist/esm/interactions/useTypeahead.mjs.map +1 -1
  103. package/dist/esm/interactions/useTypeahead.native.js +107 -64
  104. package/dist/esm/interactions/useTypeahead.native.js.map +1 -1
  105. package/dist/esm/interactions/utils.mjs +56 -28
  106. package/dist/esm/interactions/utils.mjs.map +1 -1
  107. package/dist/esm/interactions/utils.native.js +81 -44
  108. package/dist/esm/interactions/utils.native.js.map +1 -1
  109. package/dist/esm/middleware/inner.mjs +54 -44
  110. package/dist/esm/middleware/inner.mjs.map +1 -1
  111. package/dist/esm/middleware/inner.native.js +61 -53
  112. package/dist/esm/middleware/inner.native.js.map +1 -1
  113. package/dist/esm/useFloating.mjs +22 -21
  114. package/dist/esm/useFloating.mjs.map +1 -1
  115. package/dist/esm/useFloating.native.js +24 -24
  116. package/dist/esm/useFloating.native.js.map +1 -1
  117. package/package.json +3 -3
  118. package/src/middleware/inner.ts +4 -5
  119. package/types/middleware/inner.d.ts.map +1 -1
@@ -1,10 +1,10 @@
1
1
  import { useLayoutEffect, useMemo, useRef, useState } from "react";
2
2
  import { useEvent } from "@hanzogui/use-event";
3
3
  import { activeElement, enqueueFocus, findNonDisabledListIndex, getMinListIndex, getMaxListIndex, isHTMLElement, isIndexOutOfListBounds, isTypeableCombobox, isVirtualClick, isVirtualPointerEvent, stopEvent } from "./utils.native.js";
4
- var ARROW_UP = "ArrowUp",
5
- ARROW_DOWN = "ArrowDown",
6
- ARROW_LEFT = "ArrowLeft",
7
- ARROW_RIGHT = "ArrowRight";
4
+ var ARROW_UP = "ArrowUp";
5
+ var ARROW_DOWN = "ArrowDown";
6
+ var ARROW_LEFT = "ArrowLeft";
7
+ var ARROW_RIGHT = "ArrowRight";
8
8
  function doSwitch(orientation, vertical, horizontal) {
9
9
  switch (orientation) {
10
10
  case "vertical":
@@ -16,59 +16,59 @@ function doSwitch(orientation, vertical, horizontal) {
16
16
  }
17
17
  }
18
18
  function isMainOrientationKey(key, orientation) {
19
- var vertical = key === ARROW_UP || key === ARROW_DOWN,
20
- horizontal = key === ARROW_LEFT || key === ARROW_RIGHT;
19
+ var vertical = key === ARROW_UP || key === ARROW_DOWN;
20
+ var horizontal = key === ARROW_LEFT || key === ARROW_RIGHT;
21
21
  return doSwitch(orientation, vertical, horizontal);
22
22
  }
23
23
  function isMainOrientationToEndKey(key, orientation, rtl) {
24
- var vertical = key === ARROW_DOWN,
25
- horizontal = rtl ? key === ARROW_LEFT : key === ARROW_RIGHT;
24
+ var vertical = key === ARROW_DOWN;
25
+ var horizontal = rtl ? key === ARROW_LEFT : key === ARROW_RIGHT;
26
26
  return doSwitch(orientation, vertical, horizontal) || key === "Enter" || key === " " || key === "";
27
27
  }
28
28
  function isCrossOrientationOpenKey(key, orientation, rtl) {
29
- var vertical = rtl ? key === ARROW_LEFT : key === ARROW_RIGHT,
30
- horizontal = key === ARROW_DOWN;
29
+ var vertical = rtl ? key === ARROW_LEFT : key === ARROW_RIGHT;
30
+ var horizontal = key === ARROW_DOWN;
31
31
  return doSwitch(orientation, vertical, horizontal);
32
32
  }
33
33
  function isCrossOrientationCloseKey(key, orientation, rtl) {
34
- var vertical = rtl ? key === ARROW_RIGHT : key === ARROW_LEFT,
35
- horizontal = key === ARROW_UP;
34
+ var vertical = rtl ? key === ARROW_RIGHT : key === ARROW_LEFT;
35
+ var horizontal = key === ARROW_UP;
36
36
  return doSwitch(orientation, vertical, horizontal);
37
37
  }
38
38
  function useListNavigation(context, props) {
39
39
  var {
40
- open,
41
- onOpenChange,
42
- elements
43
- } = context,
44
- {
45
- listRef,
46
- activeIndex,
47
- onNavigate: unstable_onNavigate = function () {},
48
- enabled = !0,
49
- selectedIndex = null,
50
- allowEscape = !1,
51
- loop = !1,
52
- nested = !1,
53
- rtl = !1,
54
- virtual = !1,
55
- focusItemOnOpen = "auto",
56
- focusItemOnHover = !0,
57
- openOnArrowKeyDown = !0,
58
- disabledIndices = void 0,
59
- orientation = "vertical",
60
- scrollItemIntoView = !0
61
- } = props,
62
- typeableComboboxReference = isTypeableCombobox(elements.domReference),
63
- focusItemOnOpenRef = useRef(focusItemOnOpen),
64
- indexRef = useRef(selectedIndex ?? -1),
65
- keyRef = useRef(null),
66
- isPointerModalityRef = useRef(!0),
67
- previousMountedRef = useRef(!!elements.floating),
68
- previousOpenRef = useRef(open),
69
- forceSyncFocusRef = useRef(!1),
70
- forceScrollIntoViewRef = useRef(!1),
71
- disabledIndicesRef = useRef(disabledIndices);
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
72
  disabledIndicesRef.current = disabledIndices;
73
73
  var latestOpenRef = useRef(open);
74
74
  latestOpenRef.current = open;
@@ -76,189 +76,323 @@ function useListNavigation(context, props) {
76
76
  scrollItemIntoViewRef.current = scrollItemIntoView;
77
77
  var selectedIndexRef = useRef(selectedIndex);
78
78
  selectedIndexRef.current = selectedIndex;
79
- var stableOnNavigate = useEvent(unstable_onNavigate),
80
- [activeId, setActiveId] = useState(),
81
- onNavigate = useEvent(function () {
82
- stableOnNavigate(indexRef.current === -1 ? null : indexRef.current);
83
- }),
84
- previousOnNavigateRef = useRef(onNavigate),
85
- focusItem = useEvent(function () {
86
- function runFocus(item2) {
87
- virtual ? setActiveId(item2.id) : enqueueFocus(item2, {
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, {
88
91
  sync: forceSyncFocusRef.current,
89
- preventScroll: !0
92
+ preventScroll: true
90
93
  });
91
94
  }
92
- var initialItem = listRef.current[indexRef.current],
93
- forceScrollIntoView = forceScrollIntoViewRef.current;
94
- initialItem && runFocus(initialItem);
95
- var scheduler = forceSyncFocusRef.current ? function (v) {
96
- return v();
97
- } : requestAnimationFrame;
98
- scheduler(function () {
99
- var waitedItem = listRef.current[indexRef.current] || initialItem;
100
- if (waitedItem) {
101
- initialItem || runFocus(waitedItem);
102
- var scrollIntoViewOptions = scrollItemIntoViewRef.current,
103
- shouldScrollIntoView = scrollIntoViewOptions && waitedItem && (forceScrollIntoView || !isPointerModalityRef.current);
104
- if (shouldScrollIntoView) {
105
- var _waitedItem_scrollIntoView;
106
- (_waitedItem_scrollIntoView = waitedItem.scrollIntoView) === null || _waitedItem_scrollIntoView === void 0 || _waitedItem_scrollIntoView.call(waitedItem, typeof scrollIntoViewOptions == "boolean" ? {
107
- block: "nearest",
108
- inline: "nearest"
109
- } : scrollIntoViewOptions);
110
- }
111
- }
112
- });
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
+ }
113
119
  });
120
+ });
114
121
  useLayoutEffect(function () {
115
- enabled && (open && elements.floating ? focusItemOnOpenRef.current && selectedIndex != null && (forceScrollIntoViewRef.current = !0, indexRef.current = selectedIndex, onNavigate()) : previousMountedRef.current && (indexRef.current = -1, previousOnNavigateRef.current()));
116
- }, [enabled, open, elements.floating, selectedIndex, onNavigate]), useLayoutEffect(function () {
117
- if (enabled && open && elements.floating) if (activeIndex == null) {
118
- if (forceSyncFocusRef.current = !1, selectedIndexRef.current != null) return;
119
- if (previousMountedRef.current && (indexRef.current = -1, focusItem()), (!previousOpenRef.current || !previousMountedRef.current) && focusItemOnOpenRef.current && (keyRef.current != null || focusItemOnOpenRef.current === !0 && keyRef.current == null)) {
120
- var runs = 0,
121
- waitForListPopulated = function () {
122
- if (listRef.current[0] == null) {
123
- if (runs < 2) {
124
- var scheduler = runs ? requestAnimationFrame : queueMicrotask;
125
- scheduler(waitForListPopulated);
126
- }
127
- runs++;
128
- } else indexRef.current = keyRef.current == null || isMainOrientationToEndKey(keyRef.current, orientation, rtl) || nested ? getMinListIndex(listRef, disabledIndicesRef.current) : getMaxListIndex(listRef, disabledIndicesRef.current), keyRef.current = null, onNavigate();
129
- };
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
+ };
130
162
  waitForListPopulated();
131
163
  }
132
- } else isIndexOutOfListBounds(listRef, activeIndex) || (indexRef.current = activeIndex, focusItem(), forceScrollIntoViewRef.current = !1);
133
- }, [enabled, open, elements.floating, activeIndex, selectedIndexRef, nested, listRef, orientation, rtl, onNavigate, focusItem, disabledIndicesRef]), useLayoutEffect(function () {
134
- previousOnNavigateRef.current = onNavigate, previousOpenRef.current = open, previousMountedRef.current = !!elements.floating;
135
- }), useLayoutEffect(function () {
136
- open || (keyRef.current = null, focusItemOnOpenRef.current = focusItemOnOpen);
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
+ }
137
180
  }, [open, focusItemOnOpen]);
138
- var hasActiveIndex = activeIndex != null,
139
- commonOnKeyDown = useEvent(function (event) {
140
- if (isPointerModalityRef.current = !1, forceSyncFocusRef.current = !0, event.which !== 229 && !(!latestOpenRef.current && event.currentTarget === elements.floating)) {
141
- if (nested && isCrossOrientationCloseKey(event.key, orientation, rtl)) {
142
- stopEvent(event), onOpenChange(!1, event.nativeEvent, "list-navigation"), isHTMLElement(elements.domReference) && elements.domReference.focus();
143
- return;
144
- }
145
- var currentIndex = indexRef.current,
146
- minIndex = getMinListIndex(listRef, disabledIndices),
147
- maxIndex = getMaxListIndex(listRef, disabledIndices);
148
- if (typeableComboboxReference || (event.key === "Home" && (stopEvent(event), indexRef.current = minIndex, onNavigate()), event.key === "End" && (stopEvent(event), indexRef.current = maxIndex, onNavigate())), isMainOrientationKey(event.key, orientation)) {
149
- if (stopEvent(event), open && !virtual && activeElement(event.currentTarget.ownerDocument) === event.currentTarget) {
150
- indexRef.current = isMainOrientationToEndKey(event.key, orientation, rtl) ? minIndex : maxIndex, onNavigate();
151
- return;
152
- }
153
- isMainOrientationToEndKey(event.key, orientation, rtl) ? loop ? indexRef.current = currentIndex >= maxIndex ? allowEscape && currentIndex !== listRef.current.length ? -1 : minIndex : findNonDisabledListIndex(listRef, {
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, {
154
224
  startingIndex: currentIndex,
155
225
  disabledIndices
156
- }) : indexRef.current = Math.min(maxIndex, findNonDisabledListIndex(listRef, {
226
+ });
227
+ } else {
228
+ indexRef.current = Math.min(maxIndex, findNonDisabledListIndex(listRef, {
157
229
  startingIndex: currentIndex,
158
230
  disabledIndices
159
- })) : loop ? indexRef.current = currentIndex <= minIndex ? allowEscape && currentIndex !== -1 ? listRef.current.length : maxIndex : findNonDisabledListIndex(listRef, {
231
+ }));
232
+ }
233
+ } else {
234
+ if (loop) {
235
+ indexRef.current = currentIndex <= minIndex ? allowEscape && currentIndex !== -1 ? listRef.current.length : maxIndex : findNonDisabledListIndex(listRef, {
160
236
  startingIndex: currentIndex,
161
- decrement: !0,
237
+ decrement: true,
162
238
  disabledIndices
163
- }) : indexRef.current = Math.max(minIndex, findNonDisabledListIndex(listRef, {
239
+ });
240
+ } else {
241
+ indexRef.current = Math.max(minIndex, findNonDisabledListIndex(listRef, {
164
242
  startingIndex: currentIndex,
165
- decrement: !0,
243
+ decrement: true,
166
244
  disabledIndices
167
- })), isIndexOutOfListBounds(listRef, indexRef.current) && (indexRef.current = -1), onNavigate();
245
+ }));
168
246
  }
169
247
  }
170
- }),
171
- ariaActiveDescendantProp = useMemo(function () {
172
- return virtual && open && hasActiveIndex && {
173
- "aria-activedescendant": activeId
174
- };
175
- }, [virtual, open, hasActiveIndex, activeId]),
176
- floating = useMemo(function () {
177
- return {
178
- "aria-orientation": orientation === "both" ? void 0 : orientation,
179
- ...(typeableComboboxReference ? {} : ariaActiveDescendantProp),
180
- onKeyDown: commonOnKeyDown,
181
- onPointerMove() {
182
- isPointerModalityRef.current = !0;
183
- }
184
- };
185
- }, [ariaActiveDescendantProp, commonOnKeyDown, orientation, typeableComboboxReference]),
186
- reference = useMemo(function () {
187
- function checkVirtualMouse(event) {
188
- focusItemOnOpen === "auto" && isVirtualClick(event.nativeEvent) && (focusItemOnOpenRef.current = !0);
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;
189
266
  }
190
- function checkVirtualPointer(event) {
191
- focusItemOnOpenRef.current = focusItemOnOpen, focusItemOnOpen === "auto" && isVirtualPointerEvent(event.nativeEvent) && (focusItemOnOpenRef.current = !0);
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;
192
273
  }
193
- return {
194
- ...ariaActiveDescendantProp,
195
- onKeyDown(event) {
196
- isPointerModalityRef.current = !1;
197
- var isArrowKey = event.key.startsWith("Arrow"),
198
- isCrossOpenKey = isCrossOrientationOpenKey(event.key, orientation, rtl),
199
- isMainKey = isMainOrientationKey(event.key, orientation),
200
- isNavigationKey = (nested ? isCrossOpenKey : isMainKey) || event.key === "Enter" || event.key.trim() === "";
201
- if (virtual && open) return commonOnKeyDown(event);
202
- if (!(!open && !openOnArrowKeyDown && isArrowKey)) {
203
- if (isNavigationKey && (keyRef.current = event.key), nested) {
204
- isCrossOpenKey && (stopEvent(event), open ? (indexRef.current = getMinListIndex(listRef, disabledIndicesRef.current), onNavigate()) : onOpenChange(!0, event.nativeEvent, "list-navigation"));
205
- return;
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");
206
306
  }
207
- isMainKey && (selectedIndex != null && (indexRef.current = selectedIndex), stopEvent(event), !open && openOnArrowKeyDown ? onOpenChange(!0, event.nativeEvent, "list-navigation") : commonOnKeyDown(event), open && onNavigate());
208
307
  }
209
- },
210
- onFocus() {
211
- open && !virtual && (indexRef.current = -1, onNavigate());
212
- },
213
- onPointerDown: checkVirtualPointer,
214
- onPointerEnter: checkVirtualPointer,
215
- onMouseDown: checkVirtualMouse,
216
- onClick: checkVirtualMouse
217
- };
218
- }, [ariaActiveDescendantProp, commonOnKeyDown, disabledIndicesRef, focusItemOnOpen, listRef, nested, onNavigate, onOpenChange, open, openOnArrowKeyDown, orientation, rtl, selectedIndex, virtual]),
219
- item = useMemo(function () {
220
- function syncCurrentTarget(currentTarget) {
221
- if (latestOpenRef.current) {
222
- var index = listRef.current.indexOf(currentTarget);
223
- index !== -1 && indexRef.current !== index && (indexRef.current = index, onNavigate());
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();
224
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();
225
344
  }
226
- var itemProps = {
227
- onFocus(param) {
228
- var {
229
- currentTarget
230
- } = param;
231
- forceSyncFocusRef.current = !0, syncCurrentTarget(currentTarget);
232
- },
233
- onClick: function (param) {
234
- var {
235
- currentTarget
236
- } = param;
237
- return currentTarget.focus({
238
- preventScroll: !0
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
239
390
  });
240
- },
241
- // safari
242
- onMouseMove(param) {
243
- var {
244
- currentTarget
245
- } = param;
246
- forceSyncFocusRef.current = !0, forceScrollIntoViewRef.current = !1, focusItemOnHover && syncCurrentTarget(currentTarget);
247
- },
248
- onPointerLeave(param) {
249
- var {
250
- pointerType
251
- } = param;
252
- if (!(!isPointerModalityRef.current || pointerType === "touch") && (forceSyncFocusRef.current = !0, !!focusItemOnHover && (indexRef.current = -1, onNavigate(), !virtual))) {
253
- var _elements_floating;
254
- (_elements_floating = elements.floating) === null || _elements_floating === void 0 || _elements_floating.focus({
255
- preventScroll: !0
256
- });
257
- }
258
391
  }
259
- };
260
- return itemProps;
261
- }, [latestOpenRef, focusItemOnHover, listRef, onNavigate, virtual, elements.floating]);
392
+ }
393
+ };
394
+ return itemProps;
395
+ }, [latestOpenRef, focusItemOnHover, listRef, onNavigate, virtual, elements.floating]);
262
396
  return useMemo(function () {
263
397
  return enabled ? {
264
398
  reference,