@octanejs/floating-ui 0.1.32 → 0.1.33
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.
- package/dist/cjs/Composite.cjs +235 -0
- package/dist/cjs/FloatingArrow.cjs +143 -0
- package/dist/cjs/FloatingFocusManager.cjs +701 -0
- package/dist/cjs/FloatingList.cjs +145 -0
- package/dist/cjs/FloatingOverlay.cjs +100 -0
- package/dist/cjs/FloatingPortal.cjs +241 -0
- package/dist/cjs/context.cjs +184 -0
- package/dist/cjs/delayGroup.cjs +154 -0
- package/dist/cjs/index.cjs +166 -0
- package/dist/cjs/internal.cjs +51 -0
- package/dist/cjs/pubsub.cjs +44 -0
- package/dist/cjs/safePolygon.cjs +269 -0
- package/dist/cjs/transitions.cjs +150 -0
- package/dist/cjs/tree.cjs +105 -0
- package/dist/cjs/types.cjs +16 -0
- package/dist/cjs/useClick.cjs +139 -0
- package/dist/cjs/useClientPoint.cjs +201 -0
- package/dist/cjs/useDismiss.cjs +337 -0
- package/dist/cjs/useFloating.cjs +224 -0
- package/dist/cjs/useFocus.cjs +146 -0
- package/dist/cjs/useHover.cjs +393 -0
- package/dist/cjs/useId.cjs +31 -0
- package/dist/cjs/useInteractions.cjs +106 -0
- package/dist/cjs/useListNavigation.cjs +633 -0
- package/dist/cjs/useMergeRefs.cjs +77 -0
- package/dist/cjs/useRole.cjs +118 -0
- package/dist/cjs/useTypeahead.cjs +164 -0
- package/dist/cjs/utils/index.cjs +709 -0
- package/package.json +13 -6
|
@@ -0,0 +1,701 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var FloatingFocusManager_exports = {};
|
|
20
|
+
__export(FloatingFocusManager_exports, {
|
|
21
|
+
FloatingFocusManager: () => FloatingFocusManager,
|
|
22
|
+
VisuallyHiddenDismiss: () => VisuallyHiddenDismiss,
|
|
23
|
+
supportsInert: () => supportsInert
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(FloatingFocusManager_exports);
|
|
26
|
+
var import_dom = require("@floating-ui/utils/dom");
|
|
27
|
+
var import_tabbable = require("tabbable");
|
|
28
|
+
var import_octane = require("octane");
|
|
29
|
+
var import_FloatingPortal = require("./FloatingPortal.cjs");
|
|
30
|
+
var import_internal = require("./internal.cjs");
|
|
31
|
+
var import_tree = require("./tree.cjs");
|
|
32
|
+
var import_utils = require("./utils/index.cjs");
|
|
33
|
+
const HIDDEN_STYLES = {
|
|
34
|
+
border: 0,
|
|
35
|
+
clip: "rect(0 0 0 0)",
|
|
36
|
+
height: "1px",
|
|
37
|
+
margin: "-1px",
|
|
38
|
+
overflow: "hidden",
|
|
39
|
+
padding: 0,
|
|
40
|
+
position: "fixed",
|
|
41
|
+
whiteSpace: "nowrap",
|
|
42
|
+
width: "1px",
|
|
43
|
+
top: 0,
|
|
44
|
+
left: 0
|
|
45
|
+
};
|
|
46
|
+
const counters = {
|
|
47
|
+
inert: /* @__PURE__ */ new WeakMap(),
|
|
48
|
+
"aria-hidden": /* @__PURE__ */ new WeakMap(),
|
|
49
|
+
none: /* @__PURE__ */ new WeakMap()
|
|
50
|
+
};
|
|
51
|
+
function getCounterMap(control) {
|
|
52
|
+
if (control === "inert") return counters.inert;
|
|
53
|
+
if (control === "aria-hidden") return counters["aria-hidden"];
|
|
54
|
+
return counters.none;
|
|
55
|
+
}
|
|
56
|
+
let uncontrolledElementsSet = /* @__PURE__ */ new WeakSet();
|
|
57
|
+
let markerMap = {};
|
|
58
|
+
let lockCount = 0;
|
|
59
|
+
const supportsInert = () => typeof HTMLElement !== "undefined" && "inert" in HTMLElement.prototype;
|
|
60
|
+
function unwrapHost(node) {
|
|
61
|
+
if (!node) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
return (0, import_dom.isShadowRoot)(node) ? node.host : unwrapHost(node.parentNode);
|
|
65
|
+
}
|
|
66
|
+
const correctElements = (parent, targets) => targets.map((target) => {
|
|
67
|
+
if (parent.contains(target)) {
|
|
68
|
+
return target;
|
|
69
|
+
}
|
|
70
|
+
const correctedTarget = unwrapHost(target);
|
|
71
|
+
if (parent.contains(correctedTarget)) {
|
|
72
|
+
return correctedTarget;
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}).filter((x) => x != null);
|
|
76
|
+
function applyAttributeToOthers(uncorrectedAvoidElements, body, ariaHidden, inert) {
|
|
77
|
+
const markerName = "data-floating-ui-inert";
|
|
78
|
+
const controlAttribute = inert ? "inert" : ariaHidden ? "aria-hidden" : null;
|
|
79
|
+
const avoidElements = correctElements(body, uncorrectedAvoidElements);
|
|
80
|
+
const elementsToKeep = /* @__PURE__ */ new Set();
|
|
81
|
+
const elementsToStop = new Set(avoidElements);
|
|
82
|
+
const hiddenElements = [];
|
|
83
|
+
if (!markerMap[markerName]) {
|
|
84
|
+
markerMap[markerName] = /* @__PURE__ */ new WeakMap();
|
|
85
|
+
}
|
|
86
|
+
const markerCounter = markerMap[markerName];
|
|
87
|
+
avoidElements.forEach(keep);
|
|
88
|
+
deep(body);
|
|
89
|
+
elementsToKeep.clear();
|
|
90
|
+
function keep(el) {
|
|
91
|
+
if (!el || elementsToKeep.has(el)) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
elementsToKeep.add(el);
|
|
95
|
+
el.parentNode && keep(el.parentNode);
|
|
96
|
+
}
|
|
97
|
+
function deep(parent) {
|
|
98
|
+
if (!parent || elementsToStop.has(parent)) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
[].forEach.call(parent.children, (node) => {
|
|
102
|
+
if ((0, import_dom.getNodeName)(node) === "script") return;
|
|
103
|
+
if (elementsToKeep.has(node)) {
|
|
104
|
+
deep(node);
|
|
105
|
+
} else {
|
|
106
|
+
const attr = controlAttribute ? node.getAttribute(controlAttribute) : null;
|
|
107
|
+
const alreadyHidden = attr !== null && attr !== "false";
|
|
108
|
+
const counterMap = getCounterMap(controlAttribute);
|
|
109
|
+
const counterValue = (counterMap.get(node) || 0) + 1;
|
|
110
|
+
const markerValue = (markerCounter.get(node) || 0) + 1;
|
|
111
|
+
counterMap.set(node, counterValue);
|
|
112
|
+
markerCounter.set(node, markerValue);
|
|
113
|
+
hiddenElements.push(node);
|
|
114
|
+
if (counterValue === 1 && alreadyHidden) {
|
|
115
|
+
uncontrolledElementsSet.add(node);
|
|
116
|
+
}
|
|
117
|
+
if (markerValue === 1) {
|
|
118
|
+
node.setAttribute(markerName, "");
|
|
119
|
+
}
|
|
120
|
+
if (!alreadyHidden && controlAttribute) {
|
|
121
|
+
node.setAttribute(controlAttribute, controlAttribute === "inert" ? "" : "true");
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
lockCount++;
|
|
127
|
+
return () => {
|
|
128
|
+
hiddenElements.forEach((element) => {
|
|
129
|
+
const counterMap = getCounterMap(controlAttribute);
|
|
130
|
+
const currentCounterValue = counterMap.get(element) || 0;
|
|
131
|
+
const counterValue = currentCounterValue - 1;
|
|
132
|
+
const markerValue = (markerCounter.get(element) || 0) - 1;
|
|
133
|
+
counterMap.set(element, counterValue);
|
|
134
|
+
markerCounter.set(element, markerValue);
|
|
135
|
+
if (!counterValue) {
|
|
136
|
+
if (!uncontrolledElementsSet.has(element) && controlAttribute) {
|
|
137
|
+
element.removeAttribute(controlAttribute);
|
|
138
|
+
}
|
|
139
|
+
uncontrolledElementsSet.delete(element);
|
|
140
|
+
}
|
|
141
|
+
if (!markerValue) {
|
|
142
|
+
element.removeAttribute(markerName);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
lockCount--;
|
|
146
|
+
if (!lockCount) {
|
|
147
|
+
counters.inert = /* @__PURE__ */ new WeakMap();
|
|
148
|
+
counters["aria-hidden"] = /* @__PURE__ */ new WeakMap();
|
|
149
|
+
counters.none = /* @__PURE__ */ new WeakMap();
|
|
150
|
+
uncontrolledElementsSet = /* @__PURE__ */ new WeakSet();
|
|
151
|
+
markerMap = {};
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function markOthers(avoidElements, ariaHidden = false, inert = false) {
|
|
156
|
+
const body = (0, import_utils.getDocument)(avoidElements[0]).body;
|
|
157
|
+
return applyAttributeToOthers(
|
|
158
|
+
avoidElements.concat(Array.from(body.querySelectorAll('[aria-live],[role="status"],output'))),
|
|
159
|
+
body,
|
|
160
|
+
ariaHidden,
|
|
161
|
+
inert
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
const LIST_LIMIT = 20;
|
|
165
|
+
let previouslyFocusedElements = [];
|
|
166
|
+
function clearDisconnectedPreviouslyFocusedElements() {
|
|
167
|
+
previouslyFocusedElements = previouslyFocusedElements.filter(
|
|
168
|
+
(elementRef) => elementRef.deref()?.isConnected
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
function addPreviouslyFocusedElement(element) {
|
|
172
|
+
clearDisconnectedPreviouslyFocusedElements();
|
|
173
|
+
if (element && (0, import_dom.getNodeName)(element) !== "body") {
|
|
174
|
+
previouslyFocusedElements.push(new WeakRef(element));
|
|
175
|
+
if (previouslyFocusedElements.length > LIST_LIMIT) {
|
|
176
|
+
previouslyFocusedElements = previouslyFocusedElements.slice(-LIST_LIMIT);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function getPreviouslyFocusedElement() {
|
|
181
|
+
clearDisconnectedPreviouslyFocusedElements();
|
|
182
|
+
return previouslyFocusedElements[previouslyFocusedElements.length - 1]?.deref();
|
|
183
|
+
}
|
|
184
|
+
function getFirstTabbableElement(container) {
|
|
185
|
+
const tabbableOptions = (0, import_utils.getTabbableOptions)();
|
|
186
|
+
if ((0, import_tabbable.isTabbable)(container, tabbableOptions)) {
|
|
187
|
+
return container;
|
|
188
|
+
}
|
|
189
|
+
return (0, import_tabbable.tabbable)(container, tabbableOptions)[0] || container;
|
|
190
|
+
}
|
|
191
|
+
function handleTabIndex(floatingFocusElement, orderRef) {
|
|
192
|
+
if (!orderRef.current.includes("floating") && !floatingFocusElement.getAttribute("role")?.includes("dialog")) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const options = (0, import_utils.getTabbableOptions)();
|
|
196
|
+
const focusableElements = (0, import_tabbable.focusable)(floatingFocusElement, options);
|
|
197
|
+
const tabbableContent = focusableElements.filter((element) => {
|
|
198
|
+
const dataTabIndex = element.getAttribute("data-tabindex") || "";
|
|
199
|
+
return (0, import_tabbable.isTabbable)(element, options) || element.hasAttribute("data-tabindex") && !dataTabIndex.startsWith("-");
|
|
200
|
+
});
|
|
201
|
+
const tabIndex = floatingFocusElement.getAttribute("tabindex");
|
|
202
|
+
if (orderRef.current.includes("floating") || tabbableContent.length === 0) {
|
|
203
|
+
if (tabIndex !== "0") {
|
|
204
|
+
floatingFocusElement.setAttribute("tabindex", "0");
|
|
205
|
+
}
|
|
206
|
+
} else if (tabIndex !== "-1" || floatingFocusElement.hasAttribute("data-tabindex") && floatingFocusElement.getAttribute("data-tabindex") !== "-1") {
|
|
207
|
+
floatingFocusElement.setAttribute("tabindex", "-1");
|
|
208
|
+
floatingFocusElement.setAttribute("data-tabindex", "-1");
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function useLiteMergeRefs(refs, slot) {
|
|
212
|
+
return (0, import_octane.useMemo)(
|
|
213
|
+
() => {
|
|
214
|
+
return (value) => {
|
|
215
|
+
refs.forEach((ref) => {
|
|
216
|
+
if (ref) {
|
|
217
|
+
ref.current = value;
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
};
|
|
221
|
+
},
|
|
222
|
+
refs,
|
|
223
|
+
slot
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
function VisuallyHiddenDismiss(props) {
|
|
227
|
+
return (0, import_octane.createElement)("button", {
|
|
228
|
+
...props,
|
|
229
|
+
type: "button",
|
|
230
|
+
ref: props.ref,
|
|
231
|
+
tabIndex: -1,
|
|
232
|
+
style: HIDDEN_STYLES
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
function FloatingFocusManager(props) {
|
|
236
|
+
const context = props.context;
|
|
237
|
+
const children = props.children;
|
|
238
|
+
const disabled = props.disabled ?? false;
|
|
239
|
+
const order = props.order ?? ["content"];
|
|
240
|
+
const _guards = props.guards ?? true;
|
|
241
|
+
const initialFocus = props.initialFocus ?? 0;
|
|
242
|
+
const returnFocus = props.returnFocus ?? true;
|
|
243
|
+
const restoreFocus = props.restoreFocus ?? false;
|
|
244
|
+
const modal = props.modal ?? true;
|
|
245
|
+
const visuallyHiddenDismiss = props.visuallyHiddenDismiss ?? false;
|
|
246
|
+
const closeOnFocusOut = props.closeOnFocusOut ?? true;
|
|
247
|
+
const outsideElementsInert = props.outsideElementsInert ?? false;
|
|
248
|
+
const _getInsideElements = props.getInsideElements ?? (() => []);
|
|
249
|
+
const open = context.open;
|
|
250
|
+
const onOpenChange = context.onOpenChange;
|
|
251
|
+
const events = context.events;
|
|
252
|
+
const dataRef = context.dataRef;
|
|
253
|
+
const domReference = context.elements.domReference;
|
|
254
|
+
const floating = context.elements.floating;
|
|
255
|
+
const getNodeId = (0, import_utils.useEffectEvent)(
|
|
256
|
+
() => dataRef.current.floatingContext?.nodeId,
|
|
257
|
+
(0, import_internal.S)("FFM:getNodeId")
|
|
258
|
+
);
|
|
259
|
+
const getInsideElements = (0, import_utils.useEffectEvent)(_getInsideElements, (0, import_internal.S)("FFM:getInside"));
|
|
260
|
+
const ignoreInitialFocus = typeof initialFocus === "number" && initialFocus < 0;
|
|
261
|
+
const isUntrappedTypeableCombobox = (0, import_utils.isTypeableCombobox)(domReference) && ignoreInitialFocus;
|
|
262
|
+
const inertSupported = supportsInert();
|
|
263
|
+
const guards = inertSupported ? _guards : true;
|
|
264
|
+
const useInert = !guards || inertSupported && outsideElementsInert;
|
|
265
|
+
const orderRef = (0, import_utils.useLatestRef)(order, (0, import_internal.S)("FFM:order"));
|
|
266
|
+
const initialFocusRef = (0, import_utils.useLatestRef)(initialFocus, (0, import_internal.S)("FFM:initial"));
|
|
267
|
+
const returnFocusRef = (0, import_utils.useLatestRef)(returnFocus, (0, import_internal.S)("FFM:return"));
|
|
268
|
+
const tree = (0, import_tree.useFloatingTree)();
|
|
269
|
+
const portalContext = (0, import_FloatingPortal.usePortalContext)();
|
|
270
|
+
const startDismissButtonRef = (0, import_octane.useRef)(null, (0, import_internal.S)("FFM:startDismiss"));
|
|
271
|
+
const endDismissButtonRef = (0, import_octane.useRef)(null, (0, import_internal.S)("FFM:endDismiss"));
|
|
272
|
+
const preventReturnFocusRef = (0, import_octane.useRef)(false, (0, import_internal.S)("FFM:preventReturn"));
|
|
273
|
+
const isPointerDownRef = (0, import_octane.useRef)(false, (0, import_internal.S)("FFM:pointerDown"));
|
|
274
|
+
const tabbableIndexRef = (0, import_octane.useRef)(-1, (0, import_internal.S)("FFM:tabbableIndex"));
|
|
275
|
+
const blurTimeoutRef = (0, import_octane.useRef)(-1, (0, import_internal.S)("FFM:blurTimeout"));
|
|
276
|
+
const isInsidePortal = portalContext != null;
|
|
277
|
+
const floatingFocusElement = (0, import_utils.getFloatingFocusElement)(floating);
|
|
278
|
+
const getTabbableContent = (0, import_utils.useEffectEvent)((container = floatingFocusElement) => {
|
|
279
|
+
return container ? (0, import_tabbable.tabbable)(container, (0, import_utils.getTabbableOptions)()) : [];
|
|
280
|
+
}, (0, import_internal.S)("FFM:getContent"));
|
|
281
|
+
const getTabbableElements = (0, import_utils.useEffectEvent)((container) => {
|
|
282
|
+
const content = getTabbableContent(container);
|
|
283
|
+
return orderRef.current.map((type) => {
|
|
284
|
+
if (domReference && type === "reference") {
|
|
285
|
+
return domReference;
|
|
286
|
+
}
|
|
287
|
+
if (floatingFocusElement && type === "floating") {
|
|
288
|
+
return floatingFocusElement;
|
|
289
|
+
}
|
|
290
|
+
return content;
|
|
291
|
+
}).filter(Boolean).flat();
|
|
292
|
+
}, (0, import_internal.S)("FFM:getEls"));
|
|
293
|
+
(0, import_octane.useEffect)(
|
|
294
|
+
() => {
|
|
295
|
+
if (disabled) return;
|
|
296
|
+
if (!modal) return;
|
|
297
|
+
function onKeyDown(event) {
|
|
298
|
+
if (event.key === "Tab") {
|
|
299
|
+
if ((0, import_utils.contains)(
|
|
300
|
+
floatingFocusElement,
|
|
301
|
+
(0, import_utils.activeElement)((0, import_utils.getDocument)(floatingFocusElement))
|
|
302
|
+
) && getTabbableContent().length === 0 && !isUntrappedTypeableCombobox) {
|
|
303
|
+
(0, import_utils.stopEvent)(event);
|
|
304
|
+
}
|
|
305
|
+
const els = getTabbableElements();
|
|
306
|
+
const target = (0, import_utils.getTarget)(event);
|
|
307
|
+
if (orderRef.current[0] === "reference" && target === domReference) {
|
|
308
|
+
(0, import_utils.stopEvent)(event);
|
|
309
|
+
if (event.shiftKey) {
|
|
310
|
+
(0, import_utils.enqueueFocus)(els[els.length - 1]);
|
|
311
|
+
} else {
|
|
312
|
+
(0, import_utils.enqueueFocus)(els[1]);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (orderRef.current[1] === "floating" && target === floatingFocusElement && event.shiftKey) {
|
|
316
|
+
(0, import_utils.stopEvent)(event);
|
|
317
|
+
(0, import_utils.enqueueFocus)(els[0]);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
const doc = (0, import_utils.getDocument)(floatingFocusElement);
|
|
322
|
+
doc.addEventListener("keydown", onKeyDown);
|
|
323
|
+
return () => {
|
|
324
|
+
doc.removeEventListener("keydown", onKeyDown);
|
|
325
|
+
};
|
|
326
|
+
},
|
|
327
|
+
[
|
|
328
|
+
disabled,
|
|
329
|
+
domReference,
|
|
330
|
+
floatingFocusElement,
|
|
331
|
+
modal,
|
|
332
|
+
orderRef,
|
|
333
|
+
isUntrappedTypeableCombobox,
|
|
334
|
+
getTabbableContent,
|
|
335
|
+
getTabbableElements
|
|
336
|
+
],
|
|
337
|
+
(0, import_internal.S)("FFM:e:keydown")
|
|
338
|
+
);
|
|
339
|
+
(0, import_octane.useEffect)(
|
|
340
|
+
() => {
|
|
341
|
+
if (disabled) return;
|
|
342
|
+
if (!floating) return;
|
|
343
|
+
function handleFocusIn(event) {
|
|
344
|
+
const target = (0, import_utils.getTarget)(event);
|
|
345
|
+
const tabbableContent = getTabbableContent();
|
|
346
|
+
const tabbableIndex = tabbableContent.indexOf(target);
|
|
347
|
+
if (tabbableIndex !== -1) {
|
|
348
|
+
tabbableIndexRef.current = tabbableIndex;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
floating.addEventListener("focusin", handleFocusIn);
|
|
352
|
+
return () => {
|
|
353
|
+
floating.removeEventListener("focusin", handleFocusIn);
|
|
354
|
+
};
|
|
355
|
+
},
|
|
356
|
+
[disabled, floating, getTabbableContent],
|
|
357
|
+
(0, import_internal.S)("FFM:e:focusin")
|
|
358
|
+
);
|
|
359
|
+
(0, import_octane.useEffect)(
|
|
360
|
+
() => {
|
|
361
|
+
if (disabled) return;
|
|
362
|
+
if (!closeOnFocusOut) return;
|
|
363
|
+
function handlePointerDown() {
|
|
364
|
+
isPointerDownRef.current = true;
|
|
365
|
+
setTimeout(() => {
|
|
366
|
+
isPointerDownRef.current = false;
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
function handleFocusOutside(event) {
|
|
370
|
+
const relatedTarget = event.relatedTarget;
|
|
371
|
+
const currentTarget = event.currentTarget;
|
|
372
|
+
const target = (0, import_utils.getTarget)(event);
|
|
373
|
+
queueMicrotask(() => {
|
|
374
|
+
const nodeId = getNodeId();
|
|
375
|
+
const movedToUnrelatedNode = !((0, import_utils.contains)(domReference, relatedTarget) || (0, import_utils.contains)(floating, relatedTarget) || (0, import_utils.contains)(relatedTarget, floating) || (0, import_utils.contains)(portalContext?.portalNode, relatedTarget) || relatedTarget != null && relatedTarget.hasAttribute((0, import_utils.createAttribute)("focus-guard")) || tree && ((0, import_utils.getNodeChildren)(tree.nodesRef.current, nodeId).find(
|
|
376
|
+
(node) => (0, import_utils.contains)(node.context?.elements.floating, relatedTarget) || (0, import_utils.contains)(node.context?.elements.domReference, relatedTarget)
|
|
377
|
+
) || (0, import_utils.getNodeAncestors)(tree.nodesRef.current, nodeId).find(
|
|
378
|
+
(node) => [
|
|
379
|
+
node.context?.elements.floating,
|
|
380
|
+
(0, import_utils.getFloatingFocusElement)(node.context?.elements.floating)
|
|
381
|
+
].includes(relatedTarget) || node.context?.elements.domReference === relatedTarget
|
|
382
|
+
)));
|
|
383
|
+
if (currentTarget === domReference && floatingFocusElement) {
|
|
384
|
+
handleTabIndex(floatingFocusElement, orderRef);
|
|
385
|
+
}
|
|
386
|
+
if (restoreFocus && currentTarget !== domReference && !target?.isConnected && (0, import_utils.activeElement)((0, import_utils.getDocument)(floatingFocusElement)) === (0, import_utils.getDocument)(floatingFocusElement).body) {
|
|
387
|
+
if ((0, import_dom.isHTMLElement)(floatingFocusElement)) {
|
|
388
|
+
floatingFocusElement.focus();
|
|
389
|
+
}
|
|
390
|
+
const prevTabbableIndex = tabbableIndexRef.current;
|
|
391
|
+
const tabbableContent = getTabbableContent();
|
|
392
|
+
const nodeToFocus = tabbableContent[prevTabbableIndex] || tabbableContent[tabbableContent.length - 1] || floatingFocusElement;
|
|
393
|
+
if ((0, import_dom.isHTMLElement)(nodeToFocus)) {
|
|
394
|
+
nodeToFocus.focus();
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
if (dataRef.current.insideReactTree) {
|
|
398
|
+
dataRef.current.insideReactTree = false;
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
if ((isUntrappedTypeableCombobox ? true : !modal) && relatedTarget && movedToUnrelatedNode && !isPointerDownRef.current && relatedTarget !== getPreviouslyFocusedElement()) {
|
|
402
|
+
preventReturnFocusRef.current = true;
|
|
403
|
+
onOpenChange(false, event, "focus-out");
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
const shouldHandleBlurCapture = Boolean(!tree && portalContext);
|
|
408
|
+
function markInsideReactTree() {
|
|
409
|
+
(0, import_utils.clearTimeoutIfSet)(blurTimeoutRef);
|
|
410
|
+
dataRef.current.insideReactTree = true;
|
|
411
|
+
blurTimeoutRef.current = window.setTimeout(() => {
|
|
412
|
+
dataRef.current.insideReactTree = false;
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
if (floating && (0, import_dom.isHTMLElement)(domReference)) {
|
|
416
|
+
domReference.addEventListener("focusout", handleFocusOutside);
|
|
417
|
+
domReference.addEventListener("pointerdown", handlePointerDown);
|
|
418
|
+
floating.addEventListener("focusout", handleFocusOutside);
|
|
419
|
+
if (shouldHandleBlurCapture) {
|
|
420
|
+
floating.addEventListener("focusout", markInsideReactTree, true);
|
|
421
|
+
}
|
|
422
|
+
return () => {
|
|
423
|
+
domReference.removeEventListener("focusout", handleFocusOutside);
|
|
424
|
+
domReference.removeEventListener("pointerdown", handlePointerDown);
|
|
425
|
+
floating.removeEventListener("focusout", handleFocusOutside);
|
|
426
|
+
if (shouldHandleBlurCapture) {
|
|
427
|
+
floating.removeEventListener("focusout", markInsideReactTree, true);
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
},
|
|
432
|
+
[
|
|
433
|
+
disabled,
|
|
434
|
+
domReference,
|
|
435
|
+
floating,
|
|
436
|
+
floatingFocusElement,
|
|
437
|
+
modal,
|
|
438
|
+
tree,
|
|
439
|
+
portalContext,
|
|
440
|
+
onOpenChange,
|
|
441
|
+
closeOnFocusOut,
|
|
442
|
+
restoreFocus,
|
|
443
|
+
getTabbableContent,
|
|
444
|
+
isUntrappedTypeableCombobox,
|
|
445
|
+
getNodeId,
|
|
446
|
+
orderRef,
|
|
447
|
+
dataRef
|
|
448
|
+
],
|
|
449
|
+
(0, import_internal.S)("FFM:e:focusout")
|
|
450
|
+
);
|
|
451
|
+
const beforeGuardRef = (0, import_octane.useRef)(null, (0, import_internal.S)("FFM:beforeGuard"));
|
|
452
|
+
const afterGuardRef = (0, import_octane.useRef)(null, (0, import_internal.S)("FFM:afterGuard"));
|
|
453
|
+
const mergedBeforeGuardRef = useLiteMergeRefs(
|
|
454
|
+
[beforeGuardRef, portalContext?.beforeInsideRef],
|
|
455
|
+
(0, import_internal.S)("FFM:mergeBefore")
|
|
456
|
+
);
|
|
457
|
+
const mergedAfterGuardRef = useLiteMergeRefs(
|
|
458
|
+
[afterGuardRef, portalContext?.afterInsideRef],
|
|
459
|
+
(0, import_internal.S)("FFM:mergeAfter")
|
|
460
|
+
);
|
|
461
|
+
(0, import_octane.useEffect)(
|
|
462
|
+
() => {
|
|
463
|
+
if (disabled) return;
|
|
464
|
+
if (!floating) return;
|
|
465
|
+
const portalNodes = Array.from(
|
|
466
|
+
portalContext?.portalNode?.querySelectorAll("[" + (0, import_utils.createAttribute)("portal") + "]") || []
|
|
467
|
+
);
|
|
468
|
+
const ancestors = tree ? (0, import_utils.getNodeAncestors)(tree.nodesRef.current, getNodeId()) : [];
|
|
469
|
+
const rootAncestorComboboxDomReference = ancestors.find(
|
|
470
|
+
(node) => (0, import_utils.isTypeableCombobox)(node.context?.elements.domReference || null)
|
|
471
|
+
)?.context?.elements.domReference;
|
|
472
|
+
const insideElements = [
|
|
473
|
+
floating,
|
|
474
|
+
rootAncestorComboboxDomReference,
|
|
475
|
+
...portalNodes,
|
|
476
|
+
...getInsideElements(),
|
|
477
|
+
startDismissButtonRef.current,
|
|
478
|
+
endDismissButtonRef.current,
|
|
479
|
+
beforeGuardRef.current,
|
|
480
|
+
afterGuardRef.current,
|
|
481
|
+
portalContext?.beforeOutsideRef.current,
|
|
482
|
+
portalContext?.afterOutsideRef.current,
|
|
483
|
+
orderRef.current.includes("reference") || isUntrappedTypeableCombobox ? domReference : null
|
|
484
|
+
].filter((x) => x != null);
|
|
485
|
+
const cleanup = modal || isUntrappedTypeableCombobox ? markOthers(insideElements, !useInert, useInert) : markOthers(insideElements);
|
|
486
|
+
return () => {
|
|
487
|
+
cleanup();
|
|
488
|
+
};
|
|
489
|
+
},
|
|
490
|
+
[
|
|
491
|
+
disabled,
|
|
492
|
+
domReference,
|
|
493
|
+
floating,
|
|
494
|
+
modal,
|
|
495
|
+
orderRef,
|
|
496
|
+
portalContext,
|
|
497
|
+
isUntrappedTypeableCombobox,
|
|
498
|
+
guards,
|
|
499
|
+
useInert,
|
|
500
|
+
tree,
|
|
501
|
+
getNodeId,
|
|
502
|
+
getInsideElements
|
|
503
|
+
],
|
|
504
|
+
(0, import_internal.S)("FFM:e:markOthers")
|
|
505
|
+
);
|
|
506
|
+
(0, import_utils.useModernLayoutEffect)(
|
|
507
|
+
() => {
|
|
508
|
+
if (disabled || !(0, import_dom.isHTMLElement)(floatingFocusElement)) return;
|
|
509
|
+
const previouslyFocusedElement = (0, import_utils.activeElement)((0, import_utils.getDocument)(floatingFocusElement));
|
|
510
|
+
queueMicrotask(() => {
|
|
511
|
+
const focusableElements = getTabbableElements(floatingFocusElement);
|
|
512
|
+
const initialFocusValue = initialFocusRef.current;
|
|
513
|
+
const elToFocus = (typeof initialFocusValue === "number" ? focusableElements[initialFocusValue] : initialFocusValue.current) || floatingFocusElement;
|
|
514
|
+
const focusAlreadyInsideFloatingEl = (0, import_utils.contains)(
|
|
515
|
+
floatingFocusElement,
|
|
516
|
+
previouslyFocusedElement
|
|
517
|
+
);
|
|
518
|
+
if (!ignoreInitialFocus && !focusAlreadyInsideFloatingEl && open) {
|
|
519
|
+
(0, import_utils.enqueueFocus)(elToFocus, { preventScroll: elToFocus === floatingFocusElement });
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
},
|
|
523
|
+
[
|
|
524
|
+
disabled,
|
|
525
|
+
open,
|
|
526
|
+
floatingFocusElement,
|
|
527
|
+
ignoreInitialFocus,
|
|
528
|
+
getTabbableElements,
|
|
529
|
+
initialFocusRef
|
|
530
|
+
],
|
|
531
|
+
(0, import_internal.S)("FFM:e:initialFocus")
|
|
532
|
+
);
|
|
533
|
+
(0, import_utils.useModernLayoutEffect)(
|
|
534
|
+
() => {
|
|
535
|
+
if (disabled || !floatingFocusElement) return;
|
|
536
|
+
const doc = (0, import_utils.getDocument)(floatingFocusElement);
|
|
537
|
+
const previouslyFocusedElement = (0, import_utils.activeElement)(doc);
|
|
538
|
+
addPreviouslyFocusedElement(previouslyFocusedElement);
|
|
539
|
+
function onOpenChangeLocal(_ref) {
|
|
540
|
+
const { reason, event, nested } = _ref;
|
|
541
|
+
if (["hover", "safe-polygon"].includes(reason) && event.type === "mouseleave") {
|
|
542
|
+
preventReturnFocusRef.current = true;
|
|
543
|
+
}
|
|
544
|
+
if (reason !== "outside-press") return;
|
|
545
|
+
if (nested) {
|
|
546
|
+
preventReturnFocusRef.current = false;
|
|
547
|
+
} else if ((0, import_utils.isVirtualClick)(event) || (0, import_utils.isVirtualPointerEvent)(event)) {
|
|
548
|
+
preventReturnFocusRef.current = false;
|
|
549
|
+
} else {
|
|
550
|
+
let isPreventScrollSupported = false;
|
|
551
|
+
document.createElement("div").focus({
|
|
552
|
+
get preventScroll() {
|
|
553
|
+
isPreventScrollSupported = true;
|
|
554
|
+
return false;
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
preventReturnFocusRef.current = !isPreventScrollSupported;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
events.on("openchange", onOpenChangeLocal);
|
|
561
|
+
const fallbackEl = doc.createElement("span");
|
|
562
|
+
fallbackEl.setAttribute("tabindex", "-1");
|
|
563
|
+
fallbackEl.setAttribute("aria-hidden", "true");
|
|
564
|
+
Object.assign(fallbackEl.style, HIDDEN_STYLES);
|
|
565
|
+
if (isInsidePortal && domReference) {
|
|
566
|
+
domReference.insertAdjacentElement("afterend", fallbackEl);
|
|
567
|
+
}
|
|
568
|
+
function getReturnElement() {
|
|
569
|
+
if (typeof returnFocusRef.current === "boolean") {
|
|
570
|
+
const el = domReference || getPreviouslyFocusedElement();
|
|
571
|
+
return el && el.isConnected ? el : fallbackEl;
|
|
572
|
+
}
|
|
573
|
+
return returnFocusRef.current.current || fallbackEl;
|
|
574
|
+
}
|
|
575
|
+
return () => {
|
|
576
|
+
events.off("openchange", onOpenChangeLocal);
|
|
577
|
+
const activeEl = (0, import_utils.activeElement)(doc);
|
|
578
|
+
const isFocusInsideFloatingTree = (0, import_utils.contains)(floating, activeEl) || tree && (0, import_utils.getNodeChildren)(tree.nodesRef.current, getNodeId(), false).some(
|
|
579
|
+
(node) => (0, import_utils.contains)(node.context?.elements.floating, activeEl)
|
|
580
|
+
);
|
|
581
|
+
const returnElement = getReturnElement();
|
|
582
|
+
queueMicrotask(() => {
|
|
583
|
+
const tabbableReturnElement = getFirstTabbableElement(returnElement);
|
|
584
|
+
if (returnFocusRef.current && !preventReturnFocusRef.current && (0, import_dom.isHTMLElement)(tabbableReturnElement) && (tabbableReturnElement !== activeEl && activeEl !== doc.body ? isFocusInsideFloatingTree : true)) {
|
|
585
|
+
tabbableReturnElement.focus({ preventScroll: true });
|
|
586
|
+
}
|
|
587
|
+
fallbackEl.remove();
|
|
588
|
+
});
|
|
589
|
+
};
|
|
590
|
+
},
|
|
591
|
+
[
|
|
592
|
+
disabled,
|
|
593
|
+
floating,
|
|
594
|
+
floatingFocusElement,
|
|
595
|
+
returnFocusRef,
|
|
596
|
+
dataRef,
|
|
597
|
+
events,
|
|
598
|
+
tree,
|
|
599
|
+
isInsidePortal,
|
|
600
|
+
domReference,
|
|
601
|
+
getNodeId
|
|
602
|
+
],
|
|
603
|
+
(0, import_internal.S)("FFM:e:returnFocus")
|
|
604
|
+
);
|
|
605
|
+
(0, import_octane.useEffect)(
|
|
606
|
+
() => {
|
|
607
|
+
queueMicrotask(() => {
|
|
608
|
+
preventReturnFocusRef.current = false;
|
|
609
|
+
});
|
|
610
|
+
return () => {
|
|
611
|
+
queueMicrotask(clearDisconnectedPreviouslyFocusedElements);
|
|
612
|
+
};
|
|
613
|
+
},
|
|
614
|
+
[disabled],
|
|
615
|
+
(0, import_internal.S)("FFM:e:resetReturn")
|
|
616
|
+
);
|
|
617
|
+
(0, import_utils.useModernLayoutEffect)(
|
|
618
|
+
() => {
|
|
619
|
+
if (disabled) return;
|
|
620
|
+
if (!portalContext) return;
|
|
621
|
+
portalContext.setFocusManagerState({
|
|
622
|
+
modal,
|
|
623
|
+
closeOnFocusOut,
|
|
624
|
+
open,
|
|
625
|
+
onOpenChange,
|
|
626
|
+
domReference
|
|
627
|
+
});
|
|
628
|
+
return () => {
|
|
629
|
+
portalContext.setFocusManagerState(null);
|
|
630
|
+
};
|
|
631
|
+
},
|
|
632
|
+
[disabled, portalContext, modal, open, onOpenChange, closeOnFocusOut, domReference],
|
|
633
|
+
(0, import_internal.S)("FFM:e:portalState")
|
|
634
|
+
);
|
|
635
|
+
(0, import_utils.useModernLayoutEffect)(
|
|
636
|
+
() => {
|
|
637
|
+
if (disabled) return;
|
|
638
|
+
if (!floatingFocusElement) return;
|
|
639
|
+
handleTabIndex(floatingFocusElement, orderRef);
|
|
640
|
+
},
|
|
641
|
+
[disabled, floatingFocusElement, orderRef],
|
|
642
|
+
(0, import_internal.S)("FFM:e:tabIndex")
|
|
643
|
+
);
|
|
644
|
+
function renderDismissButton(location) {
|
|
645
|
+
if (disabled || !visuallyHiddenDismiss || !modal) {
|
|
646
|
+
return null;
|
|
647
|
+
}
|
|
648
|
+
return (0, import_octane.createElement)(VisuallyHiddenDismiss, {
|
|
649
|
+
ref: location === "start" ? startDismissButtonRef : endDismissButtonRef,
|
|
650
|
+
onClick: (event) => onOpenChange(false, event),
|
|
651
|
+
children: typeof visuallyHiddenDismiss === "string" ? visuallyHiddenDismiss : "Dismiss"
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
const shouldRenderGuards = !disabled && guards && (modal ? !isUntrappedTypeableCombobox : true) && (isInsidePortal || modal);
|
|
655
|
+
return [
|
|
656
|
+
shouldRenderGuards && (0, import_octane.createElement)(import_FloatingPortal.FocusGuard, {
|
|
657
|
+
"data-type": "inside",
|
|
658
|
+
ref: mergedBeforeGuardRef,
|
|
659
|
+
onFocus: (event) => {
|
|
660
|
+
if (modal) {
|
|
661
|
+
const els = getTabbableElements();
|
|
662
|
+
(0, import_utils.enqueueFocus)(order[0] === "reference" ? els[0] : els[els.length - 1]);
|
|
663
|
+
} else if (portalContext != null && portalContext.preserveTabOrder && portalContext.portalNode) {
|
|
664
|
+
preventReturnFocusRef.current = false;
|
|
665
|
+
if ((0, import_utils.isOutsideEvent)(event, portalContext.portalNode)) {
|
|
666
|
+
(0, import_utils.getNextTabbable)(domReference)?.focus();
|
|
667
|
+
} else {
|
|
668
|
+
portalContext.beforeOutsideRef.current?.focus();
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}),
|
|
673
|
+
!isUntrappedTypeableCombobox && renderDismissButton("start"),
|
|
674
|
+
children,
|
|
675
|
+
renderDismissButton("end"),
|
|
676
|
+
shouldRenderGuards && (0, import_octane.createElement)(import_FloatingPortal.FocusGuard, {
|
|
677
|
+
"data-type": "inside",
|
|
678
|
+
ref: mergedAfterGuardRef,
|
|
679
|
+
onFocus: (event) => {
|
|
680
|
+
if (modal) {
|
|
681
|
+
(0, import_utils.enqueueFocus)(getTabbableElements()[0]);
|
|
682
|
+
} else if (portalContext != null && portalContext.preserveTabOrder && portalContext.portalNode) {
|
|
683
|
+
if (closeOnFocusOut) {
|
|
684
|
+
preventReturnFocusRef.current = true;
|
|
685
|
+
}
|
|
686
|
+
if ((0, import_utils.isOutsideEvent)(event, portalContext.portalNode)) {
|
|
687
|
+
(0, import_utils.getPreviousTabbable)(domReference)?.focus();
|
|
688
|
+
} else {
|
|
689
|
+
portalContext.afterOutsideRef.current?.focus();
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
})
|
|
694
|
+
];
|
|
695
|
+
}
|
|
696
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
697
|
+
0 && (module.exports = {
|
|
698
|
+
FloatingFocusManager,
|
|
699
|
+
VisuallyHiddenDismiss,
|
|
700
|
+
supportsInert
|
|
701
|
+
});
|