@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/module.js CHANGED
@@ -1,5 +1,5 @@
1
+ import {useLayoutEffect as $6nfFC$useLayoutEffect, getOwnerDocument as $6nfFC$getOwnerDocument, runAfterTransition as $6nfFC$runAfterTransition, focusWithoutScrolling as $6nfFC$focusWithoutScrolling, getOwnerWindow as $6nfFC$getOwnerWindow, mergeProps as $6nfFC$mergeProps, useSyncRef as $6nfFC$useSyncRef, useObjectRef as $6nfFC$useObjectRef} from "@react-aria/utils";
1
2
  import $6nfFC$react, {useRef as $6nfFC$useRef, useContext as $6nfFC$useContext, useMemo as $6nfFC$useMemo, useEffect as $6nfFC$useEffect, useState as $6nfFC$useState, useCallback as $6nfFC$useCallback} from "react";
2
- import {useLayoutEffect as $6nfFC$useLayoutEffect, runAfterTransition as $6nfFC$runAfterTransition, focusWithoutScrolling as $6nfFC$focusWithoutScrolling, mergeProps as $6nfFC$mergeProps, useSyncRef as $6nfFC$useSyncRef} from "@react-aria/utils";
3
3
  import {getInteractionModality as $6nfFC$getInteractionModality, isFocusVisible as $6nfFC$isFocusVisible, useFocusVisibleListener as $6nfFC$useFocusVisibleListener, useFocus as $6nfFC$useFocus, useFocusWithin as $6nfFC$useFocusWithin, useKeyboard as $6nfFC$useKeyboard} from "@react-aria/interactions";
4
4
  import $6nfFC$clsx from "clsx";
5
5
 
@@ -41,16 +41,18 @@ function $6a99195332edec8b$export$80f3e147d781571c(element) {
41
41
  // the page before shifting focus. This avoids issues with VoiceOver on iOS
42
42
  // causing the page to scroll when moving focus if the element is transitioning
43
43
  // from off the screen.
44
+ const ownerDocument = (0, $6nfFC$getOwnerDocument)(element);
44
45
  if ((0, $6nfFC$getInteractionModality)() === "virtual") {
45
- let lastFocusedElement = document.activeElement;
46
+ let lastFocusedElement = ownerDocument.activeElement;
46
47
  (0, $6nfFC$runAfterTransition)(()=>{
47
48
  // If focus did not move and the element is still in the document, focus it.
48
- if (document.activeElement === lastFocusedElement && document.contains(element)) (0, $6nfFC$focusWithoutScrolling)(element);
49
+ if (ownerDocument.activeElement === lastFocusedElement && element.isConnected) (0, $6nfFC$focusWithoutScrolling)(element);
49
50
  });
50
51
  } else (0, $6nfFC$focusWithoutScrolling)(element);
51
52
  }
52
53
 
53
54
 
