@react-aria/focus 3.14.3 → 3.16.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.
package/dist/main.js CHANGED
@@ -1,11 +1,13 @@
1
- var $aB6Cp$react = require("react");
2
1
  var $aB6Cp$reactariautils = require("@react-aria/utils");
2
+ var $aB6Cp$react = require("react");
3
3
  var $aB6Cp$reactariainteractions = require("@react-aria/interactions");
4
4
  var $aB6Cp$clsx = require("clsx");
5
5
 
6
+
6
7
  function $parcel$export(e, n, v, s) {
7
8
  Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
8
9
  }
10
+
9
11
  function $parcel$interopDefault(a) {
10
12
  return a && a.__esModule ? a.default : a;
11
13
  }
@@ -59,16 +61,18 @@ function $1c7f9157d722357d$export$80f3e147d781571c(element) {
59
61
  // the page before shifting focus. This avoids issues with VoiceOver on iOS
60
62
  // causing the page to scroll when moving focus if the element is transitioning
61
63
  // from off the screen.
64
+ const ownerDocument = (0, $aB6Cp$reactariautils.getOwnerDocument)(element);
62
65
  if ((0, $aB6Cp$reactariainteractions.getInteractionModality)() === "virtual") {
63
- let lastFocusedElement = document.activeElement;
66
+ let lastFocusedElement = ownerDocument.activeElement;
64
67
  (0, $aB6Cp$reactariautils.runAfterTransition)(()=>{
65
68
  // If focus did not move and the element is still in the document, focus it.
66
- if (document.activeElement === lastFocusedElement && document.contains(element)) (0, $aB6Cp$reactariautils.focusWithoutScrolling)(element);
69
+ if (ownerDocument.activeElement === lastFocusedElement && element.isConnected) (0, $aB6Cp$reactariautils.focusWithoutScrolling)(element);
67
70
  });
68
71
  } else (0, $aB6Cp$reactariautils.focusWithoutScrolling)(element);
69
72
  }
70
73
 
71
74
 
