@octanejs/floating-ui 0.1.31 → 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,337 @@
|
|
|
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 useDismiss_exports = {};
|
|
20
|
+
__export(useDismiss_exports, {
|
|
21
|
+
useDismiss: () => useDismiss
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(useDismiss_exports);
|
|
24
|
+
var import_dom = require("@floating-ui/utils/dom");
|
|
25
|
+
var import_dom2 = require("@floating-ui/dom");
|
|
26
|
+
var import_octane = require("octane");
|
|
27
|
+
var import_internal = require("./internal.cjs");
|
|
28
|
+
var import_tree = require("./tree.cjs");
|
|
29
|
+
var import_utils = require("./utils/index.cjs");
|
|
30
|
+
const bubbleHandlerKeys = {
|
|
31
|
+
pointerdown: "onPointerDown",
|
|
32
|
+
mousedown: "onMouseDown",
|
|
33
|
+
click: "onClick"
|
|
34
|
+
};
|
|
35
|
+
const captureHandlerKeys = {
|
|
36
|
+
pointerdown: "onPointerDownCapture",
|
|
37
|
+
mousedown: "onMouseDownCapture",
|
|
38
|
+
click: "onClickCapture"
|
|
39
|
+
};
|
|
40
|
+
const normalizeProp = (normalizable) => ({
|
|
41
|
+
escapeKey: typeof normalizable === "boolean" ? normalizable : normalizable?.escapeKey ?? false,
|
|
42
|
+
outsidePress: typeof normalizable === "boolean" ? normalizable : normalizable?.outsidePress ?? true
|
|
43
|
+
});
|
|
44
|
+
function useDismiss(...args) {
|
|
45
|
+
const [user, slot] = (0, import_internal.splitSlot)(args);
|
|
46
|
+
const context = user[0];
|
|
47
|
+
const props = user[1] ?? {};
|
|
48
|
+
const open = context.open;
|
|
49
|
+
const onOpenChange = context.onOpenChange;
|
|
50
|
+
const elements = context.elements;
|
|
51
|
+
const dataRef = context.dataRef;
|
|
52
|
+
const enabled = props.enabled ?? true;
|
|
53
|
+
const escapeKey = props.escapeKey ?? true;
|
|
54
|
+
const unstableOutsidePress = props.outsidePress ?? true;
|
|
55
|
+
const outsidePressEvent = props.outsidePressEvent ?? "pointerdown";
|
|
56
|
+
const referencePress = props.referencePress ?? false;
|
|
57
|
+
const referencePressEvent = props.referencePressEvent ?? "pointerdown";
|
|
58
|
+
const ancestorScroll = props.ancestorScroll ?? false;
|
|
59
|
+
const bubbles = props.bubbles;
|
|
60
|
+
const capture = props.capture;
|
|
61
|
+
const tree = (0, import_tree.useFloatingTree)();
|
|
62
|
+
const outsidePressFn = (0, import_utils.useEffectEvent)(
|
|
63
|
+
typeof unstableOutsidePress === "function" ? unstableOutsidePress : () => false,
|
|
64
|
+
(0, import_internal.subSlot)(slot, "opfn")
|
|
65
|
+
);
|
|
66
|
+
const outsidePress = typeof unstableOutsidePress === "function" ? outsidePressFn : unstableOutsidePress;
|
|
67
|
+
const endedOrStartedInsideRef = (0, import_octane.useRef)(false, (0, import_internal.subSlot)(slot, "eosi"));
|
|
68
|
+
const { escapeKey: escapeKeyBubbles, outsidePress: outsidePressBubbles } = normalizeProp(bubbles);
|
|
69
|
+
const { escapeKey: escapeKeyCapture, outsidePress: outsidePressCapture } = normalizeProp(capture);
|
|
70
|
+
const isComposingRef = (0, import_octane.useRef)(false, (0, import_internal.subSlot)(slot, "comp"));
|
|
71
|
+
const closeOnEscapeKeyDown = (0, import_utils.useEffectEvent)(
|
|
72
|
+
(event) => {
|
|
73
|
+
if (!open || !enabled || !escapeKey || event.key !== "Escape") {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (isComposingRef.current) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const nodeId = dataRef.current.floatingContext?.nodeId;
|
|
80
|
+
const children = tree ? (0, import_utils.getNodeChildren)(tree.nodesRef.current, nodeId) : [];
|
|
81
|
+
if (!escapeKeyBubbles) {
|
|
82
|
+
event.stopPropagation();
|
|
83
|
+
if (children.length > 0) {
|
|
84
|
+
let shouldDismiss = true;
|
|
85
|
+
children.forEach((child) => {
|
|
86
|
+
if (child.context?.open && !child.context.dataRef.current.__escapeKeyBubbles) {
|
|
87
|
+
shouldDismiss = false;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
if (!shouldDismiss) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
onOpenChange(false, (0, import_utils.isReactEvent)(event) ? event.nativeEvent : event, "escape-key");
|
|
97
|
+
},
|
|
98
|
+
(0, import_internal.subSlot)(slot, "esc")
|
|
99
|
+
);
|
|
100
|
+
const closeOnEscapeKeyDownCapture = (0, import_utils.useEffectEvent)(
|
|
101
|
+
(event) => {
|
|
102
|
+
const callback = () => {
|
|
103
|
+
closeOnEscapeKeyDown(event);
|
|
104
|
+
(0, import_utils.getTarget)(event)?.removeEventListener("keydown", callback);
|
|
105
|
+
};
|
|
106
|
+
(0, import_utils.getTarget)(event)?.addEventListener("keydown", callback);
|
|
107
|
+
},
|
|
108
|
+
(0, import_internal.subSlot)(slot, "escc")
|
|
109
|
+
);
|
|
110
|
+
const closeOnPressOutside = (0, import_utils.useEffectEvent)(
|
|
111
|
+
(event) => {
|
|
112
|
+
const insideReactTree = dataRef.current.insideReactTree;
|
|
113
|
+
dataRef.current.insideReactTree = false;
|
|
114
|
+
const endedOrStartedInside = endedOrStartedInsideRef.current;
|
|
115
|
+
endedOrStartedInsideRef.current = false;
|
|
116
|
+
if (outsidePressEvent === "click" && endedOrStartedInside) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (insideReactTree) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (typeof outsidePress === "function" && !outsidePress(event)) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const target = (0, import_utils.getTarget)(event);
|
|
126
|
+
const inertSelector = "[" + (0, import_utils.createAttribute)("inert") + "]";
|
|
127
|
+
const markers = (0, import_utils.getDocument)(elements.floating).querySelectorAll(inertSelector);
|
|
128
|
+
let targetRootAncestor = (0, import_dom.isElement)(target) ? target : null;
|
|
129
|
+
while (targetRootAncestor && !(0, import_dom.isLastTraversableNode)(targetRootAncestor)) {
|
|
130
|
+
const nextParent = (0, import_dom.getParentNode)(targetRootAncestor);
|
|
131
|
+
if ((0, import_dom.isLastTraversableNode)(nextParent) || !(0, import_dom.isElement)(nextParent)) {
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
targetRootAncestor = nextParent;
|
|
135
|
+
}
|
|
136
|
+
if (markers.length && (0, import_dom.isElement)(target) && !(0, import_utils.isRootElement)(target) && !(0, import_utils.contains)(target, elements.floating) && Array.from(markers).every((marker) => !(0, import_utils.contains)(targetRootAncestor, marker))) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if ((0, import_dom.isHTMLElement)(target) && elements.floating) {
|
|
140
|
+
const lastTraversableNode = (0, import_dom.isLastTraversableNode)(target);
|
|
141
|
+
const style = (0, import_dom.getComputedStyle)(target);
|
|
142
|
+
const scrollRe = /auto|scroll/;
|
|
143
|
+
const isScrollableX = lastTraversableNode || scrollRe.test(style.overflowX);
|
|
144
|
+
const isScrollableY = lastTraversableNode || scrollRe.test(style.overflowY);
|
|
145
|
+
const canScrollX = isScrollableX && target.clientWidth > 0 && target.scrollWidth > target.clientWidth;
|
|
146
|
+
const canScrollY = isScrollableY && target.clientHeight > 0 && target.scrollHeight > target.clientHeight;
|
|
147
|
+
const isRTL = style.direction === "rtl";
|
|
148
|
+
const pressedVerticalScrollbar = canScrollY && (isRTL ? event.offsetX <= target.offsetWidth - target.clientWidth : event.offsetX > target.clientWidth);
|
|
149
|
+
const pressedHorizontalScrollbar = canScrollX && event.offsetY > target.clientHeight;
|
|
150
|
+
if (pressedVerticalScrollbar || pressedHorizontalScrollbar) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const nodeId = dataRef.current.floatingContext?.nodeId;
|
|
155
|
+
const targetIsInsideChildren = tree && (0, import_utils.getNodeChildren)(tree.nodesRef.current, nodeId).some(
|
|
156
|
+
(node) => (0, import_utils.isEventTargetWithin)(event, node.context?.elements.floating)
|
|
157
|
+
);
|
|
158
|
+
if ((0, import_utils.isEventTargetWithin)(event, elements.floating) || (0, import_utils.isEventTargetWithin)(event, elements.domReference) || targetIsInsideChildren) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const children = tree ? (0, import_utils.getNodeChildren)(tree.nodesRef.current, nodeId) : [];
|
|
162
|
+
if (children.length > 0) {
|
|
163
|
+
let shouldDismiss = true;
|
|
164
|
+
children.forEach((child) => {
|
|
165
|
+
if (child.context?.open && !child.context.dataRef.current.__outsidePressBubbles) {
|
|
166
|
+
shouldDismiss = false;
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
if (!shouldDismiss) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
onOpenChange(false, event, "outside-press");
|
|
175
|
+
},
|
|
176
|
+
(0, import_internal.subSlot)(slot, "press")
|
|
177
|
+
);
|
|
178
|
+
const closeOnPressOutsideCapture = (0, import_utils.useEffectEvent)(
|
|
179
|
+
(event) => {
|
|
180
|
+
const callback = () => {
|
|
181
|
+
closeOnPressOutside(event);
|
|
182
|
+
(0, import_utils.getTarget)(event)?.removeEventListener(outsidePressEvent, callback);
|
|
183
|
+
};
|
|
184
|
+
(0, import_utils.getTarget)(event)?.addEventListener(outsidePressEvent, callback);
|
|
185
|
+
},
|
|
186
|
+
(0, import_internal.subSlot)(slot, "pressc")
|
|
187
|
+
);
|
|
188
|
+
(0, import_octane.useEffect)(
|
|
189
|
+
() => {
|
|
190
|
+
if (!open || !enabled) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
dataRef.current.__escapeKeyBubbles = escapeKeyBubbles;
|
|
194
|
+
dataRef.current.__outsidePressBubbles = outsidePressBubbles;
|
|
195
|
+
let compositionTimeout = -1;
|
|
196
|
+
function onScroll(event) {
|
|
197
|
+
onOpenChange(false, event, "ancestor-scroll");
|
|
198
|
+
}
|
|
199
|
+
function handleCompositionStart() {
|
|
200
|
+
window.clearTimeout(compositionTimeout);
|
|
201
|
+
isComposingRef.current = true;
|
|
202
|
+
}
|
|
203
|
+
function handleCompositionEnd() {
|
|
204
|
+
compositionTimeout = window.setTimeout(
|
|
205
|
+
() => {
|
|
206
|
+
isComposingRef.current = false;
|
|
207
|
+
},
|
|
208
|
+
(0, import_dom.isWebKit)() ? 5 : 0
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
const doc = (0, import_utils.getDocument)(elements.floating);
|
|
212
|
+
if (escapeKey) {
|
|
213
|
+
doc.addEventListener(
|
|
214
|
+
"keydown",
|
|
215
|
+
escapeKeyCapture ? closeOnEscapeKeyDownCapture : closeOnEscapeKeyDown,
|
|
216
|
+
escapeKeyCapture
|
|
217
|
+
);
|
|
218
|
+
doc.addEventListener("compositionstart", handleCompositionStart);
|
|
219
|
+
doc.addEventListener("compositionend", handleCompositionEnd);
|
|
220
|
+
}
|
|
221
|
+
outsidePress && doc.addEventListener(
|
|
222
|
+
outsidePressEvent,
|
|
223
|
+
outsidePressCapture ? closeOnPressOutsideCapture : closeOnPressOutside,
|
|
224
|
+
outsidePressCapture
|
|
225
|
+
);
|
|
226
|
+
let ancestors = [];
|
|
227
|
+
if (ancestorScroll) {
|
|
228
|
+
if ((0, import_dom.isElement)(elements.domReference)) {
|
|
229
|
+
ancestors = (0, import_dom2.getOverflowAncestors)(elements.domReference);
|
|
230
|
+
}
|
|
231
|
+
if ((0, import_dom.isElement)(elements.floating)) {
|
|
232
|
+
ancestors = ancestors.concat((0, import_dom2.getOverflowAncestors)(elements.floating));
|
|
233
|
+
}
|
|
234
|
+
if (!(0, import_dom.isElement)(elements.reference) && elements.reference && elements.reference.contextElement) {
|
|
235
|
+
ancestors = ancestors.concat((0, import_dom2.getOverflowAncestors)(elements.reference.contextElement));
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
ancestors = ancestors.filter((ancestor) => ancestor !== doc.defaultView?.visualViewport);
|
|
239
|
+
ancestors.forEach((ancestor) => {
|
|
240
|
+
ancestor.addEventListener("scroll", onScroll, { passive: true });
|
|
241
|
+
});
|
|
242
|
+
return () => {
|
|
243
|
+
if (escapeKey) {
|
|
244
|
+
doc.removeEventListener(
|
|
245
|
+
"keydown",
|
|
246
|
+
escapeKeyCapture ? closeOnEscapeKeyDownCapture : closeOnEscapeKeyDown,
|
|
247
|
+
escapeKeyCapture
|
|
248
|
+
);
|
|
249
|
+
doc.removeEventListener("compositionstart", handleCompositionStart);
|
|
250
|
+
doc.removeEventListener("compositionend", handleCompositionEnd);
|
|
251
|
+
}
|
|
252
|
+
outsidePress && doc.removeEventListener(
|
|
253
|
+
outsidePressEvent,
|
|
254
|
+
outsidePressCapture ? closeOnPressOutsideCapture : closeOnPressOutside,
|
|
255
|
+
outsidePressCapture
|
|
256
|
+
);
|
|
257
|
+
ancestors.forEach((ancestor) => {
|
|
258
|
+
ancestor.removeEventListener("scroll", onScroll);
|
|
259
|
+
});
|
|
260
|
+
window.clearTimeout(compositionTimeout);
|
|
261
|
+
};
|
|
262
|
+
},
|
|
263
|
+
[
|
|
264
|
+
dataRef,
|
|
265
|
+
elements,
|
|
266
|
+
escapeKey,
|
|
267
|
+
outsidePress,
|
|
268
|
+
outsidePressEvent,
|
|
269
|
+
open,
|
|
270
|
+
onOpenChange,
|
|
271
|
+
ancestorScroll,
|
|
272
|
+
enabled,
|
|
273
|
+
escapeKeyBubbles,
|
|
274
|
+
outsidePressBubbles,
|
|
275
|
+
closeOnEscapeKeyDown,
|
|
276
|
+
escapeKeyCapture,
|
|
277
|
+
closeOnEscapeKeyDownCapture,
|
|
278
|
+
closeOnPressOutside,
|
|
279
|
+
outsidePressCapture,
|
|
280
|
+
closeOnPressOutsideCapture
|
|
281
|
+
],
|
|
282
|
+
(0, import_internal.subSlot)(slot, "e:listeners")
|
|
283
|
+
);
|
|
284
|
+
(0, import_octane.useEffect)(
|
|
285
|
+
() => {
|
|
286
|
+
dataRef.current.insideReactTree = false;
|
|
287
|
+
},
|
|
288
|
+
[dataRef, outsidePress, outsidePressEvent],
|
|
289
|
+
(0, import_internal.subSlot)(slot, "e:reset")
|
|
290
|
+
);
|
|
291
|
+
const reference = (0, import_octane.useMemo)(
|
|
292
|
+
() => ({
|
|
293
|
+
onKeyDown: closeOnEscapeKeyDown,
|
|
294
|
+
...referencePress && {
|
|
295
|
+
[bubbleHandlerKeys[referencePressEvent]]: (event) => {
|
|
296
|
+
onOpenChange(false, event, "reference-press");
|
|
297
|
+
},
|
|
298
|
+
...referencePressEvent !== "click" && {
|
|
299
|
+
onClick(event) {
|
|
300
|
+
onOpenChange(false, event, "reference-press");
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}),
|
|
305
|
+
[closeOnEscapeKeyDown, onOpenChange, referencePress, referencePressEvent],
|
|
306
|
+
(0, import_internal.subSlot)(slot, "m:ref")
|
|
307
|
+
);
|
|
308
|
+
const floating = (0, import_octane.useMemo)(
|
|
309
|
+
() => {
|
|
310
|
+
function setMouseDownOrUpInside(event) {
|
|
311
|
+
if (event.button !== 0) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
endedOrStartedInsideRef.current = true;
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
onKeyDown: closeOnEscapeKeyDown,
|
|
318
|
+
onMouseDown: setMouseDownOrUpInside,
|
|
319
|
+
onMouseUp: setMouseDownOrUpInside,
|
|
320
|
+
[captureHandlerKeys[outsidePressEvent]]: () => {
|
|
321
|
+
dataRef.current.insideReactTree = true;
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
},
|
|
325
|
+
[closeOnEscapeKeyDown, outsidePressEvent, dataRef],
|
|
326
|
+
(0, import_internal.subSlot)(slot, "m:flo")
|
|
327
|
+
);
|
|
328
|
+
return (0, import_octane.useMemo)(
|
|
329
|
+
() => enabled ? { reference, floating } : {},
|
|
330
|
+
[enabled, reference, floating],
|
|
331
|
+
(0, import_internal.subSlot)(slot, "m:ret")
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
335
|
+
0 && (module.exports = {
|
|
336
|
+
useDismiss
|
|
337
|
+
});
|
|
@@ -0,0 +1,224 @@
|
|
|
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 useFloating_exports = {};
|
|
20
|
+
__export(useFloating_exports, {
|
|
21
|
+
arrow: () => arrow,
|
|
22
|
+
usePositionFloating: () => usePositionFloating
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(useFloating_exports);
|
|
25
|
+
var import_dom = require("@floating-ui/dom");
|
|
26
|
+
var import_octane = require("octane");
|
|
27
|
+
var import_internal = require("./internal.cjs");
|
|
28
|
+
var import_utils = require("./utils/index.cjs");
|
|
29
|
+
function useLatestRef(value, slot) {
|
|
30
|
+
const ref = (0, import_octane.useRef)(value, (0, import_internal.subSlot)(slot, "lr:ref"));
|
|
31
|
+
(0, import_octane.useLayoutEffect)(
|
|
32
|
+
() => {
|
|
33
|
+
ref.current = value;
|
|
34
|
+
},
|
|
35
|
+
void 0,
|
|
36
|
+
(0, import_internal.subSlot)(slot, "lr:eff")
|
|
37
|
+
);
|
|
38
|
+
return ref;
|
|
39
|
+
}
|
|
40
|
+
function usePositionFloating(args) {
|
|
41
|
+
const [user, slot] = (0, import_internal.splitSlot)(args);
|
|
42
|
+
const options = user[0] ?? {};
|
|
43
|
+
const placement = options.placement ?? "bottom";
|
|
44
|
+
const strategy = options.strategy ?? "absolute";
|
|
45
|
+
const middleware = options.middleware ?? [];
|
|
46
|
+
const platform = options.platform;
|
|
47
|
+
const externalReference = options.elements ? options.elements.reference : void 0;
|
|
48
|
+
const externalFloating = options.elements ? options.elements.floating : void 0;
|
|
49
|
+
const transform = options.transform ?? true;
|
|
50
|
+
const whileElementsMounted = options.whileElementsMounted;
|
|
51
|
+
const open = options.open;
|
|
52
|
+
const [data, setData] = (0, import_octane.useState)(
|
|
53
|
+
{
|
|
54
|
+
x: 0,
|
|
55
|
+
y: 0,
|
|
56
|
+
strategy,
|
|
57
|
+
placement,
|
|
58
|
+
middlewareData: {},
|
|
59
|
+
isPositioned: false
|
|
60
|
+
},
|
|
61
|
+
(0, import_internal.subSlot)(slot, "data")
|
|
62
|
+
);
|
|
63
|
+
const [latestMiddleware, setLatestMiddleware] = (0, import_octane.useState)(middleware, (0, import_internal.subSlot)(slot, "mw"));
|
|
64
|
+
if (!(0, import_utils.deepEqual)(latestMiddleware, middleware)) {
|
|
65
|
+
setLatestMiddleware(middleware);
|
|
66
|
+
}
|
|
67
|
+
const [_reference, _setReference] = (0, import_octane.useState)(null, (0, import_internal.subSlot)(slot, "ref"));
|
|
68
|
+
const [_floating, _setFloating] = (0, import_octane.useState)(null, (0, import_internal.subSlot)(slot, "flo"));
|
|
69
|
+
const referenceRef = (0, import_octane.useRef)(null, (0, import_internal.subSlot)(slot, "rref"));
|
|
70
|
+
const floatingRef = (0, import_octane.useRef)(null, (0, import_internal.subSlot)(slot, "rflo"));
|
|
71
|
+
const setReference = (0, import_octane.useCallback)(
|
|
72
|
+
(node) => {
|
|
73
|
+
if (node !== referenceRef.current) {
|
|
74
|
+
referenceRef.current = node;
|
|
75
|
+
_setReference(node);
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
[],
|
|
79
|
+
(0, import_internal.subSlot)(slot, "sref")
|
|
80
|
+
);
|
|
81
|
+
const setFloating = (0, import_octane.useCallback)(
|
|
82
|
+
(node) => {
|
|
83
|
+
if (node !== floatingRef.current) {
|
|
84
|
+
floatingRef.current = node;
|
|
85
|
+
_setFloating(node);
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
[],
|
|
89
|
+
(0, import_internal.subSlot)(slot, "sflo")
|
|
90
|
+
);
|
|
91
|
+
const referenceEl = externalReference || _reference;
|
|
92
|
+
const floatingEl = externalFloating || _floating;
|
|
93
|
+
const dataRef = (0, import_octane.useRef)(data, (0, import_internal.subSlot)(slot, "dref"));
|
|
94
|
+
const hasWhileElementsMounted = whileElementsMounted != null;
|
|
95
|
+
const whileElementsMountedRef = useLatestRef(whileElementsMounted, (0, import_internal.subSlot)(slot, "wem"));
|
|
96
|
+
const platformRef = useLatestRef(platform, (0, import_internal.subSlot)(slot, "plat"));
|
|
97
|
+
const openRef = useLatestRef(open, (0, import_internal.subSlot)(slot, "open"));
|
|
98
|
+
const isMountedRef = (0, import_octane.useRef)(false, (0, import_internal.subSlot)(slot, "mnt"));
|
|
99
|
+
const update = (0, import_octane.useCallback)(
|
|
100
|
+
() => {
|
|
101
|
+
if (!referenceRef.current || !floatingRef.current) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const config = {
|
|
105
|
+
placement,
|
|
106
|
+
strategy,
|
|
107
|
+
middleware: latestMiddleware
|
|
108
|
+
};
|
|
109
|
+
if (platformRef.current) {
|
|
110
|
+
config.platform = platformRef.current;
|
|
111
|
+
}
|
|
112
|
+
(0, import_dom.computePosition)(referenceRef.current, floatingRef.current, config).then((computed) => {
|
|
113
|
+
const fullData = {
|
|
114
|
+
...computed,
|
|
115
|
+
isPositioned: openRef.current !== false
|
|
116
|
+
};
|
|
117
|
+
if (isMountedRef.current && !(0, import_utils.deepEqual)(dataRef.current, fullData)) {
|
|
118
|
+
dataRef.current = fullData;
|
|
119
|
+
(0, import_octane.flushSync)(() => {
|
|
120
|
+
setData(fullData);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
},
|
|
125
|
+
[latestMiddleware, placement, strategy, platformRef, openRef],
|
|
126
|
+
(0, import_internal.subSlot)(slot, "upd")
|
|
127
|
+
);
|
|
128
|
+
(0, import_octane.useLayoutEffect)(
|
|
129
|
+
() => {
|
|
130
|
+
if (open === false && dataRef.current.isPositioned) {
|
|
131
|
+
dataRef.current.isPositioned = false;
|
|
132
|
+
setData((d) => ({ ...d, isPositioned: false }));
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
[open],
|
|
136
|
+
(0, import_internal.subSlot)(slot, "e:open")
|
|
137
|
+
);
|
|
138
|
+
(0, import_octane.useLayoutEffect)(
|
|
139
|
+
() => {
|
|
140
|
+
isMountedRef.current = true;
|
|
141
|
+
return () => {
|
|
142
|
+
isMountedRef.current = false;
|
|
143
|
+
};
|
|
144
|
+
},
|
|
145
|
+
[],
|
|
146
|
+
(0, import_internal.subSlot)(slot, "e:mnt")
|
|
147
|
+
);
|
|
148
|
+
(0, import_octane.useLayoutEffect)(
|
|
149
|
+
() => {
|
|
150
|
+
if (referenceEl) referenceRef.current = referenceEl;
|
|
151
|
+
if (floatingEl) floatingRef.current = floatingEl;
|
|
152
|
+
if (referenceEl && floatingEl) {
|
|
153
|
+
if (whileElementsMountedRef.current) {
|
|
154
|
+
return whileElementsMountedRef.current(referenceEl, floatingEl, update);
|
|
155
|
+
}
|
|
156
|
+
update();
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
[referenceEl, floatingEl, update, whileElementsMountedRef, hasWhileElementsMounted],
|
|
160
|
+
(0, import_internal.subSlot)(slot, "e:el")
|
|
161
|
+
);
|
|
162
|
+
const refs = (0, import_octane.useMemo)(
|
|
163
|
+
() => ({ reference: referenceRef, floating: floatingRef, setReference, setFloating }),
|
|
164
|
+
[setReference, setFloating],
|
|
165
|
+
(0, import_internal.subSlot)(slot, "m:refs")
|
|
166
|
+
);
|
|
167
|
+
const elements = (0, import_octane.useMemo)(
|
|
168
|
+
() => ({ reference: referenceEl, floating: floatingEl }),
|
|
169
|
+
[referenceEl, floatingEl],
|
|
170
|
+
(0, import_internal.subSlot)(slot, "m:el")
|
|
171
|
+
);
|
|
172
|
+
const floatingStyles = (0, import_octane.useMemo)(
|
|
173
|
+
() => {
|
|
174
|
+
const initialStyles = { position: strategy, left: 0, top: 0 };
|
|
175
|
+
if (!elements.floating) {
|
|
176
|
+
return initialStyles;
|
|
177
|
+
}
|
|
178
|
+
const x = (0, import_utils.roundByDPR)(elements.floating, data.x);
|
|
179
|
+
const y = (0, import_utils.roundByDPR)(elements.floating, data.y);
|
|
180
|
+
if (transform) {
|
|
181
|
+
return {
|
|
182
|
+
...initialStyles,
|
|
183
|
+
transform: "translate(" + x + "px, " + y + "px)",
|
|
184
|
+
...(0, import_utils.getDPR)(elements.floating) >= 1.5 && { willChange: "transform" }
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
return { position: strategy, left: x, top: y };
|
|
188
|
+
},
|
|
189
|
+
[strategy, transform, elements.floating, data.x, data.y],
|
|
190
|
+
(0, import_internal.subSlot)(slot, "m:fs")
|
|
191
|
+
);
|
|
192
|
+
return (0, import_octane.useMemo)(
|
|
193
|
+
() => ({ ...data, update, refs, elements, floatingStyles }),
|
|
194
|
+
[data, update, refs, elements, floatingStyles],
|
|
195
|
+
(0, import_internal.subSlot)(slot, "m:ret")
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
const arrow = (options) => {
|
|
199
|
+
function isRef(value) {
|
|
200
|
+
return {}.hasOwnProperty.call(value, "current");
|
|
201
|
+
}
|
|
202
|
+
return {
|
|
203
|
+
name: "arrow",
|
|
204
|
+
options,
|
|
205
|
+
fn(state) {
|
|
206
|
+
const { element, padding } = typeof options === "function" ? options(state) : options;
|
|
207
|
+
if (element && isRef(element)) {
|
|
208
|
+
if (element.current != null) {
|
|
209
|
+
return (0, import_dom.arrow)({ element: element.current, padding }).fn(state);
|
|
210
|
+
}
|
|
211
|
+
return {};
|
|
212
|
+
}
|
|
213
|
+
if (element) {
|
|
214
|
+
return (0, import_dom.arrow)({ element, padding }).fn(state);
|
|
215
|
+
}
|
|
216
|
+
return {};
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
};
|
|
220
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
221
|
+
0 && (module.exports = {
|
|
222
|
+
arrow,
|
|
223
|
+
usePositionFloating
|
|
224
|
+
});
|