55
+
54
56
  /*
55
57
  * Copyright 2021 Adobe. All rights reserved.
56
58
  * This file is licensed to you under the Apache License, Version 2.0 (the "License");
@@ -61,8 +63,10 @@ function $6a99195332edec8b$export$80f3e147d781571c(element) {
61
63
  * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
62
64
  * OF ANY KIND, either express or implied. See the License for the specific language
63
65
  * governing permissions and limitations under the License.
64
- */ function $645f2e67b85a24c9$var$isStyleVisible(element) {
65
- if (!(element instanceof HTMLElement) && !(element instanceof SVGElement)) return false;
66
+ */
67
+ function $645f2e67b85a24c9$var$isStyleVisible(element) {
68
+ const windowObject = (0, $6nfFC$getOwnerWindow)(element);
69
+ if (!(element instanceof windowObject.HTMLElement) && !(element instanceof windowObject.SVGElement)) return false;
66
70
  let { display: display, visibility: visibility } = element.style;
67
71
  let isVisible = display !== "none" && visibility !== "hidden" && visibility !== "collapse";
68
72
  if (isVisible) {
@@ -81,13 +85,12 @@ function $645f2e67b85a24c9$export$e989c0fffaa6b27a(element, childElement) {
81
85
 
82
86
 
83
87
 
84
-
85
88
  const $9bf71ea28793e738$var$FocusContext = /*#__PURE__*/ (0, $6nfFC$react).createContext(null);
86
89
  let $9bf71ea28793e738$var$activeScope = null;
87
90
  function $9bf71ea28793e738$export$20e40289641fbbb6(props) {
88
91
  let { children: children, contain: contain, restoreFocus: restoreFocus, autoFocus: autoFocus } = props;
89
- let startRef = (0, $6nfFC$useRef)();
90
- let endRef = (0, $6nfFC$useRef)();
92
+ let startRef = (0, $6nfFC$useRef)(null);
93
+ let endRef = (0, $6nfFC$useRef)(null);
91
94
  let scopeRef = (0, $6nfFC$useRef)([]);
92
95
  let { parentNode: parentNode } = (0, $6nfFC$useContext)($9bf71ea28793e738$var$FocusContext) || {};
93
96
  // Create a tree node here so we can add children to it even before it is added to the tree.
@@ -115,13 +118,14 @@ function $9bf71ea28793e738$export$20e40289641fbbb6(props) {
115
118
  ]);
116
119
  (0, $6nfFC$useLayoutEffect)(()=>{
117
120
  let node = $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef);
118
- node.contain = contain;
121
+ if (node) node.contain = !!contain;
119
122
  }, [
120
123
  contain
121
124
  ]);
122
125
  (0, $6nfFC$useLayoutEffect)(()=>{
126
+ var _startRef_current;
123
127
  // Find all rendered nodes between the sentinels and add them to the scope.
124
- let node = startRef.current.nextSibling;
128
+ let node = (_startRef_current = startRef.current) === null || _startRef_current === void 0 ? void 0 : _startRef_current.nextSibling;
125
129
  let nodes = [];
126
130
  while(node && node !== endRef.current){
127
131
  nodes.push(node);
@@ -135,32 +139,34 @@ function $9bf71ea28793e738$export$20e40289641fbbb6(props) {
135
139
  $9bf71ea28793e738$var$useFocusContainment(scopeRef, contain);
136
140
  $9bf71ea28793e738$var$useRestoreFocus(scopeRef, restoreFocus, contain);
137
141
  $9bf71ea28793e738$var$useAutoFocus(scopeRef, autoFocus);
138
- // this layout effect needs to run last so that focusScopeTree cleanup happens at the last moment possible
142
+ // This needs to be an effect so that activeScope is updated after the FocusScope tree is complete.
143
+ // It cannot be a useLayoutEffect because the parent of this node hasn't been attached in the tree yet.
139
144
  (0, $6nfFC$useEffect)(()=>{
140
- if (scopeRef) {
141
- let activeElement = document.activeElement;
142
- let scope = null;
143
- // In strict mode, active scope is incorrectly updated since cleanup will run even though scope hasn't unmounted.
144
- // To fix this, we need to update the actual activeScope here
145
- if ($9bf71ea28793e738$var$isElementInScope(activeElement, scopeRef.current)) {
146
- // Since useLayoutEffect runs for children first, we need to traverse the focusScope tree and find the bottom most scope that
147
- // contains the active element and set that as the activeScope
148
- for (let node of $9bf71ea28793e738$export$d06fae2ee68b101e.traverse())if ($9bf71ea28793e738$var$isElementInScope(activeElement, node.scopeRef.current)) scope = node;
149
- if (scope === $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef)) $9bf71ea28793e738$var$activeScope = scope.scopeRef;
150
- }
151
- return ()=>{
152
- // Scope may have been re-parented.
153
- let parentScope = $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef).parent.scopeRef;
154
- // Restore the active scope on unmount if this scope or a descendant scope is active.
155
- // Parent effect cleanups run before children, so we need to check if the
156
- // parent scope actually still exists before restoring the active scope to it.
157
- if ((scopeRef === $9bf71ea28793e738$var$activeScope || $9bf71ea28793e738$var$isAncestorScope(scopeRef, $9bf71ea28793e738$var$activeScope)) && (!parentScope || $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(parentScope))) $9bf71ea28793e738$var$activeScope = parentScope;
158
- $9bf71ea28793e738$export$d06fae2ee68b101e.removeTreeNode(scopeRef);
159
- };
145
+ const activeElement = (0, $6nfFC$getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined).activeElement;
146
+ let scope = null;
147
+ if ($9bf71ea28793e738$var$isElementInScope(activeElement, scopeRef.current)) {
148
+ // We need to traverse the focusScope tree and find the bottom most scope that
149
+ // contains the active element and set that as the activeScope.
150
+ for (let node of $9bf71ea28793e738$export$d06fae2ee68b101e.traverse())if (node.scopeRef && $9bf71ea28793e738$var$isElementInScope(activeElement, node.scopeRef.current)) scope = node;
151
+ if (scope === $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef)) $9bf71ea28793e738$var$activeScope = scope.scopeRef;
160
152
  }
161
153
  }, [
162
154
  scopeRef
163
155
  ]);
156
+ // This layout effect cleanup is so that the tree node is removed synchronously with react before the RAF
157
+ // in useRestoreFocus cleanup runs.
158
+ (0, $6nfFC$useLayoutEffect)(()=>{
159
+ return ()=>{
160
+ var _focusScopeTree_getTreeNode_parent, _focusScopeTree_getTreeNode;
161
+ var _focusScopeTree_getTreeNode_parent_scopeRef;
162
+ // Scope may have been re-parented.
163
+ 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;
164
+ if ((scopeRef === $9bf71ea28793e738$var$activeScope || $9bf71ea28793e738$var$isAncestorScope(scopeRef, $9bf71ea28793e738$var$activeScope)) && (!parentScope || $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(parentScope))) $9bf71ea28793e738$var$activeScope = parentScope;
165
+ $9bf71ea28793e738$export$d06fae2ee68b101e.removeTreeNode(scopeRef);
166
+ };
167
+ }, [
168
+ scopeRef
169
+ ]);
164
170
  let focusManager = (0, $6nfFC$useMemo)(()=>$9bf71ea28793e738$var$createFocusManagerForScope(scopeRef), []);
165
171
  let value = (0, $6nfFC$useMemo)(()=>({
166
172
  focusManager: focusManager,
@@ -190,9 +196,10 @@ function $9bf71ea28793e738$var$createFocusManagerForScope(scopeRef) {
190
196
  focusNext (opts = {}) {
191
197
  let scope = scopeRef.current;
192
198
  let { from: from, tabbable: tabbable, wrap: wrap, accept: accept } = opts;
193
- let node = from || document.activeElement;
199
+ let node = from || (0, $6nfFC$getOwnerDocument)(scope[0]).activeElement;
194
200
  let sentinel = scope[0].previousElementSibling;
195
- let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa($9bf71ea28793e738$var$getScopeRoot(scope), {
201
+ let scopeRoot = $9bf71ea28793e738$var$getScopeRoot(scope);
202
+ let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(scopeRoot, {
196
203
  tabbable: tabbable,
197
204
  accept: accept
198
205
  }, scope);
@@ -208,9 +215,10 @@ function $9bf71ea28793e738$var$createFocusManagerForScope(scopeRef) {
208
215
  focusPrevious (opts = {}) {
209
216
  let scope = scopeRef.current;
210
217
  let { from: from, tabbable: tabbable, wrap: wrap, accept: accept } = opts;
211
- let node = from || document.activeElement;
218
+ let node = from || (0, $6nfFC$getOwnerDocument)(scope[0]).activeElement;
212
219
  let sentinel = scope[scope.length - 1].nextElementSibling;
213
- let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa($9bf71ea28793e738$var$getScopeRoot(scope), {
220
+ let scopeRoot = $9bf71ea28793e738$var$getScopeRoot(scope);
221
+ let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(scopeRoot, {
214
222
  tabbable: tabbable,
215
223
  accept: accept
216
224
  }, scope);
@@ -226,7 +234,8 @@ function $9bf71ea28793e738$var$createFocusManagerForScope(scopeRef) {
226
234
  focusFirst (opts = {}) {
227
235
  let scope = scopeRef.current;
228
236
  let { tabbable: tabbable, accept: accept } = opts;
229
- let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa($9bf71ea28793e738$var$getScopeRoot(scope), {
237
+ let scopeRoot = $9bf71ea28793e738$var$getScopeRoot(scope);
238
+ let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(scopeRoot, {
230
239
  tabbable: tabbable,
231
240
  accept: accept
232
241
  }, scope);
@@ -238,7 +247,8 @@ function $9bf71ea28793e738$var$createFocusManagerForScope(scopeRef) {
238
247
  focusLast (opts = {}) {
239
248
  let scope = scopeRef.current;
240
249
  let { tabbable: tabbable, accept: accept } = opts;
241
- let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa($9bf71ea28793e738$var$getScopeRoot(scope), {
250
+ let scopeRoot = $9bf71ea28793e738$var$getScopeRoot(scope);
251
+ let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(scopeRoot, {
242
252
  tabbable: tabbable,
243
253
  accept: accept
244
254
  }, scope);
@@ -280,26 +290,29 @@ function $9bf71ea28793e738$var$shouldContainFocus(scopeRef) {
280
290
  }
281
291
  function $9bf71ea28793e738$var$useFocusContainment(scopeRef, contain) {
282
292
  let focusedNode = (0, $6nfFC$useRef)();
283
- let raf = (0, $6nfFC$useRef)(null);
293
+ let raf = (0, $6nfFC$useRef)();
284
294
  (0, $6nfFC$useLayoutEffect)(()=>{
285
295
  let scope = scopeRef.current;
286
296
  if (!contain) {
287
297
  // if contain was changed, then we should cancel any ongoing waits to pull focus back into containment
288
298
  if (raf.current) {
289
299
  cancelAnimationFrame(raf.current);
290
- raf.current = null;
300
+ raf.current = undefined;
291
301
  }
292
302
  return;
293
303
  }
304
+ const ownerDocument = (0, $6nfFC$getOwnerDocument)(scope ? scope[0] : undefined);
294
305
  // Handle the Tab key to contain focus within the scope
295
306
  let onKeyDown = (e)=>{
296
307
  if (e.key !== "Tab" || e.altKey || e.ctrlKey || e.metaKey || !$9bf71ea28793e738$var$shouldContainFocus(scopeRef)) return;
297
- let focusedElement = document.activeElement;
308
+ let focusedElement = ownerDocument.activeElement;
298
309
  let scope = scopeRef.current;
299
- if (!$9bf71ea28793e738$var$isElementInScope(focusedElement, scope)) return;
300
- let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa($9bf71ea28793e738$var$getScopeRoot(scope), {
310
+ if (!scope || !$9bf71ea28793e738$var$isElementInScope(focusedElement, scope)) return;
311
+ let scopeRoot = $9bf71ea28793e738$var$getScopeRoot(scope);
312
+ let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(scopeRoot, {
301
313
  tabbable: true
302
314
  }, scope);
315
+ if (!focusedElement) return;
303
316
  walker.currentNode = focusedElement;
304
317
  let nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
305
318
  if (!nextElement) {
@@ -319,7 +332,7 @@ function $9bf71ea28793e738$var$useFocusContainment(scopeRef, contain) {
319
332
  // If a focus event occurs outside the active scope (e.g. user tabs from browser location bar),
320
333
  // restore focus to the previously focused node or the first tabbable element in the active scope.
321
334
  if (focusedNode.current) focusedNode.current.focus();
322
- else if ($9bf71ea28793e738$var$activeScope) $9bf71ea28793e738$var$focusFirstInScope($9bf71ea28793e738$var$activeScope.current);
335
+ else if ($9bf71ea28793e738$var$activeScope && $9bf71ea28793e738$var$activeScope.current) $9bf71ea28793e738$var$focusFirstInScope($9bf71ea28793e738$var$activeScope.current);
323
336
  } else if ($9bf71ea28793e738$var$shouldContainFocus(scopeRef)) focusedNode.current = e.target;
324
337
  };
325
338
  let onBlur = (e)=>{
@@ -327,24 +340,25 @@ function $9bf71ea28793e738$var$useFocusContainment(scopeRef, contain) {
327
340
  if (raf.current) cancelAnimationFrame(raf.current);
328
341
  raf.current = requestAnimationFrame(()=>{
329
342
  // Use document.activeElement instead of e.relatedTarget so we can tell if user clicked into iframe
330
- if ($9bf71ea28793e738$var$shouldContainFocus(scopeRef) && !$9bf71ea28793e738$var$isElementInChildScope(document.activeElement, scopeRef)) {
343
+ if (ownerDocument.activeElement && $9bf71ea28793e738$var$shouldContainFocus(scopeRef) && !$9bf71ea28793e738$var$isElementInChildScope(ownerDocument.activeElement, scopeRef)) {
331
344
  $9bf71ea28793e738$var$activeScope = scopeRef;
332
- if (document.body.contains(e.target)) {
345
+ if (ownerDocument.body.contains(e.target)) {
346
+ var _focusedNode_current;
333
347
  focusedNode.current = e.target;
334
- focusedNode.current.focus();
335
- } else if ($9bf71ea28793e738$var$activeScope) $9bf71ea28793e738$var$focusFirstInScope($9bf71ea28793e738$var$activeScope.current);
348
+ (_focusedNode_current = focusedNode.current) === null || _focusedNode_current === void 0 ? void 0 : _focusedNode_current.focus();
349
+ } else if ($9bf71ea28793e738$var$activeScope.current) $9bf71ea28793e738$var$focusFirstInScope($9bf71ea28793e738$var$activeScope.current);
336
350
  }
337
351
  });
338
352
  };
339
- document.addEventListener("keydown", onKeyDown, false);
340
- document.addEventListener("focusin", onFocus, false);
341
- scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
342
- scope.forEach((element)=>element.addEventListener("focusout", onBlur, false));
353
+ ownerDocument.addEventListener("keydown", onKeyDown, false);
354
+ ownerDocument.addEventListener("focusin", onFocus, false);
355
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
356
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusout", onBlur, false));
343
357
  return ()=>{
344
- document.removeEventListener("keydown", onKeyDown, false);
345
- document.removeEventListener("focusin", onFocus, false);
346
- scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
347
- scope.forEach((element)=>element.removeEventListener("focusout", onBlur, false));
358
+ ownerDocument.removeEventListener("keydown", onKeyDown, false);
359
+ ownerDocument.removeEventListener("focusin", onFocus, false);
360
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
361
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusout", onBlur, false));
348
362
  };
349
363
  }, [
350
364
  scopeRef,
@@ -364,6 +378,8 @@ function $9bf71ea28793e738$var$isElementInAnyScope(element) {
364
378
  return $9bf71ea28793e738$var$isElementInChildScope(element);
365
379
  }
366
380
  function $9bf71ea28793e738$var$isElementInScope(element, scope) {
381
+ if (!element) return false;
382
+ if (!scope) return false;
367
383
  return scope.some((node)=>node.contains(element));
368
384
  }
369
385
  function $9bf71ea28793e738$var$isElementInChildScope(element, scope = null) {
@@ -372,7 +388,7 @@ function $9bf71ea28793e738$var$isElementInChildScope(element, scope = null) {
372
388
  // node.contains in isElementInScope covers child scopes that are also DOM children,
373
389
  // but does not cover child scopes in portals.
374
390
  for (let { scopeRef: s } of $9bf71ea28793e738$export$d06fae2ee68b101e.traverse($9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scope))){
375
- if ($9bf71ea28793e738$var$isElementInScope(element, s.current)) return true;
391
+ if (s && $9bf71ea28793e738$var$isElementInScope(element, s.current)) return true;
376
392
  }
377
393
  return false;
378
394
  }
@@ -402,14 +418,16 @@ function $9bf71ea28793e738$var$focusElement(element, scroll = false) {
402
418
  }
403
419
  function $9bf71ea28793e738$var$focusFirstInScope(scope, tabbable = true) {
404
420
  let sentinel = scope[0].previousElementSibling;
405
- let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa($9bf71ea28793e738$var$getScopeRoot(scope), {
421
+ let scopeRoot = $9bf71ea28793e738$var$getScopeRoot(scope);
422
+ let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(scopeRoot, {
406
423
  tabbable: tabbable
407
424
  }, scope);
408
425
  walker.currentNode = sentinel;
409
426
  let nextNode = walker.nextNode();
410
427
  // If the scope does not contain a tabbable element, use the first focusable element.
411
428
  if (tabbable && !nextNode) {
412
- walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa($9bf71ea28793e738$var$getScopeRoot(scope), {
429
+ scopeRoot = $9bf71ea28793e738$var$getScopeRoot(scope);
430
+ walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(scopeRoot, {
413
431
  tabbable: false
414
432
  }, scope);
415
433
  walker.currentNode = sentinel;
@@ -422,7 +440,8 @@ function $9bf71ea28793e738$var$useAutoFocus(scopeRef, autoFocus) {
422
440
  (0, $6nfFC$useEffect)(()=>{
423
441
  if (autoFocusRef.current) {
424
442
  $9bf71ea28793e738$var$activeScope = scopeRef;
425
- if (!$9bf71ea28793e738$var$isElementInScope(document.activeElement, $9bf71ea28793e738$var$activeScope.current)) $9bf71ea28793e738$var$focusFirstInScope(scopeRef.current);
443
+ const ownerDocument = (0, $6nfFC$getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined);
444
+ if (!$9bf71ea28793e738$var$isElementInScope(ownerDocument.activeElement, $9bf71ea28793e738$var$activeScope.current) && scopeRef.current) $9bf71ea28793e738$var$focusFirstInScope(scopeRef.current);
426
445
  }
427
446
  autoFocusRef.current = false;
428
447
  }, [
@@ -435,16 +454,17 @@ function $9bf71ea28793e738$var$useActiveScopeTracker(scopeRef, restore, contain)
435
454
  (0, $6nfFC$useLayoutEffect)(()=>{
436
455
  if (restore || contain) return;
437
456
  let scope = scopeRef.current;
457
+ const ownerDocument = (0, $6nfFC$getOwnerDocument)(scope ? scope[0] : undefined);
438
458
  let onFocus = (e)=>{
439
459
  let target = e.target;
440
460
  if ($9bf71ea28793e738$var$isElementInScope(target, scopeRef.current)) $9bf71ea28793e738$var$activeScope = scopeRef;
441
461
  else if (!$9bf71ea28793e738$var$isElementInAnyScope(target)) $9bf71ea28793e738$var$activeScope = null;
442
462
  };
443
- document.addEventListener("focusin", onFocus, false);
444
- scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
463
+ ownerDocument.addEventListener("focusin", onFocus, false);
464
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
445
465
  return ()=>{
446
- document.removeEventListener("focusin", onFocus, false);
447
- scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
466
+ ownerDocument.removeEventListener("focusin", onFocus, false);
467
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
448
468
  };
449
469
  }, [
450
470
  scopeRef,
@@ -462,22 +482,24 @@ function $9bf71ea28793e738$var$shouldRestoreFocus(scopeRef) {
462
482
  }
463
483
  function $9bf71ea28793e738$var$useRestoreFocus(scopeRef, restoreFocus, contain) {
464
484
  // create a ref during render instead of useLayoutEffect so the active element is saved before a child with autoFocus=true mounts.
465
- const nodeToRestoreRef = (0, $6nfFC$useRef)(typeof document !== "undefined" ? document.activeElement : null);
485
+ // eslint-disable-next-line no-restricted-globals
486
+ const nodeToRestoreRef = (0, $6nfFC$useRef)(typeof document !== "undefined" ? (0, $6nfFC$getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined).activeElement : null);
466
487
  // restoring scopes should all track if they are active regardless of contain, but contain already tracks it plus logic to contain the focus
467
488
  // restoring-non-containing scopes should only care if they become active so they can perform the restore
468
489
  (0, $6nfFC$useLayoutEffect)(()=>{
469
490
  let scope = scopeRef.current;
491
+ const ownerDocument = (0, $6nfFC$getOwnerDocument)(scope ? scope[0] : undefined);
470
492
  if (!restoreFocus || contain) return;
471
493
  let onFocus = ()=>{
472
494
  // If focusing an element in a child scope of the currently active scope, the child becomes active.
473
495
  // Moving out of the active scope to an ancestor is not allowed.
474
- if ((!$9bf71ea28793e738$var$activeScope || $9bf71ea28793e738$var$isAncestorScope($9bf71ea28793e738$var$activeScope, scopeRef)) && $9bf71ea28793e738$var$isElementInScope(document.activeElement, scopeRef.current)) $9bf71ea28793e738$var$activeScope = scopeRef;
496
+ if ((!$9bf71ea28793e738$var$activeScope || $9bf71ea28793e738$var$isAncestorScope($9bf71ea28793e738$var$activeScope, scopeRef)) && $9bf71ea28793e738$var$isElementInScope(ownerDocument.activeElement, scopeRef.current)) $9bf71ea28793e738$var$activeScope = scopeRef;
475
497
  };
476
- document.addEventListener("focusin", onFocus, false);
477
- scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
498
+ ownerDocument.addEventListener("focusin", onFocus, false);
499
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
478
500
  return ()=>{
479
- document.removeEventListener("focusin", onFocus, false);
480
- scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
501
+ ownerDocument.removeEventListener("focusin", onFocus, false);
502
+ scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
481
503
  };
482
504
  // eslint-disable-next-line react-hooks/exhaustive-deps
483
505
  }, [
@@ -485,6 +507,7 @@ function $9bf71ea28793e738$var$useRestoreFocus(scopeRef, restoreFocus, contain)
485
507
  contain
486
508
  ]);
487
509
  (0, $6nfFC$useLayoutEffect)(()=>{
510
+ const ownerDocument = (0, $6nfFC$getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined);
488
511
  if (!restoreFocus) return;
489
512
  // Handle the Tab key so that tabbing out of the scope goes to the next element
490
513
  // after the node that had focus when the scope mounted. This is important when
@@ -492,19 +515,21 @@ function $9bf71ea28793e738$var$useRestoreFocus(scopeRef, restoreFocus, contain)
492
515
  // tabbing out of the overlay.
493
516
  let onKeyDown = (e)=>{
494
517
  if (e.key !== "Tab" || e.altKey || e.ctrlKey || e.metaKey || !$9bf71ea28793e738$var$shouldContainFocus(scopeRef)) return;
495
- let focusedElement = document.activeElement;
518
+ let focusedElement = ownerDocument.activeElement;
496
519
  if (!$9bf71ea28793e738$var$isElementInScope(focusedElement, scopeRef.current)) return;
497
- let nodeToRestore = $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef).nodeToRestore;
520
+ let treeNode = $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef);
521
+ if (!treeNode) return;
522
+ let nodeToRestore = treeNode.nodeToRestore;
498
523
  // Create a DOM tree walker that matches all tabbable elements
499
- let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(document.body, {
524
+ let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(ownerDocument.body, {
500
525
  tabbable: true
501
526
  });
502
527
  // Find the next tabbable element after the currently focused element
503
528
  walker.currentNode = focusedElement;
504
529
  let nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
505
- if (!document.body.contains(nodeToRestore) || nodeToRestore === document.body) {
506
- nodeToRestore = null;
507
- $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef).nodeToRestore = null;
530
+ if (!nodeToRestore || !ownerDocument.body.contains(nodeToRestore) || nodeToRestore === ownerDocument.body) {
531
+ nodeToRestore = undefined;
532
+ treeNode.nodeToRestore = undefined;
508
533
  }
509
534
  // If there is no next element, or it is outside the current scope, move focus to the
510
535
  // next element after the node to restore to instead.
@@ -523,9 +548,9 @@ function $9bf71ea28793e738$var$useRestoreFocus(scopeRef, restoreFocus, contain)
523
548
  else $9bf71ea28793e738$var$focusElement(nodeToRestore, true);
524
549
  }
525
550
  };
526
- if (!contain) document.addEventListener("keydown", onKeyDown, true);
551
+ if (!contain) ownerDocument.addEventListener("keydown", onKeyDown, true);
527
552
  return ()=>{
528
- if (!contain) document.removeEventListener("keydown", onKeyDown, true);
553
+ if (!contain) ownerDocument.removeEventListener("keydown", onKeyDown, true);
529
554
  };
530
555
  }, [
531
556
  scopeRef,
@@ -534,22 +559,28 @@ function $9bf71ea28793e738$var$useRestoreFocus(scopeRef, restoreFocus, contain)
534
559
  ]);
535
560
  // useLayoutEffect instead of useEffect so the active element is saved synchronously instead of asynchronously.
536
561
  (0, $6nfFC$useLayoutEffect)(()=>{
562
+ const ownerDocument = (0, $6nfFC$getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined);
537
563
  if (!restoreFocus) return;
538
- $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef).nodeToRestore = nodeToRestoreRef.current;
564
+ let treeNode = $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef);
565
+ if (!treeNode) return;
566
+ var _nodeToRestoreRef_current;
567
+ treeNode.nodeToRestore = (_nodeToRestoreRef_current = nodeToRestoreRef.current) !== null && _nodeToRestoreRef_current !== void 0 ? _nodeToRestoreRef_current : undefined;
539
568
  return ()=>{
540
- let nodeToRestore = $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef).nodeToRestore;
569
+ let treeNode = $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef);
570
+ if (!treeNode) return;
571
+ let nodeToRestore = treeNode.nodeToRestore;
541
572
  // if we already lost focus to the body and this was the active scope, then we should attempt to restore
542
573
  if (restoreFocus && nodeToRestore && // eslint-disable-next-line react-hooks/exhaustive-deps
543
- ($9bf71ea28793e738$var$isElementInScope(document.activeElement, scopeRef.current) || document.activeElement === document.body && $9bf71ea28793e738$var$shouldRestoreFocus(scopeRef))) {
574
+ ($9bf71ea28793e738$var$isElementInScope(ownerDocument.activeElement, scopeRef.current) || ownerDocument.activeElement === ownerDocument.body && $9bf71ea28793e738$var$shouldRestoreFocus(scopeRef))) {
544
575
  // freeze the focusScopeTree so it persists after the raf, otherwise during unmount nodes are removed from it
545
576
  let clonedTree = $9bf71ea28793e738$export$d06fae2ee68b101e.clone();
546
577
  requestAnimationFrame(()=>{
547
578
  // Only restore focus if we've lost focus to the body, the alternative is that focus has been purposefully moved elsewhere
548
- if (document.activeElement === document.body) {
579
+ if (ownerDocument.activeElement === ownerDocument.body) {
549
580
  // look up the tree starting with our scope to find a nodeToRestore still in the DOM
550
581
  let treeNode = clonedTree.getTreeNode(scopeRef);
551
582
  while(treeNode){
552
- if (treeNode.nodeToRestore && document.body.contains(treeNode.nodeToRestore)) {
583
+ if (treeNode.nodeToRestore && treeNode.nodeToRestore.isConnected) {
553
584
  $9bf71ea28793e738$var$focusElement(treeNode.nodeToRestore);
554
585
  return;
555
586
  }
@@ -559,7 +590,7 @@ function $9bf71ea28793e738$var$useRestoreFocus(scopeRef, restoreFocus, contain)
559
590
  // ancestor scope that is still in the tree.
560
591
  treeNode = clonedTree.getTreeNode(scopeRef);
561
592
  while(treeNode){
562
- if (treeNode.scopeRef && $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(treeNode.scopeRef)) {
593
+ if (treeNode.scopeRef && treeNode.scopeRef.current && $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(treeNode.scopeRef)) {
563
594
  $9bf71ea28793e738$var$focusFirstInScope(treeNode.scopeRef.current, true);
564
595
  return;
565
596
  }
@@ -576,7 +607,7 @@ function $9bf71ea28793e738$var$useRestoreFocus(scopeRef, restoreFocus, contain)
576
607
  }
577
608
  function $9bf71ea28793e738$export$2d6ec8fc375ceafa(root, opts, scope) {
578
609
  let selector = (opts === null || opts === void 0 ? void 0 : opts.tabbable) ? $9bf71ea28793e738$var$TABBABLE_ELEMENT_SELECTOR : $9bf71ea28793e738$var$FOCUSABLE_ELEMENT_SELECTOR;
579
- let walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT, {
610
+ let walker = (0, $6nfFC$getOwnerDocument)(root).createTreeWalker(root, NodeFilter.SHOW_ELEMENT, {
580
611
  acceptNode (node) {
581
612
  var _opts_from;
582
613
  // Skip nodes inside the starting node.
@@ -592,9 +623,9 @@ function $9bf71ea28793e738$export$c5251b9e124bf29(ref, defaultOptions = {}) {
592
623
  return {
593
624
  focusNext (opts = {}) {
594
625
  let root = ref.current;
595
- if (!root) return;
626
+ if (!root) return null;
596
627
  let { from: from, tabbable: tabbable = defaultOptions.tabbable, wrap: wrap = defaultOptions.wrap, accept: accept = defaultOptions.accept } = opts;
597
- let node = from || document.activeElement;
628
+ let node = from || (0, $6nfFC$getOwnerDocument)(root).activeElement;
598
629
  let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(root, {
599
630
  tabbable: tabbable,
600
631
  accept: accept
@@ -610,9 +641,9 @@ function $9bf71ea28793e738$export$c5251b9e124bf29(ref, defaultOptions = {}) {
610
641
  },
611
642
  focusPrevious (opts = defaultOptions) {
612
643
  let root = ref.current;
613
- if (!root) return;
644
+ if (!root) return null;
614
645
  let { from: from, tabbable: tabbable = defaultOptions.tabbable, wrap: wrap = defaultOptions.wrap, accept: accept = defaultOptions.accept } = opts;
615
- let node = from || document.activeElement;
646
+ let node = from || (0, $6nfFC$getOwnerDocument)(root).activeElement;
616
647
  let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(root, {
617
648
  tabbable: tabbable,
618
649
  accept: accept
@@ -621,19 +652,22 @@ function $9bf71ea28793e738$export$c5251b9e124bf29(ref, defaultOptions = {}) {
621
652
  else {
622
653
  let next = $9bf71ea28793e738$var$last(walker);
623
654
  if (next) $9bf71ea28793e738$var$focusElement(next, true);
624
- return next;
655
+ return next !== null && next !== void 0 ? next : null;
625
656
  }
626
657
  let previousNode = walker.previousNode();
627
658
  if (!previousNode && wrap) {
628
659
  walker.currentNode = root;
629
- previousNode = $9bf71ea28793e738$var$last(walker);
660
+ let lastNode = $9bf71ea28793e738$var$last(walker);
661
+ if (!lastNode) // couldn't wrap
662
+ return null;
663
+ previousNode = lastNode;
630
664
  }
631
665
  if (previousNode) $9bf71ea28793e738$var$focusElement(previousNode, true);
632
- return previousNode;
666
+ return previousNode !== null && previousNode !== void 0 ? previousNode : null;
633
667
  },
634
668
  focusFirst (opts = defaultOptions) {
635
669
  let root = ref.current;
636
- if (!root) return;
670
+ if (!root) return null;
637
671
  let { tabbable: tabbable = defaultOptions.tabbable, accept: accept = defaultOptions.accept } = opts;
638
672
  let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(root, {
639
673
  tabbable: tabbable,
@@ -645,7 +679,7 @@ function $9bf71ea28793e738$export$c5251b9e124bf29(ref, defaultOptions = {}) {
645
679
  },
646
680
  focusLast (opts = defaultOptions) {
647
681
  let root = ref.current;
648
- if (!root) return;
682
+ if (!root) return null;
649
683
  let { tabbable: tabbable = defaultOptions.tabbable, accept: accept = defaultOptions.accept } = opts;
650
684
  let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(root, {
651
685
  tabbable: tabbable,
@@ -653,12 +687,12 @@ function $9bf71ea28793e738$export$c5251b9e124bf29(ref, defaultOptions = {}) {
653
687
  });
654
688
  let next = $9bf71ea28793e738$var$last(walker);
655
689
  if (next) $9bf71ea28793e738$var$focusElement(next, true);
656
- return next;
690
+ return next !== null && next !== void 0 ? next : null;
657
691
  }
658
692
  };
659
693
  }
660
694
  function $9bf71ea28793e738$var$last(walker) {
661
- let next;
695
+ let next = undefined;
662
696
  let last;
663
697
  do {
664
698
  last = walker.lastChild();
@@ -675,6 +709,7 @@ class $9bf71ea28793e738$var$Tree {
675
709
  }
676
710
  addTreeNode(scopeRef, parent, nodeToRestore) {
677
711
  let parentNode = this.fastMap.get(parent !== null && parent !== void 0 ? parent : null);
712
+ if (!parentNode) return;
678
713
  let node = new $9bf71ea28793e738$var$TreeNode({
679
714
  scopeRef: scopeRef
680
715
  });
@@ -690,13 +725,16 @@ class $9bf71ea28793e738$var$Tree {
690
725
  // never remove the root
691
726
  if (scopeRef === null) return;
692
727
  let node = this.fastMap.get(scopeRef);
728
+ if (!node) return;
693
729
  let parentNode = node.parent;
694
730
  // when we remove a scope, check if any sibling scopes are trying to restore focus to something inside the scope we're removing
695
731
  // if we are, then replace the siblings restore with the restore from the scope we're removing
696
- for (let current of this.traverse())if (current !== node && node.nodeToRestore && current.nodeToRestore && node.scopeRef.current && $9bf71ea28793e738$var$isElementInScope(current.nodeToRestore, node.scopeRef.current)) current.nodeToRestore = node.nodeToRestore;
732
+ 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;
697
733
  let children = node.children;
698
- parentNode.removeChild(node);
699
- if (children.size > 0) children.forEach((child)=>parentNode.addChild(child));
734
+ if (parentNode) {
735
+ parentNode.removeChild(node);
736
+ if (children.size > 0) children.forEach((child)=>parentNode && parentNode.addChild(child));
737
+ }
700
738
  this.fastMap.delete(node.scopeRef);
701
739
  }
702
740
  // Pre Order Depth First
@@ -705,8 +743,10 @@ class $9bf71ea28793e738$var$Tree {
705
743
  if (node.children.size > 0) for (let child of node.children)yield* this.traverse(child);
706
744
  }
707
745
  clone() {
746
+ var _node_parent;
708
747
  let newTree = new $9bf71ea28793e738$var$Tree();
709
- for (let node of this.traverse())newTree.addTreeNode(node.scopeRef, node.parent.scopeRef, node.nodeToRestore);
748
+ var _node_parent_scopeRef;
749
+ 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);
710
750
  return newTree;
711
751
  }
712
752
  constructor(){
@@ -828,9 +868,10 @@ function $e6afbd83fe6ebbd2$var$useFocusableContext(ref) {
828
868
  * Provides DOM props to the nearest focusable child.
829
869
  */ function $e6afbd83fe6ebbd2$var$FocusableProvider(props, ref) {
830
870
  let { children: children, ...otherProps } = props;
871
+ let objRef = (0, $6nfFC$useObjectRef)(ref);
831
872
  let context = {
832
873
  ...otherProps,
833
- ref: ref
874
+ ref: objRef
834
875
  };
835
876
  return /*#__PURE__*/ (0, $6nfFC$react).createElement($e6afbd83fe6ebbd2$var$FocusableContext.Provider, {
836
877
  value: context