@yoopta/ui 6.0.0 → 6.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/action-menu-list.js.map +1 -0
- package/dist/block-dnd.js.map +1 -0
- package/dist/block-options.js.map +1 -0
- package/dist/chunks/_tslib-8c24563b.js +57 -0
- package/dist/chunks/_tslib-8c24563b.js.map +1 -0
- package/dist/chunks/createLucideIcon-6ba35914.js +56 -0
- package/dist/chunks/createLucideIcon-6ba35914.js.map +1 -0
- package/dist/chunks/debounce-1e7f29bf.js +40 -0
- package/dist/chunks/debounce-1e7f29bf.js.map +1 -0
- package/dist/chunks/floating-ui.react-77cadca5.js +2469 -0
- package/dist/chunks/floating-ui.react-77cadca5.js.map +1 -0
- package/dist/chunks/floating-ui.react-dom-3fffb30c.js +2356 -0
- package/dist/chunks/floating-ui.react-dom-3fffb30c.js.map +1 -0
- package/dist/chunks/style-inject.es-746bb8ed.js +29 -0
- package/dist/chunks/style-inject.es-746bb8ed.js.map +1 -0
- package/dist/chunks/throttle-278836f4.js +45 -0
- package/dist/chunks/throttle-278836f4.js.map +1 -0
- package/dist/element-options.js.map +1 -0
- package/dist/floating-block-actions.js.map +1 -0
- package/dist/floating-toolbar.js.map +1 -0
- package/dist/highlight-color-picker/highlight-color-picker.d.ts.map +1 -1
- package/dist/highlight-color-picker.js +1 -1
- package/dist/highlight-color-picker.js.map +1 -0
- package/dist/index.js.map +1 -0
- package/dist/overlay.js.map +1 -0
- package/dist/portal.js.map +1 -0
- package/dist/selection-box.js.map +1 -0
- package/dist/slash-command-menu.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,2469 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { useLayoutEffect, useEffect, useRef } from 'react';
|
|
3
|
+
import { i as isShadowRoot, a as isHTMLElement, b as isElement, g as getOverflowAncestors, u as useFloating$1, c as getNodeName, d as isLastTraversableNode, e as getParentNode, f as getComputedStyle$1, h as isWebKit } from './floating-ui.react-dom-3fffb30c.js';
|
|
4
|
+
import * as ReactDOM from 'react-dom';
|
|
5
|
+
|
|
6
|
+
function activeElement(doc) {
|
|
7
|
+
let activeElement = doc.activeElement;
|
|
8
|
+
while (((_activeElement = activeElement) == null || (_activeElement = _activeElement.shadowRoot) == null ? void 0 : _activeElement.activeElement) != null) {
|
|
9
|
+
var _activeElement;
|
|
10
|
+
activeElement = activeElement.shadowRoot.activeElement;
|
|
11
|
+
}
|
|
12
|
+
return activeElement;
|
|
13
|
+
}
|
|
14
|
+
function contains(parent, child) {
|
|
15
|
+
if (!parent || !child) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
const rootNode = child.getRootNode == null ? void 0 : child.getRootNode();
|
|
19
|
+
|
|
20
|
+
// First, attempt with faster native method
|
|
21
|
+
if (parent.contains(child)) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// then fallback to custom implementation with Shadow DOM support
|
|
26
|
+
if (rootNode && isShadowRoot(rootNode)) {
|
|
27
|
+
let next = child;
|
|
28
|
+
while (next) {
|
|
29
|
+
if (parent === next) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
next = next.parentNode || next.host;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Give up, the result is false
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
// Avoid Chrome DevTools blue warning.
|
|
41
|
+
function getPlatform() {
|
|
42
|
+
const uaData = navigator.userAgentData;
|
|
43
|
+
if (uaData != null && uaData.platform) {
|
|
44
|
+
return uaData.platform;
|
|
45
|
+
}
|
|
46
|
+
return navigator.platform;
|
|
47
|
+
}
|
|
48
|
+
function getUserAgent() {
|
|
49
|
+
const uaData = navigator.userAgentData;
|
|
50
|
+
if (uaData && Array.isArray(uaData.brands)) {
|
|
51
|
+
return uaData.brands.map(_ref => {
|
|
52
|
+
let {
|
|
53
|
+
brand,
|
|
54
|
+
version
|
|
55
|
+
} = _ref;
|
|
56
|
+
return brand + "/" + version;
|
|
57
|
+
}).join(' ');
|
|
58
|
+
}
|
|
59
|
+
return navigator.userAgent;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// License: https://github.com/adobe/react-spectrum/blob/b35d5c02fe900badccd0cf1a8f23bb593419f238/packages/@react-aria/utils/src/isVirtualEvent.ts
|
|
63
|
+
function isVirtualClick(event) {
|
|
64
|
+
// FIXME: Firefox is now emitting a deprecation warning for `mozInputSource`.
|
|
65
|
+
// Try to find a workaround for this. `react-aria` source still has the check.
|
|
66
|
+
if (event.mozInputSource === 0 && event.isTrusted) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
if (isAndroid() && event.pointerType) {
|
|
70
|
+
return event.type === 'click' && event.buttons === 1;
|
|
71
|
+
}
|
|
72
|
+
return event.detail === 0 && !event.pointerType;
|
|
73
|
+
}
|
|
74
|
+
function isVirtualPointerEvent(event) {
|
|
75
|
+
if (isJSDOM()) return false;
|
|
76
|
+
return !isAndroid() && event.width === 0 && event.height === 0 || isAndroid() && event.width === 1 && event.height === 1 && event.pressure === 0 && event.detail === 0 && event.pointerType === 'mouse' ||
|
|
77
|
+
// iOS VoiceOver returns 0.333• for width/height.
|
|
78
|
+
event.width < 1 && event.height < 1 && event.pressure === 0 && event.detail === 0 && event.pointerType === 'touch';
|
|
79
|
+
}
|
|
80
|
+
function isSafari() {
|
|
81
|
+
// Chrome DevTools does not complain about navigator.vendor
|
|
82
|
+
return /apple/i.test(navigator.vendor);
|
|
83
|
+
}
|
|
84
|
+
function isAndroid() {
|
|
85
|
+
const re = /android/i;
|
|
86
|
+
return re.test(getPlatform()) || re.test(getUserAgent());
|
|
87
|
+
}
|
|
88
|
+
function isJSDOM() {
|
|
89
|
+
return getUserAgent().includes('jsdom/');
|
|
90
|
+
}
|
|
91
|
+
function isReactEvent(event) {
|
|
92
|
+
return 'nativeEvent' in event;
|
|
93
|
+
}
|
|
94
|
+
function isRootElement(element) {
|
|
95
|
+
return element.matches('html,body');
|
|
96
|
+
}
|
|
97
|
+
function getDocument(node) {
|
|
98
|
+
return (node == null ? void 0 : node.ownerDocument) || document;
|
|
99
|
+
}
|
|
100
|
+
function isEventTargetWithin(event, node) {
|
|
101
|
+
if (node == null) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
if ('composedPath' in event) {
|
|
105
|
+
return event.composedPath().includes(node);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// TS thinks `event` is of type never as it assumes all browsers support composedPath, but browsers without shadow dom don't
|
|
109
|
+
const e = event;
|
|
110
|
+
return e.target != null && node.contains(e.target);
|
|
111
|
+
}
|
|
112
|
+
function getTarget(event) {
|
|
113
|
+
if ('composedPath' in event) {
|
|
114
|
+
return event.composedPath()[0];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// TS thinks `event` is of type never as it assumes all browsers support
|
|
118
|
+
// `composedPath()`, but browsers without shadow DOM don't.
|
|
119
|
+
return event.target;
|
|
120
|
+
}
|
|
121
|
+
const TYPEABLE_SELECTOR = "input:not([type='hidden']):not([disabled])," + "[contenteditable]:not([contenteditable='false']),textarea:not([disabled])";
|
|
122
|
+
function isTypeableElement(element) {
|
|
123
|
+
return isHTMLElement(element) && element.matches(TYPEABLE_SELECTOR);
|
|
124
|
+
}
|
|
125
|
+
function stopEvent(event) {
|
|
126
|
+
event.preventDefault();
|
|
127
|
+
event.stopPropagation();
|
|
128
|
+
}
|
|
129
|
+
function isTypeableCombobox(element) {
|
|
130
|
+
if (!element) return false;
|
|
131
|
+
return element.getAttribute('role') === 'combobox' && isTypeableElement(element);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/*!
|
|
135
|
+
* tabbable 6.4.0
|
|
136
|
+
* @license MIT, https://github.com/focus-trap/tabbable/blob/master/LICENSE
|
|
137
|
+
*/
|
|
138
|
+
// NOTE: separate `:not()` selectors has broader browser support than the newer
|
|
139
|
+
// `:not([inert], [inert] *)` (Feb 2023)
|
|
140
|
+
var candidateSelectors = ['input:not([inert]):not([inert] *)', 'select:not([inert]):not([inert] *)', 'textarea:not([inert]):not([inert] *)', 'a[href]:not([inert]):not([inert] *)', 'button:not([inert]):not([inert] *)', '[tabindex]:not(slot):not([inert]):not([inert] *)', 'audio[controls]:not([inert]):not([inert] *)', 'video[controls]:not([inert]):not([inert] *)', '[contenteditable]:not([contenteditable="false"]):not([inert]):not([inert] *)', 'details>summary:first-of-type:not([inert]):not([inert] *)', 'details:not([inert]):not([inert] *)'];
|
|
141
|
+
var candidateSelector = /* #__PURE__ */candidateSelectors.join(',');
|
|
142
|
+
var NoElement = typeof Element === 'undefined';
|
|
143
|
+
var matches = NoElement ? function () {} : Element.prototype.matches || Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
|
|
144
|
+
var getRootNode = !NoElement && Element.prototype.getRootNode ? function (element) {
|
|
145
|
+
var _element$getRootNode;
|
|
146
|
+
return element === null || element === void 0 ? void 0 : (_element$getRootNode = element.getRootNode) === null || _element$getRootNode === void 0 ? void 0 : _element$getRootNode.call(element);
|
|
147
|
+
} : function (element) {
|
|
148
|
+
return element === null || element === void 0 ? void 0 : element.ownerDocument;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Determines if a node is inert or in an inert ancestor.
|
|
153
|
+
* @param {Node} [node]
|
|
154
|
+
* @param {boolean} [lookUp] If true and `node` is not inert, looks up at ancestors to
|
|
155
|
+
* see if any of them are inert. If false, only `node` itself is considered.
|
|
156
|
+
* @returns {boolean} True if inert itself or by way of being in an inert ancestor.
|
|
157
|
+
* False if `node` is falsy.
|
|
158
|
+
*/
|
|
159
|
+
var _isInert = function isInert(node, lookUp) {
|
|
160
|
+
var _node$getAttribute;
|
|
161
|
+
if (lookUp === void 0) {
|
|
162
|
+
lookUp = true;
|
|
163
|
+
}
|
|
164
|
+
// CAREFUL: JSDom does not support inert at all, so we can't use the `HTMLElement.inert`
|
|
165
|
+
// JS API property; we have to check the attribute, which can either be empty or 'true';
|
|
166
|
+
// if it's `null` (not specified) or 'false', it's an active element
|
|
167
|
+
var inertAtt = node === null || node === void 0 ? void 0 : (_node$getAttribute = node.getAttribute) === null || _node$getAttribute === void 0 ? void 0 : _node$getAttribute.call(node, 'inert');
|
|
168
|
+
var inert = inertAtt === '' || inertAtt === 'true';
|
|
169
|
+
|
|
170
|
+
// NOTE: this could also be handled with `node.matches('[inert], :is([inert] *)')`
|
|
171
|
+
// if it weren't for `matches()` not being a function on shadow roots; the following
|
|
172
|
+
// code works for any kind of node
|
|
173
|
+
var result = inert || lookUp && node && (
|
|
174
|
+
// closest does not exist on shadow roots, so we fall back to a manual
|
|
175
|
+
// lookup upward, in case it is not defined.
|
|
176
|
+
typeof node.closest === 'function' ? node.closest('[inert]') : _isInert(node.parentNode));
|
|
177
|
+
return result;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Determines if a node's content is editable.
|
|
182
|
+
* @param {Element} [node]
|
|
183
|
+
* @returns True if it's content-editable; false if it's not or `node` is falsy.
|
|
184
|
+
*/
|
|
185
|
+
var isContentEditable = function isContentEditable(node) {
|
|
186
|
+
var _node$getAttribute2;
|
|
187
|
+
// CAREFUL: JSDom does not support the `HTMLElement.isContentEditable` API so we have
|
|
188
|
+
// to use the attribute directly to check for this, which can either be empty or 'true';
|
|
189
|
+
// if it's `null` (not specified) or 'false', it's a non-editable element
|
|
190
|
+
var attValue = node === null || node === void 0 ? void 0 : (_node$getAttribute2 = node.getAttribute) === null || _node$getAttribute2 === void 0 ? void 0 : _node$getAttribute2.call(node, 'contenteditable');
|
|
191
|
+
return attValue === '' || attValue === 'true';
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @param {Element} el container to check in
|
|
196
|
+
* @param {boolean} includeContainer add container to check
|
|
197
|
+
* @param {(node: Element) => boolean} filter filter candidates
|
|
198
|
+
* @returns {Element[]}
|
|
199
|
+
*/
|
|
200
|
+
var getCandidates = function getCandidates(el, includeContainer, filter) {
|
|
201
|
+
// even if `includeContainer=false`, we still have to check it for inertness because
|
|
202
|
+
// if it's inert (either by itself or via its parent), then all its children are inert
|
|
203
|
+
if (_isInert(el)) {
|
|
204
|
+
return [];
|
|
205
|
+
}
|
|
206
|
+
var candidates = Array.prototype.slice.apply(el.querySelectorAll(candidateSelector));
|
|
207
|
+
if (includeContainer && matches.call(el, candidateSelector)) {
|
|
208
|
+
candidates.unshift(el);
|
|
209
|
+
}
|
|
210
|
+
candidates = candidates.filter(filter);
|
|
211
|
+
return candidates;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* @callback GetShadowRoot
|
|
216
|
+
* @param {Element} element to check for shadow root
|
|
217
|
+
* @returns {ShadowRoot|boolean} ShadowRoot if available or boolean indicating if a shadowRoot is attached but not available.
|
|
218
|
+
*/
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* @callback ShadowRootFilter
|
|
222
|
+
* @param {Element} shadowHostNode the element which contains shadow content
|
|
223
|
+
* @returns {boolean} true if a shadow root could potentially contain valid candidates.
|
|
224
|
+
*/
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* @typedef {Object} CandidateScope
|
|
228
|
+
* @property {Element} scopeParent contains inner candidates
|
|
229
|
+
* @property {Element[]} candidates list of candidates found in the scope parent
|
|
230
|
+
*/
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* @typedef {Object} IterativeOptions
|
|
234
|
+
* @property {GetShadowRoot|boolean} getShadowRoot true if shadow support is enabled; falsy if not;
|
|
235
|
+
* if a function, implies shadow support is enabled and either returns the shadow root of an element
|
|
236
|
+
* or a boolean stating if it has an undisclosed shadow root
|
|
237
|
+
* @property {(node: Element) => boolean} filter filter candidates
|
|
238
|
+
* @property {boolean} flatten if true then result will flatten any CandidateScope into the returned list
|
|
239
|
+
* @property {ShadowRootFilter} shadowRootFilter filter shadow roots;
|
|
240
|
+
*/
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* @param {Element[]} elements list of element containers to match candidates from
|
|
244
|
+
* @param {boolean} includeContainer add container list to check
|
|
245
|
+
* @param {IterativeOptions} options
|
|
246
|
+
* @returns {Array.<Element|CandidateScope>}
|
|
247
|
+
*/
|
|
248
|
+
var _getCandidatesIteratively = function getCandidatesIteratively(elements, includeContainer, options) {
|
|
249
|
+
var candidates = [];
|
|
250
|
+
var elementsToCheck = Array.from(elements);
|
|
251
|
+
while (elementsToCheck.length) {
|
|
252
|
+
var element = elementsToCheck.shift();
|
|
253
|
+
if (_isInert(element, false)) {
|
|
254
|
+
// no need to look up since we're drilling down
|
|
255
|
+
// anything inside this container will also be inert
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
if (element.tagName === 'SLOT') {
|
|
259
|
+
// add shadow dom slot scope (slot itself cannot be focusable)
|
|
260
|
+
var assigned = element.assignedElements();
|
|
261
|
+
var content = assigned.length ? assigned : element.children;
|
|
262
|
+
var nestedCandidates = _getCandidatesIteratively(content, true, options);
|
|
263
|
+
if (options.flatten) {
|
|
264
|
+
candidates.push.apply(candidates, nestedCandidates);
|
|
265
|
+
} else {
|
|
266
|
+
candidates.push({
|
|
267
|
+
scopeParent: element,
|
|
268
|
+
candidates: nestedCandidates
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
} else {
|
|
272
|
+
// check candidate element
|
|
273
|
+
var validCandidate = matches.call(element, candidateSelector);
|
|
274
|
+
if (validCandidate && options.filter(element) && (includeContainer || !elements.includes(element))) {
|
|
275
|
+
candidates.push(element);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// iterate over shadow content if possible
|
|
279
|
+
var shadowRoot = element.shadowRoot ||
|
|
280
|
+
// check for an undisclosed shadow
|
|
281
|
+
typeof options.getShadowRoot === 'function' && options.getShadowRoot(element);
|
|
282
|
+
|
|
283
|
+
// no inert look up because we're already drilling down and checking for inertness
|
|
284
|
+
// on the way down, so all containers to this root node should have already been
|
|
285
|
+
// vetted as non-inert
|
|
286
|
+
var validShadowRoot = !_isInert(shadowRoot, false) && (!options.shadowRootFilter || options.shadowRootFilter(element));
|
|
287
|
+
if (shadowRoot && validShadowRoot) {
|
|
288
|
+
// add shadow dom scope IIF a shadow root node was given; otherwise, an undisclosed
|
|
289
|
+
// shadow exists, so look at light dom children as fallback BUT create a scope for any
|
|
290
|
+
// child candidates found because they're likely slotted elements (elements that are
|
|
291
|
+
// children of the web component element (which has the shadow), in the light dom, but
|
|
292
|
+
// slotted somewhere _inside_ the undisclosed shadow) -- the scope is created below,
|
|
293
|
+
// _after_ we return from this recursive call
|
|
294
|
+
var _nestedCandidates = _getCandidatesIteratively(shadowRoot === true ? element.children : shadowRoot.children, true, options);
|
|
295
|
+
if (options.flatten) {
|
|
296
|
+
candidates.push.apply(candidates, _nestedCandidates);
|
|
297
|
+
} else {
|
|
298
|
+
candidates.push({
|
|
299
|
+
scopeParent: element,
|
|
300
|
+
candidates: _nestedCandidates
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
} else {
|
|
304
|
+
// there's not shadow so just dig into the element's (light dom) children
|
|
305
|
+
// __without__ giving the element special scope treatment
|
|
306
|
+
elementsToCheck.unshift.apply(elementsToCheck, element.children);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return candidates;
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* @private
|
|
315
|
+
* Determines if the node has an explicitly specified `tabindex` attribute.
|
|
316
|
+
* @param {HTMLElement} node
|
|
317
|
+
* @returns {boolean} True if so; false if not.
|
|
318
|
+
*/
|
|
319
|
+
var hasTabIndex = function hasTabIndex(node) {
|
|
320
|
+
return !isNaN(parseInt(node.getAttribute('tabindex'), 10));
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Determine the tab index of a given node.
|
|
325
|
+
* @param {HTMLElement} node
|
|
326
|
+
* @returns {number} Tab order (negative, 0, or positive number).
|
|
327
|
+
* @throws {Error} If `node` is falsy.
|
|
328
|
+
*/
|
|
329
|
+
var getTabIndex = function getTabIndex(node) {
|
|
330
|
+
if (!node) {
|
|
331
|
+
throw new Error('No node provided');
|
|
332
|
+
}
|
|
333
|
+
if (node.tabIndex < 0) {
|
|
334
|
+
// in Chrome, <details/>, <audio controls/> and <video controls/> elements get a default
|
|
335
|
+
// `tabIndex` of -1 when the 'tabindex' attribute isn't specified in the DOM,
|
|
336
|
+
// yet they are still part of the regular tab order; in FF, they get a default
|
|
337
|
+
// `tabIndex` of 0; since Chrome still puts those elements in the regular tab
|
|
338
|
+
// order, consider their tab index to be 0.
|
|
339
|
+
// Also browsers do not return `tabIndex` correctly for contentEditable nodes;
|
|
340
|
+
// so if they don't have a tabindex attribute specifically set, assume it's 0.
|
|
341
|
+
if ((/^(AUDIO|VIDEO|DETAILS)$/.test(node.tagName) || isContentEditable(node)) && !hasTabIndex(node)) {
|
|
342
|
+
return 0;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return node.tabIndex;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Determine the tab index of a given node __for sort order purposes__.
|
|
350
|
+
* @param {HTMLElement} node
|
|
351
|
+
* @param {boolean} [isScope] True for a custom element with shadow root or slot that, by default,
|
|
352
|
+
* has tabIndex -1, but needs to be sorted by document order in order for its content to be
|
|
353
|
+
* inserted into the correct sort position.
|
|
354
|
+
* @returns {number} Tab order (negative, 0, or positive number).
|
|
355
|
+
*/
|
|
356
|
+
var getSortOrderTabIndex = function getSortOrderTabIndex(node, isScope) {
|
|
357
|
+
var tabIndex = getTabIndex(node);
|
|
358
|
+
if (tabIndex < 0 && isScope && !hasTabIndex(node)) {
|
|
359
|
+
return 0;
|
|
360
|
+
}
|
|
361
|
+
return tabIndex;
|
|
362
|
+
};
|
|
363
|
+
var sortOrderedTabbables = function sortOrderedTabbables(a, b) {
|
|
364
|
+
return a.tabIndex === b.tabIndex ? a.documentOrder - b.documentOrder : a.tabIndex - b.tabIndex;
|
|
365
|
+
};
|
|
366
|
+
var isInput = function isInput(node) {
|
|
367
|
+
return node.tagName === 'INPUT';
|
|
368
|
+
};
|
|
369
|
+
var isHiddenInput = function isHiddenInput(node) {
|
|
370
|
+
return isInput(node) && node.type === 'hidden';
|
|
371
|
+
};
|
|
372
|
+
var isDetailsWithSummary = function isDetailsWithSummary(node) {
|
|
373
|
+
var r = node.tagName === 'DETAILS' && Array.prototype.slice.apply(node.children).some(function (child) {
|
|
374
|
+
return child.tagName === 'SUMMARY';
|
|
375
|
+
});
|
|
376
|
+
return r;
|
|
377
|
+
};
|
|
378
|
+
var getCheckedRadio = function getCheckedRadio(nodes, form) {
|
|
379
|
+
for (var i = 0; i < nodes.length; i++) {
|
|
380
|
+
if (nodes[i].checked && nodes[i].form === form) {
|
|
381
|
+
return nodes[i];
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
var isTabbableRadio = function isTabbableRadio(node) {
|
|
386
|
+
if (!node.name) {
|
|
387
|
+
return true;
|
|
388
|
+
}
|
|
389
|
+
var radioScope = node.form || getRootNode(node);
|
|
390
|
+
var queryRadios = function queryRadios(name) {
|
|
391
|
+
return radioScope.querySelectorAll('input[type="radio"][name="' + name + '"]');
|
|
392
|
+
};
|
|
393
|
+
var radioSet;
|
|
394
|
+
if (typeof window !== 'undefined' && typeof window.CSS !== 'undefined' && typeof window.CSS.escape === 'function') {
|
|
395
|
+
radioSet = queryRadios(window.CSS.escape(node.name));
|
|
396
|
+
} else {
|
|
397
|
+
try {
|
|
398
|
+
radioSet = queryRadios(node.name);
|
|
399
|
+
} catch (err) {
|
|
400
|
+
// eslint-disable-next-line no-console
|
|
401
|
+
console.error('Looks like you have a radio button with a name attribute containing invalid CSS selector characters and need the CSS.escape polyfill: %s', err.message);
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
var checked = getCheckedRadio(radioSet, node.form);
|
|
406
|
+
return !checked || checked === node;
|
|
407
|
+
};
|
|
408
|
+
var isRadio = function isRadio(node) {
|
|
409
|
+
return isInput(node) && node.type === 'radio';
|
|
410
|
+
};
|
|
411
|
+
var isNonTabbableRadio = function isNonTabbableRadio(node) {
|
|
412
|
+
return isRadio(node) && !isTabbableRadio(node);
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
// determines if a node is ultimately attached to the window's document
|
|
416
|
+
var isNodeAttached = function isNodeAttached(node) {
|
|
417
|
+
var _nodeRoot;
|
|
418
|
+
// The root node is the shadow root if the node is in a shadow DOM; some document otherwise
|
|
419
|
+
// (but NOT _the_ document; see second 'If' comment below for more).
|
|
420
|
+
// If rootNode is shadow root, it'll have a host, which is the element to which the shadow
|
|
421
|
+
// is attached, and the one we need to check if it's in the document or not (because the
|
|
422
|
+
// shadow, and all nodes it contains, is never considered in the document since shadows
|
|
423
|
+
// behave like self-contained DOMs; but if the shadow's HOST, which is part of the document,
|
|
424
|
+
// is hidden, or is not in the document itself but is detached, it will affect the shadow's
|
|
425
|
+
// visibility, including all the nodes it contains). The host could be any normal node,
|
|
426
|
+
// or a custom element (i.e. web component). Either way, that's the one that is considered
|
|
427
|
+
// part of the document, not the shadow root, nor any of its children (i.e. the node being
|
|
428
|
+
// tested).
|
|
429
|
+
// To further complicate things, we have to look all the way up until we find a shadow HOST
|
|
430
|
+
// that is attached (or find none) because the node might be in nested shadows...
|
|
431
|
+
// If rootNode is not a shadow root, it won't have a host, and so rootNode should be the
|
|
432
|
+
// document (per the docs) and while it's a Document-type object, that document does not
|
|
433
|
+
// appear to be the same as the node's `ownerDocument` for some reason, so it's safer
|
|
434
|
+
// to ignore the rootNode at this point, and use `node.ownerDocument`. Otherwise,
|
|
435
|
+
// using `rootNode.contains(node)` will _always_ be true we'll get false-positives when
|
|
436
|
+
// node is actually detached.
|
|
437
|
+
// NOTE: If `nodeRootHost` or `node` happens to be the `document` itself (which is possible
|
|
438
|
+
// if a tabbable/focusable node was quickly added to the DOM, focused, and then removed
|
|
439
|
+
// from the DOM as in https://github.com/focus-trap/focus-trap-react/issues/905), then
|
|
440
|
+
// `ownerDocument` will be `null`, hence the optional chaining on it.
|
|
441
|
+
var nodeRoot = node && getRootNode(node);
|
|
442
|
+
var nodeRootHost = (_nodeRoot = nodeRoot) === null || _nodeRoot === void 0 ? void 0 : _nodeRoot.host;
|
|
443
|
+
|
|
444
|
+
// in some cases, a detached node will return itself as the root instead of a document or
|
|
445
|
+
// shadow root object, in which case, we shouldn't try to look further up the host chain
|
|
446
|
+
var attached = false;
|
|
447
|
+
if (nodeRoot && nodeRoot !== node) {
|
|
448
|
+
var _nodeRootHost, _nodeRootHost$ownerDo, _node$ownerDocument;
|
|
449
|
+
attached = !!((_nodeRootHost = nodeRootHost) !== null && _nodeRootHost !== void 0 && (_nodeRootHost$ownerDo = _nodeRootHost.ownerDocument) !== null && _nodeRootHost$ownerDo !== void 0 && _nodeRootHost$ownerDo.contains(nodeRootHost) || node !== null && node !== void 0 && (_node$ownerDocument = node.ownerDocument) !== null && _node$ownerDocument !== void 0 && _node$ownerDocument.contains(node));
|
|
450
|
+
while (!attached && nodeRootHost) {
|
|
451
|
+
var _nodeRoot2, _nodeRootHost2, _nodeRootHost2$ownerD;
|
|
452
|
+
// since it's not attached and we have a root host, the node MUST be in a nested shadow DOM,
|
|
453
|
+
// which means we need to get the host's host and check if that parent host is contained
|
|
454
|
+
// in (i.e. attached to) the document
|
|
455
|
+
nodeRoot = getRootNode(nodeRootHost);
|
|
456
|
+
nodeRootHost = (_nodeRoot2 = nodeRoot) === null || _nodeRoot2 === void 0 ? void 0 : _nodeRoot2.host;
|
|
457
|
+
attached = !!((_nodeRootHost2 = nodeRootHost) !== null && _nodeRootHost2 !== void 0 && (_nodeRootHost2$ownerD = _nodeRootHost2.ownerDocument) !== null && _nodeRootHost2$ownerD !== void 0 && _nodeRootHost2$ownerD.contains(nodeRootHost));
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return attached;
|
|
461
|
+
};
|
|
462
|
+
var isZeroArea = function isZeroArea(node) {
|
|
463
|
+
var _node$getBoundingClie = node.getBoundingClientRect(),
|
|
464
|
+
width = _node$getBoundingClie.width,
|
|
465
|
+
height = _node$getBoundingClie.height;
|
|
466
|
+
return width === 0 && height === 0;
|
|
467
|
+
};
|
|
468
|
+
var isHidden = function isHidden(node, _ref) {
|
|
469
|
+
var displayCheck = _ref.displayCheck,
|
|
470
|
+
getShadowRoot = _ref.getShadowRoot;
|
|
471
|
+
if (displayCheck === 'full-native') {
|
|
472
|
+
if ('checkVisibility' in node) {
|
|
473
|
+
// Chrome >= 105, Edge >= 105, Firefox >= 106, Safari >= 17.4
|
|
474
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/API/Element/checkVisibility#browser_compatibility
|
|
475
|
+
var visible = node.checkVisibility({
|
|
476
|
+
// Checking opacity might be desirable for some use cases, but natively,
|
|
477
|
+
// opacity zero elements _are_ focusable and tabbable.
|
|
478
|
+
checkOpacity: false,
|
|
479
|
+
opacityProperty: false,
|
|
480
|
+
contentVisibilityAuto: true,
|
|
481
|
+
visibilityProperty: true,
|
|
482
|
+
// This is an alias for `visibilityProperty`. Contemporary browsers
|
|
483
|
+
// support both. However, this alias has wider browser support (Chrome
|
|
484
|
+
// >= 105 and Firefox >= 106, vs. Chrome >= 121 and Firefox >= 122), so
|
|
485
|
+
// we include it anyway.
|
|
486
|
+
checkVisibilityCSS: true
|
|
487
|
+
});
|
|
488
|
+
return !visible;
|
|
489
|
+
}
|
|
490
|
+
// Fall through to manual visibility checks
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// NOTE: visibility will be `undefined` if node is detached from the document
|
|
494
|
+
// (see notes about this further down), which means we will consider it visible
|
|
495
|
+
// (this is legacy behavior from a very long way back)
|
|
496
|
+
// NOTE: we check this regardless of `displayCheck="none"` because this is a
|
|
497
|
+
// _visibility_ check, not a _display_ check
|
|
498
|
+
if (getComputedStyle(node).visibility === 'hidden') {
|
|
499
|
+
return true;
|
|
500
|
+
}
|
|
501
|
+
var isDirectSummary = matches.call(node, 'details>summary:first-of-type');
|
|
502
|
+
var nodeUnderDetails = isDirectSummary ? node.parentElement : node;
|
|
503
|
+
if (matches.call(nodeUnderDetails, 'details:not([open]) *')) {
|
|
504
|
+
return true;
|
|
505
|
+
}
|
|
506
|
+
if (!displayCheck || displayCheck === 'full' ||
|
|
507
|
+
// full-native can run this branch when it falls through in case
|
|
508
|
+
// Element#checkVisibility is unsupported
|
|
509
|
+
displayCheck === 'full-native' || displayCheck === 'legacy-full') {
|
|
510
|
+
if (typeof getShadowRoot === 'function') {
|
|
511
|
+
// figure out if we should consider the node to be in an undisclosed shadow and use the
|
|
512
|
+
// 'non-zero-area' fallback
|
|
513
|
+
var originalNode = node;
|
|
514
|
+
while (node) {
|
|
515
|
+
var parentElement = node.parentElement;
|
|
516
|
+
var rootNode = getRootNode(node);
|
|
517
|
+
if (parentElement && !parentElement.shadowRoot && getShadowRoot(parentElement) === true // check if there's an undisclosed shadow
|
|
518
|
+
) {
|
|
519
|
+
// node has an undisclosed shadow which means we can only treat it as a black box, so we
|
|
520
|
+
// fall back to a non-zero-area test
|
|
521
|
+
return isZeroArea(node);
|
|
522
|
+
} else if (node.assignedSlot) {
|
|
523
|
+
// iterate up slot
|
|
524
|
+
node = node.assignedSlot;
|
|
525
|
+
} else if (!parentElement && rootNode !== node.ownerDocument) {
|
|
526
|
+
// cross shadow boundary
|
|
527
|
+
node = rootNode.host;
|
|
528
|
+
} else {
|
|
529
|
+
// iterate up normal dom
|
|
530
|
+
node = parentElement;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
node = originalNode;
|
|
534
|
+
}
|
|
535
|
+
// else, `getShadowRoot` might be true, but all that does is enable shadow DOM support
|
|
536
|
+
// (i.e. it does not also presume that all nodes might have undisclosed shadows); or
|
|
537
|
+
// it might be a falsy value, which means shadow DOM support is disabled
|
|
538
|
+
|
|
539
|
+
// Since we didn't find it sitting in an undisclosed shadow (or shadows are disabled)
|
|
540
|
+
// now we can just test to see if it would normally be visible or not, provided it's
|
|
541
|
+
// attached to the main document.
|
|
542
|
+
// NOTE: We must consider case where node is inside a shadow DOM and given directly to
|
|
543
|
+
// `isTabbable()` or `isFocusable()` -- regardless of `getShadowRoot` option setting.
|
|
544
|
+
|
|
545
|
+
if (isNodeAttached(node)) {
|
|
546
|
+
// this works wherever the node is: if there's at least one client rect, it's
|
|
547
|
+
// somehow displayed; it also covers the CSS 'display: contents' case where the
|
|
548
|
+
// node itself is hidden in place of its contents; and there's no need to search
|
|
549
|
+
// up the hierarchy either
|
|
550
|
+
return !node.getClientRects().length;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// Else, the node isn't attached to the document, which means the `getClientRects()`
|
|
554
|
+
// API will __always__ return zero rects (this can happen, for example, if React
|
|
555
|
+
// is used to render nodes onto a detached tree, as confirmed in this thread:
|
|
556
|
+
// https://github.com/facebook/react/issues/9117#issuecomment-284228870)
|
|
557
|
+
//
|
|
558
|
+
// It also means that even window.getComputedStyle(node).display will return `undefined`
|
|
559
|
+
// because styles are only computed for nodes that are in the document.
|
|
560
|
+
//
|
|
561
|
+
// NOTE: THIS HAS BEEN THE CASE FOR YEARS. It is not new, nor is it caused by tabbable
|
|
562
|
+
// somehow. Though it was never stated officially, anyone who has ever used tabbable
|
|
563
|
+
// APIs on nodes in detached containers has actually implicitly used tabbable in what
|
|
564
|
+
// was later (as of v5.2.0 on Apr 9, 2021) called `displayCheck="none"` mode -- essentially
|
|
565
|
+
// considering __everything__ to be visible because of the innability to determine styles.
|
|
566
|
+
//
|
|
567
|
+
// v6.0.0: As of this major release, the default 'full' option __no longer treats detached
|
|
568
|
+
// nodes as visible with the 'none' fallback.__
|
|
569
|
+
if (displayCheck !== 'legacy-full') {
|
|
570
|
+
return true; // hidden
|
|
571
|
+
}
|
|
572
|
+
// else, fallback to 'none' mode and consider the node visible
|
|
573
|
+
} else if (displayCheck === 'non-zero-area') {
|
|
574
|
+
// NOTE: Even though this tests that the node's client rect is non-zero to determine
|
|
575
|
+
// whether it's displayed, and that a detached node will __always__ have a zero-area
|
|
576
|
+
// client rect, we don't special-case for whether the node is attached or not. In
|
|
577
|
+
// this mode, we do want to consider nodes that have a zero area to be hidden at all
|
|
578
|
+
// times, and that includes attached or not.
|
|
579
|
+
return isZeroArea(node);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// visible, as far as we can tell, or per current `displayCheck=none` mode, we assume
|
|
583
|
+
// it's visible
|
|
584
|
+
return false;
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
// form fields (nested) inside a disabled fieldset are not focusable/tabbable
|
|
588
|
+
// unless they are in the _first_ <legend> element of the top-most disabled
|
|
589
|
+
// fieldset
|
|
590
|
+
var isDisabledFromFieldset = function isDisabledFromFieldset(node) {
|
|
591
|
+
if (/^(INPUT|BUTTON|SELECT|TEXTAREA)$/.test(node.tagName)) {
|
|
592
|
+
var parentNode = node.parentElement;
|
|
593
|
+
// check if `node` is contained in a disabled <fieldset>
|
|
594
|
+
while (parentNode) {
|
|
595
|
+
if (parentNode.tagName === 'FIELDSET' && parentNode.disabled) {
|
|
596
|
+
// look for the first <legend> among the children of the disabled <fieldset>
|
|
597
|
+
for (var i = 0; i < parentNode.children.length; i++) {
|
|
598
|
+
var child = parentNode.children.item(i);
|
|
599
|
+
// when the first <legend> (in document order) is found
|
|
600
|
+
if (child.tagName === 'LEGEND') {
|
|
601
|
+
// if its parent <fieldset> is not nested in another disabled <fieldset>,
|
|
602
|
+
// return whether `node` is a descendant of its first <legend>
|
|
603
|
+
return matches.call(parentNode, 'fieldset[disabled] *') ? true : !child.contains(node);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
// the disabled <fieldset> containing `node` has no <legend>
|
|
607
|
+
return true;
|
|
608
|
+
}
|
|
609
|
+
parentNode = parentNode.parentElement;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// else, node's tabbable/focusable state should not be affected by a fieldset's
|
|
614
|
+
// enabled/disabled state
|
|
615
|
+
return false;
|
|
616
|
+
};
|
|
617
|
+
var isNodeMatchingSelectorFocusable = function isNodeMatchingSelectorFocusable(options, node) {
|
|
618
|
+
if (node.disabled || isHiddenInput(node) || isHidden(node, options) ||
|
|
619
|
+
// For a details element with a summary, the summary element gets the focus
|
|
620
|
+
isDetailsWithSummary(node) || isDisabledFromFieldset(node)) {
|
|
621
|
+
return false;
|
|
622
|
+
}
|
|
623
|
+
return true;
|
|
624
|
+
};
|
|
625
|
+
var isNodeMatchingSelectorTabbable = function isNodeMatchingSelectorTabbable(options, node) {
|
|
626
|
+
if (isNonTabbableRadio(node) || getTabIndex(node) < 0 || !isNodeMatchingSelectorFocusable(options, node)) {
|
|
627
|
+
return false;
|
|
628
|
+
}
|
|
629
|
+
return true;
|
|
630
|
+
};
|
|
631
|
+
var isShadowRootTabbable = function isShadowRootTabbable(shadowHostNode) {
|
|
632
|
+
var tabIndex = parseInt(shadowHostNode.getAttribute('tabindex'), 10);
|
|
633
|
+
if (isNaN(tabIndex) || tabIndex >= 0) {
|
|
634
|
+
return true;
|
|
635
|
+
}
|
|
636
|
+
// If a custom element has an explicit negative tabindex,
|
|
637
|
+
// browsers will not allow tab targeting said element's children.
|
|
638
|
+
return false;
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* @param {Array.<Element|CandidateScope>} candidates
|
|
643
|
+
* @returns Element[]
|
|
644
|
+
*/
|
|
645
|
+
var _sortByOrder = function sortByOrder(candidates) {
|
|
646
|
+
var regularTabbables = [];
|
|
647
|
+
var orderedTabbables = [];
|
|
648
|
+
candidates.forEach(function (item, i) {
|
|
649
|
+
var isScope = !!item.scopeParent;
|
|
650
|
+
var element = isScope ? item.scopeParent : item;
|
|
651
|
+
var candidateTabindex = getSortOrderTabIndex(element, isScope);
|
|
652
|
+
var elements = isScope ? _sortByOrder(item.candidates) : element;
|
|
653
|
+
if (candidateTabindex === 0) {
|
|
654
|
+
isScope ? regularTabbables.push.apply(regularTabbables, elements) : regularTabbables.push(element);
|
|
655
|
+
} else {
|
|
656
|
+
orderedTabbables.push({
|
|
657
|
+
documentOrder: i,
|
|
658
|
+
tabIndex: candidateTabindex,
|
|
659
|
+
item: item,
|
|
660
|
+
isScope: isScope,
|
|
661
|
+
content: elements
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
return orderedTabbables.sort(sortOrderedTabbables).reduce(function (acc, sortable) {
|
|
666
|
+
sortable.isScope ? acc.push.apply(acc, sortable.content) : acc.push(sortable.content);
|
|
667
|
+
return acc;
|
|
668
|
+
}, []).concat(regularTabbables);
|
|
669
|
+
};
|
|
670
|
+
var tabbable = function tabbable(container, options) {
|
|
671
|
+
options = options || {};
|
|
672
|
+
var candidates;
|
|
673
|
+
if (options.getShadowRoot) {
|
|
674
|
+
candidates = _getCandidatesIteratively([container], options.includeContainer, {
|
|
675
|
+
filter: isNodeMatchingSelectorTabbable.bind(null, options),
|
|
676
|
+
flatten: false,
|
|
677
|
+
getShadowRoot: options.getShadowRoot,
|
|
678
|
+
shadowRootFilter: isShadowRootTabbable
|
|
679
|
+
});
|
|
680
|
+
} else {
|
|
681
|
+
candidates = getCandidates(container, options.includeContainer, isNodeMatchingSelectorTabbable.bind(null, options));
|
|
682
|
+
}
|
|
683
|
+
return _sortByOrder(candidates);
|
|
684
|
+
};
|
|
685
|
+
var isTabbable = function isTabbable(node, options) {
|
|
686
|
+
options = options || {};
|
|
687
|
+
if (!node) {
|
|
688
|
+
throw new Error('No node provided');
|
|
689
|
+
}
|
|
690
|
+
if (matches.call(node, candidateSelector) === false) {
|
|
691
|
+
return false;
|
|
692
|
+
}
|
|
693
|
+
return isNodeMatchingSelectorTabbable(options, node);
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* Merges an array of refs into a single memoized callback ref or `null`.
|
|
698
|
+
* @see https://floating-ui.com/docs/react-utils#usemergerefs
|
|
699
|
+
*/
|
|
700
|
+
function useMergeRefs(refs) {
|
|
701
|
+
return React.useMemo(() => {
|
|
702
|
+
if (refs.every(ref => ref == null)) {
|
|
703
|
+
return null;
|
|
704
|
+
}
|
|
705
|
+
return value => {
|
|
706
|
+
refs.forEach(ref => {
|
|
707
|
+
if (typeof ref === 'function') {
|
|
708
|
+
ref(value);
|
|
709
|
+
} else if (ref != null) {
|
|
710
|
+
ref.current = value;
|
|
711
|
+
}
|
|
712
|
+
});
|
|
713
|
+
};
|
|
714
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
715
|
+
}, refs);
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
// https://github.com/mui/material-ui/issues/41190#issuecomment-2040873379
|
|
719
|
+
const SafeReact = {
|
|
720
|
+
...React
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
const useInsertionEffect = SafeReact.useInsertionEffect;
|
|
724
|
+
const useSafeInsertionEffect = useInsertionEffect || (fn => fn());
|
|
725
|
+
function useEffectEvent(callback) {
|
|
726
|
+
const ref = React.useRef(() => {
|
|
727
|
+
{
|
|
728
|
+
throw new Error('Cannot call an event handler while rendering.');
|
|
729
|
+
}
|
|
730
|
+
});
|
|
731
|
+
useSafeInsertionEffect(() => {
|
|
732
|
+
ref.current = callback;
|
|
733
|
+
});
|
|
734
|
+
return React.useCallback(function () {
|
|
735
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
736
|
+
args[_key] = arguments[_key];
|
|
737
|
+
}
|
|
738
|
+
return ref.current == null ? void 0 : ref.current(...args);
|
|
739
|
+
}, []);
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
var index = typeof document !== 'undefined' ? useLayoutEffect : useEffect;
|
|
743
|
+
|
|
744
|
+
function _extends() {
|
|
745
|
+
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
746
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
747
|
+
var source = arguments[i];
|
|
748
|
+
for (var key in source) {
|
|
749
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
750
|
+
target[key] = source[key];
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
return target;
|
|
755
|
+
};
|
|
756
|
+
return _extends.apply(this, arguments);
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
let serverHandoffComplete = false;
|
|
760
|
+
let count = 0;
|
|
761
|
+
const genId = () => // Ensure the id is unique with multiple independent versions of Floating UI
|
|
762
|
+
// on <React 18
|
|
763
|
+
"floating-ui-" + Math.random().toString(36).slice(2, 6) + count++;
|
|
764
|
+
function useFloatingId() {
|
|
765
|
+
const [id, setId] = React.useState(() => serverHandoffComplete ? genId() : undefined);
|
|
766
|
+
index(() => {
|
|
767
|
+
if (id == null) {
|
|
768
|
+
setId(genId());
|
|
769
|
+
}
|
|
770
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
771
|
+
}, []);
|
|
772
|
+
React.useEffect(() => {
|
|
773
|
+
serverHandoffComplete = true;
|
|
774
|
+
}, []);
|
|
775
|
+
return id;
|
|
776
|
+
}
|
|
777
|
+
const useReactId = SafeReact.useId;
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Uses React 18's built-in `useId()` when available, or falls back to a
|
|
781
|
+
* slightly less performant (requiring a double render) implementation for
|
|
782
|
+
* earlier React versions.
|
|
783
|
+
* @see https://floating-ui.com/docs/react-utils#useid
|
|
784
|
+
*/
|
|
785
|
+
const useId = useReactId || useFloatingId;
|
|
786
|
+
|
|
787
|
+
let devMessageSet;
|
|
788
|
+
{
|
|
789
|
+
devMessageSet = /*#__PURE__*/new Set();
|
|
790
|
+
}
|
|
791
|
+
function error() {
|
|
792
|
+
var _devMessageSet3;
|
|
793
|
+
for (var _len2 = arguments.length, messages = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
794
|
+
messages[_key2] = arguments[_key2];
|
|
795
|
+
}
|
|
796
|
+
const message = "Floating UI: " + messages.join(' ');
|
|
797
|
+
if (!((_devMessageSet3 = devMessageSet) != null && _devMessageSet3.has(message))) {
|
|
798
|
+
var _devMessageSet4;
|
|
799
|
+
(_devMessageSet4 = devMessageSet) == null || _devMessageSet4.add(message);
|
|
800
|
+
console.error(message);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
function createPubSub() {
|
|
805
|
+
const map = new Map();
|
|
806
|
+
return {
|
|
807
|
+
emit(event, data) {
|
|
808
|
+
var _map$get;
|
|
809
|
+
(_map$get = map.get(event)) == null || _map$get.forEach(handler => handler(data));
|
|
810
|
+
},
|
|
811
|
+
on(event, listener) {
|
|
812
|
+
map.set(event, [...(map.get(event) || []), listener]);
|
|
813
|
+
},
|
|
814
|
+
off(event, listener) {
|
|
815
|
+
var _map$get2;
|
|
816
|
+
map.set(event, ((_map$get2 = map.get(event)) == null ? void 0 : _map$get2.filter(l => l !== listener)) || []);
|
|
817
|
+
}
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
const FloatingNodeContext = /*#__PURE__*/React.createContext(null);
|
|
822
|
+
const FloatingTreeContext = /*#__PURE__*/React.createContext(null);
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* Returns the parent node id for nested floating elements, if available.
|
|
826
|
+
* Returns `null` for top-level floating elements.
|
|
827
|
+
*/
|
|
828
|
+
const useFloatingParentNodeId = () => {
|
|
829
|
+
var _React$useContext;
|
|
830
|
+
return ((_React$useContext = React.useContext(FloatingNodeContext)) == null ? void 0 : _React$useContext.id) || null;
|
|
831
|
+
};
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* Returns the nearest floating tree context, if available.
|
|
835
|
+
*/
|
|
836
|
+
const useFloatingTree = () => React.useContext(FloatingTreeContext);
|
|
837
|
+
|
|
838
|
+
function createAttribute(name) {
|
|
839
|
+
return "data-floating-ui-" + name;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
function useLatestRef(value) {
|
|
843
|
+
const ref = useRef(value);
|
|
844
|
+
index(() => {
|
|
845
|
+
ref.current = value;
|
|
846
|
+
});
|
|
847
|
+
return ref;
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
let rafId = 0;
|
|
851
|
+
function enqueueFocus(el, options) {
|
|
852
|
+
if (options === void 0) {
|
|
853
|
+
options = {};
|
|
854
|
+
}
|
|
855
|
+
const {
|
|
856
|
+
preventScroll = false,
|
|
857
|
+
cancelPrevious = true,
|
|
858
|
+
sync = false
|
|
859
|
+
} = options;
|
|
860
|
+
cancelPrevious && cancelAnimationFrame(rafId);
|
|
861
|
+
const exec = () => el == null ? void 0 : el.focus({
|
|
862
|
+
preventScroll
|
|
863
|
+
});
|
|
864
|
+
if (sync) {
|
|
865
|
+
exec();
|
|
866
|
+
} else {
|
|
867
|
+
rafId = requestAnimationFrame(exec);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
function getAncestors(nodes, id) {
|
|
872
|
+
var _nodes$find;
|
|
873
|
+
let allAncestors = [];
|
|
874
|
+
let currentParentId = (_nodes$find = nodes.find(node => node.id === id)) == null ? void 0 : _nodes$find.parentId;
|
|
875
|
+
while (currentParentId) {
|
|
876
|
+
const currentNode = nodes.find(node => node.id === currentParentId);
|
|
877
|
+
currentParentId = currentNode == null ? void 0 : currentNode.parentId;
|
|
878
|
+
if (currentNode) {
|
|
879
|
+
allAncestors = allAncestors.concat(currentNode);
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
return allAncestors;
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
function getChildren(nodes, id) {
|
|
886
|
+
let allChildren = nodes.filter(node => {
|
|
887
|
+
var _node$context;
|
|
888
|
+
return node.parentId === id && ((_node$context = node.context) == null ? void 0 : _node$context.open);
|
|
889
|
+
});
|
|
890
|
+
let currentChildren = allChildren;
|
|
891
|
+
while (currentChildren.length) {
|
|
892
|
+
currentChildren = nodes.filter(node => {
|
|
893
|
+
var _currentChildren;
|
|
894
|
+
return (_currentChildren = currentChildren) == null ? void 0 : _currentChildren.some(n => {
|
|
895
|
+
var _node$context2;
|
|
896
|
+
return node.parentId === n.id && ((_node$context2 = node.context) == null ? void 0 : _node$context2.open);
|
|
897
|
+
});
|
|
898
|
+
});
|
|
899
|
+
allChildren = allChildren.concat(currentChildren);
|
|
900
|
+
}
|
|
901
|
+
return allChildren;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
// Modified to add conditional `aria-hidden` support:
|
|
905
|
+
// https://github.com/theKashey/aria-hidden/blob/9220c8f4a4fd35f63bee5510a9f41a37264382d4/src/index.ts
|
|
906
|
+
let counterMap = /*#__PURE__*/new WeakMap();
|
|
907
|
+
let uncontrolledElementsSet = /*#__PURE__*/new WeakSet();
|
|
908
|
+
let markerMap = {};
|
|
909
|
+
let lockCount$1 = 0;
|
|
910
|
+
const supportsInert = () => typeof HTMLElement !== 'undefined' && 'inert' in HTMLElement.prototype;
|
|
911
|
+
const unwrapHost = node => node && (node.host || unwrapHost(node.parentNode));
|
|
912
|
+
const correctElements = (parent, targets) => targets.map(target => {
|
|
913
|
+
if (parent.contains(target)) {
|
|
914
|
+
return target;
|
|
915
|
+
}
|
|
916
|
+
const correctedTarget = unwrapHost(target);
|
|
917
|
+
if (parent.contains(correctedTarget)) {
|
|
918
|
+
return correctedTarget;
|
|
919
|
+
}
|
|
920
|
+
return null;
|
|
921
|
+
}).filter(x => x != null);
|
|
922
|
+
function applyAttributeToOthers(uncorrectedAvoidElements, body, ariaHidden, inert) {
|
|
923
|
+
const markerName = 'data-floating-ui-inert';
|
|
924
|
+
const controlAttribute = inert ? 'inert' : ariaHidden ? 'aria-hidden' : null;
|
|
925
|
+
const avoidElements = correctElements(body, uncorrectedAvoidElements);
|
|
926
|
+
const elementsToKeep = new Set();
|
|
927
|
+
const elementsToStop = new Set(avoidElements);
|
|
928
|
+
const hiddenElements = [];
|
|
929
|
+
if (!markerMap[markerName]) {
|
|
930
|
+
markerMap[markerName] = new WeakMap();
|
|
931
|
+
}
|
|
932
|
+
const markerCounter = markerMap[markerName];
|
|
933
|
+
avoidElements.forEach(keep);
|
|
934
|
+
deep(body);
|
|
935
|
+
elementsToKeep.clear();
|
|
936
|
+
function keep(el) {
|
|
937
|
+
if (!el || elementsToKeep.has(el)) {
|
|
938
|
+
return;
|
|
939
|
+
}
|
|
940
|
+
elementsToKeep.add(el);
|
|
941
|
+
el.parentNode && keep(el.parentNode);
|
|
942
|
+
}
|
|
943
|
+
function deep(parent) {
|
|
944
|
+
if (!parent || elementsToStop.has(parent)) {
|
|
945
|
+
return;
|
|
946
|
+
}
|
|
947
|
+
[].forEach.call(parent.children, node => {
|
|
948
|
+
if (getNodeName(node) === 'script') return;
|
|
949
|
+
if (elementsToKeep.has(node)) {
|
|
950
|
+
deep(node);
|
|
951
|
+
} else {
|
|
952
|
+
const attr = controlAttribute ? node.getAttribute(controlAttribute) : null;
|
|
953
|
+
const alreadyHidden = attr !== null && attr !== 'false';
|
|
954
|
+
const counterValue = (counterMap.get(node) || 0) + 1;
|
|
955
|
+
const markerValue = (markerCounter.get(node) || 0) + 1;
|
|
956
|
+
counterMap.set(node, counterValue);
|
|
957
|
+
markerCounter.set(node, markerValue);
|
|
958
|
+
hiddenElements.push(node);
|
|
959
|
+
if (counterValue === 1 && alreadyHidden) {
|
|
960
|
+
uncontrolledElementsSet.add(node);
|
|
961
|
+
}
|
|
962
|
+
if (markerValue === 1) {
|
|
963
|
+
node.setAttribute(markerName, '');
|
|
964
|
+
}
|
|
965
|
+
if (!alreadyHidden && controlAttribute) {
|
|
966
|
+
node.setAttribute(controlAttribute, 'true');
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
});
|
|
970
|
+
}
|
|
971
|
+
lockCount$1++;
|
|
972
|
+
return () => {
|
|
973
|
+
hiddenElements.forEach(element => {
|
|
974
|
+
const counterValue = (counterMap.get(element) || 0) - 1;
|
|
975
|
+
const markerValue = (markerCounter.get(element) || 0) - 1;
|
|
976
|
+
counterMap.set(element, counterValue);
|
|
977
|
+
markerCounter.set(element, markerValue);
|
|
978
|
+
if (!counterValue) {
|
|
979
|
+
if (!uncontrolledElementsSet.has(element) && controlAttribute) {
|
|
980
|
+
element.removeAttribute(controlAttribute);
|
|
981
|
+
}
|
|
982
|
+
uncontrolledElementsSet.delete(element);
|
|
983
|
+
}
|
|
984
|
+
if (!markerValue) {
|
|
985
|
+
element.removeAttribute(markerName);
|
|
986
|
+
}
|
|
987
|
+
});
|
|
988
|
+
lockCount$1--;
|
|
989
|
+
if (!lockCount$1) {
|
|
990
|
+
counterMap = new WeakMap();
|
|
991
|
+
counterMap = new WeakMap();
|
|
992
|
+
uncontrolledElementsSet = new WeakSet();
|
|
993
|
+
markerMap = {};
|
|
994
|
+
}
|
|
995
|
+
};
|
|
996
|
+
}
|
|
997
|
+
function markOthers(avoidElements, ariaHidden, inert) {
|
|
998
|
+
if (ariaHidden === void 0) {
|
|
999
|
+
ariaHidden = false;
|
|
1000
|
+
}
|
|
1001
|
+
if (inert === void 0) {
|
|
1002
|
+
inert = false;
|
|
1003
|
+
}
|
|
1004
|
+
const body = getDocument(avoidElements[0]).body;
|
|
1005
|
+
return applyAttributeToOthers(avoidElements.concat(Array.from(body.querySelectorAll('[aria-live]'))), body, ariaHidden, inert);
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
const getTabbableOptions = () => ({
|
|
1009
|
+
getShadowRoot: true,
|
|
1010
|
+
displayCheck:
|
|
1011
|
+
// JSDOM does not support the `tabbable` library. To solve this we can
|
|
1012
|
+
// check if `ResizeObserver` is a real function (not polyfilled), which
|
|
1013
|
+
// determines if the current environment is JSDOM-like.
|
|
1014
|
+
typeof ResizeObserver === 'function' && ResizeObserver.toString().includes('[native code]') ? 'full' : 'none'
|
|
1015
|
+
});
|
|
1016
|
+
function getTabbableIn(container, direction) {
|
|
1017
|
+
const allTabbable = tabbable(container, getTabbableOptions());
|
|
1018
|
+
if (direction === 'prev') {
|
|
1019
|
+
allTabbable.reverse();
|
|
1020
|
+
}
|
|
1021
|
+
const activeIndex = allTabbable.indexOf(activeElement(getDocument(container)));
|
|
1022
|
+
const nextTabbableElements = allTabbable.slice(activeIndex + 1);
|
|
1023
|
+
return nextTabbableElements[0];
|
|
1024
|
+
}
|
|
1025
|
+
function getNextTabbable() {
|
|
1026
|
+
return getTabbableIn(document.body, 'next');
|
|
1027
|
+
}
|
|
1028
|
+
function getPreviousTabbable() {
|
|
1029
|
+
return getTabbableIn(document.body, 'prev');
|
|
1030
|
+
}
|
|
1031
|
+
function isOutsideEvent(event, container) {
|
|
1032
|
+
const containerElement = container || event.currentTarget;
|
|
1033
|
+
const relatedTarget = event.relatedTarget;
|
|
1034
|
+
return !relatedTarget || !contains(containerElement, relatedTarget);
|
|
1035
|
+
}
|
|
1036
|
+
function disableFocusInside(container) {
|
|
1037
|
+
const tabbableElements = tabbable(container, getTabbableOptions());
|
|
1038
|
+
tabbableElements.forEach(element => {
|
|
1039
|
+
element.dataset.tabindex = element.getAttribute('tabindex') || '';
|
|
1040
|
+
element.setAttribute('tabindex', '-1');
|
|
1041
|
+
});
|
|
1042
|
+
}
|
|
1043
|
+
function enableFocusInside(container) {
|
|
1044
|
+
const elements = container.querySelectorAll('[data-tabindex]');
|
|
1045
|
+
elements.forEach(element => {
|
|
1046
|
+
const tabindex = element.dataset.tabindex;
|
|
1047
|
+
delete element.dataset.tabindex;
|
|
1048
|
+
if (tabindex) {
|
|
1049
|
+
element.setAttribute('tabindex', tabindex);
|
|
1050
|
+
} else {
|
|
1051
|
+
element.removeAttribute('tabindex');
|
|
1052
|
+
}
|
|
1053
|
+
});
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
// See Diego Haz's Sandbox for making this logic work well on Safari/iOS:
|
|
1057
|
+
// https://codesandbox.io/s/tabbable-portal-f4tng?file=/src/FocusTrap.tsx
|
|
1058
|
+
|
|
1059
|
+
const HIDDEN_STYLES = {
|
|
1060
|
+
border: 0,
|
|
1061
|
+
clip: 'rect(0 0 0 0)',
|
|
1062
|
+
height: '1px',
|
|
1063
|
+
margin: '-1px',
|
|
1064
|
+
overflow: 'hidden',
|
|
1065
|
+
padding: 0,
|
|
1066
|
+
position: 'fixed',
|
|
1067
|
+
whiteSpace: 'nowrap',
|
|
1068
|
+
width: '1px',
|
|
1069
|
+
top: 0,
|
|
1070
|
+
left: 0
|
|
1071
|
+
};
|
|
1072
|
+
let timeoutId;
|
|
1073
|
+
function setActiveElementOnTab(event) {
|
|
1074
|
+
if (event.key === 'Tab') {
|
|
1075
|
+
event.target;
|
|
1076
|
+
clearTimeout(timeoutId);
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
const FocusGuard = /*#__PURE__*/React.forwardRef(function FocusGuard(props, ref) {
|
|
1080
|
+
const [role, setRole] = React.useState();
|
|
1081
|
+
index(() => {
|
|
1082
|
+
if (isSafari()) {
|
|
1083
|
+
// Unlike other screen readers such as NVDA and JAWS, the virtual cursor
|
|
1084
|
+
// on VoiceOver does trigger the onFocus event, so we can use the focus
|
|
1085
|
+
// trap element. On Safari, only buttons trigger the onFocus event.
|
|
1086
|
+
// NB: "group" role in the Sandbox no longer appears to work, must be a
|
|
1087
|
+
// button role.
|
|
1088
|
+
setRole('button');
|
|
1089
|
+
}
|
|
1090
|
+
document.addEventListener('keydown', setActiveElementOnTab);
|
|
1091
|
+
return () => {
|
|
1092
|
+
document.removeEventListener('keydown', setActiveElementOnTab);
|
|
1093
|
+
};
|
|
1094
|
+
}, []);
|
|
1095
|
+
const restProps = {
|
|
1096
|
+
ref,
|
|
1097
|
+
tabIndex: 0,
|
|
1098
|
+
// Role is only for VoiceOver
|
|
1099
|
+
role,
|
|
1100
|
+
'aria-hidden': role ? undefined : true,
|
|
1101
|
+
[createAttribute('focus-guard')]: '',
|
|
1102
|
+
style: HIDDEN_STYLES
|
|
1103
|
+
};
|
|
1104
|
+
return /*#__PURE__*/React.createElement("span", _extends({}, props, restProps));
|
|
1105
|
+
});
|
|
1106
|
+
|
|
1107
|
+
const PortalContext = /*#__PURE__*/React.createContext(null);
|
|
1108
|
+
const attr = /*#__PURE__*/createAttribute('portal');
|
|
1109
|
+
/**
|
|
1110
|
+
* @see https://floating-ui.com/docs/FloatingPortal#usefloatingportalnode
|
|
1111
|
+
*/
|
|
1112
|
+
function useFloatingPortalNode(props) {
|
|
1113
|
+
if (props === void 0) {
|
|
1114
|
+
props = {};
|
|
1115
|
+
}
|
|
1116
|
+
const {
|
|
1117
|
+
id,
|
|
1118
|
+
root
|
|
1119
|
+
} = props;
|
|
1120
|
+
const uniqueId = useId();
|
|
1121
|
+
const portalContext = usePortalContext();
|
|
1122
|
+
const [portalNode, setPortalNode] = React.useState(null);
|
|
1123
|
+
const portalNodeRef = React.useRef(null);
|
|
1124
|
+
index(() => {
|
|
1125
|
+
return () => {
|
|
1126
|
+
portalNode == null || portalNode.remove();
|
|
1127
|
+
// Allow the subsequent layout effects to create a new node on updates.
|
|
1128
|
+
// The portal node will still be cleaned up on unmount.
|
|
1129
|
+
// https://github.com/floating-ui/floating-ui/issues/2454
|
|
1130
|
+
queueMicrotask(() => {
|
|
1131
|
+
portalNodeRef.current = null;
|
|
1132
|
+
});
|
|
1133
|
+
};
|
|
1134
|
+
}, [portalNode]);
|
|
1135
|
+
index(() => {
|
|
1136
|
+
// Wait for the uniqueId to be generated before creating the portal node in
|
|
1137
|
+
// React <18 (using `useFloatingId` instead of the native `useId`).
|
|
1138
|
+
// https://github.com/floating-ui/floating-ui/issues/2778
|
|
1139
|
+
if (!uniqueId) return;
|
|
1140
|
+
if (portalNodeRef.current) return;
|
|
1141
|
+
const existingIdRoot = id ? document.getElementById(id) : null;
|
|
1142
|
+
if (!existingIdRoot) return;
|
|
1143
|
+
const subRoot = document.createElement('div');
|
|
1144
|
+
subRoot.id = uniqueId;
|
|
1145
|
+
subRoot.setAttribute(attr, '');
|
|
1146
|
+
existingIdRoot.appendChild(subRoot);
|
|
1147
|
+
portalNodeRef.current = subRoot;
|
|
1148
|
+
setPortalNode(subRoot);
|
|
1149
|
+
}, [id, uniqueId]);
|
|
1150
|
+
index(() => {
|
|
1151
|
+
// Wait for the root to exist before creating the portal node. The root must
|
|
1152
|
+
// be stored in state, not a ref, for this to work reactively.
|
|
1153
|
+
if (root === null) return;
|
|
1154
|
+
if (!uniqueId) return;
|
|
1155
|
+
if (portalNodeRef.current) return;
|
|
1156
|
+
let container = root || (portalContext == null ? void 0 : portalContext.portalNode);
|
|
1157
|
+
if (container && !isElement(container)) container = container.current;
|
|
1158
|
+
container = container || document.body;
|
|
1159
|
+
let idWrapper = null;
|
|
1160
|
+
if (id) {
|
|
1161
|
+
idWrapper = document.createElement('div');
|
|
1162
|
+
idWrapper.id = id;
|
|
1163
|
+
container.appendChild(idWrapper);
|
|
1164
|
+
}
|
|
1165
|
+
const subRoot = document.createElement('div');
|
|
1166
|
+
subRoot.id = uniqueId;
|
|
1167
|
+
subRoot.setAttribute(attr, '');
|
|
1168
|
+
container = idWrapper || container;
|
|
1169
|
+
container.appendChild(subRoot);
|
|
1170
|
+
portalNodeRef.current = subRoot;
|
|
1171
|
+
setPortalNode(subRoot);
|
|
1172
|
+
}, [id, root, uniqueId, portalContext]);
|
|
1173
|
+
return portalNode;
|
|
1174
|
+
}
|
|
1175
|
+
/**
|
|
1176
|
+
* Portals the floating element into a given container element — by default,
|
|
1177
|
+
* outside of the app root and into the body.
|
|
1178
|
+
* This is necessary to ensure the floating element can appear outside any
|
|
1179
|
+
* potential parent containers that cause clipping (such as `overflow: hidden`),
|
|
1180
|
+
* while retaining its location in the React tree.
|
|
1181
|
+
* @see https://floating-ui.com/docs/FloatingPortal
|
|
1182
|
+
*/
|
|
1183
|
+
function FloatingPortal(props) {
|
|
1184
|
+
const {
|
|
1185
|
+
children,
|
|
1186
|
+
id,
|
|
1187
|
+
root,
|
|
1188
|
+
preserveTabOrder = true
|
|
1189
|
+
} = props;
|
|
1190
|
+
const portalNode = useFloatingPortalNode({
|
|
1191
|
+
id,
|
|
1192
|
+
root
|
|
1193
|
+
});
|
|
1194
|
+
const [focusManagerState, setFocusManagerState] = React.useState(null);
|
|
1195
|
+
const beforeOutsideRef = React.useRef(null);
|
|
1196
|
+
const afterOutsideRef = React.useRef(null);
|
|
1197
|
+
const beforeInsideRef = React.useRef(null);
|
|
1198
|
+
const afterInsideRef = React.useRef(null);
|
|
1199
|
+
const modal = focusManagerState == null ? void 0 : focusManagerState.modal;
|
|
1200
|
+
const open = focusManagerState == null ? void 0 : focusManagerState.open;
|
|
1201
|
+
const shouldRenderGuards =
|
|
1202
|
+
// The FocusManager and therefore floating element are currently open/
|
|
1203
|
+
// rendered.
|
|
1204
|
+
!!focusManagerState &&
|
|
1205
|
+
// Guards are only for non-modal focus management.
|
|
1206
|
+
!focusManagerState.modal &&
|
|
1207
|
+
// Don't render if unmount is transitioning.
|
|
1208
|
+
focusManagerState.open && preserveTabOrder && !!(root || portalNode);
|
|
1209
|
+
|
|
1210
|
+
// https://codesandbox.io/s/tabbable-portal-f4tng?file=/src/TabbablePortal.tsx
|
|
1211
|
+
React.useEffect(() => {
|
|
1212
|
+
if (!portalNode || !preserveTabOrder || modal) {
|
|
1213
|
+
return;
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1216
|
+
// Make sure elements inside the portal element are tabbable only when the
|
|
1217
|
+
// portal has already been focused, either by tabbing into a focus trap
|
|
1218
|
+
// element outside or using the mouse.
|
|
1219
|
+
function onFocus(event) {
|
|
1220
|
+
if (portalNode && isOutsideEvent(event)) {
|
|
1221
|
+
const focusing = event.type === 'focusin';
|
|
1222
|
+
const manageFocus = focusing ? enableFocusInside : disableFocusInside;
|
|
1223
|
+
manageFocus(portalNode);
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
// Listen to the event on the capture phase so they run before the focus
|
|
1227
|
+
// trap elements onFocus prop is called.
|
|
1228
|
+
portalNode.addEventListener('focusin', onFocus, true);
|
|
1229
|
+
portalNode.addEventListener('focusout', onFocus, true);
|
|
1230
|
+
return () => {
|
|
1231
|
+
portalNode.removeEventListener('focusin', onFocus, true);
|
|
1232
|
+
portalNode.removeEventListener('focusout', onFocus, true);
|
|
1233
|
+
};
|
|
1234
|
+
}, [portalNode, preserveTabOrder, modal]);
|
|
1235
|
+
React.useEffect(() => {
|
|
1236
|
+
if (!portalNode) return;
|
|
1237
|
+
if (open) return;
|
|
1238
|
+
enableFocusInside(portalNode);
|
|
1239
|
+
}, [open, portalNode]);
|
|
1240
|
+
return /*#__PURE__*/React.createElement(PortalContext.Provider, {
|
|
1241
|
+
value: React.useMemo(() => ({
|
|
1242
|
+
preserveTabOrder,
|
|
1243
|
+
beforeOutsideRef,
|
|
1244
|
+
afterOutsideRef,
|
|
1245
|
+
beforeInsideRef,
|
|
1246
|
+
afterInsideRef,
|
|
1247
|
+
portalNode,
|
|
1248
|
+
setFocusManagerState
|
|
1249
|
+
}), [preserveTabOrder, portalNode])
|
|
1250
|
+
}, shouldRenderGuards && portalNode && /*#__PURE__*/React.createElement(FocusGuard, {
|
|
1251
|
+
"data-type": "outside",
|
|
1252
|
+
ref: beforeOutsideRef,
|
|
1253
|
+
onFocus: event => {
|
|
1254
|
+
if (isOutsideEvent(event, portalNode)) {
|
|
1255
|
+
var _beforeInsideRef$curr;
|
|
1256
|
+
(_beforeInsideRef$curr = beforeInsideRef.current) == null || _beforeInsideRef$curr.focus();
|
|
1257
|
+
} else {
|
|
1258
|
+
const prevTabbable = getPreviousTabbable() || (focusManagerState == null ? void 0 : focusManagerState.refs.domReference.current);
|
|
1259
|
+
prevTabbable == null || prevTabbable.focus();
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
}), shouldRenderGuards && portalNode && /*#__PURE__*/React.createElement("span", {
|
|
1263
|
+
"aria-owns": portalNode.id,
|
|
1264
|
+
style: HIDDEN_STYLES
|
|
1265
|
+
}), portalNode && /*#__PURE__*/ReactDOM.createPortal(children, portalNode), shouldRenderGuards && portalNode && /*#__PURE__*/React.createElement(FocusGuard, {
|
|
1266
|
+
"data-type": "outside",
|
|
1267
|
+
ref: afterOutsideRef,
|
|
1268
|
+
onFocus: event => {
|
|
1269
|
+
if (isOutsideEvent(event, portalNode)) {
|
|
1270
|
+
var _afterInsideRef$curre;
|
|
1271
|
+
(_afterInsideRef$curre = afterInsideRef.current) == null || _afterInsideRef$curre.focus();
|
|
1272
|
+
} else {
|
|
1273
|
+
const nextTabbable = getNextTabbable() || (focusManagerState == null ? void 0 : focusManagerState.refs.domReference.current);
|
|
1274
|
+
nextTabbable == null || nextTabbable.focus();
|
|
1275
|
+
(focusManagerState == null ? void 0 : focusManagerState.closeOnFocusOut) && (focusManagerState == null ? void 0 : focusManagerState.onOpenChange(false, event.nativeEvent, 'focus-out'));
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
}));
|
|
1279
|
+
}
|
|
1280
|
+
const usePortalContext = () => React.useContext(PortalContext);
|
|
1281
|
+
|
|
1282
|
+
const FOCUSABLE_ATTRIBUTE = 'data-floating-ui-focusable';
|
|
1283
|
+
function getFloatingFocusElement(floatingElement) {
|
|
1284
|
+
if (!floatingElement) {
|
|
1285
|
+
return null;
|
|
1286
|
+
}
|
|
1287
|
+
// Try to find the element that has `{...getFloatingProps()}` spread on it.
|
|
1288
|
+
// This indicates the floating element is acting as a positioning wrapper, and
|
|
1289
|
+
// so focus should be managed on the child element with the event handlers and
|
|
1290
|
+
// aria props.
|
|
1291
|
+
return floatingElement.hasAttribute(FOCUSABLE_ATTRIBUTE) ? floatingElement : floatingElement.querySelector("[" + FOCUSABLE_ATTRIBUTE + "]") || floatingElement;
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
const LIST_LIMIT = 20;
|
|
1295
|
+
let previouslyFocusedElements = [];
|
|
1296
|
+
function addPreviouslyFocusedElement(element) {
|
|
1297
|
+
previouslyFocusedElements = previouslyFocusedElements.filter(el => el.isConnected);
|
|
1298
|
+
let tabbableEl = element;
|
|
1299
|
+
if (!tabbableEl || getNodeName(tabbableEl) === 'body') return;
|
|
1300
|
+
if (!isTabbable(tabbableEl, getTabbableOptions())) {
|
|
1301
|
+
const tabbableChild = tabbable(tabbableEl, getTabbableOptions())[0];
|
|
1302
|
+
if (tabbableChild) {
|
|
1303
|
+
tabbableEl = tabbableChild;
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
previouslyFocusedElements.push(tabbableEl);
|
|
1307
|
+
if (previouslyFocusedElements.length > LIST_LIMIT) {
|
|
1308
|
+
previouslyFocusedElements = previouslyFocusedElements.slice(-LIST_LIMIT);
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
function getPreviouslyFocusedElement() {
|
|
1312
|
+
return previouslyFocusedElements.slice().reverse().find(el => el.isConnected);
|
|
1313
|
+
}
|
|
1314
|
+
const VisuallyHiddenDismiss = /*#__PURE__*/React.forwardRef(function VisuallyHiddenDismiss(props, ref) {
|
|
1315
|
+
return /*#__PURE__*/React.createElement("button", _extends({}, props, {
|
|
1316
|
+
type: "button",
|
|
1317
|
+
ref: ref,
|
|
1318
|
+
tabIndex: -1,
|
|
1319
|
+
style: HIDDEN_STYLES
|
|
1320
|
+
}));
|
|
1321
|
+
});
|
|
1322
|
+
/**
|
|
1323
|
+
* Provides focus management for the floating element.
|
|
1324
|
+
* @see https://floating-ui.com/docs/FloatingFocusManager
|
|
1325
|
+
*/
|
|
1326
|
+
function FloatingFocusManager(props) {
|
|
1327
|
+
const {
|
|
1328
|
+
context,
|
|
1329
|
+
children,
|
|
1330
|
+
disabled = false,
|
|
1331
|
+
order = ['content'],
|
|
1332
|
+
guards: _guards = true,
|
|
1333
|
+
initialFocus = 0,
|
|
1334
|
+
returnFocus = true,
|
|
1335
|
+
restoreFocus = false,
|
|
1336
|
+
modal = true,
|
|
1337
|
+
visuallyHiddenDismiss = false,
|
|
1338
|
+
closeOnFocusOut = true
|
|
1339
|
+
} = props;
|
|
1340
|
+
const {
|
|
1341
|
+
open,
|
|
1342
|
+
refs,
|
|
1343
|
+
nodeId,
|
|
1344
|
+
onOpenChange,
|
|
1345
|
+
events,
|
|
1346
|
+
dataRef,
|
|
1347
|
+
floatingId,
|
|
1348
|
+
elements: {
|
|
1349
|
+
domReference,
|
|
1350
|
+
floating
|
|
1351
|
+
}
|
|
1352
|
+
} = context;
|
|
1353
|
+
const ignoreInitialFocus = typeof initialFocus === 'number' && initialFocus < 0;
|
|
1354
|
+
// If the reference is a combobox and is typeable (e.g. input/textarea),
|
|
1355
|
+
// there are different focus semantics. The guards should not be rendered, but
|
|
1356
|
+
// aria-hidden should be applied to all nodes still. Further, the visually
|
|
1357
|
+
// hidden dismiss button should only appear at the end of the list, not the
|
|
1358
|
+
// start.
|
|
1359
|
+
const isUntrappedTypeableCombobox = isTypeableCombobox(domReference) && ignoreInitialFocus;
|
|
1360
|
+
|
|
1361
|
+
// Force the guards to be rendered if the `inert` attribute is not supported.
|
|
1362
|
+
const guards = supportsInert() ? _guards : true;
|
|
1363
|
+
const orderRef = useLatestRef(order);
|
|
1364
|
+
const initialFocusRef = useLatestRef(initialFocus);
|
|
1365
|
+
const returnFocusRef = useLatestRef(returnFocus);
|
|
1366
|
+
const tree = useFloatingTree();
|
|
1367
|
+
const portalContext = usePortalContext();
|
|
1368
|
+
const startDismissButtonRef = React.useRef(null);
|
|
1369
|
+
const endDismissButtonRef = React.useRef(null);
|
|
1370
|
+
const preventReturnFocusRef = React.useRef(false);
|
|
1371
|
+
const isPointerDownRef = React.useRef(false);
|
|
1372
|
+
const tabbableIndexRef = React.useRef(-1);
|
|
1373
|
+
const isInsidePortal = portalContext != null;
|
|
1374
|
+
const floatingFocusElement = getFloatingFocusElement(floating);
|
|
1375
|
+
const getTabbableContent = useEffectEvent(function (container) {
|
|
1376
|
+
if (container === void 0) {
|
|
1377
|
+
container = floatingFocusElement;
|
|
1378
|
+
}
|
|
1379
|
+
return container ? tabbable(container, getTabbableOptions()) : [];
|
|
1380
|
+
});
|
|
1381
|
+
const getTabbableElements = useEffectEvent(container => {
|
|
1382
|
+
const content = getTabbableContent(container);
|
|
1383
|
+
return orderRef.current.map(type => {
|
|
1384
|
+
if (domReference && type === 'reference') {
|
|
1385
|
+
return domReference;
|
|
1386
|
+
}
|
|
1387
|
+
if (floatingFocusElement && type === 'floating') {
|
|
1388
|
+
return floatingFocusElement;
|
|
1389
|
+
}
|
|
1390
|
+
return content;
|
|
1391
|
+
}).filter(Boolean).flat();
|
|
1392
|
+
});
|
|
1393
|
+
React.useEffect(() => {
|
|
1394
|
+
if (disabled) return;
|
|
1395
|
+
if (!modal) return;
|
|
1396
|
+
function onKeyDown(event) {
|
|
1397
|
+
if (event.key === 'Tab') {
|
|
1398
|
+
// The focus guards have nothing to focus, so we need to stop the event.
|
|
1399
|
+
if (contains(floatingFocusElement, activeElement(getDocument(floatingFocusElement))) && getTabbableContent().length === 0 && !isUntrappedTypeableCombobox) {
|
|
1400
|
+
stopEvent(event);
|
|
1401
|
+
}
|
|
1402
|
+
const els = getTabbableElements();
|
|
1403
|
+
const target = getTarget(event);
|
|
1404
|
+
if (orderRef.current[0] === 'reference' && target === domReference) {
|
|
1405
|
+
stopEvent(event);
|
|
1406
|
+
if (event.shiftKey) {
|
|
1407
|
+
enqueueFocus(els[els.length - 1]);
|
|
1408
|
+
} else {
|
|
1409
|
+
enqueueFocus(els[1]);
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
if (orderRef.current[1] === 'floating' && target === floatingFocusElement && event.shiftKey) {
|
|
1413
|
+
stopEvent(event);
|
|
1414
|
+
enqueueFocus(els[0]);
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
const doc = getDocument(floatingFocusElement);
|
|
1419
|
+
doc.addEventListener('keydown', onKeyDown);
|
|
1420
|
+
return () => {
|
|
1421
|
+
doc.removeEventListener('keydown', onKeyDown);
|
|
1422
|
+
};
|
|
1423
|
+
}, [disabled, domReference, floatingFocusElement, modal, orderRef, isUntrappedTypeableCombobox, getTabbableContent, getTabbableElements]);
|
|
1424
|
+
React.useEffect(() => {
|
|
1425
|
+
if (disabled) return;
|
|
1426
|
+
if (!floating) return;
|
|
1427
|
+
function handleFocusIn(event) {
|
|
1428
|
+
const target = getTarget(event);
|
|
1429
|
+
const tabbableContent = getTabbableContent();
|
|
1430
|
+
const tabbableIndex = tabbableContent.indexOf(target);
|
|
1431
|
+
if (tabbableIndex !== -1) {
|
|
1432
|
+
tabbableIndexRef.current = tabbableIndex;
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
floating.addEventListener('focusin', handleFocusIn);
|
|
1436
|
+
return () => {
|
|
1437
|
+
floating.removeEventListener('focusin', handleFocusIn);
|
|
1438
|
+
};
|
|
1439
|
+
}, [disabled, floating, getTabbableContent]);
|
|
1440
|
+
React.useEffect(() => {
|
|
1441
|
+
if (disabled) return;
|
|
1442
|
+
if (!closeOnFocusOut) return;
|
|
1443
|
+
|
|
1444
|
+
// In Safari, buttons lose focus when pressing them.
|
|
1445
|
+
function handlePointerDown() {
|
|
1446
|
+
isPointerDownRef.current = true;
|
|
1447
|
+
setTimeout(() => {
|
|
1448
|
+
isPointerDownRef.current = false;
|
|
1449
|
+
});
|
|
1450
|
+
}
|
|
1451
|
+
function handleFocusOutside(event) {
|
|
1452
|
+
const relatedTarget = event.relatedTarget;
|
|
1453
|
+
queueMicrotask(() => {
|
|
1454
|
+
const movedToUnrelatedNode = !(contains(domReference, relatedTarget) || contains(floating, relatedTarget) || contains(relatedTarget, floating) || contains(portalContext == null ? void 0 : portalContext.portalNode, relatedTarget) || relatedTarget != null && relatedTarget.hasAttribute(createAttribute('focus-guard')) || tree && (getChildren(tree.nodesRef.current, nodeId).find(node => {
|
|
1455
|
+
var _node$context, _node$context2;
|
|
1456
|
+
return contains((_node$context = node.context) == null ? void 0 : _node$context.elements.floating, relatedTarget) || contains((_node$context2 = node.context) == null ? void 0 : _node$context2.elements.domReference, relatedTarget);
|
|
1457
|
+
}) || getAncestors(tree.nodesRef.current, nodeId).find(node => {
|
|
1458
|
+
var _node$context3, _node$context4;
|
|
1459
|
+
return ((_node$context3 = node.context) == null ? void 0 : _node$context3.elements.floating) === relatedTarget || ((_node$context4 = node.context) == null ? void 0 : _node$context4.elements.domReference) === relatedTarget;
|
|
1460
|
+
})));
|
|
1461
|
+
|
|
1462
|
+
// Restore focus to the previous tabbable element index to prevent
|
|
1463
|
+
// focus from being lost outside the floating tree.
|
|
1464
|
+
if (restoreFocus && movedToUnrelatedNode && activeElement(getDocument(floatingFocusElement)) === getDocument(floatingFocusElement).body) {
|
|
1465
|
+
// Let `FloatingPortal` effect knows that focus is still inside the
|
|
1466
|
+
// floating tree.
|
|
1467
|
+
if (isHTMLElement(floatingFocusElement)) {
|
|
1468
|
+
floatingFocusElement.focus();
|
|
1469
|
+
}
|
|
1470
|
+
const prevTabbableIndex = tabbableIndexRef.current;
|
|
1471
|
+
const tabbableContent = getTabbableContent();
|
|
1472
|
+
const nodeToFocus = tabbableContent[prevTabbableIndex] || tabbableContent[tabbableContent.length - 1] || floatingFocusElement;
|
|
1473
|
+
if (isHTMLElement(nodeToFocus)) {
|
|
1474
|
+
nodeToFocus.focus();
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
// Focus did not move inside the floating tree, and there are no tabbable
|
|
1479
|
+
// portal guards to handle closing.
|
|
1480
|
+
if ((isUntrappedTypeableCombobox ? true : !modal) && relatedTarget && movedToUnrelatedNode && !isPointerDownRef.current &&
|
|
1481
|
+
// Fix React 18 Strict Mode returnFocus due to double rendering.
|
|
1482
|
+
relatedTarget !== getPreviouslyFocusedElement()) {
|
|
1483
|
+
preventReturnFocusRef.current = true;
|
|
1484
|
+
onOpenChange(false, event, 'focus-out');
|
|
1485
|
+
}
|
|
1486
|
+
});
|
|
1487
|
+
}
|
|
1488
|
+
if (floating && isHTMLElement(domReference)) {
|
|
1489
|
+
domReference.addEventListener('focusout', handleFocusOutside);
|
|
1490
|
+
domReference.addEventListener('pointerdown', handlePointerDown);
|
|
1491
|
+
floating.addEventListener('focusout', handleFocusOutside);
|
|
1492
|
+
return () => {
|
|
1493
|
+
domReference.removeEventListener('focusout', handleFocusOutside);
|
|
1494
|
+
domReference.removeEventListener('pointerdown', handlePointerDown);
|
|
1495
|
+
floating.removeEventListener('focusout', handleFocusOutside);
|
|
1496
|
+
};
|
|
1497
|
+
}
|
|
1498
|
+
}, [disabled, domReference, floating, floatingFocusElement, modal, nodeId, tree, portalContext, onOpenChange, closeOnFocusOut, restoreFocus, getTabbableContent, isUntrappedTypeableCombobox]);
|
|
1499
|
+
React.useEffect(() => {
|
|
1500
|
+
var _portalContext$portal;
|
|
1501
|
+
if (disabled) return;
|
|
1502
|
+
|
|
1503
|
+
// Don't hide portals nested within the parent portal.
|
|
1504
|
+
const portalNodes = Array.from((portalContext == null || (_portalContext$portal = portalContext.portalNode) == null ? void 0 : _portalContext$portal.querySelectorAll("[" + createAttribute('portal') + "]")) || []);
|
|
1505
|
+
if (floating) {
|
|
1506
|
+
const insideElements = [floating, ...portalNodes, startDismissButtonRef.current, endDismissButtonRef.current, orderRef.current.includes('reference') || isUntrappedTypeableCombobox ? domReference : null].filter(x => x != null);
|
|
1507
|
+
const cleanup = modal || isUntrappedTypeableCombobox ? markOthers(insideElements, guards, !guards) : markOthers(insideElements);
|
|
1508
|
+
return () => {
|
|
1509
|
+
cleanup();
|
|
1510
|
+
};
|
|
1511
|
+
}
|
|
1512
|
+
}, [disabled, domReference, floating, modal, orderRef, portalContext, isUntrappedTypeableCombobox, guards]);
|
|
1513
|
+
index(() => {
|
|
1514
|
+
if (disabled || !isHTMLElement(floatingFocusElement)) return;
|
|
1515
|
+
const doc = getDocument(floatingFocusElement);
|
|
1516
|
+
const previouslyFocusedElement = activeElement(doc);
|
|
1517
|
+
|
|
1518
|
+
// Wait for any layout effect state setters to execute to set `tabIndex`.
|
|
1519
|
+
queueMicrotask(() => {
|
|
1520
|
+
const focusableElements = getTabbableElements(floatingFocusElement);
|
|
1521
|
+
const initialFocusValue = initialFocusRef.current;
|
|
1522
|
+
const elToFocus = (typeof initialFocusValue === 'number' ? focusableElements[initialFocusValue] : initialFocusValue.current) || floatingFocusElement;
|
|
1523
|
+
const focusAlreadyInsideFloatingEl = contains(floatingFocusElement, previouslyFocusedElement);
|
|
1524
|
+
if (!ignoreInitialFocus && !focusAlreadyInsideFloatingEl && open) {
|
|
1525
|
+
enqueueFocus(elToFocus, {
|
|
1526
|
+
preventScroll: elToFocus === floatingFocusElement
|
|
1527
|
+
});
|
|
1528
|
+
}
|
|
1529
|
+
});
|
|
1530
|
+
}, [disabled, open, floatingFocusElement, ignoreInitialFocus, getTabbableElements, initialFocusRef]);
|
|
1531
|
+
index(() => {
|
|
1532
|
+
if (disabled || !floatingFocusElement) return;
|
|
1533
|
+
let preventReturnFocusScroll = false;
|
|
1534
|
+
const doc = getDocument(floatingFocusElement);
|
|
1535
|
+
const previouslyFocusedElement = activeElement(doc);
|
|
1536
|
+
const contextData = dataRef.current;
|
|
1537
|
+
let openEvent = contextData.openEvent;
|
|
1538
|
+
addPreviouslyFocusedElement(previouslyFocusedElement);
|
|
1539
|
+
|
|
1540
|
+
// Dismissing via outside press should always ignore `returnFocus` to
|
|
1541
|
+
// prevent unwanted scrolling.
|
|
1542
|
+
function onOpenChange(_ref) {
|
|
1543
|
+
let {
|
|
1544
|
+
open,
|
|
1545
|
+
reason,
|
|
1546
|
+
event,
|
|
1547
|
+
nested
|
|
1548
|
+
} = _ref;
|
|
1549
|
+
if (open) {
|
|
1550
|
+
openEvent = event;
|
|
1551
|
+
}
|
|
1552
|
+
if (reason === 'escape-key' && refs.domReference.current) {
|
|
1553
|
+
addPreviouslyFocusedElement(refs.domReference.current);
|
|
1554
|
+
}
|
|
1555
|
+
if (reason === 'hover' && event.type === 'mouseleave') {
|
|
1556
|
+
preventReturnFocusRef.current = true;
|
|
1557
|
+
}
|
|
1558
|
+
if (reason !== 'outside-press') return;
|
|
1559
|
+
if (nested) {
|
|
1560
|
+
preventReturnFocusRef.current = false;
|
|
1561
|
+
preventReturnFocusScroll = true;
|
|
1562
|
+
} else {
|
|
1563
|
+
preventReturnFocusRef.current = !(isVirtualClick(event) || isVirtualPointerEvent(event));
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
events.on('openchange', onOpenChange);
|
|
1567
|
+
const fallbackEl = doc.createElement('span');
|
|
1568
|
+
fallbackEl.setAttribute('tabindex', '-1');
|
|
1569
|
+
fallbackEl.setAttribute('aria-hidden', 'true');
|
|
1570
|
+
Object.assign(fallbackEl.style, HIDDEN_STYLES);
|
|
1571
|
+
if (isInsidePortal && domReference) {
|
|
1572
|
+
domReference.insertAdjacentElement('afterend', fallbackEl);
|
|
1573
|
+
}
|
|
1574
|
+
function getReturnElement() {
|
|
1575
|
+
if (typeof returnFocusRef.current === 'boolean') {
|
|
1576
|
+
return getPreviouslyFocusedElement() || fallbackEl;
|
|
1577
|
+
}
|
|
1578
|
+
return returnFocusRef.current.current || fallbackEl;
|
|
1579
|
+
}
|
|
1580
|
+
return () => {
|
|
1581
|
+
events.off('openchange', onOpenChange);
|
|
1582
|
+
const activeEl = activeElement(doc);
|
|
1583
|
+
const isFocusInsideFloatingTree = contains(floating, activeEl) || tree && getChildren(tree.nodesRef.current, nodeId).some(node => {
|
|
1584
|
+
var _node$context5;
|
|
1585
|
+
return contains((_node$context5 = node.context) == null ? void 0 : _node$context5.elements.floating, activeEl);
|
|
1586
|
+
});
|
|
1587
|
+
const shouldFocusReference = isFocusInsideFloatingTree || openEvent && ['click', 'mousedown'].includes(openEvent.type);
|
|
1588
|
+
if (shouldFocusReference && refs.domReference.current) {
|
|
1589
|
+
addPreviouslyFocusedElement(refs.domReference.current);
|
|
1590
|
+
}
|
|
1591
|
+
const returnElement = getReturnElement();
|
|
1592
|
+
queueMicrotask(() => {
|
|
1593
|
+
if (
|
|
1594
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1595
|
+
returnFocusRef.current && !preventReturnFocusRef.current && isHTMLElement(returnElement) && (
|
|
1596
|
+
// If the focus moved somewhere else after mount, avoid returning focus
|
|
1597
|
+
// since it likely entered a different element which should be
|
|
1598
|
+
// respected: https://github.com/floating-ui/floating-ui/issues/2607
|
|
1599
|
+
returnElement !== activeEl && activeEl !== doc.body ? isFocusInsideFloatingTree : true)) {
|
|
1600
|
+
returnElement.focus({
|
|
1601
|
+
preventScroll: preventReturnFocusScroll
|
|
1602
|
+
});
|
|
1603
|
+
}
|
|
1604
|
+
fallbackEl.remove();
|
|
1605
|
+
});
|
|
1606
|
+
};
|
|
1607
|
+
}, [disabled, floating, floatingFocusElement, returnFocusRef, dataRef, refs, events, tree, nodeId, isInsidePortal, domReference]);
|
|
1608
|
+
React.useEffect(() => {
|
|
1609
|
+
// The `returnFocus` cleanup behavior is inside a microtask; ensure we
|
|
1610
|
+
// wait for it to complete before resetting the flag.
|
|
1611
|
+
queueMicrotask(() => {
|
|
1612
|
+
preventReturnFocusRef.current = false;
|
|
1613
|
+
});
|
|
1614
|
+
}, [disabled]);
|
|
1615
|
+
|
|
1616
|
+
// Synchronize the `context` & `modal` value to the FloatingPortal context.
|
|
1617
|
+
// It will decide whether or not it needs to render its own guards.
|
|
1618
|
+
index(() => {
|
|
1619
|
+
if (disabled) return;
|
|
1620
|
+
if (!portalContext) return;
|
|
1621
|
+
portalContext.setFocusManagerState({
|
|
1622
|
+
modal,
|
|
1623
|
+
closeOnFocusOut,
|
|
1624
|
+
open,
|
|
1625
|
+
onOpenChange,
|
|
1626
|
+
refs
|
|
1627
|
+
});
|
|
1628
|
+
return () => {
|
|
1629
|
+
portalContext.setFocusManagerState(null);
|
|
1630
|
+
};
|
|
1631
|
+
}, [disabled, portalContext, modal, open, onOpenChange, refs, closeOnFocusOut]);
|
|
1632
|
+
index(() => {
|
|
1633
|
+
if (disabled) return;
|
|
1634
|
+
if (!floatingFocusElement) return;
|
|
1635
|
+
if (typeof MutationObserver !== 'function') return;
|
|
1636
|
+
if (ignoreInitialFocus) return;
|
|
1637
|
+
const handleMutation = () => {
|
|
1638
|
+
const tabIndex = floatingFocusElement.getAttribute('tabindex');
|
|
1639
|
+
const tabbableContent = getTabbableContent();
|
|
1640
|
+
const activeEl = activeElement(getDocument(floating));
|
|
1641
|
+
const tabbableIndex = tabbableContent.indexOf(activeEl);
|
|
1642
|
+
if (tabbableIndex !== -1) {
|
|
1643
|
+
tabbableIndexRef.current = tabbableIndex;
|
|
1644
|
+
}
|
|
1645
|
+
if (orderRef.current.includes('floating') || activeEl !== refs.domReference.current && tabbableContent.length === 0) {
|
|
1646
|
+
if (tabIndex !== '0') {
|
|
1647
|
+
floatingFocusElement.setAttribute('tabindex', '0');
|
|
1648
|
+
}
|
|
1649
|
+
} else if (tabIndex !== '-1') {
|
|
1650
|
+
floatingFocusElement.setAttribute('tabindex', '-1');
|
|
1651
|
+
}
|
|
1652
|
+
};
|
|
1653
|
+
handleMutation();
|
|
1654
|
+
const observer = new MutationObserver(handleMutation);
|
|
1655
|
+
observer.observe(floatingFocusElement, {
|
|
1656
|
+
childList: true,
|
|
1657
|
+
subtree: true,
|
|
1658
|
+
attributes: true
|
|
1659
|
+
});
|
|
1660
|
+
return () => {
|
|
1661
|
+
observer.disconnect();
|
|
1662
|
+
};
|
|
1663
|
+
}, [disabled, floating, floatingFocusElement, refs, orderRef, getTabbableContent, ignoreInitialFocus]);
|
|
1664
|
+
function renderDismissButton(location) {
|
|
1665
|
+
if (disabled || !visuallyHiddenDismiss || !modal) {
|
|
1666
|
+
return null;
|
|
1667
|
+
}
|
|
1668
|
+
return /*#__PURE__*/React.createElement(VisuallyHiddenDismiss, {
|
|
1669
|
+
ref: location === 'start' ? startDismissButtonRef : endDismissButtonRef,
|
|
1670
|
+
onClick: event => onOpenChange(false, event.nativeEvent)
|
|
1671
|
+
}, typeof visuallyHiddenDismiss === 'string' ? visuallyHiddenDismiss : 'Dismiss');
|
|
1672
|
+
}
|
|
1673
|
+
const shouldRenderGuards = !disabled && guards && (modal ? !isUntrappedTypeableCombobox : true) && (isInsidePortal || modal);
|
|
1674
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, shouldRenderGuards && /*#__PURE__*/React.createElement(FocusGuard, {
|
|
1675
|
+
"data-type": "inside",
|
|
1676
|
+
ref: portalContext == null ? void 0 : portalContext.beforeInsideRef,
|
|
1677
|
+
onFocus: event => {
|
|
1678
|
+
if (modal) {
|
|
1679
|
+
const els = getTabbableElements();
|
|
1680
|
+
enqueueFocus(order[0] === 'reference' ? els[0] : els[els.length - 1]);
|
|
1681
|
+
} else if (portalContext != null && portalContext.preserveTabOrder && portalContext.portalNode) {
|
|
1682
|
+
preventReturnFocusRef.current = false;
|
|
1683
|
+
if (isOutsideEvent(event, portalContext.portalNode)) {
|
|
1684
|
+
const nextTabbable = getNextTabbable() || domReference;
|
|
1685
|
+
nextTabbable == null || nextTabbable.focus();
|
|
1686
|
+
} else {
|
|
1687
|
+
var _portalContext$before;
|
|
1688
|
+
(_portalContext$before = portalContext.beforeOutsideRef.current) == null || _portalContext$before.focus();
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
}), !isUntrappedTypeableCombobox && renderDismissButton('start'), children, renderDismissButton('end'), shouldRenderGuards && /*#__PURE__*/React.createElement(FocusGuard, {
|
|
1693
|
+
"data-type": "inside",
|
|
1694
|
+
ref: portalContext == null ? void 0 : portalContext.afterInsideRef,
|
|
1695
|
+
onFocus: event => {
|
|
1696
|
+
if (modal) {
|
|
1697
|
+
enqueueFocus(getTabbableElements()[0]);
|
|
1698
|
+
} else if (portalContext != null && portalContext.preserveTabOrder && portalContext.portalNode) {
|
|
1699
|
+
if (closeOnFocusOut) {
|
|
1700
|
+
preventReturnFocusRef.current = true;
|
|
1701
|
+
}
|
|
1702
|
+
if (isOutsideEvent(event, portalContext.portalNode)) {
|
|
1703
|
+
const prevTabbable = getPreviousTabbable() || domReference;
|
|
1704
|
+
prevTabbable == null || prevTabbable.focus();
|
|
1705
|
+
} else {
|
|
1706
|
+
var _portalContext$afterO;
|
|
1707
|
+
(_portalContext$afterO = portalContext.afterOutsideRef.current) == null || _portalContext$afterO.focus();
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
}));
|
|
1712
|
+
}
|
|
1713
|
+
|
|
1714
|
+
let lockCount = 0;
|
|
1715
|
+
function enableScrollLock() {
|
|
1716
|
+
const isIOS = /iP(hone|ad|od)|iOS/.test(getPlatform());
|
|
1717
|
+
const bodyStyle = document.body.style;
|
|
1718
|
+
// RTL <body> scrollbar
|
|
1719
|
+
const scrollbarX = Math.round(document.documentElement.getBoundingClientRect().left) + document.documentElement.scrollLeft;
|
|
1720
|
+
const paddingProp = scrollbarX ? 'paddingLeft' : 'paddingRight';
|
|
1721
|
+
const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
|
|
1722
|
+
const scrollX = bodyStyle.left ? parseFloat(bodyStyle.left) : window.scrollX;
|
|
1723
|
+
const scrollY = bodyStyle.top ? parseFloat(bodyStyle.top) : window.scrollY;
|
|
1724
|
+
bodyStyle.overflow = 'hidden';
|
|
1725
|
+
if (scrollbarWidth) {
|
|
1726
|
+
bodyStyle[paddingProp] = scrollbarWidth + "px";
|
|
1727
|
+
}
|
|
1728
|
+
|
|
1729
|
+
// Only iOS doesn't respect `overflow: hidden` on document.body, and this
|
|
1730
|
+
// technique has fewer side effects.
|
|
1731
|
+
if (isIOS) {
|
|
1732
|
+
var _window$visualViewpor, _window$visualViewpor2;
|
|
1733
|
+
// iOS 12 does not support `visualViewport`.
|
|
1734
|
+
const offsetLeft = ((_window$visualViewpor = window.visualViewport) == null ? void 0 : _window$visualViewpor.offsetLeft) || 0;
|
|
1735
|
+
const offsetTop = ((_window$visualViewpor2 = window.visualViewport) == null ? void 0 : _window$visualViewpor2.offsetTop) || 0;
|
|
1736
|
+
Object.assign(bodyStyle, {
|
|
1737
|
+
position: 'fixed',
|
|
1738
|
+
top: -(scrollY - Math.floor(offsetTop)) + "px",
|
|
1739
|
+
left: -(scrollX - Math.floor(offsetLeft)) + "px",
|
|
1740
|
+
right: '0'
|
|
1741
|
+
});
|
|
1742
|
+
}
|
|
1743
|
+
return () => {
|
|
1744
|
+
Object.assign(bodyStyle, {
|
|
1745
|
+
overflow: '',
|
|
1746
|
+
[paddingProp]: ''
|
|
1747
|
+
});
|
|
1748
|
+
if (isIOS) {
|
|
1749
|
+
Object.assign(bodyStyle, {
|
|
1750
|
+
position: '',
|
|
1751
|
+
top: '',
|
|
1752
|
+
left: '',
|
|
1753
|
+
right: ''
|
|
1754
|
+
});
|
|
1755
|
+
window.scrollTo(scrollX, scrollY);
|
|
1756
|
+
}
|
|
1757
|
+
};
|
|
1758
|
+
}
|
|
1759
|
+
let cleanup = () => {};
|
|
1760
|
+
|
|
1761
|
+
/**
|
|
1762
|
+
* Provides base styling for a fixed overlay element to dim content or block
|
|
1763
|
+
* pointer events behind a floating element.
|
|
1764
|
+
* It's a regular `<div>`, so it can be styled via any CSS solution you prefer.
|
|
1765
|
+
* @see https://floating-ui.com/docs/FloatingOverlay
|
|
1766
|
+
*/
|
|
1767
|
+
const FloatingOverlay = /*#__PURE__*/React.forwardRef(function FloatingOverlay(props, ref) {
|
|
1768
|
+
const {
|
|
1769
|
+
lockScroll = false,
|
|
1770
|
+
...rest
|
|
1771
|
+
} = props;
|
|
1772
|
+
index(() => {
|
|
1773
|
+
if (!lockScroll) return;
|
|
1774
|
+
lockCount++;
|
|
1775
|
+
if (lockCount === 1) {
|
|
1776
|
+
cleanup = enableScrollLock();
|
|
1777
|
+
}
|
|
1778
|
+
return () => {
|
|
1779
|
+
lockCount--;
|
|
1780
|
+
if (lockCount === 0) {
|
|
1781
|
+
cleanup();
|
|
1782
|
+
}
|
|
1783
|
+
};
|
|
1784
|
+
}, [lockScroll]);
|
|
1785
|
+
return /*#__PURE__*/React.createElement("div", _extends({
|
|
1786
|
+
ref: ref
|
|
1787
|
+
}, rest, {
|
|
1788
|
+
style: {
|
|
1789
|
+
position: 'fixed',
|
|
1790
|
+
overflow: 'auto',
|
|
1791
|
+
top: 0,
|
|
1792
|
+
right: 0,
|
|
1793
|
+
bottom: 0,
|
|
1794
|
+
left: 0,
|
|
1795
|
+
...rest.style
|
|
1796
|
+
}
|
|
1797
|
+
}));
|
|
1798
|
+
});
|
|
1799
|
+
|
|
1800
|
+
const bubbleHandlerKeys = {
|
|
1801
|
+
pointerdown: 'onPointerDown',
|
|
1802
|
+
mousedown: 'onMouseDown',
|
|
1803
|
+
click: 'onClick'
|
|
1804
|
+
};
|
|
1805
|
+
const captureHandlerKeys = {
|
|
1806
|
+
pointerdown: 'onPointerDownCapture',
|
|
1807
|
+
mousedown: 'onMouseDownCapture',
|
|
1808
|
+
click: 'onClickCapture'
|
|
1809
|
+
};
|
|
1810
|
+
const normalizeProp = normalizable => {
|
|
1811
|
+
var _normalizable$escapeK, _normalizable$outside;
|
|
1812
|
+
return {
|
|
1813
|
+
escapeKey: typeof normalizable === 'boolean' ? normalizable : (_normalizable$escapeK = normalizable == null ? void 0 : normalizable.escapeKey) != null ? _normalizable$escapeK : false,
|
|
1814
|
+
outsidePress: typeof normalizable === 'boolean' ? normalizable : (_normalizable$outside = normalizable == null ? void 0 : normalizable.outsidePress) != null ? _normalizable$outside : true
|
|
1815
|
+
};
|
|
1816
|
+
};
|
|
1817
|
+
/**
|
|
1818
|
+
* Closes the floating element when a dismissal is requested — by default, when
|
|
1819
|
+
* the user presses the `escape` key or outside of the floating element.
|
|
1820
|
+
* @see https://floating-ui.com/docs/useDismiss
|
|
1821
|
+
*/
|
|
1822
|
+
function useDismiss(context, props) {
|
|
1823
|
+
if (props === void 0) {
|
|
1824
|
+
props = {};
|
|
1825
|
+
}
|
|
1826
|
+
const {
|
|
1827
|
+
open,
|
|
1828
|
+
onOpenChange,
|
|
1829
|
+
elements,
|
|
1830
|
+
dataRef
|
|
1831
|
+
} = context;
|
|
1832
|
+
const {
|
|
1833
|
+
enabled = true,
|
|
1834
|
+
escapeKey = true,
|
|
1835
|
+
outsidePress: unstable_outsidePress = true,
|
|
1836
|
+
outsidePressEvent = 'pointerdown',
|
|
1837
|
+
referencePress = false,
|
|
1838
|
+
referencePressEvent = 'pointerdown',
|
|
1839
|
+
ancestorScroll = false,
|
|
1840
|
+
bubbles,
|
|
1841
|
+
capture
|
|
1842
|
+
} = props;
|
|
1843
|
+
const tree = useFloatingTree();
|
|
1844
|
+
const outsidePressFn = useEffectEvent(typeof unstable_outsidePress === 'function' ? unstable_outsidePress : () => false);
|
|
1845
|
+
const outsidePress = typeof unstable_outsidePress === 'function' ? outsidePressFn : unstable_outsidePress;
|
|
1846
|
+
const insideReactTreeRef = React.useRef(false);
|
|
1847
|
+
const endedOrStartedInsideRef = React.useRef(false);
|
|
1848
|
+
const {
|
|
1849
|
+
escapeKey: escapeKeyBubbles,
|
|
1850
|
+
outsidePress: outsidePressBubbles
|
|
1851
|
+
} = normalizeProp(bubbles);
|
|
1852
|
+
const {
|
|
1853
|
+
escapeKey: escapeKeyCapture,
|
|
1854
|
+
outsidePress: outsidePressCapture
|
|
1855
|
+
} = normalizeProp(capture);
|
|
1856
|
+
const isComposingRef = React.useRef(false);
|
|
1857
|
+
const closeOnEscapeKeyDown = useEffectEvent(event => {
|
|
1858
|
+
var _dataRef$current$floa;
|
|
1859
|
+
if (!open || !enabled || !escapeKey || event.key !== 'Escape') {
|
|
1860
|
+
return;
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
// Wait until IME is settled. Pressing `Escape` while composing should
|
|
1864
|
+
// close the compose menu, but not the floating element.
|
|
1865
|
+
if (isComposingRef.current) {
|
|
1866
|
+
return;
|
|
1867
|
+
}
|
|
1868
|
+
const nodeId = (_dataRef$current$floa = dataRef.current.floatingContext) == null ? void 0 : _dataRef$current$floa.nodeId;
|
|
1869
|
+
const children = tree ? getChildren(tree.nodesRef.current, nodeId) : [];
|
|
1870
|
+
if (!escapeKeyBubbles) {
|
|
1871
|
+
event.stopPropagation();
|
|
1872
|
+
if (children.length > 0) {
|
|
1873
|
+
let shouldDismiss = true;
|
|
1874
|
+
children.forEach(child => {
|
|
1875
|
+
var _child$context;
|
|
1876
|
+
if ((_child$context = child.context) != null && _child$context.open && !child.context.dataRef.current.__escapeKeyBubbles) {
|
|
1877
|
+
shouldDismiss = false;
|
|
1878
|
+
return;
|
|
1879
|
+
}
|
|
1880
|
+
});
|
|
1881
|
+
if (!shouldDismiss) {
|
|
1882
|
+
return;
|
|
1883
|
+
}
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
onOpenChange(false, isReactEvent(event) ? event.nativeEvent : event, 'escape-key');
|
|
1887
|
+
});
|
|
1888
|
+
const closeOnEscapeKeyDownCapture = useEffectEvent(event => {
|
|
1889
|
+
var _getTarget2;
|
|
1890
|
+
const callback = () => {
|
|
1891
|
+
var _getTarget;
|
|
1892
|
+
closeOnEscapeKeyDown(event);
|
|
1893
|
+
(_getTarget = getTarget(event)) == null || _getTarget.removeEventListener('keydown', callback);
|
|
1894
|
+
};
|
|
1895
|
+
(_getTarget2 = getTarget(event)) == null || _getTarget2.addEventListener('keydown', callback);
|
|
1896
|
+
});
|
|
1897
|
+
const closeOnPressOutside = useEffectEvent(event => {
|
|
1898
|
+
var _dataRef$current$floa2;
|
|
1899
|
+
// Given developers can stop the propagation of the synthetic event,
|
|
1900
|
+
// we can only be confident with a positive value.
|
|
1901
|
+
const insideReactTree = insideReactTreeRef.current;
|
|
1902
|
+
insideReactTreeRef.current = false;
|
|
1903
|
+
|
|
1904
|
+
// When click outside is lazy (`click` event), handle dragging.
|
|
1905
|
+
// Don't close if:
|
|
1906
|
+
// - The click started inside the floating element.
|
|
1907
|
+
// - The click ended inside the floating element.
|
|
1908
|
+
const endedOrStartedInside = endedOrStartedInsideRef.current;
|
|
1909
|
+
endedOrStartedInsideRef.current = false;
|
|
1910
|
+
if (outsidePressEvent === 'click' && endedOrStartedInside) {
|
|
1911
|
+
return;
|
|
1912
|
+
}
|
|
1913
|
+
if (insideReactTree) {
|
|
1914
|
+
return;
|
|
1915
|
+
}
|
|
1916
|
+
if (typeof outsidePress === 'function' && !outsidePress(event)) {
|
|
1917
|
+
return;
|
|
1918
|
+
}
|
|
1919
|
+
const target = getTarget(event);
|
|
1920
|
+
const inertSelector = "[" + createAttribute('inert') + "]";
|
|
1921
|
+
const markers = getDocument(elements.floating).querySelectorAll(inertSelector);
|
|
1922
|
+
let targetRootAncestor = isElement(target) ? target : null;
|
|
1923
|
+
while (targetRootAncestor && !isLastTraversableNode(targetRootAncestor)) {
|
|
1924
|
+
const nextParent = getParentNode(targetRootAncestor);
|
|
1925
|
+
if (isLastTraversableNode(nextParent) || !isElement(nextParent)) {
|
|
1926
|
+
break;
|
|
1927
|
+
}
|
|
1928
|
+
targetRootAncestor = nextParent;
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
// Check if the click occurred on a third-party element injected after the
|
|
1932
|
+
// floating element rendered.
|
|
1933
|
+
if (markers.length && isElement(target) && !isRootElement(target) &&
|
|
1934
|
+
// Clicked on a direct ancestor (e.g. FloatingOverlay).
|
|
1935
|
+
!contains(target, elements.floating) &&
|
|
1936
|
+
// If the target root element contains none of the markers, then the
|
|
1937
|
+
// element was injected after the floating element rendered.
|
|
1938
|
+
Array.from(markers).every(marker => !contains(targetRootAncestor, marker))) {
|
|
1939
|
+
return;
|
|
1940
|
+
}
|
|
1941
|
+
|
|
1942
|
+
// Check if the click occurred on the scrollbar
|
|
1943
|
+
if (isHTMLElement(target) && floating) {
|
|
1944
|
+
// In Firefox, `target.scrollWidth > target.clientWidth` for inline
|
|
1945
|
+
// elements.
|
|
1946
|
+
const canScrollX = target.clientWidth > 0 && target.scrollWidth > target.clientWidth;
|
|
1947
|
+
const canScrollY = target.clientHeight > 0 && target.scrollHeight > target.clientHeight;
|
|
1948
|
+
let xCond = canScrollY && event.offsetX > target.clientWidth;
|
|
1949
|
+
|
|
1950
|
+
// In some browsers it is possible to change the <body> (or window)
|
|
1951
|
+
// scrollbar to the left side, but is very rare and is difficult to
|
|
1952
|
+
// check for. Plus, for modal dialogs with backdrops, it is more
|
|
1953
|
+
// important that the backdrop is checked but not so much the window.
|
|
1954
|
+
if (canScrollY) {
|
|
1955
|
+
const isRTL = getComputedStyle$1(target).direction === 'rtl';
|
|
1956
|
+
if (isRTL) {
|
|
1957
|
+
xCond = event.offsetX <= target.offsetWidth - target.clientWidth;
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1960
|
+
if (xCond || canScrollX && event.offsetY > target.clientHeight) {
|
|
1961
|
+
return;
|
|
1962
|
+
}
|
|
1963
|
+
}
|
|
1964
|
+
const nodeId = (_dataRef$current$floa2 = dataRef.current.floatingContext) == null ? void 0 : _dataRef$current$floa2.nodeId;
|
|
1965
|
+
const targetIsInsideChildren = tree && getChildren(tree.nodesRef.current, nodeId).some(node => {
|
|
1966
|
+
var _node$context;
|
|
1967
|
+
return isEventTargetWithin(event, (_node$context = node.context) == null ? void 0 : _node$context.elements.floating);
|
|
1968
|
+
});
|
|
1969
|
+
if (isEventTargetWithin(event, elements.floating) || isEventTargetWithin(event, elements.domReference) || targetIsInsideChildren) {
|
|
1970
|
+
return;
|
|
1971
|
+
}
|
|
1972
|
+
const children = tree ? getChildren(tree.nodesRef.current, nodeId) : [];
|
|
1973
|
+
if (children.length > 0) {
|
|
1974
|
+
let shouldDismiss = true;
|
|
1975
|
+
children.forEach(child => {
|
|
1976
|
+
var _child$context2;
|
|
1977
|
+
if ((_child$context2 = child.context) != null && _child$context2.open && !child.context.dataRef.current.__outsidePressBubbles) {
|
|
1978
|
+
shouldDismiss = false;
|
|
1979
|
+
return;
|
|
1980
|
+
}
|
|
1981
|
+
});
|
|
1982
|
+
if (!shouldDismiss) {
|
|
1983
|
+
return;
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1986
|
+
onOpenChange(false, event, 'outside-press');
|
|
1987
|
+
});
|
|
1988
|
+
const closeOnPressOutsideCapture = useEffectEvent(event => {
|
|
1989
|
+
var _getTarget4;
|
|
1990
|
+
const callback = () => {
|
|
1991
|
+
var _getTarget3;
|
|
1992
|
+
closeOnPressOutside(event);
|
|
1993
|
+
(_getTarget3 = getTarget(event)) == null || _getTarget3.removeEventListener(outsidePressEvent, callback);
|
|
1994
|
+
};
|
|
1995
|
+
(_getTarget4 = getTarget(event)) == null || _getTarget4.addEventListener(outsidePressEvent, callback);
|
|
1996
|
+
});
|
|
1997
|
+
React.useEffect(() => {
|
|
1998
|
+
if (!open || !enabled) {
|
|
1999
|
+
return;
|
|
2000
|
+
}
|
|
2001
|
+
dataRef.current.__escapeKeyBubbles = escapeKeyBubbles;
|
|
2002
|
+
dataRef.current.__outsidePressBubbles = outsidePressBubbles;
|
|
2003
|
+
let compositionTimeout = -1;
|
|
2004
|
+
function onScroll(event) {
|
|
2005
|
+
onOpenChange(false, event, 'ancestor-scroll');
|
|
2006
|
+
}
|
|
2007
|
+
function handleCompositionStart() {
|
|
2008
|
+
window.clearTimeout(compositionTimeout);
|
|
2009
|
+
isComposingRef.current = true;
|
|
2010
|
+
}
|
|
2011
|
+
function handleCompositionEnd() {
|
|
2012
|
+
// Safari fires `compositionend` before `keydown`, so we need to wait
|
|
2013
|
+
// until the next tick to set `isComposing` to `false`.
|
|
2014
|
+
// https://bugs.webkit.org/show_bug.cgi?id=165004
|
|
2015
|
+
compositionTimeout = window.setTimeout(() => {
|
|
2016
|
+
isComposingRef.current = false;
|
|
2017
|
+
},
|
|
2018
|
+
// 0ms or 1ms don't work in Safari. 5ms appears to consistently work.
|
|
2019
|
+
// Only apply to WebKit for the test to remain 0ms.
|
|
2020
|
+
isWebKit() ? 5 : 0);
|
|
2021
|
+
}
|
|
2022
|
+
const doc = getDocument(elements.floating);
|
|
2023
|
+
if (escapeKey) {
|
|
2024
|
+
doc.addEventListener('keydown', escapeKeyCapture ? closeOnEscapeKeyDownCapture : closeOnEscapeKeyDown, escapeKeyCapture);
|
|
2025
|
+
doc.addEventListener('compositionstart', handleCompositionStart);
|
|
2026
|
+
doc.addEventListener('compositionend', handleCompositionEnd);
|
|
2027
|
+
}
|
|
2028
|
+
outsidePress && doc.addEventListener(outsidePressEvent, outsidePressCapture ? closeOnPressOutsideCapture : closeOnPressOutside, outsidePressCapture);
|
|
2029
|
+
let ancestors = [];
|
|
2030
|
+
if (ancestorScroll) {
|
|
2031
|
+
if (isElement(elements.domReference)) {
|
|
2032
|
+
ancestors = getOverflowAncestors(elements.domReference);
|
|
2033
|
+
}
|
|
2034
|
+
if (isElement(elements.floating)) {
|
|
2035
|
+
ancestors = ancestors.concat(getOverflowAncestors(elements.floating));
|
|
2036
|
+
}
|
|
2037
|
+
if (!isElement(elements.reference) && elements.reference && elements.reference.contextElement) {
|
|
2038
|
+
ancestors = ancestors.concat(getOverflowAncestors(elements.reference.contextElement));
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2042
|
+
// Ignore the visual viewport for scrolling dismissal (allow pinch-zoom)
|
|
2043
|
+
ancestors = ancestors.filter(ancestor => {
|
|
2044
|
+
var _doc$defaultView;
|
|
2045
|
+
return ancestor !== ((_doc$defaultView = doc.defaultView) == null ? void 0 : _doc$defaultView.visualViewport);
|
|
2046
|
+
});
|
|
2047
|
+
ancestors.forEach(ancestor => {
|
|
2048
|
+
ancestor.addEventListener('scroll', onScroll, {
|
|
2049
|
+
passive: true
|
|
2050
|
+
});
|
|
2051
|
+
});
|
|
2052
|
+
return () => {
|
|
2053
|
+
if (escapeKey) {
|
|
2054
|
+
doc.removeEventListener('keydown', escapeKeyCapture ? closeOnEscapeKeyDownCapture : closeOnEscapeKeyDown, escapeKeyCapture);
|
|
2055
|
+
doc.removeEventListener('compositionstart', handleCompositionStart);
|
|
2056
|
+
doc.removeEventListener('compositionend', handleCompositionEnd);
|
|
2057
|
+
}
|
|
2058
|
+
outsidePress && doc.removeEventListener(outsidePressEvent, outsidePressCapture ? closeOnPressOutsideCapture : closeOnPressOutside, outsidePressCapture);
|
|
2059
|
+
ancestors.forEach(ancestor => {
|
|
2060
|
+
ancestor.removeEventListener('scroll', onScroll);
|
|
2061
|
+
});
|
|
2062
|
+
window.clearTimeout(compositionTimeout);
|
|
2063
|
+
};
|
|
2064
|
+
}, [dataRef, elements, escapeKey, outsidePress, outsidePressEvent, open, onOpenChange, ancestorScroll, enabled, escapeKeyBubbles, outsidePressBubbles, closeOnEscapeKeyDown, escapeKeyCapture, closeOnEscapeKeyDownCapture, closeOnPressOutside, outsidePressCapture, closeOnPressOutsideCapture]);
|
|
2065
|
+
React.useEffect(() => {
|
|
2066
|
+
insideReactTreeRef.current = false;
|
|
2067
|
+
}, [outsidePress, outsidePressEvent]);
|
|
2068
|
+
const reference = React.useMemo(() => ({
|
|
2069
|
+
onKeyDown: closeOnEscapeKeyDown,
|
|
2070
|
+
[bubbleHandlerKeys[referencePressEvent]]: event => {
|
|
2071
|
+
if (referencePress) {
|
|
2072
|
+
onOpenChange(false, event.nativeEvent, 'reference-press');
|
|
2073
|
+
}
|
|
2074
|
+
}
|
|
2075
|
+
}), [closeOnEscapeKeyDown, onOpenChange, referencePress, referencePressEvent]);
|
|
2076
|
+
const floating = React.useMemo(() => ({
|
|
2077
|
+
onKeyDown: closeOnEscapeKeyDown,
|
|
2078
|
+
onMouseDown() {
|
|
2079
|
+
endedOrStartedInsideRef.current = true;
|
|
2080
|
+
},
|
|
2081
|
+
onMouseUp() {
|
|
2082
|
+
endedOrStartedInsideRef.current = true;
|
|
2083
|
+
},
|
|
2084
|
+
[captureHandlerKeys[outsidePressEvent]]: () => {
|
|
2085
|
+
insideReactTreeRef.current = true;
|
|
2086
|
+
}
|
|
2087
|
+
}), [closeOnEscapeKeyDown, outsidePressEvent]);
|
|
2088
|
+
return React.useMemo(() => enabled ? {
|
|
2089
|
+
reference,
|
|
2090
|
+
floating
|
|
2091
|
+
} : {}, [enabled, reference, floating]);
|
|
2092
|
+
}
|
|
2093
|
+
|
|
2094
|
+
function useFloatingRootContext(options) {
|
|
2095
|
+
const {
|
|
2096
|
+
open = false,
|
|
2097
|
+
onOpenChange: onOpenChangeProp,
|
|
2098
|
+
elements: elementsProp
|
|
2099
|
+
} = options;
|
|
2100
|
+
const floatingId = useId();
|
|
2101
|
+
const dataRef = React.useRef({});
|
|
2102
|
+
const [events] = React.useState(() => createPubSub());
|
|
2103
|
+
const nested = useFloatingParentNodeId() != null;
|
|
2104
|
+
{
|
|
2105
|
+
const optionDomReference = elementsProp.reference;
|
|
2106
|
+
if (optionDomReference && !isElement(optionDomReference)) {
|
|
2107
|
+
error('Cannot pass a virtual element to the `elements.reference` option,', 'as it must be a real DOM element. Use `refs.setPositionReference()`', 'instead.');
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
const [positionReference, setPositionReference] = React.useState(elementsProp.reference);
|
|
2111
|
+
const onOpenChange = useEffectEvent((open, event, reason) => {
|
|
2112
|
+
dataRef.current.openEvent = open ? event : undefined;
|
|
2113
|
+
events.emit('openchange', {
|
|
2114
|
+
open,
|
|
2115
|
+
event,
|
|
2116
|
+
reason,
|
|
2117
|
+
nested
|
|
2118
|
+
});
|
|
2119
|
+
onOpenChangeProp == null || onOpenChangeProp(open, event, reason);
|
|
2120
|
+
});
|
|
2121
|
+
const refs = React.useMemo(() => ({
|
|
2122
|
+
setPositionReference
|
|
2123
|
+
}), []);
|
|
2124
|
+
const elements = React.useMemo(() => ({
|
|
2125
|
+
reference: positionReference || elementsProp.reference || null,
|
|
2126
|
+
floating: elementsProp.floating || null,
|
|
2127
|
+
domReference: elementsProp.reference
|
|
2128
|
+
}), [positionReference, elementsProp.reference, elementsProp.floating]);
|
|
2129
|
+
return React.useMemo(() => ({
|
|
2130
|
+
dataRef,
|
|
2131
|
+
open,
|
|
2132
|
+
onOpenChange,
|
|
2133
|
+
elements,
|
|
2134
|
+
events,
|
|
2135
|
+
floatingId,
|
|
2136
|
+
refs
|
|
2137
|
+
}), [open, onOpenChange, elements, events, floatingId, refs]);
|
|
2138
|
+
}
|
|
2139
|
+
|
|
2140
|
+
/**
|
|
2141
|
+
* Provides data to position a floating element and context to add interactions.
|
|
2142
|
+
* @see https://floating-ui.com/docs/useFloating
|
|
2143
|
+
*/
|
|
2144
|
+
function useFloating(options) {
|
|
2145
|
+
if (options === void 0) {
|
|
2146
|
+
options = {};
|
|
2147
|
+
}
|
|
2148
|
+
const {
|
|
2149
|
+
nodeId
|
|
2150
|
+
} = options;
|
|
2151
|
+
const internalRootContext = useFloatingRootContext({
|
|
2152
|
+
...options,
|
|
2153
|
+
elements: {
|
|
2154
|
+
reference: null,
|
|
2155
|
+
floating: null,
|
|
2156
|
+
...options.elements
|
|
2157
|
+
}
|
|
2158
|
+
});
|
|
2159
|
+
const rootContext = options.rootContext || internalRootContext;
|
|
2160
|
+
const computedElements = rootContext.elements;
|
|
2161
|
+
const [_domReference, setDomReference] = React.useState(null);
|
|
2162
|
+
const [positionReference, _setPositionReference] = React.useState(null);
|
|
2163
|
+
const optionDomReference = computedElements == null ? void 0 : computedElements.domReference;
|
|
2164
|
+
const domReference = optionDomReference || _domReference;
|
|
2165
|
+
const domReferenceRef = React.useRef(null);
|
|
2166
|
+
const tree = useFloatingTree();
|
|
2167
|
+
index(() => {
|
|
2168
|
+
if (domReference) {
|
|
2169
|
+
domReferenceRef.current = domReference;
|
|
2170
|
+
}
|
|
2171
|
+
}, [domReference]);
|
|
2172
|
+
const position = useFloating$1({
|
|
2173
|
+
...options,
|
|
2174
|
+
elements: {
|
|
2175
|
+
...computedElements,
|
|
2176
|
+
...(positionReference && {
|
|
2177
|
+
reference: positionReference
|
|
2178
|
+
})
|
|
2179
|
+
}
|
|
2180
|
+
});
|
|
2181
|
+
const setPositionReference = React.useCallback(node => {
|
|
2182
|
+
const computedPositionReference = isElement(node) ? {
|
|
2183
|
+
getBoundingClientRect: () => node.getBoundingClientRect(),
|
|
2184
|
+
contextElement: node
|
|
2185
|
+
} : node;
|
|
2186
|
+
// Store the positionReference in state if the DOM reference is specified externally via the
|
|
2187
|
+
// `elements.reference` option. This ensures that it won't be overridden on future renders.
|
|
2188
|
+
_setPositionReference(computedPositionReference);
|
|
2189
|
+
position.refs.setReference(computedPositionReference);
|
|
2190
|
+
}, [position.refs]);
|
|
2191
|
+
const setReference = React.useCallback(node => {
|
|
2192
|
+
if (isElement(node) || node === null) {
|
|
2193
|
+
domReferenceRef.current = node;
|
|
2194
|
+
setDomReference(node);
|
|
2195
|
+
}
|
|
2196
|
+
|
|
2197
|
+
// Backwards-compatibility for passing a virtual element to `reference`
|
|
2198
|
+
// after it has set the DOM reference.
|
|
2199
|
+
if (isElement(position.refs.reference.current) || position.refs.reference.current === null ||
|
|
2200
|
+
// Don't allow setting virtual elements using the old technique back to
|
|
2201
|
+
// `null` to support `positionReference` + an unstable `reference`
|
|
2202
|
+
// callback ref.
|
|
2203
|
+
node !== null && !isElement(node)) {
|
|
2204
|
+
position.refs.setReference(node);
|
|
2205
|
+
}
|
|
2206
|
+
}, [position.refs]);
|
|
2207
|
+
const refs = React.useMemo(() => ({
|
|
2208
|
+
...position.refs,
|
|
2209
|
+
setReference,
|
|
2210
|
+
setPositionReference,
|
|
2211
|
+
domReference: domReferenceRef
|
|
2212
|
+
}), [position.refs, setReference, setPositionReference]);
|
|
2213
|
+
const elements = React.useMemo(() => ({
|
|
2214
|
+
...position.elements,
|
|
2215
|
+
domReference: domReference
|
|
2216
|
+
}), [position.elements, domReference]);
|
|
2217
|
+
const context = React.useMemo(() => ({
|
|
2218
|
+
...position,
|
|
2219
|
+
...rootContext,
|
|
2220
|
+
refs,
|
|
2221
|
+
elements,
|
|
2222
|
+
nodeId
|
|
2223
|
+
}), [position, refs, elements, nodeId, rootContext]);
|
|
2224
|
+
index(() => {
|
|
2225
|
+
rootContext.dataRef.current.floatingContext = context;
|
|
2226
|
+
const node = tree == null ? void 0 : tree.nodesRef.current.find(node => node.id === nodeId);
|
|
2227
|
+
if (node) {
|
|
2228
|
+
node.context = context;
|
|
2229
|
+
}
|
|
2230
|
+
});
|
|
2231
|
+
return React.useMemo(() => ({
|
|
2232
|
+
...position,
|
|
2233
|
+
context,
|
|
2234
|
+
refs,
|
|
2235
|
+
elements
|
|
2236
|
+
}), [position, refs, elements, context]);
|
|
2237
|
+
}
|
|
2238
|
+
|
|
2239
|
+
const ACTIVE_KEY = 'active';
|
|
2240
|
+
const SELECTED_KEY = 'selected';
|
|
2241
|
+
function mergeProps(userProps, propsList, elementKey) {
|
|
2242
|
+
const map = new Map();
|
|
2243
|
+
const isItem = elementKey === 'item';
|
|
2244
|
+
let domUserProps = userProps;
|
|
2245
|
+
if (isItem && userProps) {
|
|
2246
|
+
const {
|
|
2247
|
+
[ACTIVE_KEY]: _,
|
|
2248
|
+
[SELECTED_KEY]: __,
|
|
2249
|
+
...validProps
|
|
2250
|
+
} = userProps;
|
|
2251
|
+
domUserProps = validProps;
|
|
2252
|
+
}
|
|
2253
|
+
return {
|
|
2254
|
+
...(elementKey === 'floating' && {
|
|
2255
|
+
tabIndex: -1,
|
|
2256
|
+
[FOCUSABLE_ATTRIBUTE]: ''
|
|
2257
|
+
}),
|
|
2258
|
+
...domUserProps,
|
|
2259
|
+
...propsList.map(value => {
|
|
2260
|
+
const propsOrGetProps = value ? value[elementKey] : null;
|
|
2261
|
+
if (typeof propsOrGetProps === 'function') {
|
|
2262
|
+
return userProps ? propsOrGetProps(userProps) : null;
|
|
2263
|
+
}
|
|
2264
|
+
return propsOrGetProps;
|
|
2265
|
+
}).concat(userProps).reduce((acc, props) => {
|
|
2266
|
+
if (!props) {
|
|
2267
|
+
return acc;
|
|
2268
|
+
}
|
|
2269
|
+
Object.entries(props).forEach(_ref => {
|
|
2270
|
+
let [key, value] = _ref;
|
|
2271
|
+
if (isItem && [ACTIVE_KEY, SELECTED_KEY].includes(key)) {
|
|
2272
|
+
return;
|
|
2273
|
+
}
|
|
2274
|
+
if (key.indexOf('on') === 0) {
|
|
2275
|
+
if (!map.has(key)) {
|
|
2276
|
+
map.set(key, []);
|
|
2277
|
+
}
|
|
2278
|
+
if (typeof value === 'function') {
|
|
2279
|
+
var _map$get;
|
|
2280
|
+
(_map$get = map.get(key)) == null || _map$get.push(value);
|
|
2281
|
+
acc[key] = function () {
|
|
2282
|
+
var _map$get2;
|
|
2283
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
2284
|
+
args[_key] = arguments[_key];
|
|
2285
|
+
}
|
|
2286
|
+
return (_map$get2 = map.get(key)) == null ? void 0 : _map$get2.map(fn => fn(...args)).find(val => val !== undefined);
|
|
2287
|
+
};
|
|
2288
|
+
}
|
|
2289
|
+
} else {
|
|
2290
|
+
acc[key] = value;
|
|
2291
|
+
}
|
|
2292
|
+
});
|
|
2293
|
+
return acc;
|
|
2294
|
+
}, {})
|
|
2295
|
+
};
|
|
2296
|
+
}
|
|
2297
|
+
/**
|
|
2298
|
+
* Merges an array of interaction hooks' props into prop getters, allowing
|
|
2299
|
+
* event handler functions to be composed together without overwriting one
|
|
2300
|
+
* another.
|
|
2301
|
+
* @see https://floating-ui.com/docs/useInteractions
|
|
2302
|
+
*/
|
|
2303
|
+
function useInteractions(propsList) {
|
|
2304
|
+
if (propsList === void 0) {
|
|
2305
|
+
propsList = [];
|
|
2306
|
+
}
|
|
2307
|
+
const referenceDeps = propsList.map(key => key == null ? void 0 : key.reference);
|
|
2308
|
+
const floatingDeps = propsList.map(key => key == null ? void 0 : key.floating);
|
|
2309
|
+
const itemDeps = propsList.map(key => key == null ? void 0 : key.item);
|
|
2310
|
+
const getReferenceProps = React.useCallback(userProps => mergeProps(userProps, propsList, 'reference'),
|
|
2311
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2312
|
+
referenceDeps);
|
|
2313
|
+
const getFloatingProps = React.useCallback(userProps => mergeProps(userProps, propsList, 'floating'),
|
|
2314
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2315
|
+
floatingDeps);
|
|
2316
|
+
const getItemProps = React.useCallback(userProps => mergeProps(userProps, propsList, 'item'),
|
|
2317
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2318
|
+
itemDeps);
|
|
2319
|
+
return React.useMemo(() => ({
|
|
2320
|
+
getReferenceProps,
|
|
2321
|
+
getFloatingProps,
|
|
2322
|
+
getItemProps
|
|
2323
|
+
}), [getReferenceProps, getFloatingProps, getItemProps]);
|
|
2324
|
+
}
|
|
2325
|
+
|
|
2326
|
+
// Converts a JS style key like `backgroundColor` to a CSS transition-property
|
|
2327
|
+
// like `background-color`.
|
|
2328
|
+
const camelCaseToKebabCase = str => str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? '-' : '') + $.toLowerCase());
|
|
2329
|
+
function execWithArgsOrReturn(valueOrFn, args) {
|
|
2330
|
+
return typeof valueOrFn === 'function' ? valueOrFn(args) : valueOrFn;
|
|
2331
|
+
}
|
|
2332
|
+
function useDelayUnmount(open, durationMs) {
|
|
2333
|
+
const [isMounted, setIsMounted] = React.useState(open);
|
|
2334
|
+
if (open && !isMounted) {
|
|
2335
|
+
setIsMounted(true);
|
|
2336
|
+
}
|
|
2337
|
+
React.useEffect(() => {
|
|
2338
|
+
if (!open && isMounted) {
|
|
2339
|
+
const timeout = setTimeout(() => setIsMounted(false), durationMs);
|
|
2340
|
+
return () => clearTimeout(timeout);
|
|
2341
|
+
}
|
|
2342
|
+
}, [open, isMounted, durationMs]);
|
|
2343
|
+
return isMounted;
|
|
2344
|
+
}
|
|
2345
|
+
/**
|
|
2346
|
+
* Provides a status string to apply CSS transitions to a floating element,
|
|
2347
|
+
* correctly handling placement-aware transitions.
|
|
2348
|
+
* @see https://floating-ui.com/docs/useTransition#usetransitionstatus
|
|
2349
|
+
*/
|
|
2350
|
+
function useTransitionStatus(context, props) {
|
|
2351
|
+
if (props === void 0) {
|
|
2352
|
+
props = {};
|
|
2353
|
+
}
|
|
2354
|
+
const {
|
|
2355
|
+
open,
|
|
2356
|
+
elements: {
|
|
2357
|
+
floating
|
|
2358
|
+
}
|
|
2359
|
+
} = context;
|
|
2360
|
+
const {
|
|
2361
|
+
duration = 250
|
|
2362
|
+
} = props;
|
|
2363
|
+
const isNumberDuration = typeof duration === 'number';
|
|
2364
|
+
const closeDuration = (isNumberDuration ? duration : duration.close) || 0;
|
|
2365
|
+
const [status, setStatus] = React.useState('unmounted');
|
|
2366
|
+
const isMounted = useDelayUnmount(open, closeDuration);
|
|
2367
|
+
if (!isMounted && status === 'close') {
|
|
2368
|
+
setStatus('unmounted');
|
|
2369
|
+
}
|
|
2370
|
+
index(() => {
|
|
2371
|
+
if (!floating) return;
|
|
2372
|
+
if (open) {
|
|
2373
|
+
setStatus('initial');
|
|
2374
|
+
const frame = requestAnimationFrame(() => {
|
|
2375
|
+
setStatus('open');
|
|
2376
|
+
});
|
|
2377
|
+
return () => {
|
|
2378
|
+
cancelAnimationFrame(frame);
|
|
2379
|
+
};
|
|
2380
|
+
}
|
|
2381
|
+
setStatus('close');
|
|
2382
|
+
}, [open, floating]);
|
|
2383
|
+
return {
|
|
2384
|
+
isMounted,
|
|
2385
|
+
status
|
|
2386
|
+
};
|
|
2387
|
+
}
|
|
2388
|
+
/**
|
|
2389
|
+
* Provides styles to apply CSS transitions to a floating element, correctly
|
|
2390
|
+
* handling placement-aware transitions. Wrapper around `useTransitionStatus`.
|
|
2391
|
+
* @see https://floating-ui.com/docs/useTransition#usetransitionstyles
|
|
2392
|
+
*/
|
|
2393
|
+
function useTransitionStyles(context, props) {
|
|
2394
|
+
if (props === void 0) {
|
|
2395
|
+
props = {};
|
|
2396
|
+
}
|
|
2397
|
+
const {
|
|
2398
|
+
initial: unstable_initial = {
|
|
2399
|
+
opacity: 0
|
|
2400
|
+
},
|
|
2401
|
+
open: unstable_open,
|
|
2402
|
+
close: unstable_close,
|
|
2403
|
+
common: unstable_common,
|
|
2404
|
+
duration = 250
|
|
2405
|
+
} = props;
|
|
2406
|
+
const placement = context.placement;
|
|
2407
|
+
const side = placement.split('-')[0];
|
|
2408
|
+
const fnArgs = React.useMemo(() => ({
|
|
2409
|
+
side,
|
|
2410
|
+
placement
|
|
2411
|
+
}), [side, placement]);
|
|
2412
|
+
const isNumberDuration = typeof duration === 'number';
|
|
2413
|
+
const openDuration = (isNumberDuration ? duration : duration.open) || 0;
|
|
2414
|
+
const closeDuration = (isNumberDuration ? duration : duration.close) || 0;
|
|
2415
|
+
const [styles, setStyles] = React.useState(() => ({
|
|
2416
|
+
...execWithArgsOrReturn(unstable_common, fnArgs),
|
|
2417
|
+
...execWithArgsOrReturn(unstable_initial, fnArgs)
|
|
2418
|
+
}));
|
|
2419
|
+
const {
|
|
2420
|
+
isMounted,
|
|
2421
|
+
status
|
|
2422
|
+
} = useTransitionStatus(context, {
|
|
2423
|
+
duration
|
|
2424
|
+
});
|
|
2425
|
+
const initialRef = useLatestRef(unstable_initial);
|
|
2426
|
+
const openRef = useLatestRef(unstable_open);
|
|
2427
|
+
const closeRef = useLatestRef(unstable_close);
|
|
2428
|
+
const commonRef = useLatestRef(unstable_common);
|
|
2429
|
+
index(() => {
|
|
2430
|
+
const initialStyles = execWithArgsOrReturn(initialRef.current, fnArgs);
|
|
2431
|
+
const closeStyles = execWithArgsOrReturn(closeRef.current, fnArgs);
|
|
2432
|
+
const commonStyles = execWithArgsOrReturn(commonRef.current, fnArgs);
|
|
2433
|
+
const openStyles = execWithArgsOrReturn(openRef.current, fnArgs) || Object.keys(initialStyles).reduce((acc, key) => {
|
|
2434
|
+
acc[key] = '';
|
|
2435
|
+
return acc;
|
|
2436
|
+
}, {});
|
|
2437
|
+
if (status === 'initial') {
|
|
2438
|
+
setStyles(styles => ({
|
|
2439
|
+
transitionProperty: styles.transitionProperty,
|
|
2440
|
+
...commonStyles,
|
|
2441
|
+
...initialStyles
|
|
2442
|
+
}));
|
|
2443
|
+
}
|
|
2444
|
+
if (status === 'open') {
|
|
2445
|
+
setStyles({
|
|
2446
|
+
transitionProperty: Object.keys(openStyles).map(camelCaseToKebabCase).join(','),
|
|
2447
|
+
transitionDuration: openDuration + "ms",
|
|
2448
|
+
...commonStyles,
|
|
2449
|
+
...openStyles
|
|
2450
|
+
});
|
|
2451
|
+
}
|
|
2452
|
+
if (status === 'close') {
|
|
2453
|
+
const styles = closeStyles || initialStyles;
|
|
2454
|
+
setStyles({
|
|
2455
|
+
transitionProperty: Object.keys(styles).map(camelCaseToKebabCase).join(','),
|
|
2456
|
+
transitionDuration: closeDuration + "ms",
|
|
2457
|
+
...commonStyles,
|
|
2458
|
+
...styles
|
|
2459
|
+
});
|
|
2460
|
+
}
|
|
2461
|
+
}, [closeDuration, closeRef, initialRef, openRef, commonRef, openDuration, status, fnArgs]);
|
|
2462
|
+
return {
|
|
2463
|
+
isMounted,
|
|
2464
|
+
styles
|
|
2465
|
+
};
|
|
2466
|
+
}
|
|
2467
|
+
|
|
2468
|
+
export { FloatingPortal as F, useTransitionStyles as a, useMergeRefs as b, useDismiss as c, useInteractions as d, FloatingFocusManager as e, useId as f, FloatingOverlay as g, useFloating as u };
|
|
2469
|
+
//# sourceMappingURL=floating-ui.react-77cadca5.js.map
|