@react-aria/focus 3.0.0-nightly-641446f65-240905

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