@react-aria/focus 3.14.3 → 3.15.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/import.mjs +102 -71
- package/dist/main.js +103 -70
- package/dist/main.js.map +1 -1
- package/dist/module.js +102 -71
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +5 -5
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/FocusScope.tsx +156 -113
- package/src/isElementVisible.ts +3 -3
- package/src/useFocusable.tsx +6 -5
package/dist/import.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
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";
|
|
2
|
+
import {useLayoutEffect as $6nfFC$useLayoutEffect, runAfterTransition as $6nfFC$runAfterTransition, focusWithoutScrolling as $6nfFC$focusWithoutScrolling, mergeProps as $6nfFC$mergeProps, useSyncRef as $6nfFC$useSyncRef, useObjectRef as $6nfFC$useObjectRef} 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
|
|
|
@@ -86,8 +86,8 @@ const $9bf71ea28793e738$var$FocusContext = /*#__PURE__*/ (0, $6nfFC$react).creat
|
|
|
86
86
|
let $9bf71ea28793e738$var$activeScope = null;
|
|
87
87
|
function $9bf71ea28793e738$export$20e40289641fbbb6(props) {
|
|
88
88
|
let { children: children, contain: contain, restoreFocus: restoreFocus, autoFocus: autoFocus } = props;
|
|
89
|
-
let startRef = (0, $6nfFC$useRef)();
|
|
90
|
-
let endRef = (0, $6nfFC$useRef)();
|
|
89
|
+
let startRef = (0, $6nfFC$useRef)(null);
|
|
90
|
+
let endRef = (0, $6nfFC$useRef)(null);
|
|
91
91
|
let scopeRef = (0, $6nfFC$useRef)([]);
|
|
92
92
|
let { parentNode: parentNode } = (0, $6nfFC$useContext)($9bf71ea28793e738$var$FocusContext) || {};
|
|
93
93
|
// Create a tree node here so we can add children to it even before it is added to the tree.
|
|
@@ -115,13 +115,14 @@ function $9bf71ea28793e738$export$20e40289641fbbb6(props) {
|
|
|
115
115
|
]);
|
|
116
116
|
(0, $6nfFC$useLayoutEffect)(()=>{
|
|
117
117
|
let node = $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef);
|
|
118
|
-
node.contain = contain;
|
|
118
|
+
if (node) node.contain = !!contain;
|
|
119
119
|
}, [
|
|
120
120
|
contain
|
|
121
121
|
]);
|
|
122
122
|
(0, $6nfFC$useLayoutEffect)(()=>{
|
|
123
|
+
var _startRef_current;
|
|
123
124
|
// Find all rendered nodes between the sentinels and add them to the scope.
|
|
124
|
-
let node = startRef.current.nextSibling;
|
|
125
|
+
let node = (_startRef_current = startRef.current) === null || _startRef_current === void 0 ? void 0 : _startRef_current.nextSibling;
|
|
125
126
|
let nodes = [];
|
|
126
127
|
while(node && node !== endRef.current){
|
|
127
128
|
nodes.push(node);
|
|
@@ -135,32 +136,34 @@ function $9bf71ea28793e738$export$20e40289641fbbb6(props) {
|
|
|
135
136
|
$9bf71ea28793e738$var$useFocusContainment(scopeRef, contain);
|
|
136
137
|
$9bf71ea28793e738$var$useRestoreFocus(scopeRef, restoreFocus, contain);
|
|
137
138
|
$9bf71ea28793e738$var$useAutoFocus(scopeRef, autoFocus);
|
|
138
|
-
//
|
|
139
|
+
// This needs to be an effect so that activeScope is updated after the FocusScope tree is complete.
|
|
140
|
+
// It cannot be a useLayoutEffect because the parent of this node hasn't been attached in the tree yet.
|
|
139
141
|
(0, $6nfFC$useEffect)(()=>{
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
//
|
|
144
|
-
//
|
|
145
|
-
if ($9bf71ea28793e738$var$isElementInScope(activeElement, scopeRef.current))
|
|
146
|
-
|
|
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
|
-
};
|
|
142
|
+
let activeElement = document.activeElement;
|
|
143
|
+
let scope = null;
|
|
144
|
+
if ($9bf71ea28793e738$var$isElementInScope(activeElement, scopeRef.current)) {
|
|
145
|
+
// We need to traverse the focusScope tree and find the bottom most scope that
|
|
146
|
+
// contains the active element and set that as the activeScope.
|
|
147
|
+
for (let node of $9bf71ea28793e738$export$d06fae2ee68b101e.traverse())if (node.scopeRef && $9bf71ea28793e738$var$isElementInScope(activeElement, node.scopeRef.current)) scope = node;
|
|
148
|
+
if (scope === $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef)) $9bf71ea28793e738$var$activeScope = scope.scopeRef;
|
|
160
149
|
}
|
|
161
150
|
}, [
|
|
162
151
|
scopeRef
|
|
163
152
|
]);
|
|
153
|
+
// This layout effect cleanup is so that the tree node is removed synchronously with react before the RAF
|
|
154
|
+
// in useRestoreFocus cleanup runs.
|
|
155
|
+
(0, $6nfFC$useLayoutEffect)(()=>{
|
|
156
|
+
return ()=>{
|
|
157
|
+
var _focusScopeTree_getTreeNode_parent, _focusScopeTree_getTreeNode;
|
|
158
|
+
var _focusScopeTree_getTreeNode_parent_scopeRef;
|
|
159
|
+
// Scope may have been re-parented.
|
|
160
|
+
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;
|
|
161
|
+
if ((scopeRef === $9bf71ea28793e738$var$activeScope || $9bf71ea28793e738$var$isAncestorScope(scopeRef, $9bf71ea28793e738$var$activeScope)) && (!parentScope || $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(parentScope))) $9bf71ea28793e738$var$activeScope = parentScope;
|
|
162
|
+
$9bf71ea28793e738$export$d06fae2ee68b101e.removeTreeNode(scopeRef);
|
|
163
|
+
};
|
|
164
|
+
}, [
|
|
165
|
+
scopeRef
|
|
166
|
+
]);
|
|
164
167
|
let focusManager = (0, $6nfFC$useMemo)(()=>$9bf71ea28793e738$var$createFocusManagerForScope(scopeRef), []);
|
|
165
168
|
let value = (0, $6nfFC$useMemo)(()=>({
|
|
166
169
|
focusManager: focusManager,
|
|
@@ -192,7 +195,8 @@ function $9bf71ea28793e738$var$createFocusManagerForScope(scopeRef) {
|
|
|
192
195
|
let { from: from, tabbable: tabbable, wrap: wrap, accept: accept } = opts;
|
|
193
196
|
let node = from || document.activeElement;
|
|
194
197
|
let sentinel = scope[0].previousElementSibling;
|
|
195
|
-
let
|
|
198
|
+
let scopeRoot = $9bf71ea28793e738$var$getScopeRoot(scope);
|
|
199
|
+
let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(scopeRoot, {
|
|
196
200
|
tabbable: tabbable,
|
|
197
201
|
accept: accept
|
|
198
202
|
}, scope);
|
|
@@ -210,7 +214,8 @@ function $9bf71ea28793e738$var$createFocusManagerForScope(scopeRef) {
|
|
|
210
214
|
let { from: from, tabbable: tabbable, wrap: wrap, accept: accept } = opts;
|
|
211
215
|
let node = from || document.activeElement;
|
|
212
216
|
let sentinel = scope[scope.length - 1].nextElementSibling;
|
|
213
|
-
let
|
|
217
|
+
let scopeRoot = $9bf71ea28793e738$var$getScopeRoot(scope);
|
|
218
|
+
let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(scopeRoot, {
|
|
214
219
|
tabbable: tabbable,
|
|
215
220
|
accept: accept
|
|
216
221
|
}, scope);
|
|
@@ -226,7 +231,8 @@ function $9bf71ea28793e738$var$createFocusManagerForScope(scopeRef) {
|
|
|
226
231
|
focusFirst (opts = {}) {
|
|
227
232
|
let scope = scopeRef.current;
|
|
228
233
|
let { tabbable: tabbable, accept: accept } = opts;
|
|
229
|
-
let
|
|
234
|
+
let scopeRoot = $9bf71ea28793e738$var$getScopeRoot(scope);
|
|
235
|
+
let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(scopeRoot, {
|
|
230
236
|
tabbable: tabbable,
|
|
231
237
|
accept: accept
|
|
232
238
|
}, scope);
|
|
@@ -238,7 +244,8 @@ function $9bf71ea28793e738$var$createFocusManagerForScope(scopeRef) {
|
|
|
238
244
|
focusLast (opts = {}) {
|
|
239
245
|
let scope = scopeRef.current;
|
|
240
246
|
let { tabbable: tabbable, accept: accept } = opts;
|
|
241
|
-
let
|
|
247
|
+
let scopeRoot = $9bf71ea28793e738$var$getScopeRoot(scope);
|
|
248
|
+
let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(scopeRoot, {
|
|
242
249
|
tabbable: tabbable,
|
|
243
250
|
accept: accept
|
|
244
251
|
}, scope);
|
|
@@ -280,14 +287,14 @@ function $9bf71ea28793e738$var$shouldContainFocus(scopeRef) {
|
|
|
280
287
|
}
|
|
281
288
|
function $9bf71ea28793e738$var$useFocusContainment(scopeRef, contain) {
|
|
282
289
|
let focusedNode = (0, $6nfFC$useRef)();
|
|
283
|
-
let raf = (0, $6nfFC$useRef)(
|
|
290
|
+
let raf = (0, $6nfFC$useRef)();
|
|
284
291
|
(0, $6nfFC$useLayoutEffect)(()=>{
|
|
285
292
|
let scope = scopeRef.current;
|
|
286
293
|
if (!contain) {
|
|
287
294
|
// if contain was changed, then we should cancel any ongoing waits to pull focus back into containment
|
|
288
295
|
if (raf.current) {
|
|
289
296
|
cancelAnimationFrame(raf.current);
|
|
290
|
-
raf.current =
|
|
297
|
+
raf.current = undefined;
|
|
291
298
|
}
|
|
292
299
|
return;
|
|
293
300
|
}
|
|
@@ -296,10 +303,12 @@ function $9bf71ea28793e738$var$useFocusContainment(scopeRef, contain) {
|
|
|
296
303
|
if (e.key !== "Tab" || e.altKey || e.ctrlKey || e.metaKey || !$9bf71ea28793e738$var$shouldContainFocus(scopeRef)) return;
|
|
297
304
|
let focusedElement = document.activeElement;
|
|
298
305
|
let scope = scopeRef.current;
|
|
299
|
-
if (!$9bf71ea28793e738$var$isElementInScope(focusedElement, scope)) return;
|
|
300
|
-
let
|
|
306
|
+
if (!scope || !$9bf71ea28793e738$var$isElementInScope(focusedElement, scope)) return;
|
|
307
|
+
let scopeRoot = $9bf71ea28793e738$var$getScopeRoot(scope);
|
|
308
|
+
let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(scopeRoot, {
|
|
301
309
|
tabbable: true
|
|
302
310
|
}, scope);
|
|
311
|
+
if (!focusedElement) return;
|
|
303
312
|
walker.currentNode = focusedElement;
|
|
304
313
|
let nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
|
|
305
314
|
if (!nextElement) {
|
|
@@ -319,7 +328,7 @@ function $9bf71ea28793e738$var$useFocusContainment(scopeRef, contain) {
|
|
|
319
328
|
// If a focus event occurs outside the active scope (e.g. user tabs from browser location bar),
|
|
320
329
|
// restore focus to the previously focused node or the first tabbable element in the active scope.
|
|
321
330
|
if (focusedNode.current) focusedNode.current.focus();
|
|
322
|
-
else if ($9bf71ea28793e738$var$activeScope) $9bf71ea28793e738$var$focusFirstInScope($9bf71ea28793e738$var$activeScope.current);
|
|
331
|
+
else if ($9bf71ea28793e738$var$activeScope && $9bf71ea28793e738$var$activeScope.current) $9bf71ea28793e738$var$focusFirstInScope($9bf71ea28793e738$var$activeScope.current);
|
|
323
332
|
} else if ($9bf71ea28793e738$var$shouldContainFocus(scopeRef)) focusedNode.current = e.target;
|
|
324
333
|
};
|
|
325
334
|
let onBlur = (e)=>{
|
|
@@ -327,24 +336,25 @@ function $9bf71ea28793e738$var$useFocusContainment(scopeRef, contain) {
|
|
|
327
336
|
if (raf.current) cancelAnimationFrame(raf.current);
|
|
328
337
|
raf.current = requestAnimationFrame(()=>{
|
|
329
338
|
// 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)) {
|
|
339
|
+
if (document.activeElement && $9bf71ea28793e738$var$shouldContainFocus(scopeRef) && !$9bf71ea28793e738$var$isElementInChildScope(document.activeElement, scopeRef)) {
|
|
331
340
|
$9bf71ea28793e738$var$activeScope = scopeRef;
|
|
332
341
|
if (document.body.contains(e.target)) {
|
|
342
|
+
var _focusedNode_current;
|
|
333
343
|
focusedNode.current = e.target;
|
|
334
|
-
focusedNode.current.focus();
|
|
335
|
-
} else if ($9bf71ea28793e738$var$activeScope) $9bf71ea28793e738$var$focusFirstInScope($9bf71ea28793e738$var$activeScope.current);
|
|
344
|
+
(_focusedNode_current = focusedNode.current) === null || _focusedNode_current === void 0 ? void 0 : _focusedNode_current.focus();
|
|
345
|
+
} else if ($9bf71ea28793e738$var$activeScope.current) $9bf71ea28793e738$var$focusFirstInScope($9bf71ea28793e738$var$activeScope.current);
|
|
336
346
|
}
|
|
337
347
|
});
|
|
338
348
|
};
|
|
339
349
|
document.addEventListener("keydown", onKeyDown, false);
|
|
340
350
|
document.addEventListener("focusin", onFocus, false);
|
|
341
|
-
scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
|
|
342
|
-
scope.forEach((element)=>element.addEventListener("focusout", onBlur, false));
|
|
351
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
|
|
352
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusout", onBlur, false));
|
|
343
353
|
return ()=>{
|
|
344
354
|
document.removeEventListener("keydown", onKeyDown, false);
|
|
345
355
|
document.removeEventListener("focusin", onFocus, false);
|
|
346
|
-
scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
|
|
347
|
-
scope.forEach((element)=>element.removeEventListener("focusout", onBlur, false));
|
|
356
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
|
|
357
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusout", onBlur, false));
|
|
348
358
|
};
|
|
349
359
|
}, [
|
|
350
360
|
scopeRef,
|
|
@@ -364,6 +374,8 @@ function $9bf71ea28793e738$var$isElementInAnyScope(element) {
|
|
|
364
374
|
return $9bf71ea28793e738$var$isElementInChildScope(element);
|
|
365
375
|
}
|
|
366
376
|
function $9bf71ea28793e738$var$isElementInScope(element, scope) {
|
|
377
|
+
if (!element) return false;
|
|
378
|
+
if (!scope) return false;
|
|
367
379
|
return scope.some((node)=>node.contains(element));
|
|
368
380
|
}
|
|
369
381
|
function $9bf71ea28793e738$var$isElementInChildScope(element, scope = null) {
|
|
@@ -372,7 +384,7 @@ function $9bf71ea28793e738$var$isElementInChildScope(element, scope = null) {
|
|
|
372
384
|
// node.contains in isElementInScope covers child scopes that are also DOM children,
|
|
373
385
|
// but does not cover child scopes in portals.
|
|
374
386
|
for (let { scopeRef: s } of $9bf71ea28793e738$export$d06fae2ee68b101e.traverse($9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scope))){
|
|
375
|
-
if ($9bf71ea28793e738$var$isElementInScope(element, s.current)) return true;
|
|
387
|
+
if (s && $9bf71ea28793e738$var$isElementInScope(element, s.current)) return true;
|
|
376
388
|
}
|
|
377
389
|
return false;
|
|
378
390
|
}
|
|
@@ -402,14 +414,16 @@ function $9bf71ea28793e738$var$focusElement(element, scroll = false) {
|
|
|
402
414
|
}
|
|
403
415
|
function $9bf71ea28793e738$var$focusFirstInScope(scope, tabbable = true) {
|
|
404
416
|
let sentinel = scope[0].previousElementSibling;
|
|
405
|
-
let
|
|
417
|
+
let scopeRoot = $9bf71ea28793e738$var$getScopeRoot(scope);
|
|
418
|
+
let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(scopeRoot, {
|
|
406
419
|
tabbable: tabbable
|
|
407
420
|
}, scope);
|
|
408
421
|
walker.currentNode = sentinel;
|
|
409
422
|
let nextNode = walker.nextNode();
|
|
410
423
|
// If the scope does not contain a tabbable element, use the first focusable element.
|
|
411
424
|
if (tabbable && !nextNode) {
|
|
412
|
-
|
|
425
|
+
scopeRoot = $9bf71ea28793e738$var$getScopeRoot(scope);
|
|
426
|
+
walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(scopeRoot, {
|
|
413
427
|
tabbable: false
|
|
414
428
|
}, scope);
|
|
415
429
|
walker.currentNode = sentinel;
|
|
@@ -422,7 +436,7 @@ function $9bf71ea28793e738$var$useAutoFocus(scopeRef, autoFocus) {
|
|
|
422
436
|
(0, $6nfFC$useEffect)(()=>{
|
|
423
437
|
if (autoFocusRef.current) {
|
|
424
438
|
$9bf71ea28793e738$var$activeScope = scopeRef;
|
|
425
|
-
if (!$9bf71ea28793e738$var$isElementInScope(document.activeElement, $9bf71ea28793e738$var$activeScope.current)) $9bf71ea28793e738$var$focusFirstInScope(scopeRef.current);
|
|
439
|
+
if (!$9bf71ea28793e738$var$isElementInScope(document.activeElement, $9bf71ea28793e738$var$activeScope.current) && scopeRef.current) $9bf71ea28793e738$var$focusFirstInScope(scopeRef.current);
|
|
426
440
|
}
|
|
427
441
|
autoFocusRef.current = false;
|
|
428
442
|
}, [
|
|
@@ -441,10 +455,10 @@ function $9bf71ea28793e738$var$useActiveScopeTracker(scopeRef, restore, contain)
|
|
|
441
455
|
else if (!$9bf71ea28793e738$var$isElementInAnyScope(target)) $9bf71ea28793e738$var$activeScope = null;
|
|
442
456
|
};
|
|
443
457
|
document.addEventListener("focusin", onFocus, false);
|
|
444
|
-
scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
|
|
458
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
|
|
445
459
|
return ()=>{
|
|
446
460
|
document.removeEventListener("focusin", onFocus, false);
|
|
447
|
-
scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
|
|
461
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
|
|
448
462
|
};
|
|
449
463
|
}, [
|
|
450
464
|
scopeRef,
|
|
@@ -474,10 +488,10 @@ function $9bf71ea28793e738$var$useRestoreFocus(scopeRef, restoreFocus, contain)
|
|
|
474
488
|
if ((!$9bf71ea28793e738$var$activeScope || $9bf71ea28793e738$var$isAncestorScope($9bf71ea28793e738$var$activeScope, scopeRef)) && $9bf71ea28793e738$var$isElementInScope(document.activeElement, scopeRef.current)) $9bf71ea28793e738$var$activeScope = scopeRef;
|
|
475
489
|
};
|
|
476
490
|
document.addEventListener("focusin", onFocus, false);
|
|
477
|
-
scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
|
|
491
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
|
|
478
492
|
return ()=>{
|
|
479
493
|
document.removeEventListener("focusin", onFocus, false);
|
|
480
|
-
scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
|
|
494
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
|
|
481
495
|
};
|
|
482
496
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
483
497
|
}, [
|
|
@@ -494,7 +508,9 @@ function $9bf71ea28793e738$var$useRestoreFocus(scopeRef, restoreFocus, contain)
|
|
|
494
508
|
if (e.key !== "Tab" || e.altKey || e.ctrlKey || e.metaKey || !$9bf71ea28793e738$var$shouldContainFocus(scopeRef)) return;
|
|
495
509
|
let focusedElement = document.activeElement;
|
|
496
510
|
if (!$9bf71ea28793e738$var$isElementInScope(focusedElement, scopeRef.current)) return;
|
|
497
|
-
let
|
|
511
|
+
let treeNode = $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef);
|
|
512
|
+
if (!treeNode) return;
|
|
513
|
+
let nodeToRestore = treeNode.nodeToRestore;
|
|
498
514
|
// Create a DOM tree walker that matches all tabbable elements
|
|
499
515
|
let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(document.body, {
|
|
500
516
|
tabbable: true
|
|
@@ -502,9 +518,9 @@ function $9bf71ea28793e738$var$useRestoreFocus(scopeRef, restoreFocus, contain)
|
|
|
502
518
|
// Find the next tabbable element after the currently focused element
|
|
503
519
|
walker.currentNode = focusedElement;
|
|
504
520
|
let nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
|
|
505
|
-
if (!document.body.contains(nodeToRestore) || nodeToRestore === document.body) {
|
|
506
|
-
nodeToRestore =
|
|
507
|
-
|
|
521
|
+
if (!nodeToRestore || !document.body.contains(nodeToRestore) || nodeToRestore === document.body) {
|
|
522
|
+
nodeToRestore = undefined;
|
|
523
|
+
treeNode.nodeToRestore = undefined;
|
|
508
524
|
}
|
|
509
525
|
// If there is no next element, or it is outside the current scope, move focus to the
|
|
510
526
|
// next element after the node to restore to instead.
|
|
@@ -535,9 +551,14 @@ function $9bf71ea28793e738$var$useRestoreFocus(scopeRef, restoreFocus, contain)
|
|
|
535
551
|
// useLayoutEffect instead of useEffect so the active element is saved synchronously instead of asynchronously.
|
|
536
552
|
(0, $6nfFC$useLayoutEffect)(()=>{
|
|
537
553
|
if (!restoreFocus) return;
|
|
538
|
-
$9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef)
|
|
554
|
+
let treeNode = $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef);
|
|
555
|
+
if (!treeNode) return;
|
|
556
|
+
var _nodeToRestoreRef_current;
|
|
557
|
+
treeNode.nodeToRestore = (_nodeToRestoreRef_current = nodeToRestoreRef.current) !== null && _nodeToRestoreRef_current !== void 0 ? _nodeToRestoreRef_current : undefined;
|
|
539
558
|
return ()=>{
|
|
540
|
-
let
|
|
559
|
+
let treeNode = $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(scopeRef);
|
|
560
|
+
if (!treeNode) return;
|
|
561
|
+
let nodeToRestore = treeNode.nodeToRestore;
|
|
541
562
|
// if we already lost focus to the body and this was the active scope, then we should attempt to restore
|
|
542
563
|
if (restoreFocus && nodeToRestore && // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
543
564
|
($9bf71ea28793e738$var$isElementInScope(document.activeElement, scopeRef.current) || document.activeElement === document.body && $9bf71ea28793e738$var$shouldRestoreFocus(scopeRef))) {
|
|
@@ -559,7 +580,7 @@ function $9bf71ea28793e738$var$useRestoreFocus(scopeRef, restoreFocus, contain)
|
|
|
559
580
|
// ancestor scope that is still in the tree.
|
|
560
581
|
treeNode = clonedTree.getTreeNode(scopeRef);
|
|
561
582
|
while(treeNode){
|
|
562
|
-
if (treeNode.scopeRef && $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(treeNode.scopeRef)) {
|
|
583
|
+
if (treeNode.scopeRef && treeNode.scopeRef.current && $9bf71ea28793e738$export$d06fae2ee68b101e.getTreeNode(treeNode.scopeRef)) {
|
|
563
584
|
$9bf71ea28793e738$var$focusFirstInScope(treeNode.scopeRef.current, true);
|
|
564
585
|
return;
|
|
565
586
|
}
|
|
@@ -592,7 +613,7 @@ function $9bf71ea28793e738$export$c5251b9e124bf29(ref, defaultOptions = {}) {
|
|
|
592
613
|
return {
|
|
593
614
|
focusNext (opts = {}) {
|
|
594
615
|
let root = ref.current;
|
|
595
|
-
if (!root) return;
|
|
616
|
+
if (!root) return null;
|
|
596
617
|
let { from: from, tabbable: tabbable = defaultOptions.tabbable, wrap: wrap = defaultOptions.wrap, accept: accept = defaultOptions.accept } = opts;
|
|
597
618
|
let node = from || document.activeElement;
|
|
598
619
|
let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(root, {
|
|
@@ -610,7 +631,7 @@ function $9bf71ea28793e738$export$c5251b9e124bf29(ref, defaultOptions = {}) {
|
|
|
610
631
|
},
|
|
611
632
|
focusPrevious (opts = defaultOptions) {
|
|
612
633
|
let root = ref.current;
|
|
613
|
-
if (!root) return;
|
|
634
|
+
if (!root) return null;
|
|
614
635
|
let { from: from, tabbable: tabbable = defaultOptions.tabbable, wrap: wrap = defaultOptions.wrap, accept: accept = defaultOptions.accept } = opts;
|
|
615
636
|
let node = from || document.activeElement;
|
|
616
637
|
let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(root, {
|
|
@@ -621,19 +642,22 @@ function $9bf71ea28793e738$export$c5251b9e124bf29(ref, defaultOptions = {}) {
|
|
|
621
642
|
else {
|
|
622
643
|
let next = $9bf71ea28793e738$var$last(walker);
|
|
623
644
|
if (next) $9bf71ea28793e738$var$focusElement(next, true);
|
|
624
|
-
return next;
|
|
645
|
+
return next !== null && next !== void 0 ? next : null;
|
|
625
646
|
}
|
|
626
647
|
let previousNode = walker.previousNode();
|
|
627
648
|
if (!previousNode && wrap) {
|
|
628
649
|
walker.currentNode = root;
|
|
629
|
-
|
|
650
|
+
let lastNode = $9bf71ea28793e738$var$last(walker);
|
|
651
|
+
if (!lastNode) // couldn't wrap
|
|
652
|
+
return null;
|
|
653
|
+
previousNode = lastNode;
|
|
630
654
|
}
|
|
631
655
|
if (previousNode) $9bf71ea28793e738$var$focusElement(previousNode, true);
|
|
632
|
-
return previousNode;
|
|
656
|
+
return previousNode !== null && previousNode !== void 0 ? previousNode : null;
|
|
633
657
|
},
|
|
634
658
|
focusFirst (opts = defaultOptions) {
|
|
635
659
|
let root = ref.current;
|
|
636
|
-
if (!root) return;
|
|
660
|
+
if (!root) return null;
|
|
637
661
|
let { tabbable: tabbable = defaultOptions.tabbable, accept: accept = defaultOptions.accept } = opts;
|
|
638
662
|
let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(root, {
|
|
639
663
|
tabbable: tabbable,
|
|
@@ -645,7 +669,7 @@ function $9bf71ea28793e738$export$c5251b9e124bf29(ref, defaultOptions = {}) {
|
|
|
645
669
|
},
|
|
646
670
|
focusLast (opts = defaultOptions) {
|
|
647
671
|
let root = ref.current;
|
|
648
|
-
if (!root) return;
|
|
672
|
+
if (!root) return null;
|
|
649
673
|
let { tabbable: tabbable = defaultOptions.tabbable, accept: accept = defaultOptions.accept } = opts;
|
|
650
674
|
let walker = $9bf71ea28793e738$export$2d6ec8fc375ceafa(root, {
|
|
651
675
|
tabbable: tabbable,
|
|
@@ -653,12 +677,12 @@ function $9bf71ea28793e738$export$c5251b9e124bf29(ref, defaultOptions = {}) {
|
|
|
653
677
|
});
|
|
654
678
|
let next = $9bf71ea28793e738$var$last(walker);
|
|
655
679
|
if (next) $9bf71ea28793e738$var$focusElement(next, true);
|
|
656
|
-
return next;
|
|
680
|
+
return next !== null && next !== void 0 ? next : null;
|
|
657
681
|
}
|
|
658
682
|
};
|
|
659
683
|
}
|
|
660
684
|
function $9bf71ea28793e738$var$last(walker) {
|
|
661
|
-
let next;
|
|
685
|
+
let next = undefined;
|
|
662
686
|
let last;
|
|
663
687
|
do {
|
|
664
688
|
last = walker.lastChild();
|
|
@@ -675,6 +699,7 @@ class $9bf71ea28793e738$var$Tree {
|
|
|
675
699
|
}
|
|
676
700
|
addTreeNode(scopeRef, parent, nodeToRestore) {
|
|
677
701
|
let parentNode = this.fastMap.get(parent !== null && parent !== void 0 ? parent : null);
|
|
702
|
+
if (!parentNode) return;
|
|
678
703
|
let node = new $9bf71ea28793e738$var$TreeNode({
|
|
679
704
|
scopeRef: scopeRef
|
|
680
705
|
});
|
|
@@ -690,13 +715,16 @@ class $9bf71ea28793e738$var$Tree {
|
|
|
690
715
|
// never remove the root
|
|
691
716
|
if (scopeRef === null) return;
|
|
692
717
|
let node = this.fastMap.get(scopeRef);
|
|
718
|
+
if (!node) return;
|
|
693
719
|
let parentNode = node.parent;
|
|
694
720
|
// when we remove a scope, check if any sibling scopes are trying to restore focus to something inside the scope we're removing
|
|
695
721
|
// 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;
|
|
722
|
+
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
723
|
let children = node.children;
|
|
698
|
-
parentNode
|
|
699
|
-
|
|
724
|
+
if (parentNode) {
|
|
725
|
+
parentNode.removeChild(node);
|
|
726
|
+
if (children.size > 0) children.forEach((child)=>parentNode && parentNode.addChild(child));
|
|
727
|
+
}
|
|
700
728
|
this.fastMap.delete(node.scopeRef);
|
|
701
729
|
}
|
|
702
730
|
// Pre Order Depth First
|
|
@@ -705,8 +733,10 @@ class $9bf71ea28793e738$var$Tree {
|
|
|
705
733
|
if (node.children.size > 0) for (let child of node.children)yield* this.traverse(child);
|
|
706
734
|
}
|
|
707
735
|
clone() {
|
|
736
|
+
var _node_parent;
|
|
708
737
|
let newTree = new $9bf71ea28793e738$var$Tree();
|
|
709
|
-
|
|
738
|
+
var _node_parent_scopeRef;
|
|
739
|
+
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
740
|
return newTree;
|
|
711
741
|
}
|
|
712
742
|
constructor(){
|
|
@@ -828,9 +858,10 @@ function $e6afbd83fe6ebbd2$var$useFocusableContext(ref) {
|
|
|
828
858
|
* Provides DOM props to the nearest focusable child.
|
|
829
859
|
*/ function $e6afbd83fe6ebbd2$var$FocusableProvider(props, ref) {
|
|
830
860
|
let { children: children, ...otherProps } = props;
|
|
861
|
+
let objRef = (0, $6nfFC$useObjectRef)(ref);
|
|
831
862
|
let context = {
|
|
832
863
|
...otherProps,
|
|
833
|
-
ref:
|
|
864
|
+
ref: objRef
|
|
834
865
|
};
|
|
835
866
|
return /*#__PURE__*/ (0, $6nfFC$react).createElement($e6afbd83fe6ebbd2$var$FocusableContext.Provider, {
|
|
836
867
|
value: context
|