@react-aria/focus 3.14.2 → 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/main.js
CHANGED
|
@@ -3,9 +3,11 @@ var $aB6Cp$reactariautils = require("@react-aria/utils");
|
|
|
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
|
}
|
|
@@ -104,8 +106,8 @@ const $a7a032acae3ddda9$var$FocusContext = /*#__PURE__*/ (0, ($parcel$interopDef
|
|
|
104
106
|
let $a7a032acae3ddda9$var$activeScope = null;
|
|
105
107
|
function $a7a032acae3ddda9$export$20e40289641fbbb6(props) {
|
|
106
108
|
let { children: children, contain: contain, restoreFocus: restoreFocus, autoFocus: autoFocus } = props;
|
|
107
|
-
let startRef = (0, $aB6Cp$react.useRef)();
|
|
108
|
-
let endRef = (0, $aB6Cp$react.useRef)();
|
|
109
|
+
let startRef = (0, $aB6Cp$react.useRef)(null);
|
|
110
|
+
let endRef = (0, $aB6Cp$react.useRef)(null);
|
|
109
111
|
let scopeRef = (0, $aB6Cp$react.useRef)([]);
|
|
110
112
|
let { parentNode: parentNode } = (0, $aB6Cp$react.useContext)($a7a032acae3ddda9$var$FocusContext) || {};
|
|
111
113
|
// Create a tree node here so we can add children to it even before it is added to the tree.
|
|
@@ -133,13 +135,14 @@ function $a7a032acae3ddda9$export$20e40289641fbbb6(props) {
|
|
|
133
135
|
]);
|
|
134
136
|
(0, $aB6Cp$reactariautils.useLayoutEffect)(()=>{
|
|
135
137
|
let node = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
|
|
136
|
-
node.contain = contain;
|
|
138
|
+
if (node) node.contain = !!contain;
|
|
137
139
|
}, [
|
|
138
140
|
contain
|
|
139
141
|
]);
|
|
140
142
|
(0, $aB6Cp$reactariautils.useLayoutEffect)(()=>{
|
|
143
|
+
var _startRef_current;
|
|
141
144
|
// Find all rendered nodes between the sentinels and add them to the scope.
|
|
142
|
-
let node = startRef.current.nextSibling;
|
|
145
|
+
let node = (_startRef_current = startRef.current) === null || _startRef_current === void 0 ? void 0 : _startRef_current.nextSibling;
|
|
143
146
|
let nodes = [];
|
|
144
147
|
while(node && node !== endRef.current){
|
|
145
148
|
nodes.push(node);
|
|
@@ -153,32 +156,34 @@ function $a7a032acae3ddda9$export$20e40289641fbbb6(props) {
|
|
|
153
156
|
$a7a032acae3ddda9$var$useFocusContainment(scopeRef, contain);
|
|
154
157
|
$a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain);
|
|
155
158
|
$a7a032acae3ddda9$var$useAutoFocus(scopeRef, autoFocus);
|
|
156
|
-
//
|
|
159
|
+
// This needs to be an effect so that activeScope is updated after the FocusScope tree is complete.
|
|
160
|
+
// It cannot be a useLayoutEffect because the parent of this node hasn't been attached in the tree yet.
|
|
157
161
|
(0, $aB6Cp$react.useEffect)(()=>{
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
//
|
|
162
|
-
//
|
|
163
|
-
if ($a7a032acae3ddda9$var$isElementInScope(activeElement, scopeRef.current))
|
|
164
|
-
|
|
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
|
-
};
|
|
162
|
+
let activeElement = document.activeElement;
|
|
163
|
+
let scope = null;
|
|
164
|
+
if ($a7a032acae3ddda9$var$isElementInScope(activeElement, scopeRef.current)) {
|
|
165
|
+
// We need to traverse the focusScope tree and find the bottom most scope that
|
|
166
|
+
// contains the active element and set that as the activeScope.
|
|
167
|
+
for (let node of $a7a032acae3ddda9$export$d06fae2ee68b101e.traverse())if (node.scopeRef && $a7a032acae3ddda9$var$isElementInScope(activeElement, node.scopeRef.current)) scope = node;
|
|
168
|
+
if (scope === $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef)) $a7a032acae3ddda9$var$activeScope = scope.scopeRef;
|
|
178
169
|
}
|
|
179
170
|
}, [
|
|
180
171
|
scopeRef
|
|
181
172
|
]);
|
|
173
|
+
// This layout effect cleanup is so that the tree node is removed synchronously with react before the RAF
|
|
174
|
+
// in useRestoreFocus cleanup runs.
|
|
175
|
+
(0, $aB6Cp$reactariautils.useLayoutEffect)(()=>{
|
|
176
|
+
return ()=>{
|
|
177
|
+
var _focusScopeTree_getTreeNode_parent, _focusScopeTree_getTreeNode;
|
|
178
|
+
var _focusScopeTree_getTreeNode_parent_scopeRef;
|
|
179
|
+
// Scope may have been re-parented.
|
|
180
|
+
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;
|
|
181
|
+
if ((scopeRef === $a7a032acae3ddda9$var$activeScope || $a7a032acae3ddda9$var$isAncestorScope(scopeRef, $a7a032acae3ddda9$var$activeScope)) && (!parentScope || $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(parentScope))) $a7a032acae3ddda9$var$activeScope = parentScope;
|
|
182
|
+
$a7a032acae3ddda9$export$d06fae2ee68b101e.removeTreeNode(scopeRef);
|
|
183
|
+
};
|
|
184
|
+
}, [
|
|
185
|
+
scopeRef
|
|
186
|
+
]);
|
|
182
187
|
let focusManager = (0, $aB6Cp$react.useMemo)(()=>$a7a032acae3ddda9$var$createFocusManagerForScope(scopeRef), []);
|
|
183
188
|
let value = (0, $aB6Cp$react.useMemo)(()=>({
|
|
184
189
|
focusManager: focusManager,
|
|
@@ -210,7 +215,8 @@ function $a7a032acae3ddda9$var$createFocusManagerForScope(scopeRef) {
|
|
|
210
215
|
let { from: from, tabbable: tabbable, wrap: wrap, accept: accept } = opts;
|
|
211
216
|
let node = from || document.activeElement;
|
|
212
217
|
let sentinel = scope[0].previousElementSibling;
|
|
213
|
-
let
|
|
218
|
+
let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
|
|
219
|
+
let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
|
|
214
220
|
tabbable: tabbable,
|
|
215
221
|
accept: accept
|
|
216
222
|
}, scope);
|
|
@@ -228,7 +234,8 @@ function $a7a032acae3ddda9$var$createFocusManagerForScope(scopeRef) {
|
|
|
228
234
|
let { from: from, tabbable: tabbable, wrap: wrap, accept: accept } = opts;
|
|
229
235
|
let node = from || document.activeElement;
|
|
230
236
|
let sentinel = scope[scope.length - 1].nextElementSibling;
|
|
231
|
-
let
|
|
237
|
+
let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
|
|
238
|
+
let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
|
|
232
239
|
tabbable: tabbable,
|
|
233
240
|
accept: accept
|
|
234
241
|
}, scope);
|
|
@@ -244,7 +251,8 @@ function $a7a032acae3ddda9$var$createFocusManagerForScope(scopeRef) {
|
|
|
244
251
|
focusFirst (opts = {}) {
|
|
245
252
|
let scope = scopeRef.current;
|
|
246
253
|
let { tabbable: tabbable, accept: accept } = opts;
|
|
247
|
-
let
|
|
254
|
+
let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
|
|
255
|
+
let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
|
|
248
256
|
tabbable: tabbable,
|
|
249
257
|
accept: accept
|
|
250
258
|
}, scope);
|
|
@@ -256,7 +264,8 @@ function $a7a032acae3ddda9$var$createFocusManagerForScope(scopeRef) {
|
|
|
256
264
|
focusLast (opts = {}) {
|
|
257
265
|
let scope = scopeRef.current;
|
|
258
266
|
let { tabbable: tabbable, accept: accept } = opts;
|
|
259
|
-
let
|
|
267
|
+
let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
|
|
268
|
+
let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
|
|
260
269
|
tabbable: tabbable,
|
|
261
270
|
accept: accept
|
|
262
271
|
}, scope);
|
|
@@ -298,14 +307,14 @@ function $a7a032acae3ddda9$var$shouldContainFocus(scopeRef) {
|
|
|
298
307
|
}
|
|
299
308
|
function $a7a032acae3ddda9$var$useFocusContainment(scopeRef, contain) {
|
|
300
309
|
let focusedNode = (0, $aB6Cp$react.useRef)();
|
|
301
|
-
let raf = (0, $aB6Cp$react.useRef)(
|
|
310
|
+
let raf = (0, $aB6Cp$react.useRef)();
|
|
302
311
|
(0, $aB6Cp$reactariautils.useLayoutEffect)(()=>{
|
|
303
312
|
let scope = scopeRef.current;
|
|
304
313
|
if (!contain) {
|
|
305
314
|
// if contain was changed, then we should cancel any ongoing waits to pull focus back into containment
|
|
306
315
|
if (raf.current) {
|
|
307
316
|
cancelAnimationFrame(raf.current);
|
|
308
|
-
raf.current =
|
|
317
|
+
raf.current = undefined;
|
|
309
318
|
}
|
|
310
319
|
return;
|
|
311
320
|
}
|
|
@@ -314,10 +323,12 @@ function $a7a032acae3ddda9$var$useFocusContainment(scopeRef, contain) {
|
|
|
314
323
|
if (e.key !== "Tab" || e.altKey || e.ctrlKey || e.metaKey || !$a7a032acae3ddda9$var$shouldContainFocus(scopeRef)) return;
|
|
315
324
|
let focusedElement = document.activeElement;
|
|
316
325
|
let scope = scopeRef.current;
|
|
317
|
-
if (!$a7a032acae3ddda9$var$isElementInScope(focusedElement, scope)) return;
|
|
318
|
-
let
|
|
326
|
+
if (!scope || !$a7a032acae3ddda9$var$isElementInScope(focusedElement, scope)) return;
|
|
327
|
+
let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
|
|
328
|
+
let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
|
|
319
329
|
tabbable: true
|
|
320
330
|
}, scope);
|
|
331
|
+
if (!focusedElement) return;
|
|
321
332
|
walker.currentNode = focusedElement;
|
|
322
333
|
let nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
|
|
323
334
|
if (!nextElement) {
|
|
@@ -337,7 +348,7 @@ function $a7a032acae3ddda9$var$useFocusContainment(scopeRef, contain) {
|
|
|
337
348
|
// If a focus event occurs outside the active scope (e.g. user tabs from browser location bar),
|
|
338
349
|
// restore focus to the previously focused node or the first tabbable element in the active scope.
|
|
339
350
|
if (focusedNode.current) focusedNode.current.focus();
|
|
340
|
-
else if ($a7a032acae3ddda9$var$activeScope) $a7a032acae3ddda9$var$focusFirstInScope($a7a032acae3ddda9$var$activeScope.current);
|
|
351
|
+
else if ($a7a032acae3ddda9$var$activeScope && $a7a032acae3ddda9$var$activeScope.current) $a7a032acae3ddda9$var$focusFirstInScope($a7a032acae3ddda9$var$activeScope.current);
|
|
341
352
|
} else if ($a7a032acae3ddda9$var$shouldContainFocus(scopeRef)) focusedNode.current = e.target;
|
|
342
353
|
};
|
|
343
354
|
let onBlur = (e)=>{
|
|
@@ -345,24 +356,25 @@ function $a7a032acae3ddda9$var$useFocusContainment(scopeRef, contain) {
|
|
|
345
356
|
if (raf.current) cancelAnimationFrame(raf.current);
|
|
346
357
|
raf.current = requestAnimationFrame(()=>{
|
|
347
358
|
// 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)) {
|
|
359
|
+
if (document.activeElement && $a7a032acae3ddda9$var$shouldContainFocus(scopeRef) && !$a7a032acae3ddda9$var$isElementInChildScope(document.activeElement, scopeRef)) {
|
|
349
360
|
$a7a032acae3ddda9$var$activeScope = scopeRef;
|
|
350
361
|
if (document.body.contains(e.target)) {
|
|
362
|
+
var _focusedNode_current;
|
|
351
363
|
focusedNode.current = e.target;
|
|
352
|
-
focusedNode.current.focus();
|
|
353
|
-
} else if ($a7a032acae3ddda9$var$activeScope) $a7a032acae3ddda9$var$focusFirstInScope($a7a032acae3ddda9$var$activeScope.current);
|
|
364
|
+
(_focusedNode_current = focusedNode.current) === null || _focusedNode_current === void 0 ? void 0 : _focusedNode_current.focus();
|
|
365
|
+
} else if ($a7a032acae3ddda9$var$activeScope.current) $a7a032acae3ddda9$var$focusFirstInScope($a7a032acae3ddda9$var$activeScope.current);
|
|
354
366
|
}
|
|
355
367
|
});
|
|
356
368
|
};
|
|
357
369
|
document.addEventListener("keydown", onKeyDown, false);
|
|
358
370
|
document.addEventListener("focusin", onFocus, false);
|
|
359
|
-
scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
|
|
360
|
-
scope.forEach((element)=>element.addEventListener("focusout", onBlur, false));
|
|
371
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
|
|
372
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusout", onBlur, false));
|
|
361
373
|
return ()=>{
|
|
362
374
|
document.removeEventListener("keydown", onKeyDown, false);
|
|
363
375
|
document.removeEventListener("focusin", onFocus, false);
|
|
364
|
-
scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
|
|
365
|
-
scope.forEach((element)=>element.removeEventListener("focusout", onBlur, false));
|
|
376
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
|
|
377
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusout", onBlur, false));
|
|
366
378
|
};
|
|
367
379
|
}, [
|
|
368
380
|
scopeRef,
|
|
@@ -382,6 +394,8 @@ function $a7a032acae3ddda9$var$isElementInAnyScope(element) {
|
|
|
382
394
|
return $a7a032acae3ddda9$var$isElementInChildScope(element);
|
|
383
395
|
}
|
|
384
396
|
function $a7a032acae3ddda9$var$isElementInScope(element, scope) {
|
|
397
|
+
if (!element) return false;
|
|
398
|
+
if (!scope) return false;
|
|
385
399
|
return scope.some((node)=>node.contains(element));
|
|
386
400
|
}
|
|
387
401
|
function $a7a032acae3ddda9$var$isElementInChildScope(element, scope = null) {
|
|
@@ -390,7 +404,7 @@ function $a7a032acae3ddda9$var$isElementInChildScope(element, scope = null) {
|
|
|
390
404
|
// node.contains in isElementInScope covers child scopes that are also DOM children,
|
|
391
405
|
// but does not cover child scopes in portals.
|
|
392
406
|
for (let { scopeRef: s } of $a7a032acae3ddda9$export$d06fae2ee68b101e.traverse($a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scope))){
|
|
393
|
-
if ($a7a032acae3ddda9$var$isElementInScope(element, s.current)) return true;
|
|
407
|
+
if (s && $a7a032acae3ddda9$var$isElementInScope(element, s.current)) return true;
|
|
394
408
|
}
|
|
395
409
|
return false;
|
|
396
410
|
}
|
|
@@ -420,14 +434,16 @@ function $a7a032acae3ddda9$var$focusElement(element, scroll = false) {
|
|
|
420
434
|
}
|
|
421
435
|
function $a7a032acae3ddda9$var$focusFirstInScope(scope, tabbable = true) {
|
|
422
436
|
let sentinel = scope[0].previousElementSibling;
|
|
423
|
-
let
|
|
437
|
+
let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
|
|
438
|
+
let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
|
|
424
439
|
tabbable: tabbable
|
|
425
440
|
}, scope);
|
|
426
441
|
walker.currentNode = sentinel;
|
|
427
442
|
let nextNode = walker.nextNode();
|
|
428
443
|
// If the scope does not contain a tabbable element, use the first focusable element.
|
|
429
444
|
if (tabbable && !nextNode) {
|
|
430
|
-
|
|
445
|
+
scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
|
|
446
|
+
walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
|
|
431
447
|
tabbable: false
|
|
432
448
|
}, scope);
|
|
433
449
|
walker.currentNode = sentinel;
|
|
@@ -440,7 +456,7 @@ function $a7a032acae3ddda9$var$useAutoFocus(scopeRef, autoFocus) {
|
|
|
440
456
|
(0, $aB6Cp$react.useEffect)(()=>{
|
|
441
457
|
if (autoFocusRef.current) {
|
|
442
458
|
$a7a032acae3ddda9$var$activeScope = scopeRef;
|
|
443
|
-
if (!$a7a032acae3ddda9$var$isElementInScope(document.activeElement, $a7a032acae3ddda9$var$activeScope.current)) $a7a032acae3ddda9$var$focusFirstInScope(scopeRef.current);
|
|
459
|
+
if (!$a7a032acae3ddda9$var$isElementInScope(document.activeElement, $a7a032acae3ddda9$var$activeScope.current) && scopeRef.current) $a7a032acae3ddda9$var$focusFirstInScope(scopeRef.current);
|
|
444
460
|
}
|
|
445
461
|
autoFocusRef.current = false;
|
|
446
462
|
}, [
|
|
@@ -459,10 +475,10 @@ function $a7a032acae3ddda9$var$useActiveScopeTracker(scopeRef, restore, contain)
|
|
|
459
475
|
else if (!$a7a032acae3ddda9$var$isElementInAnyScope(target)) $a7a032acae3ddda9$var$activeScope = null;
|
|
460
476
|
};
|
|
461
477
|
document.addEventListener("focusin", onFocus, false);
|
|
462
|
-
scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
|
|
478
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
|
|
463
479
|
return ()=>{
|
|
464
480
|
document.removeEventListener("focusin", onFocus, false);
|
|
465
|
-
scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
|
|
481
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
|
|
466
482
|
};
|
|
467
483
|
}, [
|
|
468
484
|
scopeRef,
|
|
@@ -492,10 +508,10 @@ function $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain)
|
|
|
492
508
|
if ((!$a7a032acae3ddda9$var$activeScope || $a7a032acae3ddda9$var$isAncestorScope($a7a032acae3ddda9$var$activeScope, scopeRef)) && $a7a032acae3ddda9$var$isElementInScope(document.activeElement, scopeRef.current)) $a7a032acae3ddda9$var$activeScope = scopeRef;
|
|
493
509
|
};
|
|
494
510
|
document.addEventListener("focusin", onFocus, false);
|
|
495
|
-
scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
|
|
511
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener("focusin", onFocus, false));
|
|
496
512
|
return ()=>{
|
|
497
513
|
document.removeEventListener("focusin", onFocus, false);
|
|
498
|
-
scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
|
|
514
|
+
scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener("focusin", onFocus, false));
|
|
499
515
|
};
|
|
500
516
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
501
517
|
}, [
|
|
@@ -512,7 +528,9 @@ function $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain)
|
|
|
512
528
|
if (e.key !== "Tab" || e.altKey || e.ctrlKey || e.metaKey || !$a7a032acae3ddda9$var$shouldContainFocus(scopeRef)) return;
|
|
513
529
|
let focusedElement = document.activeElement;
|
|
514
530
|
if (!$a7a032acae3ddda9$var$isElementInScope(focusedElement, scopeRef.current)) return;
|
|
515
|
-
let
|
|
531
|
+
let treeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
|
|
532
|
+
if (!treeNode) return;
|
|
533
|
+
let nodeToRestore = treeNode.nodeToRestore;
|
|
516
534
|
// Create a DOM tree walker that matches all tabbable elements
|
|
517
535
|
let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(document.body, {
|
|
518
536
|
tabbable: true
|
|
@@ -520,9 +538,9 @@ function $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain)
|
|
|
520
538
|
// Find the next tabbable element after the currently focused element
|
|
521
539
|
walker.currentNode = focusedElement;
|
|
522
540
|
let nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
|
|
523
|
-
if (!document.body.contains(nodeToRestore) || nodeToRestore === document.body) {
|
|
524
|
-
nodeToRestore =
|
|
525
|
-
|
|
541
|
+
if (!nodeToRestore || !document.body.contains(nodeToRestore) || nodeToRestore === document.body) {
|
|
542
|
+
nodeToRestore = undefined;
|
|
543
|
+
treeNode.nodeToRestore = undefined;
|
|
526
544
|
}
|
|
527
545
|
// If there is no next element, or it is outside the current scope, move focus to the
|
|
528
546
|
// next element after the node to restore to instead.
|
|
@@ -553,9 +571,14 @@ function $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain)
|
|
|
553
571
|
// useLayoutEffect instead of useEffect so the active element is saved synchronously instead of asynchronously.
|
|
554
572
|
(0, $aB6Cp$reactariautils.useLayoutEffect)(()=>{
|
|
555
573
|
if (!restoreFocus) return;
|
|
556
|
-
$a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef)
|
|
574
|
+
let treeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
|
|
575
|
+
if (!treeNode) return;
|
|
576
|
+
var _nodeToRestoreRef_current;
|
|
577
|
+
treeNode.nodeToRestore = (_nodeToRestoreRef_current = nodeToRestoreRef.current) !== null && _nodeToRestoreRef_current !== void 0 ? _nodeToRestoreRef_current : undefined;
|
|
557
578
|
return ()=>{
|
|
558
|
-
let
|
|
579
|
+
let treeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
|
|
580
|
+
if (!treeNode) return;
|
|
581
|
+
let nodeToRestore = treeNode.nodeToRestore;
|
|
559
582
|
// if we already lost focus to the body and this was the active scope, then we should attempt to restore
|
|
560
583
|
if (restoreFocus && nodeToRestore && // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
561
584
|
($a7a032acae3ddda9$var$isElementInScope(document.activeElement, scopeRef.current) || document.activeElement === document.body && $a7a032acae3ddda9$var$shouldRestoreFocus(scopeRef))) {
|
|
@@ -577,7 +600,7 @@ function $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain)
|
|
|
577
600
|
// ancestor scope that is still in the tree.
|
|
578
601
|
treeNode = clonedTree.getTreeNode(scopeRef);
|
|
579
602
|
while(treeNode){
|
|
580
|
-
if (treeNode.scopeRef && $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(treeNode.scopeRef)) {
|
|
603
|
+
if (treeNode.scopeRef && treeNode.scopeRef.current && $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(treeNode.scopeRef)) {
|
|
581
604
|
$a7a032acae3ddda9$var$focusFirstInScope(treeNode.scopeRef.current, true);
|
|
582
605
|
return;
|
|
583
606
|
}
|
|
@@ -610,7 +633,7 @@ function $a7a032acae3ddda9$export$c5251b9e124bf29(ref, defaultOptions = {}) {
|
|
|
610
633
|
return {
|
|
611
634
|
focusNext (opts = {}) {
|
|
612
635
|
let root = ref.current;
|
|
613
|
-
if (!root) return;
|
|
636
|
+
if (!root) return null;
|
|
614
637
|
let { from: from, tabbable: tabbable = defaultOptions.tabbable, wrap: wrap = defaultOptions.wrap, accept: accept = defaultOptions.accept } = opts;
|
|
615
638
|
let node = from || document.activeElement;
|
|
616
639
|
let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
|
|
@@ -628,7 +651,7 @@ function $a7a032acae3ddda9$export$c5251b9e124bf29(ref, defaultOptions = {}) {
|
|
|
628
651
|
},
|
|
629
652
|
focusPrevious (opts = defaultOptions) {
|
|
630
653
|
let root = ref.current;
|
|
631
|
-
if (!root) return;
|
|
654
|
+
if (!root) return null;
|
|
632
655
|
let { from: from, tabbable: tabbable = defaultOptions.tabbable, wrap: wrap = defaultOptions.wrap, accept: accept = defaultOptions.accept } = opts;
|
|
633
656
|
let node = from || document.activeElement;
|
|
634
657
|
let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
|
|
@@ -639,19 +662,22 @@ function $a7a032acae3ddda9$export$c5251b9e124bf29(ref, defaultOptions = {}) {
|
|
|
639
662
|
else {
|
|
640
663
|
let next = $a7a032acae3ddda9$var$last(walker);
|
|
641
664
|
if (next) $a7a032acae3ddda9$var$focusElement(next, true);
|
|
642
|
-
return next;
|
|
665
|
+
return next !== null && next !== void 0 ? next : null;
|
|
643
666
|
}
|
|
644
667
|
let previousNode = walker.previousNode();
|
|
645
668
|
if (!previousNode && wrap) {
|
|
646
669
|
walker.currentNode = root;
|
|
647
|
-
|
|
670
|
+
let lastNode = $a7a032acae3ddda9$var$last(walker);
|
|
671
|
+
if (!lastNode) // couldn't wrap
|
|
672
|
+
return null;
|
|
673
|
+
previousNode = lastNode;
|
|
648
674
|
}
|
|
649
675
|
if (previousNode) $a7a032acae3ddda9$var$focusElement(previousNode, true);
|
|
650
|
-
return previousNode;
|
|
676
|
+
return previousNode !== null && previousNode !== void 0 ? previousNode : null;
|
|
651
677
|
},
|
|
652
678
|
focusFirst (opts = defaultOptions) {
|
|
653
679
|
let root = ref.current;
|
|
654
|
-
if (!root) return;
|
|
680
|
+
if (!root) return null;
|
|
655
681
|
let { tabbable: tabbable = defaultOptions.tabbable, accept: accept = defaultOptions.accept } = opts;
|
|
656
682
|
let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
|
|
657
683
|
tabbable: tabbable,
|
|
@@ -663,7 +689,7 @@ function $a7a032acae3ddda9$export$c5251b9e124bf29(ref, defaultOptions = {}) {
|
|
|
663
689
|
},
|
|
664
690
|
focusLast (opts = defaultOptions) {
|
|
665
691
|
let root = ref.current;
|
|
666
|
-
if (!root) return;
|
|
692
|
+
if (!root) return null;
|
|
667
693
|
let { tabbable: tabbable = defaultOptions.tabbable, accept: accept = defaultOptions.accept } = opts;
|
|
668
694
|
let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
|
|
669
695
|
tabbable: tabbable,
|
|
@@ -671,12 +697,12 @@ function $a7a032acae3ddda9$export$c5251b9e124bf29(ref, defaultOptions = {}) {
|
|
|
671
697
|
});
|
|
672
698
|
let next = $a7a032acae3ddda9$var$last(walker);
|
|
673
699
|
if (next) $a7a032acae3ddda9$var$focusElement(next, true);
|
|
674
|
-
return next;
|
|
700
|
+
return next !== null && next !== void 0 ? next : null;
|
|
675
701
|
}
|
|
676
702
|
};
|
|
677
703
|
}
|
|
678
704
|
function $a7a032acae3ddda9$var$last(walker) {
|
|
679
|
-
let next;
|
|
705
|
+
let next = undefined;
|
|
680
706
|
let last;
|
|
681
707
|
do {
|
|
682
708
|
last = walker.lastChild();
|
|
@@ -693,6 +719,7 @@ class $a7a032acae3ddda9$var$Tree {
|
|
|
693
719
|
}
|
|
694
720
|
addTreeNode(scopeRef, parent, nodeToRestore) {
|
|
695
721
|
let parentNode = this.fastMap.get(parent !== null && parent !== void 0 ? parent : null);
|
|
722
|
+
if (!parentNode) return;
|
|
696
723
|
let node = new $a7a032acae3ddda9$var$TreeNode({
|
|
697
724
|
scopeRef: scopeRef
|
|
698
725
|
});
|
|
@@ -708,13 +735,16 @@ class $a7a032acae3ddda9$var$Tree {
|
|
|
708
735
|
// never remove the root
|
|
709
736
|
if (scopeRef === null) return;
|
|
710
737
|
let node = this.fastMap.get(scopeRef);
|
|
738
|
+
if (!node) return;
|
|
711
739
|
let parentNode = node.parent;
|
|
712
740
|
// when we remove a scope, check if any sibling scopes are trying to restore focus to something inside the scope we're removing
|
|
713
741
|
// 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;
|
|
742
|
+
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
743
|
let children = node.children;
|
|
716
|
-
parentNode
|
|
717
|
-
|
|
744
|
+
if (parentNode) {
|
|
745
|
+
parentNode.removeChild(node);
|
|
746
|
+
if (children.size > 0) children.forEach((child)=>parentNode && parentNode.addChild(child));
|
|
747
|
+
}
|
|
718
748
|
this.fastMap.delete(node.scopeRef);
|
|
719
749
|
}
|
|
720
750
|
// Pre Order Depth First
|
|
@@ -723,8 +753,10 @@ class $a7a032acae3ddda9$var$Tree {
|
|
|
723
753
|
if (node.children.size > 0) for (let child of node.children)yield* this.traverse(child);
|
|
724
754
|
}
|
|
725
755
|
clone() {
|
|
756
|
+
var _node_parent;
|
|
726
757
|
let newTree = new $a7a032acae3ddda9$var$Tree();
|
|
727
|
-
|
|
758
|
+
var _node_parent_scopeRef;
|
|
759
|
+
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
760
|
return newTree;
|
|
729
761
|
}
|
|
730
762
|
constructor(){
|
|
@@ -846,9 +878,10 @@ function $fb504d83237fd6ac$var$useFocusableContext(ref) {
|
|
|
846
878
|
* Provides DOM props to the nearest focusable child.
|
|
847
879
|
*/ function $fb504d83237fd6ac$var$FocusableProvider(props, ref) {
|
|
848
880
|
let { children: children, ...otherProps } = props;
|
|
881
|
+
let objRef = (0, $aB6Cp$reactariautils.useObjectRef)(ref);
|
|
849
882
|
let context = {
|
|
850
883
|
...otherProps,
|
|
851
|
-
ref:
|
|
884
|
+
ref: objRef
|
|
852
885
|
};
|
|
853
886
|
return /*#__PURE__*/ (0, ($parcel$interopDefault($aB6Cp$react))).createElement($fb504d83237fd6ac$var$FocusableContext.Provider, {
|
|
854
887
|
value: context
|