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