75
+
72
76
  /*
73
77
  * Copyright 2021 Adobe. All rights reserved.
74
78
  * This file is licensed to you under the Apache License, Version 2.0 (the "License");
@@ -79,8 +83,10 @@ function $1c7f9157d722357d$export$80f3e147d781571c(element) {
79
83
  * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
80
84
  * OF ANY KIND, either express or implied. See the License for the specific language
81
85
  * governing permissions and limitations under the License.
82
- */ function $d5156037ad898a4d$var$isStyleVisible(element) {
83
- if (!(element instanceof HTMLElement) && !(element instanceof SVGElement)) return false;
86
+ */
87
+ function $d5156037ad898a4d$var$isStyleVisible(element) {
88
+ const windowObject = (0, $aB6Cp$reactariautils.getOwnerWindow)(element);
89
+ if (!(element instanceof windowObject.HTMLElement) && !(element instanceof windowObject.SVGElement)) return false;
84
90
  let { display: display, visibility: visibility } = element.style;
85
91
  let isVisible = display !== "none" && visibility !== "hidden" && visibility !== "collapse";
86
92
  if (isVisible) {
@@ -99,13 +105,12 @@ function $d5156037ad898a4d$export$e989c0fffaa6b27a(element, childElement) {
99
105
 
100
106
 
101
107
 
102
-
103
108
  const $a7a032acae3ddda9$var$FocusContext = /*#__PURE__*/ (0, ($parcel$interopDefault($aB6Cp$react))).createContext(null);
104
109
  let $a7a032acae3ddda9$var$activeScope = null;
105
110
  function $a7a032acae3ddda9$export$20e40289641fbbb6(props) {
106
111
  let { children: children, contain: contain, restoreFocus: restoreFocus, autoFocus: autoFocus } = props;
107
- let startRef = (0, $aB6Cp$react.useRef)();
108
- let endRef = (0, $aB6Cp$react.useRef)();
112
+ let startRef = (0, $aB6Cp$react.useRef)(null);
113
+ let endRef = (0, $aB6Cp$react.useRef)(null);
109
114
  let scopeRef = (0, $aB6Cp$react.useRef)([]);
110
115
  let { parentNode: parentNode } = (0, $aB6Cp$react.useContext)($a7a032acae3ddda9$var$FocusContext) || {};
111
116
  // Create a tree node here so we can add children to it even before it is added to the tree.
@@ -133,13 +138,14 @@ function $a7a032acae3ddda9$export$20e40289641fbbb6(props) {
133
138
  ]);
134
139
  (0, $aB6Cp$reactariautils.useLayoutEffect)(()=>{
135
140
  let node = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
136
- node.contain = contain;
141
+ if (node) node.contain = !!contain;
137
142
  }, [
138
143
  contain
139
144
  ]);
140
145
  (0, $aB6Cp$reactariautils.useLayoutEffect)(()=>{
146
+ var _startRef_current;
141
147
  // Find all rendered nodes between the sentinels and add them to the scope.
142
- let node = startRef.current.nextSibling;
148
+ let node = (_startRef_current = startRef.current) === null || _startRef_current === void 0 ? void 0 : _startRef_current.nextSibling;
143
149
  let nodes = [];
144
150
  while(node && node !== endRef.current){
145
151
  nodes.push(node);
@@ -153,32 +159,34 @@ function $a7a032acae3ddda9$export$20e40289641fbbb6(props) {
153
159
  $a7a032acae3ddda9$var$useFocusContainment(scopeRef, contain);
154
160
  $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain);
155
161
  $a7a032acae3ddda9$var$useAutoFocus(scopeRef, autoFocus);
156
- // this layout effect needs to run last so that focusScopeTree cleanup happens at the last moment possible
162
+ // This needs to be an effect so that activeScope is updated after the FocusScope tree is complete.
163
+ // It cannot be a useLayoutEffect because the parent of this node hasn't been attached in the tree yet.
157
164
  (0, $aB6Cp$react.useEffect)(()=>{
158
- if (scopeRef) {
159
- let activeElement = document.activeElement;
160
- let scope = null;
161
- // In strict mode, active scope is incorrectly updated since cleanup will run even though scope hasn't unmounted.
162
- // To fix this, we need to update the actual activeScope here
163
- if ($a7a032acae3ddda9$var$isElementInScope(activeElement, scopeRef.current)) {
164
- // Since useLayoutEffect runs for children first, we need to traverse the focusScope tree and find the bottom most scope that
165
- // contains the active element and set that as the activeScope
166
- for (let node of $a7a032acae3ddda9$export$d06fae2ee68b101e.traverse())if ($a7a032acae3ddda9$var$isElementInScope(activeElement, node.scopeRef.current)) scope = node;
167
- if (scope === $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef)) $a7a032acae3ddda9$var$activeScope = scope.scopeRef;
168
- }
169
- return ()=>{
170
- // Scope may have been re-parented.
171
- let parentScope = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef).parent.scopeRef;
172
- // Restore the active scope on unmount if this scope or a descendant scope is active.
173
- // Parent effect cleanups run before children, so we need to check if the
174
- // parent scope actually still exists before restoring the active scope to it.
175
- if ((scopeRef === $a7a032acae3ddda9$var$activeScope || $a7a032acae3ddda9$var$isAncestorScope(scopeRef, $a7a032acae3ddda9$var$activeScope)) && (!parentScope || $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(parentScope))) $a7a032acae3ddda9$var$activeScope = parentScope;
176
- $a7a032acae3ddda9$export$d06fae2ee68b101e.removeTreeNode(scopeRef);
177
- };
165
+ const activeElement = (0, $aB6Cp$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined).activeElement;
166
+ let scope = null;
167
+ if ($a7a032acae3ddda9$var$isElementInScope(activeElement, scopeRef.current)) {
168
+ // We need to traverse the focusScope tree and find the bottom most scope that
169
+ // contains the active element and set that as the activeScope.
170
+ for (let node of $a7a032acae3ddda9$export$d06fae2ee68b101e.traverse())if (node.scopeRef && $a7a032acae3ddda9$var$isElementInScope(activeElement, node.scopeRef.current)) scope = node;
171
+ if (scope === $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef)) $a7a032acae3ddda9$var$activeScope = scope.scopeRef;
178
172
  }
179
173
  }, [
180
174
  scopeRef
181
175
  ]);
176
+ // This layout effect cleanup is so that the tree node is removed synchronously with react before the RAF
177
+ // in useRestoreFocus cleanup runs.
178
+ (0, $aB6Cp$reactariautils.useLayoutEffect)(()=>{
179
+ return ()=>{
180
+ var _focusScopeTree_getTreeNode_parent, _focusScopeTree_getTreeNode;
181
+ var _focusScopeTree_getTreeNode_parent_scopeRef;
182
+ // Scope may have been re-parented.
183
+ 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;
184
+ if ((scopeRef === $a7a032acae3ddda9$var$activeScope || $a7a032acae3ddda9$var$isAncestorScope(scopeRef, $a7a032acae3ddda9$var$activeScope)) && (!parentScope || $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(parentScope))) $a7a032acae3ddda9$var$activeScope = parentScope;
185
+ $a7a032acae3ddda9$export$d06fae2ee68b101e.removeTreeNode(scopeRef);
186
+ };
187
+ }, [
188
+ scopeRef
189
+ ]);
182
190
  let focusManager = (0, $aB6Cp$react.useMemo)(()=>$a7a032acae3ddda9$var$createFocusManagerForScope(scopeRef), []);
183
191
  let value = (0, $aB6Cp$react.useMemo)(()=>({
184
192
  focusManager: focusManager,
@@ -208,9 +216,10 @@ function $a7a032acae3ddda9$var$createFocusManagerForScope(scopeRef) {
208
216
  focusNext (opts = {}) {
209
217
  let scope = scopeRef.current;
210
218
  let { from: from, tabbable: tabbable, wrap: wrap, accept: accept } = opts;
211
- let node = from || document.activeElement;
219
+ let node = from || (0, $aB6Cp$reactariautils.getOwnerDocument)(scope[0]).activeElement;
212
220
  let sentinel = scope[0].previousElementSibling;
213
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa($a7a032acae3ddda9$var$getScopeRoot(scope), {
221
+ let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
222
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
214
223
  tabbable: tabbable,
215
224
  accept: accept
216
225
  }, scope);
@@ -226,9 +235,10 @@ function $a7a032acae3ddda9$var$createFocusManagerForScope(scopeRef) {
226
235
  focusPrevious (opts = {}) {
227
236
  let scope = scopeRef.current;
228
237
  let { from: from, tabbable: tabbable, wrap: wrap, accept: accept } = opts;
229
- let node = from || document.activeElement;
238
+ let node = from || (0, $aB6Cp$reactariautils.getOwnerDocument)(scope[0]).activeElement;
230
239
  let sentinel = scope[scope.length - 1].nextElementSibling;
231
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa($a7a032acae3ddda9$var$getScopeRoot(scope), {
240
+ let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
241
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
232
242
  tabbable: tabbable,
233
243
  accept: accept
234
244
  }, scope);
@@ -244,7 +254,8 @@ function $a7a032acae3ddda9$var$createFocusManagerForScope(scopeRef) {
244
254
  focusFirst (opts = {}) {
245
255
  let scope = scopeRef.current;
246
256
  let { tabbable: tabbable, accept: accept } = opts;
247
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa($a7a032acae3ddda9$var$getScopeRoot(scope), {
257
+ let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
258
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
248
259
  tabbable: tabbable,
249
260
  accept: accept
250
261
  }, scope);
@@ -256,7 +267,8 @@ function $a7a032acae3ddda9$var$createFocusManagerForScope(scopeRef) {
256
267
  focusLast (opts = {}) {
257
268
  let scope = scopeRef.current;
258
269
  let { tabbable: tabbable, accept: accept } = opts;
259
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa($a7a032acae3ddda9$var$getScopeRoot(scope), {
270
+ let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
271
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
260
272
  tabbable: tabbable,
261
273
  accept: accept
262
274
  }, scope);
@@ -298,26 +310,29 @@ function $a7a032acae3ddda9$var$shouldContainFocus(scopeRef) {
298
310
  }
299
311
  function $a7a032acae3ddda9$var$useFocusContainment(scopeRef, contain) {
300
312
  let focusedNode = (0, $aB6Cp$react.useRef)();
301
- let raf = (0, $aB6Cp$react.useRef)(null);
313
+ let raf = (0, $aB6Cp$react.useRef)();
302
314
  (0, $aB6Cp$reactariautils.useLayoutEffect)(()=>{
303
315
  let scope = scopeRef.current;
304
316
  if (!contain) {
305
317
  // if contain was changed, then we should cancel any ongoing waits to pull focus back into containment
306
318
  if (raf.current) {
307
319
  cancelAnimationFrame(raf.current);
308
- raf.current = null;
320
+ raf.current = undefined;
309
321
  }
310
322
  return;
311
323
  }
324
+ const ownerDocument = (0, $aB6Cp$reactariautils.getOwnerDocument)(scope ? scope[0] : undefined);
312
325
  // Handle the Tab key to contain focus within the scope
313
326
  let onKeyDown = (e)=>{
314
327
  if (e.key !== "Tab" || e.altKey || e.ctrlKey || e.metaKey || !$a7a032acae3ddda9$var$shouldContainFocus(scopeRef)) return;
315
- let focusedElement = document.activeElement;
328
+ let focusedElement = ownerDocument.activeElement;
316
329
  let scope = scopeRef.current;
317
- if (!$a7a032acae3ddda9$var$isElementInScope(focusedElement, scope)) return;
318
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa($a7a032acae3ddda9$var$getScopeRoot(scope), {
330
+ if (!scope || !$a7a032acae3ddda9$var$isElementInScope(focusedElement, scope)) return;
331
+ let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
332
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
319
333
  tabbable: true
320
334
  }, scope);
335
+ if (!focusedElement) return;
321
336
  walker.currentNode = focusedElement;
322
337
  let nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
323
338
  if (!nextElement) {
@@ -337,7 +352,7 @@ function $a7a032acae3ddda9$var$useFocusContainment(scopeRef, contain) {
337
352
  // If a focus event occurs outside the active scope (e.g. user tabs from browser location bar),
338
353
  // restore focus to the previously focused node or the first tabbable element in the active scope.
339
354
  if (focusedNode.current) focusedNode.current.focus();
340
- else if ($a7a032acae3ddda9$var$activeScope) $a7a032acae3ddda9$var$focusFirstInScope($a7a032acae3ddda9$var$activeScope.current);
355
+ else if ($a7a032acae3ddda9$var$activeScope && $a7a032acae3ddda9$var$activeScope.current) $a7a032acae3ddda9$var$focusFirstInScope($a7a032acae3ddda9$var$activeScope.current);
341
356
  } else if ($a7a032acae3ddda9$var$shouldContainFocus(scopeRef)) focusedNode.current = e.target;
342
357
  };
343
358
  let onBlur = (e)=>{
@@ -345,24 +360,25 @@ function $a7a032acae3ddda9$var$useFocusContainment(scopeRef, contain) {
345
360
  if (raf.current) cancelAnimationFrame(raf.current);
346
361
  raf.current = requestAnimationFrame(()=>{
347
362
  // Use document.activeElement instead of e.relatedTarget so we can tell if user clicked into iframe
348
- if ($a7a032acae3ddda9$var$shouldContainFocus(scopeRef) && !$a7a032acae3ddda9$var$isElementInChildScope(document.activeElement, scopeRef)) {
363
+ if (ownerDocument.activeElement && $a7a032acae3ddda9$var$shouldContainFocus(scopeRef) && !$a7a032acae3ddda9$var$isElementInChildScope(ownerDocument.activeElement, scopeRef)) {
349
364
  $a7a032acae3ddda9$var$activeScope = scopeRef;
350
- if (document.body.contains(e.target)) {
365
+ if (ownerDocument.body.contains(e.target)) {
366
+ var _focusedNode_current;
351
367
  focusedNode.current = e.target;
352
- focusedNode.current.focus();
353
- } else if ($a7a032acae3ddda9$var$activeScope) $a7a032acae3ddda9$var$focusFirstInScope($a7a032acae3ddda9$var$activeScope.current);
368
+ (_focusedNode_current = focusedNode.current) === null || _focusedNode_current === void 0 ? void 0 : _focusedNode_current.focus();
369
+ } else if ($a7a032acae3ddda9$var$activeScope.current) $a7a032acae3ddda9$var$focusFirstInScope($a7a032acae3ddda9$var$activeScope.current);
354
370
  }
355
371
  });
356
372
  };
357
- document.addEventListener("keydown", onKeyDown, false);
358
- document.addEventListener("focusin", onFocus, false);
359
- scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
360
- scope.forEach((element)=>element.addEventListener("focusout", onBlur, false));
373
+ ownerDocument.addEventListener("keydown", onKeyDown, false);
374
+ ownerDocument.addEventListener("focusin", onFocus, false);
375
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
376
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusout", onBlur, false));
361
377
  return ()=>{
362
- document.removeEventListener("keydown", onKeyDown, false);
363
- document.removeEventListener("focusin", onFocus, false);
364
- scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
365
- scope.forEach((element)=>element.removeEventListener("focusout", onBlur, false));
378
+ ownerDocument.removeEventListener("keydown", onKeyDown, false);
379
+ ownerDocument.removeEventListener("focusin", onFocus, false);
380
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
381
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusout", onBlur, false));
366
382
  };
367
383
  }, [
368
384
  scopeRef,
@@ -382,6 +398,8 @@ function $a7a032acae3ddda9$var$isElementInAnyScope(element) {
382
398
  return $a7a032acae3ddda9$var$isElementInChildScope(element);
383
399
  }
384
400
  function $a7a032acae3ddda9$var$isElementInScope(element, scope) {
401
+ if (!element) return false;
402
+ if (!scope) return false;
385
403
  return scope.some((node)=>node.contains(element));
386
404
  }
387
405
  function $a7a032acae3ddda9$var$isElementInChildScope(element, scope = null) {
@@ -390,7 +408,7 @@ function $a7a032acae3ddda9$var$isElementInChildScope(element, scope = null) {
390
408
  // node.contains in isElementInScope covers child scopes that are also DOM children,
391
409
  // but does not cover child scopes in portals.
392
410
  for (let { scopeRef: s } of $a7a032acae3ddda9$export$d06fae2ee68b101e.traverse($a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scope))){
393
- if ($a7a032acae3ddda9$var$isElementInScope(element, s.current)) return true;
411
+ if (s && $a7a032acae3ddda9$var$isElementInScope(element, s.current)) return true;
394
412
  }
395
413
  return false;
396
414
  }
@@ -420,14 +438,16 @@ function $a7a032acae3ddda9$var$focusElement(element, scroll = false) {
420
438
  }
421
439
  function $a7a032acae3ddda9$var$focusFirstInScope(scope, tabbable = true) {
422
440
  let sentinel = scope[0].previousElementSibling;
423
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa($a7a032acae3ddda9$var$getScopeRoot(scope), {
441
+ let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
442
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
424
443
  tabbable: tabbable
425
444
  }, scope);
426
445
  walker.currentNode = sentinel;
427
446
  let nextNode = walker.nextNode();
428
447
  // If the scope does not contain a tabbable element, use the first focusable element.
429
448
  if (tabbable && !nextNode) {
430
- walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa($a7a032acae3ddda9$var$getScopeRoot(scope), {
449
+ scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
450
+ walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
431
451
  tabbable: false
432
452
  }, scope);
433
453
  walker.currentNode = sentinel;
@@ -440,7 +460,8 @@ function $a7a032acae3ddda9$var$useAutoFocus(scopeRef, autoFocus) {
440
460
  (0, $aB6Cp$react.useEffect)(()=>{
441
461
  if (autoFocusRef.current) {
442
462
  $a7a032acae3ddda9$var$activeScope = scopeRef;
443
- if (!$a7a032acae3ddda9$var$isElementInScope(document.activeElement, $a7a032acae3ddda9$var$activeScope.current)) $a7a032acae3ddda9$var$focusFirstInScope(scopeRef.current);
463
+ const ownerDocument = (0, $aB6Cp$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined);
464
+ if (!$a7a032acae3ddda9$var$isElementInScope(ownerDocument.activeElement, $a7a032acae3ddda9$var$activeScope.current) && scopeRef.current) $a7a032acae3ddda9$var$focusFirstInScope(scopeRef.current);
444
465
  }
445
466
  autoFocusRef.current = false;
446
467
  }, [
@@ -453,16 +474,17 @@ function $a7a032acae3ddda9$var$useActiveScopeTracker(scopeRef, restore, contain)
453
474
  (0, $aB6Cp$reactariautils.useLayoutEffect)(()=>{
454
475
  if (restore || contain) return;
455
476
  let scope = scopeRef.current;
477
+ const ownerDocument = (0, $aB6Cp$reactariautils.getOwnerDocument)(scope ? scope[0] : undefined);
456
478
  let onFocus = (e)=>{
457
479
  let target = e.target;
458
480
  if ($a7a032acae3ddda9$var$isElementInScope(target, scopeRef.current)) $a7a032acae3ddda9$var$activeScope = scopeRef;
459
481
  else if (!$a7a032acae3ddda9$var$isElementInAnyScope(target)) $a7a032acae3ddda9$var$activeScope = null;
460
482
  };
461
- document.addEventListener("focusin", onFocus, false);
462
- scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
483
+ ownerDocument.addEventListener("focusin", onFocus, false);
484
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
463
485
  return ()=>{
464
- document.removeEventListener("focusin", onFocus, false);
465
- scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
486
+ ownerDocument.removeEventListener("focusin", onFocus, false);
487
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
466
488
  };
467
489
  }, [
468
490
  scopeRef,
@@ -480,22 +502,24 @@ function $a7a032acae3ddda9$var$shouldRestoreFocus(scopeRef) {
480
502
  }
481
503
  function $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain) {
482
504
  // create a ref during render instead of useLayoutEffect so the active element is saved before a child with autoFocus=true mounts.
483
- const nodeToRestoreRef = (0, $aB6Cp$react.useRef)(typeof document !== "undefined" ? document.activeElement : null);
505
+ // eslint-disable-next-line no-restricted-globals
506
+ const nodeToRestoreRef = (0, $aB6Cp$react.useRef)(typeof document !== "undefined" ? (0, $aB6Cp$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined).activeElement : null);
484
507
  // restoring scopes should all track if they are active regardless of contain, but contain already tracks it plus logic to contain the focus
485
508
  // restoring-non-containing scopes should only care if they become active so they can perform the restore
486
509
  (0, $aB6Cp$reactariautils.useLayoutEffect)(()=>{
487
510
  let scope = scopeRef.current;
511
+ const ownerDocument = (0, $aB6Cp$reactariautils.getOwnerDocument)(scope ? scope[0] : undefined);
488
512
  if (!restoreFocus || contain) return;
489
513
  let onFocus = ()=>{
490
514
  // If focusing an element in a child scope of the currently active scope, the child becomes active.
491
515
  // Moving out of the active scope to an ancestor is not allowed.
492
- if ((!$a7a032acae3ddda9$var$activeScope || $a7a032acae3ddda9$var$isAncestorScope($a7a032acae3ddda9$var$activeScope, scopeRef)) && $a7a032acae3ddda9$var$isElementInScope(document.activeElement, scopeRef.current)) $a7a032acae3ddda9$var$activeScope = scopeRef;
516
+ if ((!$a7a032acae3ddda9$var$activeScope || $a7a032acae3ddda9$var$isAncestorScope($a7a032acae3ddda9$var$activeScope, scopeRef)) && $a7a032acae3ddda9$var$isElementInScope(ownerDocument.activeElement, scopeRef.current)) $a7a032acae3ddda9$var$activeScope = scopeRef;
493
517
  };
494
- document.addEventListener("focusin", onFocus, false);
495
- scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
518
+ ownerDocument.addEventListener("focusin", onFocus, false);
519
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
496
520
  return ()=>{
497
- document.removeEventListener("focusin", onFocus, false);
498
- scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
521
+ ownerDocument.removeEventListener("focusin", onFocus, false);
522
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
499
523
  };
500
524
  // eslint-disable-next-line react-hooks/exhaustive-deps
501
525
  }, [
@@ -503,6 +527,7 @@ function $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain)
503
527
  contain
504
528
  ]);
505
529
  (0, $aB6Cp$reactariautils.useLayoutEffect)(()=>{
530
+ const ownerDocument = (0, $aB6Cp$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined);
506
531
  if (!restoreFocus) return;
507
532
  // Handle the Tab key so that tabbing out of the scope goes to the next element
508
533
  // after the node that had focus when the scope mounted. This is important when
@@ -510,19 +535,21 @@ function $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain)
510
535
  // tabbing out of the overlay.
511
536
  let onKeyDown = (e)=>{
512
537
  if (e.key !== "Tab" || e.altKey || e.ctrlKey || e.metaKey || !$a7a032acae3ddda9$var$shouldContainFocus(scopeRef)) return;
513
- let focusedElement = document.activeElement;
538
+ let focusedElement = ownerDocument.activeElement;
514
539
  if (!$a7a032acae3ddda9$var$isElementInScope(focusedElement, scopeRef.current)) return;
515
- let nodeToRestore = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef).nodeToRestore;
540
+ let treeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
541
+ if (!treeNode) return;
542
+ let nodeToRestore = treeNode.nodeToRestore;
516
543
  // Create a DOM tree walker that matches all tabbable elements
517
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(document.body, {
544
+ let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(ownerDocument.body, {
518
545
  tabbable: true
519
546
  });
520
547
  // Find the next tabbable element after the currently focused element
521
548
  walker.currentNode = focusedElement;
522
549
  let nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
523
- if (!document.body.contains(nodeToRestore) || nodeToRestore === document.body) {
524
- nodeToRestore = null;
525
- $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef).nodeToRestore = null;
550
+ if (!nodeToRestore || !ownerDocument.body.contains(nodeToRestore) || nodeToRestore === ownerDocument.body) {
551
+ nodeToRestore = undefined;
552
+ treeNode.nodeToRestore = undefined;
526
553
  }
527
554
  // If there is no next element, or it is outside the current scope, move focus to the
528
555
  // next element after the node to restore to instead.
@@ -541,9 +568,9 @@ function $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain)
541
568
  else $a7a032acae3ddda9$var$focusElement(nodeToRestore, true);
542
569
  }
543
570
  };
544
- if (!contain) document.addEventListener("keydown", onKeyDown, true);
571
+ if (!contain) ownerDocument.addEventListener("keydown", onKeyDown, true);
545
572
  return ()=>{
546
- if (!contain) document.removeEventListener("keydown", onKeyDown, true);
573
+ if (!contain) ownerDocument.removeEventListener("keydown", onKeyDown, true);
547
574
  };
548
575
  }, [
549
576
  scopeRef,
@@ -552,22 +579,28 @@ function $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain)
552
579
  ]);
553
580
  // useLayoutEffect instead of useEffect so the active element is saved synchronously instead of asynchronously.
554
581
  (0, $aB6Cp$reactariautils.useLayoutEffect)(()=>{
582
+ const ownerDocument = (0, $aB6Cp$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined);
555
583
  if (!restoreFocus) return;
556
- $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef).nodeToRestore = nodeToRestoreRef.current;
584
+ let treeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
585
+ if (!treeNode) return;
586
+ var _nodeToRestoreRef_current;
587
+ treeNode.nodeToRestore = (_nodeToRestoreRef_current = nodeToRestoreRef.current) !== null && _nodeToRestoreRef_current !== void 0 ? _nodeToRestoreRef_current : undefined;
557
588
  return ()=>{
558
- let nodeToRestore = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef).nodeToRestore;
589
+ let treeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
590
+ if (!treeNode) return;
591
+ let nodeToRestore = treeNode.nodeToRestore;
559
592
  // if we already lost focus to the body and this was the active scope, then we should attempt to restore
560
593
  if (restoreFocus && nodeToRestore && // eslint-disable-next-line react-hooks/exhaustive-deps
561
- ($a7a032acae3ddda9$var$isElementInScope(document.activeElement, scopeRef.current) || document.activeElement === document.body && $a7a032acae3ddda9$var$shouldRestoreFocus(scopeRef))) {
594
+ ($a7a032acae3ddda9$var$isElementInScope(ownerDocument.activeElement, scopeRef.current) || ownerDocument.activeElement === ownerDocument.body && $a7a032acae3ddda9$var$shouldRestoreFocus(scopeRef))) {
562
595
  // freeze the focusScopeTree so it persists after the raf, otherwise during unmount nodes are removed from it
563
596
  let clonedTree = $a7a032acae3ddda9$export$d06fae2ee68b101e.clone();
564
597
  requestAnimationFrame(()=>{
565
598
  // Only restore focus if we've lost focus to the body, the alternative is that focus has been purposefully moved elsewhere
566
- if (document.activeElement === document.body) {
599
+ if (ownerDocument.activeElement === ownerDocument.body) {
567
600
  // look up the tree starting with our scope to find a nodeToRestore still in the DOM
568
601
  let treeNode = clonedTree.getTreeNode(scopeRef);
569
602
  while(treeNode){
570
- if (treeNode.nodeToRestore && document.body.contains(treeNode.nodeToRestore)) {
603
+ if (treeNode.nodeToRestore && treeNode.nodeToRestore.isConnected) {
571
604
  $a7a032acae3ddda9$var$focusElement(treeNode.nodeToRestore);
572
605
  return;
573
606
  }
@@ -577,7 +610,7 @@ function $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain)
577
610
  // ancestor scope that is still in the tree.
578
611
  treeNode = clonedTree.getTreeNode(scopeRef);
579
612
  while(treeNode){
580
- if (treeNode.scopeRef && $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(treeNode.scopeRef)) {
613
+ if (treeNode.scopeRef && treeNode.scopeRef.current && $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(treeNode.scopeRef)) {
581
614
  $a7a032acae3ddda9$var$focusFirstInScope(treeNode.scopeRef.current, true);
582
615
  return;
583
616
  }
@@ -594,7 +627,7 @@ function $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain)
594
627
  }
595
628
  function $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, opts, scope) {
596
629
  let selector = (opts === null || opts === void 0 ? void 0 : opts.tabbable) ? $a7a032acae3ddda9$var$TABBABLE_ELEMENT_SELECTOR : $a7a032acae3ddda9$var$FOCUSABLE_ELEMENT_SELECTOR;
597
- let walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT, {
630
+ let walker = (0, $aB6Cp$reactariautils.getOwnerDocument)(root).createTreeWalker(root, NodeFilter.SHOW_ELEMENT, {
598
631
  acceptNode (node) {
599
632
  var _opts_from;
600
633
  // Skip nodes inside the starting node.
@@ -610,9 +643,9 @@ function $a7a032acae3ddda9$export$c5251b9e124bf29(ref, defaultOptions = {}) {
610
643
  return {
611
644
  focusNext (opts = {}) {
612
645
  let root = ref.current;
613
- if (!root) return;
646
+ if (!root) return null;
614
647
  let { from: from, tabbable: tabbable = defaultOptions.tabbable, wrap: wrap = defaultOptions.wrap, accept: accept = defaultOptions.accept } = opts;
615
- let node = from || document.activeElement;
648
+ let node = from || (0, $aB6Cp$reactariautils.getOwnerDocument)(root).activeElement;
616
649
  let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
617
650
  tabbable: tabbable,
618
651
  accept: accept
@@ -628,9 +661,9 @@ function $a7a032acae3ddda9$export$c5251b9e124bf29(ref, defaultOptions = {}) {
628
661
  },
629
662
  focusPrevious (opts = defaultOptions) {
630
663
  let root = ref.current;
631
- if (!root) return;
664
+ if (!root) return null;
632
665
  let { from: from, tabbable: tabbable = defaultOptions.tabbable, wrap: wrap = defaultOptions.wrap, accept: accept = defaultOptions.accept } = opts;
633
- let node = from || document.activeElement;
666
+ let node = from || (0, $aB6Cp$reactariautils.getOwnerDocument)(root).activeElement;
634
667
  let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
635
668
  tabbable: tabbable,
636
669
  accept: accept
@@ -639,19 +672,22 @@ function $a7a032acae3ddda9$export$c5251b9e124bf29(ref, defaultOptions = {}) {
639
672
  else {
640
673
  let next = $a7a032acae3ddda9$var$last(walker);
641
674
  if (next) $a7a032acae3ddda9$var$focusElement(next, true);
642
- return next;
675
+ return next !== null && next !== void 0 ? next : null;
643
676
  }
644
677
  let previousNode = walker.previousNode();
645
678
  if (!previousNode && wrap) {
646
679
  walker.currentNode = root;
647
- previousNode = $a7a032acae3ddda9$var$last(walker);
680
+ let lastNode = $a7a032acae3ddda9$var$last(walker);
681
+ if (!lastNode) // couldn't wrap
682
+ return null;
683
+ previousNode = lastNode;
648
684
  }
649
685
  if (previousNode) $a7a032acae3ddda9$var$focusElement(previousNode, true);
650
- return previousNode;
686
+ return previousNode !== null && previousNode !== void 0 ? previousNode : null;
651
687
  },
652
688
  focusFirst (opts = defaultOptions) {
653
689
  let root = ref.current;
654
- if (!root) return;
690
+ if (!root) return null;
655
691
  let { tabbable: tabbable = defaultOptions.tabbable, accept: accept = defaultOptions.accept } = opts;
656
692
  let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
657
693
  tabbable: tabbable,
@@ -663,7 +699,7 @@ function $a7a032acae3ddda9$export$c5251b9e124bf29(ref, defaultOptions = {}) {
663
699
  },
664
700
  focusLast (opts = defaultOptions) {
665
701
  let root = ref.current;
666
- if (!root) return;
702
+ if (!root) return null;
667
703
  let { tabbable: tabbable = defaultOptions.tabbable, accept: accept = defaultOptions.accept } = opts;
668
704
  let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
669
705
  tabbable: tabbable,
@@ -671,12 +707,12 @@ function $a7a032acae3ddda9$export$c5251b9e124bf29(ref, defaultOptions = {}) {
671
707
  });
672
708
  let next = $a7a032acae3ddda9$var$last(walker);
673
709
  if (next) $a7a032acae3ddda9$var$focusElement(next, true);
674
- return next;
710
+ return next !== null && next !== void 0 ? next : null;
675
711
  }
676
712
  };
677
713
  }
678
714
  function $a7a032acae3ddda9$var$last(walker) {
679
- let next;
715
+ let next = undefined;
680
716
  let last;
681
717
  do {
682
718
  last = walker.lastChild();
@@ -693,6 +729,7 @@ class $a7a032acae3ddda9$var$Tree {
693
729
  }
694
730
  addTreeNode(scopeRef, parent, nodeToRestore) {
695
731
  let parentNode = this.fastMap.get(parent !== null && parent !== void 0 ? parent : null);
732
+ if (!parentNode) return;
696
733
  let node = new $a7a032acae3ddda9$var$TreeNode({
697
734
  scopeRef: scopeRef
698
735
  });
@@ -708,13 +745,16 @@ class $a7a032acae3ddda9$var$Tree {
708
745
  // never remove the root
709
746
  if (scopeRef === null) return;
710
747
  let node = this.fastMap.get(scopeRef);
748
+ if (!node) return;
711
749
  let parentNode = node.parent;
712
750
  // when we remove a scope, check if any sibling scopes are trying to restore focus to something inside the scope we're removing
713
751
  // if we are, then replace the siblings restore with the restore from the scope we're removing
714
- for (let current of this.traverse())if (current !== node && node.nodeToRestore && current.nodeToRestore && node.scopeRef.current && $a7a032acae3ddda9$var$isElementInScope(current.nodeToRestore, node.scopeRef.current)) current.nodeToRestore = node.nodeToRestore;
752
+ 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;
715
753
  let children = node.children;
716
- parentNode.removeChild(node);
717
- if (children.size > 0) children.forEach((child)=>parentNode.addChild(child));
754
+ if (parentNode) {
755
+ parentNode.removeChild(node);
756
+ if (children.size > 0) children.forEach((child)=>parentNode && parentNode.addChild(child));
757
+ }
718
758
  this.fastMap.delete(node.scopeRef);
719
759
  }
720
760
  // Pre Order Depth First
@@ -723,8 +763,10 @@ class $a7a032acae3ddda9$var$Tree {
723
763
  if (node.children.size > 0) for (let child of node.children)yield* this.traverse(child);
724
764
  }
725
765
  clone() {
766
+ var _node_parent;
726
767
  let newTree = new $a7a032acae3ddda9$var$Tree();
727
- for (let node of this.traverse())newTree.addTreeNode(node.scopeRef, node.parent.scopeRef, node.nodeToRestore);
768
+ var _node_parent_scopeRef;
769
+ 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);
728
770
  return newTree;
729
771
  }
730
772
  constructor(){
@@ -846,9 +888,10 @@ function $fb504d83237fd6ac$var$useFocusableContext(ref) {
846
888
  * Provides DOM props to the nearest focusable child.
847
889
  */ function $fb504d83237fd6ac$var$FocusableProvider(props, ref) {
848
890
  let { children: children, ...otherProps } = props;
891
+ let objRef = (0, $aB6Cp$reactariautils.useObjectRef)(ref);
849
892
  let context = {
850
893
  ...otherProps,
851
- ref: ref
894
+ ref: objRef
852
895
  };
853
896
  return /*#__PURE__*/ (0, ($parcel$interopDefault($aB6Cp$react))).createElement($fb504d83237fd6ac$var$FocusableContext.Provider, {
854
897
  value: context