@react-aria/focus 3.0.0-nightly.2843 → 3.0.0-nightly.2848

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/FocusRing.main.js +44 -0
  2. package/dist/FocusRing.main.js.map +1 -0
  3. package/dist/FocusRing.mjs +35 -0
  4. package/dist/FocusRing.module.js +35 -0
  5. package/dist/FocusRing.module.js.map +1 -0
  6. package/dist/FocusScope.main.js +724 -0
  7. package/dist/FocusScope.main.js.map +1 -0
  8. package/dist/FocusScope.mjs +711 -0
  9. package/dist/FocusScope.module.js +711 -0
  10. package/dist/FocusScope.module.js.map +1 -0
  11. package/dist/focusSafely.main.js +39 -0
  12. package/dist/focusSafely.main.js.map +1 -0
  13. package/dist/focusSafely.mjs +34 -0
  14. package/dist/focusSafely.module.js +34 -0
  15. package/dist/focusSafely.module.js.map +1 -0
  16. package/dist/import.mjs +6 -939
  17. package/dist/isElementVisible.main.js +41 -0
  18. package/dist/isElementVisible.main.js.map +1 -0
  19. package/dist/isElementVisible.mjs +36 -0
  20. package/dist/isElementVisible.module.js +36 -0
  21. package/dist/isElementVisible.module.js.map +1 -0
  22. package/dist/main.js +17 -954
  23. package/dist/main.js.map +1 -1
  24. package/dist/module.js +6 -939
  25. package/dist/module.js.map +1 -1
  26. package/dist/useFocusRing.main.js +50 -0
  27. package/dist/useFocusRing.main.js.map +1 -0
  28. package/dist/useFocusRing.mjs +45 -0
  29. package/dist/useFocusRing.module.js +45 -0
  30. package/dist/useFocusRing.module.js.map +1 -0
  31. package/dist/useFocusable.main.js +75 -0
  32. package/dist/useFocusable.main.js.map +1 -0
  33. package/dist/useFocusable.mjs +65 -0
  34. package/dist/useFocusable.module.js +65 -0
  35. package/dist/useFocusable.module.js.map +1 -0
  36. package/dist/useHasTabbableChild.main.js +62 -0
  37. package/dist/useHasTabbableChild.main.js.map +1 -0
  38. package/dist/useHasTabbableChild.mjs +57 -0
  39. package/dist/useHasTabbableChild.module.js +57 -0
  40. package/dist/useHasTabbableChild.module.js.map +1 -0
  41. package/package.json +5 -5
