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