@react-aria/focus 3.16.1 → 3.17.0

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