@@ -0,0 +1,724 @@
1
+ var $1c7f9157d722357d$exports = require("./focusSafely.main.js");
2
+ var $d5156037ad898a4d$exports = require("./isElementVisible.main.js");
3
+ var $euGna$reactariautils = require("@react-aria/utils");
4
+ var $euGna$react = require("react");
5
+
6
+
7
+ function $parcel$interopDefault(a) {
8
+ return a && a.__esModule ? a.default : a;
9
+ }
10
+
11
+ function $parcel$export(e, n, v, s) {
12
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
13
+ }
14
+
15
+ $parcel$export(module.exports, "FocusScope", () => $a7a032acae3ddda9$export$20e40289641fbbb6);
16
+ $parcel$export(module.exports, "useFocusManager", () => $a7a032acae3ddda9$export$10c5169755ce7bd7);
17
+ $parcel$export(module.exports, "getFocusableTreeWalker", () => $a7a032acae3ddda9$export$2d6ec8fc375ceafa);
18
+ $parcel$export(module.exports, "isElementInChildOfActiveScope", () => $a7a032acae3ddda9$export$1258395f99bf9cbf);
19
+ $parcel$export(module.exports, "createFocusManager", () => $a7a032acae3ddda9$export$c5251b9e124bf29);
20
+ /*
21
+ * Copyright 2020 Adobe. All rights reserved.
22
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
23
+ * you may not use this file except in compliance with the License. You may obtain a copy
24
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
25
+ *
26
+ * Unless required by applicable law or agreed to in writing, software distributed under
27
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
28
+ * OF ANY KIND, either express or implied. See the License for the specific language
29
+ * governing permissions and limitations under the License.
30
+ */
31
+
32
+
33
+
34
+ const $a7a032acae3ddda9$var$FocusContext = /*#__PURE__*/ (0, ($parcel$interopDefault($euGna$react))).createContext(null);
35
+ let $a7a032acae3ddda9$var$activeScope = null;
36
+ function $a7a032acae3ddda9$export$20e40289641fbbb6(props) {
37
+ let { children: children, contain: contain, restoreFocus: restoreFocus, autoFocus: autoFocus } = props;
38
+ let startRef = (0, $euGna$react.useRef)(null);
39
+ let endRef = (0, $euGna$react.useRef)(null);
40
+ let scopeRef = (0, $euGna$react.useRef)([]);
41
+ let { parentNode: parentNode } = (0, $euGna$react.useContext)($a7a032acae3ddda9$var$FocusContext) || {};
42
+ // Create a tree node here so we can add children to it even before it is added to the tree.
43
+ let node = (0, $euGna$react.useMemo)(()=>new $a7a032acae3ddda9$var$TreeNode({
44
+ scopeRef: scopeRef
45
+ }), [
46
+ scopeRef
47
+ ]);
48
+ (0, $euGna$reactariautils.useLayoutEffect)(()=>{
49
+ // If a new scope mounts outside the active scope, (e.g. DialogContainer launched from a menu),
50
+ // use the active scope as the parent instead of the parent from context. Layout effects run bottom
51
+ // up, so if the parent is not yet added to the tree, don't do this. Only the outer-most FocusScope
52
+ // that is being added should get the activeScope as its parent.
53
+ let parent = parentNode || $a7a032acae3ddda9$export$d06fae2ee68b101e.root;
54
+ if ($a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(parent.scopeRef) && $a7a032acae3ddda9$var$activeScope && !$a7a032acae3ddda9$var$isAncestorScope($a7a032acae3ddda9$var$activeScope, parent.scopeRef)) {
55
+ let activeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode($a7a032acae3ddda9$var$activeScope);
56
+ if (activeNode) parent = activeNode;
57
+ }
58
+ // Add the node to the parent, and to the tree.
59
+ parent.addChild(node);
60
+ $a7a032acae3ddda9$export$d06fae2ee68b101e.addNode(node);
61
+ }, [
62
+ node,
63
+ parentNode
64
+ ]);
65
+ (0, $euGna$reactariautils.useLayoutEffect)(()=>{
66
+ let node = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
67
+ if (node) node.contain = !!contain;
68
+ }, [
69
+ contain
70
+ ]);
71
+ (0, $euGna$reactariautils.useLayoutEffect)(()=>{
72
+ var _startRef_current;
73
+ // Find all rendered nodes between the sentinels and add them to the scope.
74
+ let node = (_startRef_current = startRef.current) === null || _startRef_current === void 0 ? void 0 : _startRef_current.nextSibling;
75
+ let nodes = [];
76
+ while(node && node !== endRef.current){
77
+ nodes.push(node);
78
+ node = node.nextSibling;
79
+ }
80
+ scopeRef.current = nodes;
81
+ }, [
82
+ children
83
+ ]);
84
+ $a7a032acae3ddda9$var$useActiveScopeTracker(scopeRef, restoreFocus, contain);
85
+ $a7a032acae3ddda9$var$useFocusContainment(scopeRef, contain);
86
+ $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain);
87
+ $a7a032acae3ddda9$var$useAutoFocus(scopeRef, autoFocus);
88
+ // This needs to be an effect so that activeScope is updated after the FocusScope tree is complete.
89
+ // It cannot be a useLayoutEffect because the parent of this node hasn't been attached in the tree yet.
90
+ (0, $euGna$react.useEffect)(()=>{
91
+ const activeElement = (0, $euGna$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined).activeElement;
92
+ let scope = null;
93
+ if ($a7a032acae3ddda9$var$isElementInScope(activeElement, scopeRef.current)) {
94
+ // We need to traverse the focusScope tree and find the bottom most scope that
95
+ // contains the active element and set that as the activeScope.
96
+ for (let node of $a7a032acae3ddda9$export$d06fae2ee68b101e.traverse())if (node.scopeRef && $a7a032acae3ddda9$var$isElementInScope(activeElement, node.scopeRef.current)) scope = node;
97
+ if (scope === $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef)) $a7a032acae3ddda9$var$activeScope = scope.scopeRef;
98
+ }
99
+ }, [
100
+ scopeRef
101
+ ]);
102
+ // This layout effect cleanup is so that the tree node is removed synchronously with react before the RAF
103
+ // in useRestoreFocus cleanup runs.
104
+ (0, $euGna$reactariautils.useLayoutEffect)(()=>{
105
+ return ()=>{
106
+ var _focusScopeTree_getTreeNode_parent, _focusScopeTree_getTreeNode;
107
+ var _focusScopeTree_getTreeNode_parent_scopeRef;
108
+ // Scope may have been re-parented.
109
+ let parentScope = (_focusScopeTree_getTreeNode_parent_scopeRef = (_focusScopeTree_getTreeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef)) === null || _focusScopeTree_getTreeNode === void 0 ? void 0 : (_focusScopeTree_getTreeNode_parent = _focusScopeTree_getTreeNode.parent) === null || _focusScopeTree_getTreeNode_parent === void 0 ? void 0 : _focusScopeTree_getTreeNode_parent.scopeRef) !== null && _focusScopeTree_getTreeNode_parent_scopeRef !== void 0 ? _focusScopeTree_getTreeNode_parent_scopeRef : null;
110
+ if ((scopeRef === $a7a032acae3ddda9$var$activeScope || $a7a032acae3ddda9$var$isAncestorScope(scopeRef, $a7a032acae3ddda9$var$activeScope)) && (!parentScope || $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(parentScope))) $a7a032acae3ddda9$var$activeScope = parentScope;
111
+ $a7a032acae3ddda9$export$d06fae2ee68b101e.removeTreeNode(scopeRef);
112
+ };
113
+ }, [
114
+ scopeRef
115
+ ]);
116
+ let focusManager = (0, $euGna$react.useMemo)(()=>$a7a032acae3ddda9$var$createFocusManagerForScope(scopeRef), []);
117
+ let value = (0, $euGna$react.useMemo)(()=>({
118
+ focusManager: focusManager,
119
+ parentNode: node
120
+ }), [
121
+ node,
122
+ focusManager
123
+ ]);
124
+ return /*#__PURE__*/ (0, ($parcel$interopDefault($euGna$react))).createElement($a7a032acae3ddda9$var$FocusContext.Provider, {
125
+ value: value
126
+ }, /*#__PURE__*/ (0, ($parcel$interopDefault($euGna$react))).createElement("span", {
127
+ "data-focus-scope-start": true,
128
+ hidden: true,
129
+ ref: startRef
130
+ }), children, /*#__PURE__*/ (0, ($parcel$interopDefault($euGna$react))).createElement("span", {
131
+ "data-focus-scope-end": true,
132
+ hidden: true,
133
+ ref: endRef
134
+ }));
135
+ }
136
+ function $a7a032acae3ddda9$export$10c5169755ce7bd7() {
137
+ var _useContext;
138
+ return (_useContext = (0, $euGna$react.useContext)($a7a032acae3ddda9$var$FocusContext)) === null || _useContext === void 0 ? void 0 : _useContext.focusManager;
139
+ }
140
+ function $a7a032acae3ddda9$var$createFocusManagerForScope(scopeRef) {
141
+ return {
142
+ focusNext (opts = {}) {
143
+ let scope = scopeRef.current;
144
+ let { from: from, tabbable: tabbable, wrap: wrap, accept: accept } = opts;
145
+ let node = from || (0, $euGna$reactariautils.getOwnerDocument)(scope[0]).activeElement;
146
+ let sentinel = scope[0].previousElementSibling;
147
+ let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
148
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
149
+ tabbable: tabbable,
150
+ accept: accept
151
+ }, scope);
152
+ walker.currentNode = $a7a032acae3ddda9$var$isElementInScope(node, scope) ? node : sentinel;
153
+ let nextNode = walker.nextNode();
154
+ if (!nextNode && wrap) {
155
+ walker.currentNode = sentinel;
156
+ nextNode = walker.nextNode();
157
+ }
158
+ if (nextNode) $a7a032acae3ddda9$var$focusElement(nextNode, true);
159
+ return nextNode;
160
+ },
161
+ focusPrevious (opts = {}) {
162
+ let scope = scopeRef.current;
163
+ let { from: from, tabbable: tabbable, wrap: wrap, accept: accept } = opts;
164
+ let node = from || (0, $euGna$reactariautils.getOwnerDocument)(scope[0]).activeElement;
165
+ let sentinel = scope[scope.length - 1].nextElementSibling;
166
+ let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
167
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
168
+ tabbable: tabbable,
169
+ accept: accept
170
+ }, scope);
171
+ walker.currentNode = $a7a032acae3ddda9$var$isElementInScope(node, scope) ? node : sentinel;
172
+ let previousNode = walker.previousNode();
173
+ if (!previousNode && wrap) {
174
+ walker.currentNode = sentinel;
175
+ previousNode = walker.previousNode();
176
+ }
177
+ if (previousNode) $a7a032acae3ddda9$var$focusElement(previousNode, true);
178
+ return previousNode;
179
+ },
180
+ focusFirst (opts = {}) {
181
+ let scope = scopeRef.current;
182
+ let { tabbable: tabbable, accept: accept } = opts;
183
+ let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
184
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
185
+ tabbable: tabbable,
186
+ accept: accept
187
+ }, scope);
188
+ walker.currentNode = scope[0].previousElementSibling;
189
+ let nextNode = walker.nextNode();
190
+ if (nextNode) $a7a032acae3ddda9$var$focusElement(nextNode, true);
191
+ return nextNode;
192
+ },
193
+ focusLast (opts = {}) {
194
+ let scope = scopeRef.current;
195
+ let { tabbable: tabbable, accept: accept } = opts;
196
+ let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
197
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
198
+ tabbable: tabbable,
199
+ accept: accept
200
+ }, scope);
201
+ walker.currentNode = scope[scope.length - 1].nextElementSibling;
202
+ let previousNode = walker.previousNode();
203
+ if (previousNode) $a7a032acae3ddda9$var$focusElement(previousNode, true);
204
+ return previousNode;
205
+ }
206
+ };
207
+ }
208
+ const $a7a032acae3ddda9$var$focusableElements = [
209
+ "input:not([disabled]):not([type=hidden])",
210
+ "select:not([disabled])",
211
+ "textarea:not([disabled])",
212
+ "button:not([disabled])",
213
+ "a[href]",
214
+ "area[href]",
215
+ "summary",
216
+ "iframe",
217
+ "object",
218
+ "embed",
219
+ "audio[controls]",
220
+ "video[controls]",
221
+ "[contenteditable]"
222
+ ];
223
+ const $a7a032acae3ddda9$var$FOCUSABLE_ELEMENT_SELECTOR = $a7a032acae3ddda9$var$focusableElements.join(":not([hidden]),") + ",[tabindex]:not([disabled]):not([hidden])";
224
+ $a7a032acae3ddda9$var$focusableElements.push('[tabindex]:not([tabindex="-1"]):not([disabled])');
225
+ const $a7a032acae3ddda9$var$TABBABLE_ELEMENT_SELECTOR = $a7a032acae3ddda9$var$focusableElements.join(':not([hidden]):not([tabindex="-1"]),');
226
+ function $a7a032acae3ddda9$var$getScopeRoot(scope) {
227
+ return scope[0].parentElement;
228
+ }
229
+ function $a7a032acae3ddda9$var$shouldContainFocus(scopeRef) {
230
+ let scope = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode($a7a032acae3ddda9$var$activeScope);
231
+ while(scope && scope.scopeRef !== scopeRef){
232
+ if (scope.contain) return false;
233
+ scope = scope.parent;
234
+ }
235
+ return true;
236
+ }
237
+ function $a7a032acae3ddda9$var$useFocusContainment(scopeRef, contain) {
238
+ let focusedNode = (0, $euGna$react.useRef)();
239
+ let raf = (0, $euGna$react.useRef)();
240
+ (0, $euGna$reactariautils.useLayoutEffect)(()=>{
241
+ let scope = scopeRef.current;
242
+ if (!contain) {
243
+ // if contain was changed, then we should cancel any ongoing waits to pull focus back into containment
244
+ if (raf.current) {
245
+ cancelAnimationFrame(raf.current);
246
+ raf.current = undefined;
247
+ }
248
+ return;
249
+ }
250
+ const ownerDocument = (0, $euGna$reactariautils.getOwnerDocument)(scope ? scope[0] : undefined);
251
+ // Handle the Tab key to contain focus within the scope
252
+ let onKeyDown = (e)=>{
253
+ if (e.key !== "Tab" || e.altKey || e.ctrlKey || e.metaKey || !$a7a032acae3ddda9$var$shouldContainFocus(scopeRef) || e.isComposing) return;
254
+ let focusedElement = ownerDocument.activeElement;
255
+ let scope = scopeRef.current;
256
+ if (!scope || !$a7a032acae3ddda9$var$isElementInScope(focusedElement, scope)) return;
257
+ let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
258
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
259
+ tabbable: true
260
+ }, scope);
261
+ if (!focusedElement) return;
262
+ walker.currentNode = focusedElement;
263
+ let nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
264
+ if (!nextElement) {
265
+ walker.currentNode = e.shiftKey ? scope[scope.length - 1].nextElementSibling : scope[0].previousElementSibling;
266
+ nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
267
+ }
268
+ e.preventDefault();
269
+ if (nextElement) $a7a032acae3ddda9$var$focusElement(nextElement, true);
270
+ };
271
+ let onFocus = (e)=>{
272
+ // If focusing an element in a child scope of the currently active scope, the child becomes active.
273
+ // Moving out of the active scope to an ancestor is not allowed.
274
+ if ((!$a7a032acae3ddda9$var$activeScope || $a7a032acae3ddda9$var$isAncestorScope($a7a032acae3ddda9$var$activeScope, scopeRef)) && $a7a032acae3ddda9$var$isElementInScope(e.target, scopeRef.current)) {
275
+ $a7a032acae3ddda9$var$activeScope = scopeRef;
276
+ focusedNode.current = e.target;
277
+ } else if ($a7a032acae3ddda9$var$shouldContainFocus(scopeRef) && !$a7a032acae3ddda9$var$isElementInChildScope(e.target, scopeRef)) {
278
+ // If a focus event occurs outside the active scope (e.g. user tabs from browser location bar),
279
+ // restore focus to the previously focused node or the first tabbable element in the active scope.
280
+ if (focusedNode.current) focusedNode.current.focus();
281
+ else if ($a7a032acae3ddda9$var$activeScope && $a7a032acae3ddda9$var$activeScope.current) $a7a032acae3ddda9$var$focusFirstInScope($a7a032acae3ddda9$var$activeScope.current);
282
+ } else if ($a7a032acae3ddda9$var$shouldContainFocus(scopeRef)) focusedNode.current = e.target;
283
+ };
284
+ let onBlur = (e)=>{
285
+ // Firefox doesn't shift focus back to the Dialog properly without this
286
+ if (raf.current) cancelAnimationFrame(raf.current);
287
+ raf.current = requestAnimationFrame(()=>{
288
+ // Use document.activeElement instead of e.relatedTarget so we can tell if user clicked into iframe
289
+ if (ownerDocument.activeElement && $a7a032acae3ddda9$var$shouldContainFocus(scopeRef) && !$a7a032acae3ddda9$var$isElementInChildScope(ownerDocument.activeElement, scopeRef)) {
290
+ $a7a032acae3ddda9$var$activeScope = scopeRef;
291
+ if (ownerDocument.body.contains(e.target)) {
292
+ var _focusedNode_current;
293
+ focusedNode.current = e.target;
294
+ (_focusedNode_current = focusedNode.current) === null || _focusedNode_current === void 0 ? void 0 : _focusedNode_current.focus();
295
+ } else if ($a7a032acae3ddda9$var$activeScope.current) $a7a032acae3ddda9$var$focusFirstInScope($a7a032acae3ddda9$var$activeScope.current);
296
+ }
297
+ });
298
+ };
299
+ ownerDocument.addEventListener("keydown", onKeyDown, false);
300
+ ownerDocument.addEventListener("focusin", onFocus, false);
301
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
302
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusout", onBlur, false));
303
+ return ()=>{
304
+ ownerDocument.removeEventListener("keydown", onKeyDown, false);
305
+ ownerDocument.removeEventListener("focusin", onFocus, false);
306
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
307
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusout", onBlur, false));
308
+ };
309
+ }, [
310
+ scopeRef,
311
+ contain
312
+ ]);
313
+ // This is a useLayoutEffect so it is guaranteed to run before our async synthetic blur
314
+ // eslint-disable-next-line arrow-body-style
315
+ (0, $euGna$reactariautils.useLayoutEffect)(()=>{
316
+ return ()=>{
317
+ if (raf.current) cancelAnimationFrame(raf.current);
318
+ };
319
+ }, [
320
+ raf
321
+ ]);
322
+ }
323
+ function $a7a032acae3ddda9$var$isElementInAnyScope(element) {
324
+ return $a7a032acae3ddda9$var$isElementInChildScope(element);
325
+ }
326
+ function $a7a032acae3ddda9$var$isElementInScope(element, scope) {
327
+ if (!element) return false;
328
+ if (!scope) return false;
329
+ return scope.some((node)=>node.contains(element));
330
+ }
331
+ function $a7a032acae3ddda9$var$isElementInChildScope(element, scope = null) {
332
+ // If the element is within a top layer element (e.g. toasts), always allow moving focus there.
333
+ if (element instanceof Element && element.closest("[data-react-aria-top-layer]")) return true;
334
+ // node.contains in isElementInScope covers child scopes that are also DOM children,
335
+ // but does not cover child scopes in portals.
336
+ for (let { scopeRef: s } of $a7a032acae3ddda9$export$d06fae2ee68b101e.traverse($a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scope))){
337
+ if (s && $a7a032acae3ddda9$var$isElementInScope(element, s.current)) return true;
338
+ }
339
+ return false;
340
+ }
341
+ function $a7a032acae3ddda9$export$1258395f99bf9cbf(element) {
342
+ return $a7a032acae3ddda9$var$isElementInChildScope(element, $a7a032acae3ddda9$var$activeScope);
343
+ }
344
+ function $a7a032acae3ddda9$var$isAncestorScope(ancestor, scope) {
345
+ var _focusScopeTree_getTreeNode;
346
+ let parent = (_focusScopeTree_getTreeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scope)) === null || _focusScopeTree_getTreeNode === void 0 ? void 0 : _focusScopeTree_getTreeNode.parent;
347
+ while(parent){
348
+ if (parent.scopeRef === ancestor) return true;
349
+ parent = parent.parent;
350
+ }
351
+ return false;
352
+ }
353
+ function $a7a032acae3ddda9$var$focusElement(element, scroll = false) {
354
+ if (element != null && !scroll) try {
355
+ (0, $1c7f9157d722357d$exports.focusSafely)(element);
356
+ } catch (err) {
357
+ // ignore
358
+ }
359
+ else if (element != null) try {
360
+ element.focus();
361
+ } catch (err) {
362
+ // ignore
363
+ }
364
+ }
365
+ function $a7a032acae3ddda9$var$focusFirstInScope(scope, tabbable = true) {
366
+ let sentinel = scope[0].previousElementSibling;
367
+ let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
368
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
369
+ tabbable: tabbable
370
+ }, scope);
371
+ walker.currentNode = sentinel;
372
+ let nextNode = walker.nextNode();
373
+ // If the scope does not contain a tabbable element, use the first focusable element.
374
+ if (tabbable && !nextNode) {
375
+ scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
376
+ walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
377
+ tabbable: false
378
+ }, scope);
379
+ walker.currentNode = sentinel;
380
+ nextNode = walker.nextNode();
381
+ }
382
+ $a7a032acae3ddda9$var$focusElement(nextNode);
383
+ }
384
+ function $a7a032acae3ddda9$var$useAutoFocus(scopeRef, autoFocus) {
385
+ const autoFocusRef = (0, ($parcel$interopDefault($euGna$react))).useRef(autoFocus);
386
+ (0, $euGna$react.useEffect)(()=>{
387
+ if (autoFocusRef.current) {
388
+ $a7a032acae3ddda9$var$activeScope = scopeRef;
389
+ const ownerDocument = (0, $euGna$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined);
390
+ if (!$a7a032acae3ddda9$var$isElementInScope(ownerDocument.activeElement, $a7a032acae3ddda9$var$activeScope.current) && scopeRef.current) $a7a032acae3ddda9$var$focusFirstInScope(scopeRef.current);
391
+ }
392
+ autoFocusRef.current = false;
393
+ }, [
394
+ scopeRef
395
+ ]);
396
+ }
397
+ function $a7a032acae3ddda9$var$useActiveScopeTracker(scopeRef, restore, contain) {
398
+ // tracks the active scope, in case restore and contain are both false.
399
+ // if either are true, this is tracked in useRestoreFocus or useFocusContainment.
400
+ (0, $euGna$reactariautils.useLayoutEffect)(()=>{
401
+ if (restore || contain) return;
402
+ let scope = scopeRef.current;
403
+ const ownerDocument = (0, $euGna$reactariautils.getOwnerDocument)(scope ? scope[0] : undefined);
404
+ let onFocus = (e)=>{
405
+ let target = e.target;
406
+ if ($a7a032acae3ddda9$var$isElementInScope(target, scopeRef.current)) $a7a032acae3ddda9$var$activeScope = scopeRef;
407
+ else if (!$a7a032acae3ddda9$var$isElementInAnyScope(target)) $a7a032acae3ddda9$var$activeScope = null;
408
+ };
409
+ ownerDocument.addEventListener("focusin", onFocus, false);
410
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
411
+ return ()=>{
412
+ ownerDocument.removeEventListener("focusin", onFocus, false);
413
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
414
+ };
415
+ }, [
416
+ scopeRef,
417
+ restore,
418
+ contain
419
+ ]);
420
+ }
421
+ function $a7a032acae3ddda9$var$shouldRestoreFocus(scopeRef) {
422
+ let scope = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode($a7a032acae3ddda9$var$activeScope);
423
+ while(scope && scope.scopeRef !== scopeRef){
424
+ if (scope.nodeToRestore) return false;
425
+ scope = scope.parent;
426
+ }
427
+ return (scope === null || scope === void 0 ? void 0 : scope.scopeRef) === scopeRef;
428
+ }
429
+ function $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain) {
430
+ // create a ref during render instead of useLayoutEffect so the active element is saved before a child with autoFocus=true mounts.
431
+ // eslint-disable-next-line no-restricted-globals
432
+ const nodeToRestoreRef = (0, $euGna$react.useRef)(typeof document !== "undefined" ? (0, $euGna$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined).activeElement : null);
433
+ // restoring scopes should all track if they are active regardless of contain, but contain already tracks it plus logic to contain the focus
434
+ // restoring-non-containing scopes should only care if they become active so they can perform the restore
435
+ (0, $euGna$reactariautils.useLayoutEffect)(()=>{
436
+ let scope = scopeRef.current;
437
+ const ownerDocument = (0, $euGna$reactariautils.getOwnerDocument)(scope ? scope[0] : undefined);
438
+ if (!restoreFocus || contain) return;
439
+ let onFocus = ()=>{
440
+ // If focusing an element in a child scope of the currently active scope, the child becomes active.
441
+ // Moving out of the active scope to an ancestor is not allowed.
442
+ if ((!$a7a032acae3ddda9$var$activeScope || $a7a032acae3ddda9$var$isAncestorScope($a7a032acae3ddda9$var$activeScope, scopeRef)) && $a7a032acae3ddda9$var$isElementInScope(ownerDocument.activeElement, scopeRef.current)) $a7a032acae3ddda9$var$activeScope = scopeRef;
443
+ };
444
+ ownerDocument.addEventListener("focusin", onFocus, false);
445
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
446
+ return ()=>{
447
+ ownerDocument.removeEventListener("focusin", onFocus, false);
448
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
449
+ };
450
+ // eslint-disable-next-line react-hooks/exhaustive-deps
451
+ }, [
452
+ scopeRef,
453
+ contain
454
+ ]);
455
+ (0, $euGna$reactariautils.useLayoutEffect)(()=>{
456
+ const ownerDocument = (0, $euGna$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined);
457
+ if (!restoreFocus) return;
458
+ // Handle the Tab key so that tabbing out of the scope goes to the next element
459
+ // after the node that had focus when the scope mounted. This is important when
460
+ // using portals for overlays, so that focus goes to the expected element when
461
+ // tabbing out of the overlay.
462
+ let onKeyDown = (e)=>{
463
+ if (e.key !== "Tab" || e.altKey || e.ctrlKey || e.metaKey || !$a7a032acae3ddda9$var$shouldContainFocus(scopeRef) || e.isComposing) return;
464
+ let focusedElement = ownerDocument.activeElement;
465
+ if (!$a7a032acae3ddda9$var$isElementInScope(focusedElement, scopeRef.current)) return;
466
+ let treeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
467
+ if (!treeNode) return;
468
+ let nodeToRestore = treeNode.nodeToRestore;
469
+ // Create a DOM tree walker that matches all tabbable elements
470
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(ownerDocument.body, {
471
+ tabbable: true
472
+ });
473
+ // Find the next tabbable element after the currently focused element
474
+ walker.currentNode = focusedElement;
475
+ let nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
476
+ if (!nodeToRestore || !ownerDocument.body.contains(nodeToRestore) || nodeToRestore === ownerDocument.body) {
477
+ nodeToRestore = undefined;
478
+ treeNode.nodeToRestore = undefined;
479
+ }
480
+ // If there is no next element, or it is outside the current scope, move focus to the
481
+ // next element after the node to restore to instead.
482
+ if ((!nextElement || !$a7a032acae3ddda9$var$isElementInScope(nextElement, scopeRef.current)) && nodeToRestore) {
483
+ walker.currentNode = nodeToRestore;
484
+ // Skip over elements within the scope, in case the scope immediately follows the node to restore.
485
+ do nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
486
+ while ($a7a032acae3ddda9$var$isElementInScope(nextElement, scopeRef.current));
487
+ e.preventDefault();
488
+ e.stopPropagation();
489
+ if (nextElement) $a7a032acae3ddda9$var$focusElement(nextElement, true);
490
+ else // If there is no next element and the nodeToRestore isn't within a FocusScope (i.e. we are leaving the top level focus scope)
491
+ // then move focus to the body.
492
+ // Otherwise restore focus to the nodeToRestore (e.g menu within a popover -> tabbing to close the menu should move focus to menu trigger)
493
+ if (!$a7a032acae3ddda9$var$isElementInAnyScope(nodeToRestore)) focusedElement.blur();
494
+ else $a7a032acae3ddda9$var$focusElement(nodeToRestore, true);
495
+ }
496
+ };
497
+ if (!contain) ownerDocument.addEventListener("keydown", onKeyDown, true);
498
+ return ()=>{
499
+ if (!contain) ownerDocument.removeEventListener("keydown", onKeyDown, true);
500
+ };
501
+ }, [
502
+ scopeRef,
503
+ restoreFocus,
504
+ contain
505
+ ]);
506
+ // useLayoutEffect instead of useEffect so the active element is saved synchronously instead of asynchronously.
507
+ (0, $euGna$reactariautils.useLayoutEffect)(()=>{
508
+ const ownerDocument = (0, $euGna$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined);
509
+ if (!restoreFocus) return;
510
+ let treeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
511
+ if (!treeNode) return;
512
+ var _nodeToRestoreRef_current;
513
+ treeNode.nodeToRestore = (_nodeToRestoreRef_current = nodeToRestoreRef.current) !== null && _nodeToRestoreRef_current !== void 0 ? _nodeToRestoreRef_current : undefined;
514
+ return ()=>{
515
+ let treeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
516
+ if (!treeNode) return;
517
+ let nodeToRestore = treeNode.nodeToRestore;
518
+ // if we already lost focus to the body and this was the active scope, then we should attempt to restore
519
+ if (restoreFocus && nodeToRestore && // eslint-disable-next-line react-hooks/exhaustive-deps
520
+ ($a7a032acae3ddda9$var$isElementInScope(ownerDocument.activeElement, scopeRef.current) || ownerDocument.activeElement === ownerDocument.body && $a7a032acae3ddda9$var$shouldRestoreFocus(scopeRef))) {
521
+ // freeze the focusScopeTree so it persists after the raf, otherwise during unmount nodes are removed from it
522
+ let clonedTree = $a7a032acae3ddda9$export$d06fae2ee68b101e.clone();
523
+ requestAnimationFrame(()=>{
524
+ // Only restore focus if we've lost focus to the body, the alternative is that focus has been purposefully moved elsewhere
525
+ if (ownerDocument.activeElement === ownerDocument.body) {
526
+ // look up the tree starting with our scope to find a nodeToRestore still in the DOM
527
+ let treeNode = clonedTree.getTreeNode(scopeRef);
528
+ while(treeNode){
529
+ if (treeNode.nodeToRestore && treeNode.nodeToRestore.isConnected) {
530
+ $a7a032acae3ddda9$var$focusElement(treeNode.nodeToRestore);
531
+ return;
532
+ }
533
+ treeNode = treeNode.parent;
534
+ }
535
+ // If no nodeToRestore was found, focus the first element in the nearest
536
+ // ancestor scope that is still in the tree.
537
+ treeNode = clonedTree.getTreeNode(scopeRef);
538
+ while(treeNode){
539
+ if (treeNode.scopeRef && treeNode.scopeRef.current && $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(treeNode.scopeRef)) {
540
+ $a7a032acae3ddda9$var$focusFirstInScope(treeNode.scopeRef.current, true);
541
+ return;
542
+ }
543
+ treeNode = treeNode.parent;
544
+ }
545
+ }
546
+ });
547
+ }
548
+ };
549
+ }, [
550
+ scopeRef,
551
+ restoreFocus
552
+ ]);
553
+ }
554
+ function $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, opts, scope) {
555
+ let selector = (opts === null || opts === void 0 ? void 0 : opts.tabbable) ? $a7a032acae3ddda9$var$TABBABLE_ELEMENT_SELECTOR : $a7a032acae3ddda9$var$FOCUSABLE_ELEMENT_SELECTOR;
556
+ let walker = (0, $euGna$reactariautils.getOwnerDocument)(root).createTreeWalker(root, NodeFilter.SHOW_ELEMENT, {
557
+ acceptNode (node) {
558
+ var _opts_from;
559
+ // Skip nodes inside the starting node.
560
+ if (opts === null || opts === void 0 ? void 0 : (_opts_from = opts.from) === null || _opts_from === void 0 ? void 0 : _opts_from.contains(node)) return NodeFilter.FILTER_REJECT;
561
+ if (node.matches(selector) && (0, $d5156037ad898a4d$exports.isElementVisible)(node) && (!scope || $a7a032acae3ddda9$var$isElementInScope(node, scope)) && (!(opts === null || opts === void 0 ? void 0 : opts.accept) || opts.accept(node))) return NodeFilter.FILTER_ACCEPT;
562
+ return NodeFilter.FILTER_SKIP;
563
+ }
564
+ });
565
+ if (opts === null || opts === void 0 ? void 0 : opts.from) walker.currentNode = opts.from;
566
+ return walker;
567
+ }
568
+ function $a7a032acae3ddda9$export$c5251b9e124bf29(ref, defaultOptions = {}) {
569
+ return {
570
+ focusNext (opts = {}) {
571
+ let root = ref.current;
572
+ if (!root) return null;
573
+ let { from: from, tabbable: tabbable = defaultOptions.tabbable, wrap: wrap = defaultOptions.wrap, accept: accept = defaultOptions.accept } = opts;
574
+ let node = from || (0, $euGna$reactariautils.getOwnerDocument)(root).activeElement;
575
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
576
+ tabbable: tabbable,
577
+ accept: accept
578
+ });
579
+ if (root.contains(node)) walker.currentNode = node;
580
+ let nextNode = walker.nextNode();
581
+ if (!nextNode && wrap) {
582
+ walker.currentNode = root;
583
+ nextNode = walker.nextNode();
584
+ }
585
+ if (nextNode) $a7a032acae3ddda9$var$focusElement(nextNode, true);
586
+ return nextNode;
587
+ },
588
+ focusPrevious (opts = defaultOptions) {
589
+ let root = ref.current;
590
+ if (!root) return null;
591
+ let { from: from, tabbable: tabbable = defaultOptions.tabbable, wrap: wrap = defaultOptions.wrap, accept: accept = defaultOptions.accept } = opts;
592
+ let node = from || (0, $euGna$reactariautils.getOwnerDocument)(root).activeElement;
593
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
594
+ tabbable: tabbable,
595
+ accept: accept
596
+ });
597
+ if (root.contains(node)) walker.currentNode = node;
598
+ else {
599
+ let next = $a7a032acae3ddda9$var$last(walker);
600
+ if (next) $a7a032acae3ddda9$var$focusElement(next, true);
601
+ return next !== null && next !== void 0 ? next : null;
602
+ }
603
+ let previousNode = walker.previousNode();
604
+ if (!previousNode && wrap) {
605
+ walker.currentNode = root;
606
+ let lastNode = $a7a032acae3ddda9$var$last(walker);
607
+ if (!lastNode) // couldn't wrap
608
+ return null;
609
+ previousNode = lastNode;
610
+ }
611
+ if (previousNode) $a7a032acae3ddda9$var$focusElement(previousNode, true);
612
+ return previousNode !== null && previousNode !== void 0 ? previousNode : null;
613
+ },
614
+ focusFirst (opts = defaultOptions) {
615
+ let root = ref.current;
616
+ if (!root) return null;
617
+ let { tabbable: tabbable = defaultOptions.tabbable, accept: accept = defaultOptions.accept } = opts;
618
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
619
+ tabbable: tabbable,
620
+ accept: accept
621
+ });
622
+ let nextNode = walker.nextNode();
623
+ if (nextNode) $a7a032acae3ddda9$var$focusElement(nextNode, true);
624
+ return nextNode;
625
+ },
626
+ focusLast (opts = defaultOptions) {
627
+ let root = ref.current;
628
+ if (!root) return null;
629
+ let { tabbable: tabbable = defaultOptions.tabbable, accept: accept = defaultOptions.accept } = opts;
630
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
631
+ tabbable: tabbable,
632
+ accept: accept
633
+ });
634
+ let next = $a7a032acae3ddda9$var$last(walker);
635
+ if (next) $a7a032acae3ddda9$var$focusElement(next, true);
636
+ return next !== null && next !== void 0 ? next : null;
637
+ }
638
+ };
639
+ }
640
+ function $a7a032acae3ddda9$var$last(walker) {
641
+ let next = undefined;
642
+ let last;
643
+ do {
644
+ last = walker.lastChild();
645
+ if (last) next = last;
646
+ }while (last);
647
+ return next;
648
+ }
649
+ class $a7a032acae3ddda9$var$Tree {
650
+ get size() {
651
+ return this.fastMap.size;
652
+ }
653
+ getTreeNode(data) {
654
+ return this.fastMap.get(data);
655
+ }
656
+ addTreeNode(scopeRef, parent, nodeToRestore) {
657
+ let parentNode = this.fastMap.get(parent !== null && parent !== void 0 ? parent : null);
658
+ if (!parentNode) return;
659
+ let node = new $a7a032acae3ddda9$var$TreeNode({
660
+ scopeRef: scopeRef
661
+ });
662
+ parentNode.addChild(node);
663
+ node.parent = parentNode;
664
+ this.fastMap.set(scopeRef, node);
665
+ if (nodeToRestore) node.nodeToRestore = nodeToRestore;
666
+ }
667
+ addNode(node) {
668
+ this.fastMap.set(node.scopeRef, node);
669
+ }
670
+ removeTreeNode(scopeRef) {
671
+ // never remove the root
672
+ if (scopeRef === null) return;
673
+ let node = this.fastMap.get(scopeRef);
674
+ if (!node) return;
675
+ let parentNode = node.parent;
676
+ // when we remove a scope, check if any sibling scopes are trying to restore focus to something inside the scope we're removing
677
+ // if we are, then replace the siblings restore with the restore from the scope we're removing
678
+ for (let current of this.traverse())if (current !== node && node.nodeToRestore && current.nodeToRestore && node.scopeRef && node.scopeRef.current && $a7a032acae3ddda9$var$isElementInScope(current.nodeToRestore, node.scopeRef.current)) current.nodeToRestore = node.nodeToRestore;
679
+ let children = node.children;
680
+ if (parentNode) {
681
+ parentNode.removeChild(node);
682
+ if (children.size > 0) children.forEach((child)=>parentNode && parentNode.addChild(child));
683
+ }
684
+ this.fastMap.delete(node.scopeRef);
685
+ }
686
+ // Pre Order Depth First
687
+ *traverse(node = this.root) {
688
+ if (node.scopeRef != null) yield node;
689
+ if (node.children.size > 0) for (let child of node.children)yield* this.traverse(child);
690
+ }
691
+ clone() {
692
+ var _node_parent;
693
+ let newTree = new $a7a032acae3ddda9$var$Tree();
694
+ var _node_parent_scopeRef;
695
+ for (let node of this.traverse())newTree.addTreeNode(node.scopeRef, (_node_parent_scopeRef = (_node_parent = node.parent) === null || _node_parent === void 0 ? void 0 : _node_parent.scopeRef) !== null && _node_parent_scopeRef !== void 0 ? _node_parent_scopeRef : null, node.nodeToRestore);
696
+ return newTree;
697
+ }
698
+ constructor(){
699
+ this.fastMap = new Map();
700
+ this.root = new $a7a032acae3ddda9$var$TreeNode({
701
+ scopeRef: null
702
+ });
703
+ this.fastMap.set(null, this.root);
704
+ }
705
+ }
706
+ class $a7a032acae3ddda9$var$TreeNode {
707
+ addChild(node) {
708
+ this.children.add(node);
709
+ node.parent = this;
710
+ }
711
+ removeChild(node) {
712
+ this.children.delete(node);
713
+ node.parent = undefined;
714
+ }
715
+ constructor(props){
716
+ this.children = new Set();
717
+ this.contain = false;
718
+ this.scopeRef = props.scopeRef;
719
+ }
720
+ }
721
+ let $a7a032acae3ddda9$export$d06fae2ee68b101e = new $a7a032acae3ddda9$var$Tree();
722
+
723
+
724
+ //# sourceMappingURL=FocusScope.main.js.map