@react-aria/focus 3.21.4 → 3.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/import.mjs +16 -8
  2. package/dist/main.js +32 -24
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +16 -8
  5. package/dist/module.js.map +1 -1
  6. package/dist/types/src/index.d.ts +16 -0
  7. package/package.json +14 -14
  8. package/src/index.ts +16 -13
  9. package/dist/FocusRing.main.js +0 -44
  10. package/dist/FocusRing.main.js.map +0 -1
  11. package/dist/FocusRing.mjs +0 -35
  12. package/dist/FocusRing.module.js +0 -35
  13. package/dist/FocusRing.module.js.map +0 -1
  14. package/dist/FocusScope.main.js +0 -759
  15. package/dist/FocusScope.main.js.map +0 -1
  16. package/dist/FocusScope.mjs +0 -746
  17. package/dist/FocusScope.module.js +0 -746
  18. package/dist/FocusScope.module.js.map +0 -1
  19. package/dist/types.d.ts +0 -133
  20. package/dist/types.d.ts.map +0 -1
  21. package/dist/useFocusRing.main.js +0 -55
  22. package/dist/useFocusRing.main.js.map +0 -1
  23. package/dist/useFocusRing.mjs +0 -50
  24. package/dist/useFocusRing.module.js +0 -50
  25. package/dist/useFocusRing.module.js.map +0 -1
  26. package/dist/useHasTabbableChild.main.js +0 -62
  27. package/dist/useHasTabbableChild.main.js.map +0 -1
  28. package/dist/useHasTabbableChild.mjs +0 -57
  29. package/dist/useHasTabbableChild.module.js +0 -57
  30. package/dist/useHasTabbableChild.module.js.map +0 -1
  31. package/dist/virtualFocus.main.js +0 -46
  32. package/dist/virtualFocus.main.js.map +0 -1
  33. package/dist/virtualFocus.mjs +0 -38
  34. package/dist/virtualFocus.module.js +0 -38
  35. package/dist/virtualFocus.module.js.map +0 -1
  36. package/src/FocusRing.tsx +0 -55
  37. package/src/FocusScope.tsx +0 -1024
  38. package/src/useFocusRing.ts +0 -79
  39. package/src/useHasTabbableChild.ts +0 -66
  40. package/src/virtualFocus.ts +0 -33
@@ -1,759 +0,0 @@
1
- var $euGna$reactariautils = require("@react-aria/utils");
2
- var $euGna$reactariainteractions = require("@react-aria/interactions");
3
- var $euGna$react = require("react");
4
-
5
-
6
- function $parcel$interopDefault(a) {
7
- return a && a.__esModule ? a.default : a;
8
- }
9
-
10
- function $parcel$export(e, n, v, s) {
11
- Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
12
- }
13
-
14
- $parcel$export(module.exports, "FocusScope", () => $a7a032acae3ddda9$export$20e40289641fbbb6);
15
- $parcel$export(module.exports, "useFocusManager", () => $a7a032acae3ddda9$export$10c5169755ce7bd7);
16
- $parcel$export(module.exports, "getFocusableTreeWalker", () => $a7a032acae3ddda9$export$2d6ec8fc375ceafa);
17
- $parcel$export(module.exports, "isElementInChildOfActiveScope", () => $a7a032acae3ddda9$export$1258395f99bf9cbf);
18
- $parcel$export(module.exports, "createFocusManager", () => $a7a032acae3ddda9$export$c5251b9e124bf29);
19
- /*
20
- * Copyright 2020 Adobe. All rights reserved.
21
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
22
- * you may not use this file except in compliance with the License. You may obtain a copy
23
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
24
- *
25
- * Unless required by applicable law or agreed to in writing, software distributed under
26
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
27
- * OF ANY KIND, either express or implied. See the License for the specific language
28
- * governing permissions and limitations under the License.
29
- */
30
-
31
-
32
- const $a7a032acae3ddda9$var$FocusContext = /*#__PURE__*/ (0, ($parcel$interopDefault($euGna$react))).createContext(null);
33
- const $a7a032acae3ddda9$var$RESTORE_FOCUS_EVENT = 'react-aria-focus-scope-restore';
34
- let $a7a032acae3ddda9$var$activeScope = null;
35
- function $a7a032acae3ddda9$export$20e40289641fbbb6(props) {
36
- let { children: children, contain: contain, restoreFocus: restoreFocus, autoFocus: autoFocus } = props;
37
- let startRef = (0, $euGna$react.useRef)(null);
38
- let endRef = (0, $euGna$react.useRef)(null);
39
- let scopeRef = (0, $euGna$react.useRef)([]);
40
- let { parentNode: parentNode } = (0, $euGna$react.useContext)($a7a032acae3ddda9$var$FocusContext) || {};
41
- // Create a tree node here so we can add children to it even before it is added to the tree.
42
- let node = (0, $euGna$react.useMemo)(()=>new $a7a032acae3ddda9$var$TreeNode({
43
- scopeRef: scopeRef
44
- }), [
45
- scopeRef
46
- ]);
47
- (0, $euGna$reactariautils.useLayoutEffect)(()=>{
48
- // If a new scope mounts outside the active scope, (e.g. DialogContainer launched from a menu),
49
- // use the active scope as the parent instead of the parent from context. Layout effects run bottom
50
- // up, so if the parent is not yet added to the tree, don't do this. Only the outer-most FocusScope
51
- // that is being added should get the activeScope as its parent.
52
- let parent = parentNode || $a7a032acae3ddda9$export$d06fae2ee68b101e.root;
53
- if ($a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(parent.scopeRef) && $a7a032acae3ddda9$var$activeScope && !$a7a032acae3ddda9$var$isAncestorScope($a7a032acae3ddda9$var$activeScope, parent.scopeRef)) {
54
- let activeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode($a7a032acae3ddda9$var$activeScope);
55
- if (activeNode) parent = activeNode;
56
- }
57
- // Add the node to the parent, and to the tree.
58
- parent.addChild(node);
59
- $a7a032acae3ddda9$export$d06fae2ee68b101e.addNode(node);
60
- }, [
61
- node,
62
- parentNode
63
- ]);
64
- (0, $euGna$reactariautils.useLayoutEffect)(()=>{
65
- let node = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
66
- if (node) node.contain = !!contain;
67
- }, [
68
- contain
69
- ]);
70
- (0, $euGna$reactariautils.useLayoutEffect)(()=>{
71
- var _startRef_current;
72
- // Find all rendered nodes between the sentinels and add them to the scope.
73
- let node = (_startRef_current = startRef.current) === null || _startRef_current === void 0 ? void 0 : _startRef_current.nextSibling;
74
- let nodes = [];
75
- let stopPropagation = (e)=>e.stopPropagation();
76
- while(node && node !== endRef.current){
77
- nodes.push(node);
78
- // Stop custom restore focus event from propagating to parent focus scopes.
79
- node.addEventListener($a7a032acae3ddda9$var$RESTORE_FOCUS_EVENT, stopPropagation);
80
- node = node.nextSibling;
81
- }
82
- scopeRef.current = nodes;
83
- return ()=>{
84
- for (let node of nodes)node.removeEventListener($a7a032acae3ddda9$var$RESTORE_FOCUS_EVENT, stopPropagation);
85
- };
86
- }, [
87
- children
88
- ]);
89
- $a7a032acae3ddda9$var$useActiveScopeTracker(scopeRef, restoreFocus, contain);
90
- $a7a032acae3ddda9$var$useFocusContainment(scopeRef, contain);
91
- $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain);
92
- $a7a032acae3ddda9$var$useAutoFocus(scopeRef, autoFocus);
93
- // This needs to be an effect so that activeScope is updated after the FocusScope tree is complete.
94
- // It cannot be a useLayoutEffect because the parent of this node hasn't been attached in the tree yet.
95
- (0, $euGna$react.useEffect)(()=>{
96
- const activeElement = (0, $euGna$reactariautils.getActiveElement)((0, $euGna$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined));
97
- let scope = null;
98
- if ($a7a032acae3ddda9$var$isElementInScope(activeElement, scopeRef.current)) {
99
- // We need to traverse the focusScope tree and find the bottom most scope that
100
- // contains the active element and set that as the activeScope.
101
- for (let node of $a7a032acae3ddda9$export$d06fae2ee68b101e.traverse())if (node.scopeRef && $a7a032acae3ddda9$var$isElementInScope(activeElement, node.scopeRef.current)) scope = node;
102
- if (scope === $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef)) $a7a032acae3ddda9$var$activeScope = scope.scopeRef;
103
- }
104
- }, [
105
- scopeRef
106
- ]);
107
- // This layout effect cleanup is so that the tree node is removed synchronously with react before the RAF
108
- // in useRestoreFocus cleanup runs.
109
- (0, $euGna$reactariautils.useLayoutEffect)(()=>{
110
- return ()=>{
111
- var _focusScopeTree_getTreeNode_parent, _focusScopeTree_getTreeNode;
112
- var _focusScopeTree_getTreeNode_parent_scopeRef;
113
- // Scope may have been re-parented.
114
- 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;
115
- if ((scopeRef === $a7a032acae3ddda9$var$activeScope || $a7a032acae3ddda9$var$isAncestorScope(scopeRef, $a7a032acae3ddda9$var$activeScope)) && (!parentScope || $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(parentScope))) $a7a032acae3ddda9$var$activeScope = parentScope;
116
- $a7a032acae3ddda9$export$d06fae2ee68b101e.removeTreeNode(scopeRef);
117
- };
118
- }, [
119
- scopeRef
120
- ]);
121
- let focusManager = (0, $euGna$react.useMemo)(()=>$a7a032acae3ddda9$var$createFocusManagerForScope(scopeRef), []);
122
- let value = (0, $euGna$react.useMemo)(()=>({
123
- focusManager: focusManager,
124
- parentNode: node
125
- }), [
126
- node,
127
- focusManager
128
- ]);
129
- return /*#__PURE__*/ (0, ($parcel$interopDefault($euGna$react))).createElement($a7a032acae3ddda9$var$FocusContext.Provider, {
130
- value: value
131
- }, /*#__PURE__*/ (0, ($parcel$interopDefault($euGna$react))).createElement("span", {
132
- "data-focus-scope-start": true,
133
- hidden: true,
134
- ref: startRef
135
- }), children, /*#__PURE__*/ (0, ($parcel$interopDefault($euGna$react))).createElement("span", {
136
- "data-focus-scope-end": true,
137
- hidden: true,
138
- ref: endRef
139
- }));
140
- }
141
- function $a7a032acae3ddda9$export$10c5169755ce7bd7() {
142
- var _useContext;
143
- return (_useContext = (0, $euGna$react.useContext)($a7a032acae3ddda9$var$FocusContext)) === null || _useContext === void 0 ? void 0 : _useContext.focusManager;
144
- }
145
- function $a7a032acae3ddda9$var$createFocusManagerForScope(scopeRef) {
146
- return {
147
- focusNext (opts = {}) {
148
- let scope = scopeRef.current;
149
- let { from: from, tabbable: tabbable, wrap: wrap, accept: accept } = opts;
150
- var _scope_;
151
- let node = from || (0, $euGna$reactariautils.getActiveElement)((0, $euGna$reactariautils.getOwnerDocument)((_scope_ = scope[0]) !== null && _scope_ !== void 0 ? _scope_ : undefined));
152
- let sentinel = scope[0].previousElementSibling;
153
- let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
154
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
155
- tabbable: tabbable,
156
- accept: accept
157
- }, scope);
158
- walker.currentNode = $a7a032acae3ddda9$var$isElementInScope(node, scope) ? node : sentinel;
159
- let nextNode = walker.nextNode();
160
- if (!nextNode && wrap) {
161
- walker.currentNode = sentinel;
162
- nextNode = walker.nextNode();
163
- }
164
- if (nextNode) $a7a032acae3ddda9$var$focusElement(nextNode, true);
165
- return nextNode;
166
- },
167
- focusPrevious (opts = {}) {
168
- let scope = scopeRef.current;
169
- let { from: from, tabbable: tabbable, wrap: wrap, accept: accept } = opts;
170
- var _scope_;
171
- let node = from || (0, $euGna$reactariautils.getActiveElement)((0, $euGna$reactariautils.getOwnerDocument)((_scope_ = scope[0]) !== null && _scope_ !== void 0 ? _scope_ : undefined));
172
- let sentinel = scope[scope.length - 1].nextElementSibling;
173
- let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
174
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
175
- tabbable: tabbable,
176
- accept: accept
177
- }, scope);
178
- walker.currentNode = $a7a032acae3ddda9$var$isElementInScope(node, scope) ? node : sentinel;
179
- let previousNode = walker.previousNode();
180
- if (!previousNode && wrap) {
181
- walker.currentNode = sentinel;
182
- previousNode = walker.previousNode();
183
- }
184
- if (previousNode) $a7a032acae3ddda9$var$focusElement(previousNode, true);
185
- return previousNode;
186
- },
187
- focusFirst (opts = {}) {
188
- let scope = scopeRef.current;
189
- let { tabbable: tabbable, accept: accept } = opts;
190
- let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
191
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
192
- tabbable: tabbable,
193
- accept: accept
194
- }, scope);
195
- walker.currentNode = scope[0].previousElementSibling;
196
- let nextNode = walker.nextNode();
197
- if (nextNode) $a7a032acae3ddda9$var$focusElement(nextNode, true);
198
- return nextNode;
199
- },
200
- focusLast (opts = {}) {
201
- let scope = scopeRef.current;
202
- let { tabbable: tabbable, accept: accept } = opts;
203
- let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
204
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
205
- tabbable: tabbable,
206
- accept: accept
207
- }, scope);
208
- walker.currentNode = scope[scope.length - 1].nextElementSibling;
209
- let previousNode = walker.previousNode();
210
- if (previousNode) $a7a032acae3ddda9$var$focusElement(previousNode, true);
211
- return previousNode;
212
- }
213
- };
214
- }
215
- function $a7a032acae3ddda9$var$getScopeRoot(scope) {
216
- return scope[0].parentElement;
217
- }
218
- function $a7a032acae3ddda9$var$shouldContainFocus(scopeRef) {
219
- let scope = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode($a7a032acae3ddda9$var$activeScope);
220
- while(scope && scope.scopeRef !== scopeRef){
221
- if (scope.contain) return false;
222
- scope = scope.parent;
223
- }
224
- return true;
225
- }
226
- function $a7a032acae3ddda9$var$isTabbableRadio(element) {
227
- if (element.checked) return true;
228
- let radios = [];
229
- if (!element.form) radios = [
230
- ...(0, $euGna$reactariautils.getOwnerDocument)(element).querySelectorAll(`input[type="radio"][name="${CSS.escape(element.name)}"]`)
231
- ].filter((radio)=>!radio.form);
232
- else {
233
- var _element_form_elements, _element_form;
234
- let radioList = (_element_form = element.form) === null || _element_form === void 0 ? void 0 : (_element_form_elements = _element_form.elements) === null || _element_form_elements === void 0 ? void 0 : _element_form_elements.namedItem(element.name);
235
- radios = [
236
- ...radioList !== null && radioList !== void 0 ? radioList : []
237
- ];
238
- }
239
- if (!radios) return false;
240
- let anyChecked = radios.some((radio)=>radio.checked);
241
- return !anyChecked;
242
- }
243
- function $a7a032acae3ddda9$var$useFocusContainment(scopeRef, contain) {
244
- let focusedNode = (0, $euGna$react.useRef)(undefined);
245
- let raf = (0, $euGna$react.useRef)(undefined);
246
- (0, $euGna$reactariautils.useLayoutEffect)(()=>{
247
- let scope = scopeRef.current;
248
- if (!contain) {
249
- // if contain was changed, then we should cancel any ongoing waits to pull focus back into containment
250
- if (raf.current) {
251
- cancelAnimationFrame(raf.current);
252
- raf.current = undefined;
253
- }
254
- return;
255
- }
256
- const ownerDocument = (0, $euGna$reactariautils.getOwnerDocument)(scope ? scope[0] : undefined);
257
- // Handle the Tab key to contain focus within the scope
258
- let onKeyDown = (e)=>{
259
- if (e.key !== 'Tab' || e.altKey || e.ctrlKey || e.metaKey || !$a7a032acae3ddda9$var$shouldContainFocus(scopeRef) || e.isComposing) return;
260
- let focusedElement = (0, $euGna$reactariautils.getActiveElement)(ownerDocument);
261
- let scope = scopeRef.current;
262
- if (!scope || !$a7a032acae3ddda9$var$isElementInScope(focusedElement, scope)) return;
263
- let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
264
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
265
- tabbable: true
266
- }, scope);
267
- if (!focusedElement) return;
268
- walker.currentNode = focusedElement;
269
- let nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
270
- if (!nextElement) {
271
- walker.currentNode = e.shiftKey ? scope[scope.length - 1].nextElementSibling : scope[0].previousElementSibling;
272
- nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
273
- }
274
- e.preventDefault();
275
- if (nextElement) $a7a032acae3ddda9$var$focusElement(nextElement, true);
276
- };
277
- let onFocus = (e)=>{
278
- // If focusing an element in a child scope of the currently active scope, the child becomes active.
279
- // Moving out of the active scope to an ancestor is not allowed.
280
- if ((!$a7a032acae3ddda9$var$activeScope || $a7a032acae3ddda9$var$isAncestorScope($a7a032acae3ddda9$var$activeScope, scopeRef)) && $a7a032acae3ddda9$var$isElementInScope((0, $euGna$reactariautils.getEventTarget)(e), scopeRef.current)) {
281
- $a7a032acae3ddda9$var$activeScope = scopeRef;
282
- focusedNode.current = (0, $euGna$reactariautils.getEventTarget)(e);
283
- } else if ($a7a032acae3ddda9$var$shouldContainFocus(scopeRef) && !$a7a032acae3ddda9$var$isElementInChildScope((0, $euGna$reactariautils.getEventTarget)(e), scopeRef)) {
284
- // If a focus event occurs outside the active scope (e.g. user tabs from browser location bar),
285
- // restore focus to the previously focused node or the first tabbable element in the active scope.
286
- if (focusedNode.current) focusedNode.current.focus();
287
- else if ($a7a032acae3ddda9$var$activeScope && $a7a032acae3ddda9$var$activeScope.current) $a7a032acae3ddda9$var$focusFirstInScope($a7a032acae3ddda9$var$activeScope.current);
288
- } else if ($a7a032acae3ddda9$var$shouldContainFocus(scopeRef)) focusedNode.current = (0, $euGna$reactariautils.getEventTarget)(e);
289
- };
290
- let onBlur = (e)=>{
291
- // Firefox doesn't shift focus back to the Dialog properly without this
292
- if (raf.current) cancelAnimationFrame(raf.current);
293
- raf.current = requestAnimationFrame(()=>{
294
- // Patches infinite focus coersion loop for Android Talkback where the user isn't able to move the virtual cursor
295
- // if within a containing focus scope. Bug filed against Chrome: https://issuetracker.google.com/issues/384844019.
296
- // Note that this means focus can leave focus containing modals due to this, but it is isolated to Chrome Talkback.
297
- let modality = (0, $euGna$reactariainteractions.getInteractionModality)();
298
- let shouldSkipFocusRestore = (modality === 'virtual' || modality === null) && (0, $euGna$reactariautils.isAndroid)() && (0, $euGna$reactariautils.isChrome)();
299
- // Use document.activeElement instead of e.relatedTarget so we can tell if user clicked into iframe
300
- let activeElement = (0, $euGna$reactariautils.getActiveElement)(ownerDocument);
301
- if (!shouldSkipFocusRestore && activeElement && $a7a032acae3ddda9$var$shouldContainFocus(scopeRef) && !$a7a032acae3ddda9$var$isElementInChildScope(activeElement, scopeRef)) {
302
- $a7a032acae3ddda9$var$activeScope = scopeRef;
303
- let target = (0, $euGna$reactariautils.getEventTarget)(e);
304
- if (target && target.isConnected) {
305
- var _focusedNode_current;
306
- focusedNode.current = target;
307
- (_focusedNode_current = focusedNode.current) === null || _focusedNode_current === void 0 ? void 0 : _focusedNode_current.focus();
308
- } else if ($a7a032acae3ddda9$var$activeScope.current) $a7a032acae3ddda9$var$focusFirstInScope($a7a032acae3ddda9$var$activeScope.current);
309
- }
310
- });
311
- };
312
- ownerDocument.addEventListener('keydown', onKeyDown, false);
313
- ownerDocument.addEventListener('focusin', onFocus, false);
314
- scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener('focusin', onFocus, false));
315
- scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener('focusout', onBlur, false));
316
- return ()=>{
317
- ownerDocument.removeEventListener('keydown', onKeyDown, false);
318
- ownerDocument.removeEventListener('focusin', onFocus, false);
319
- scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener('focusin', onFocus, false));
320
- scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener('focusout', onBlur, false));
321
- };
322
- }, [
323
- scopeRef,
324
- contain
325
- ]);
326
- // This is a useLayoutEffect so it is guaranteed to run before our async synthetic blur
327
- (0, $euGna$reactariautils.useLayoutEffect)(()=>{
328
- return ()=>{
329
- if (raf.current) cancelAnimationFrame(raf.current);
330
- };
331
- }, [
332
- raf
333
- ]);
334
- }
335
- function $a7a032acae3ddda9$var$isElementInAnyScope(element) {
336
- return $a7a032acae3ddda9$var$isElementInChildScope(element);
337
- }
338
- function $a7a032acae3ddda9$var$isElementInScope(element, scope) {
339
- if (!element) return false;
340
- if (!scope) return false;
341
- return scope.some((node)=>(0, $euGna$reactariautils.nodeContains)(node, element));
342
- }
343
- function $a7a032acae3ddda9$var$isElementInChildScope(element, scope = null) {
344
- // If the element is within a top layer element (e.g. toasts), always allow moving focus there.
345
- if (element instanceof Element && element.closest('[data-react-aria-top-layer]')) return true;
346
- // node.contains in isElementInScope covers child scopes that are also DOM children,
347
- // but does not cover child scopes in portals.
348
- for (let { scopeRef: s } of $a7a032acae3ddda9$export$d06fae2ee68b101e.traverse($a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scope))){
349
- if (s && $a7a032acae3ddda9$var$isElementInScope(element, s.current)) return true;
350
- }
351
- return false;
352
- }
353
- function $a7a032acae3ddda9$export$1258395f99bf9cbf(element) {
354
- return $a7a032acae3ddda9$var$isElementInChildScope(element, $a7a032acae3ddda9$var$activeScope);
355
- }
356
- function $a7a032acae3ddda9$var$isAncestorScope(ancestor, scope) {
357
- var _focusScopeTree_getTreeNode;
358
- let parent = (_focusScopeTree_getTreeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scope)) === null || _focusScopeTree_getTreeNode === void 0 ? void 0 : _focusScopeTree_getTreeNode.parent;
359
- while(parent){
360
- if (parent.scopeRef === ancestor) return true;
361
- parent = parent.parent;
362
- }
363
- return false;
364
- }
365
- function $a7a032acae3ddda9$var$focusElement(element, scroll = false) {
366
- if (element != null && !scroll) try {
367
- (0, $euGna$reactariainteractions.focusSafely)(element);
368
- } catch {
369
- // ignore
370
- }
371
- else if (element != null) try {
372
- element.focus();
373
- } catch {
374
- // ignore
375
- }
376
- }
377
- function $a7a032acae3ddda9$var$getFirstInScope(scope, tabbable = true) {
378
- let sentinel = scope[0].previousElementSibling;
379
- let scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
380
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
381
- tabbable: tabbable
382
- }, scope);
383
- walker.currentNode = sentinel;
384
- let nextNode = walker.nextNode();
385
- // If the scope does not contain a tabbable element, use the first focusable element.
386
- if (tabbable && !nextNode) {
387
- scopeRoot = $a7a032acae3ddda9$var$getScopeRoot(scope);
388
- walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(scopeRoot, {
389
- tabbable: false
390
- }, scope);
391
- walker.currentNode = sentinel;
392
- nextNode = walker.nextNode();
393
- }
394
- return nextNode;
395
- }
396
- function $a7a032acae3ddda9$var$focusFirstInScope(scope, tabbable = true) {
397
- $a7a032acae3ddda9$var$focusElement($a7a032acae3ddda9$var$getFirstInScope(scope, tabbable));
398
- }
399
- function $a7a032acae3ddda9$var$useAutoFocus(scopeRef, autoFocus) {
400
- const autoFocusRef = (0, ($parcel$interopDefault($euGna$react))).useRef(autoFocus);
401
- (0, $euGna$react.useEffect)(()=>{
402
- if (autoFocusRef.current) {
403
- $a7a032acae3ddda9$var$activeScope = scopeRef;
404
- const ownerDocument = (0, $euGna$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined);
405
- if (!$a7a032acae3ddda9$var$isElementInScope((0, $euGna$reactariautils.getActiveElement)(ownerDocument), $a7a032acae3ddda9$var$activeScope.current) && scopeRef.current) $a7a032acae3ddda9$var$focusFirstInScope(scopeRef.current);
406
- }
407
- autoFocusRef.current = false;
408
- }, [
409
- scopeRef
410
- ]);
411
- }
412
- function $a7a032acae3ddda9$var$useActiveScopeTracker(scopeRef, restore, contain) {
413
- // tracks the active scope, in case restore and contain are both false.
414
- // if either are true, this is tracked in useRestoreFocus or useFocusContainment.
415
- (0, $euGna$reactariautils.useLayoutEffect)(()=>{
416
- if (restore || contain) return;
417
- let scope = scopeRef.current;
418
- const ownerDocument = (0, $euGna$reactariautils.getOwnerDocument)(scope ? scope[0] : undefined);
419
- let onFocus = (e)=>{
420
- let target = (0, $euGna$reactariautils.getEventTarget)(e);
421
- if ($a7a032acae3ddda9$var$isElementInScope(target, scopeRef.current)) $a7a032acae3ddda9$var$activeScope = scopeRef;
422
- else if (!$a7a032acae3ddda9$var$isElementInAnyScope(target)) $a7a032acae3ddda9$var$activeScope = null;
423
- };
424
- ownerDocument.addEventListener('focusin', onFocus, false);
425
- scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener('focusin', onFocus, false));
426
- return ()=>{
427
- ownerDocument.removeEventListener('focusin', onFocus, false);
428
- scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener('focusin', onFocus, false));
429
- };
430
- }, [
431
- scopeRef,
432
- restore,
433
- contain
434
- ]);
435
- }
436
- function $a7a032acae3ddda9$var$shouldRestoreFocus(scopeRef) {
437
- let scope = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode($a7a032acae3ddda9$var$activeScope);
438
- while(scope && scope.scopeRef !== scopeRef){
439
- if (scope.nodeToRestore) return false;
440
- scope = scope.parent;
441
- }
442
- return (scope === null || scope === void 0 ? void 0 : scope.scopeRef) === scopeRef;
443
- }
444
- function $a7a032acae3ddda9$var$useRestoreFocus(scopeRef, restoreFocus, contain) {
445
- // create a ref during render instead of useLayoutEffect so the active element is saved before a child with autoFocus=true mounts.
446
- // eslint-disable-next-line no-restricted-globals
447
- const nodeToRestoreRef = (0, $euGna$react.useRef)(typeof document !== 'undefined' ? (0, $euGna$reactariautils.getActiveElement)((0, $euGna$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined)) : null);
448
- // restoring scopes should all track if they are active regardless of contain, but contain already tracks it plus logic to contain the focus
449
- // restoring-non-containing scopes should only care if they become active so they can perform the restore
450
- (0, $euGna$reactariautils.useLayoutEffect)(()=>{
451
- let scope = scopeRef.current;
452
- const ownerDocument = (0, $euGna$reactariautils.getOwnerDocument)(scope ? scope[0] : undefined);
453
- if (!restoreFocus || contain) return;
454
- let onFocus = ()=>{
455
- // If focusing an element in a child scope of the currently active scope, the child becomes active.
456
- // Moving out of the active scope to an ancestor is not allowed.
457
- if ((!$a7a032acae3ddda9$var$activeScope || $a7a032acae3ddda9$var$isAncestorScope($a7a032acae3ddda9$var$activeScope, scopeRef)) && $a7a032acae3ddda9$var$isElementInScope((0, $euGna$reactariautils.getActiveElement)(ownerDocument), scopeRef.current)) $a7a032acae3ddda9$var$activeScope = scopeRef;
458
- };
459
- ownerDocument.addEventListener('focusin', onFocus, false);
460
- scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.addEventListener('focusin', onFocus, false));
461
- return ()=>{
462
- ownerDocument.removeEventListener('focusin', onFocus, false);
463
- scope === null || scope === void 0 ? void 0 : scope.forEach((element)=>element.removeEventListener('focusin', onFocus, false));
464
- };
465
- // eslint-disable-next-line react-hooks/exhaustive-deps
466
- }, [
467
- scopeRef,
468
- contain
469
- ]);
470
- (0, $euGna$reactariautils.useLayoutEffect)(()=>{
471
- const ownerDocument = (0, $euGna$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined);
472
- if (!restoreFocus) return;
473
- // Handle the Tab key so that tabbing out of the scope goes to the next element
474
- // after the node that had focus when the scope mounted. This is important when
475
- // using portals for overlays, so that focus goes to the expected element when
476
- // tabbing out of the overlay.
477
- let onKeyDown = (e)=>{
478
- if (e.key !== 'Tab' || e.altKey || e.ctrlKey || e.metaKey || !$a7a032acae3ddda9$var$shouldContainFocus(scopeRef) || e.isComposing) return;
479
- let focusedElement = ownerDocument.activeElement;
480
- if (!$a7a032acae3ddda9$var$isElementInChildScope(focusedElement, scopeRef) || !$a7a032acae3ddda9$var$shouldRestoreFocus(scopeRef)) return;
481
- let treeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
482
- if (!treeNode) return;
483
- let nodeToRestore = treeNode.nodeToRestore;
484
- // Create a DOM tree walker that matches all tabbable elements
485
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(ownerDocument.body, {
486
- tabbable: true
487
- });
488
- // Find the next tabbable element after the currently focused element
489
- walker.currentNode = focusedElement;
490
- let nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
491
- if (!nodeToRestore || !nodeToRestore.isConnected || nodeToRestore === ownerDocument.body) {
492
- nodeToRestore = undefined;
493
- treeNode.nodeToRestore = undefined;
494
- }
495
- // If there is no next element, or it is outside the current scope, move focus to the
496
- // next element after the node to restore to instead.
497
- if ((!nextElement || !$a7a032acae3ddda9$var$isElementInChildScope(nextElement, scopeRef)) && nodeToRestore) {
498
- walker.currentNode = nodeToRestore;
499
- // Skip over elements within the scope, in case the scope immediately follows the node to restore.
500
- do nextElement = e.shiftKey ? walker.previousNode() : walker.nextNode();
501
- while ($a7a032acae3ddda9$var$isElementInChildScope(nextElement, scopeRef));
502
- e.preventDefault();
503
- e.stopPropagation();
504
- if (nextElement) $a7a032acae3ddda9$var$focusElement(nextElement, true);
505
- else // If there is no next element and the nodeToRestore isn't within a FocusScope (i.e. we are leaving the top level focus scope)
506
- // then move focus to the body.
507
- // Otherwise restore focus to the nodeToRestore (e.g menu within a popover -> tabbing to close the menu should move focus to menu trigger)
508
- if (!$a7a032acae3ddda9$var$isElementInAnyScope(nodeToRestore)) focusedElement.blur();
509
- else $a7a032acae3ddda9$var$focusElement(nodeToRestore, true);
510
- }
511
- };
512
- if (!contain) ownerDocument.addEventListener('keydown', onKeyDown, true);
513
- return ()=>{
514
- if (!contain) ownerDocument.removeEventListener('keydown', onKeyDown, true);
515
- };
516
- }, [
517
- scopeRef,
518
- restoreFocus,
519
- contain
520
- ]);
521
- // useLayoutEffect instead of useEffect so the active element is saved synchronously instead of asynchronously.
522
- (0, $euGna$reactariautils.useLayoutEffect)(()=>{
523
- const ownerDocument = (0, $euGna$reactariautils.getOwnerDocument)(scopeRef.current ? scopeRef.current[0] : undefined);
524
- if (!restoreFocus) return;
525
- let treeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
526
- if (!treeNode) return;
527
- var _nodeToRestoreRef_current;
528
- treeNode.nodeToRestore = (_nodeToRestoreRef_current = nodeToRestoreRef.current) !== null && _nodeToRestoreRef_current !== void 0 ? _nodeToRestoreRef_current : undefined;
529
- return ()=>{
530
- let treeNode = $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(scopeRef);
531
- if (!treeNode) return;
532
- let nodeToRestore = treeNode.nodeToRestore;
533
- // if we already lost focus to the body and this was the active scope, then we should attempt to restore
534
- let activeElement = (0, $euGna$reactariautils.getActiveElement)(ownerDocument);
535
- if (restoreFocus && nodeToRestore && (activeElement && $a7a032acae3ddda9$var$isElementInChildScope(activeElement, scopeRef) || activeElement === ownerDocument.body && $a7a032acae3ddda9$var$shouldRestoreFocus(scopeRef))) {
536
- // freeze the focusScopeTree so it persists after the raf, otherwise during unmount nodes are removed from it
537
- let clonedTree = $a7a032acae3ddda9$export$d06fae2ee68b101e.clone();
538
- requestAnimationFrame(()=>{
539
- // Only restore focus if we've lost focus to the body, the alternative is that focus has been purposefully moved elsewhere
540
- if (ownerDocument.activeElement === ownerDocument.body) {
541
- // look up the tree starting with our scope to find a nodeToRestore still in the DOM
542
- let treeNode = clonedTree.getTreeNode(scopeRef);
543
- while(treeNode){
544
- if (treeNode.nodeToRestore && treeNode.nodeToRestore.isConnected) {
545
- $a7a032acae3ddda9$var$restoreFocusToElement(treeNode.nodeToRestore);
546
- return;
547
- }
548
- treeNode = treeNode.parent;
549
- }
550
- // If no nodeToRestore was found, focus the first element in the nearest
551
- // ancestor scope that is still in the tree.
552
- treeNode = clonedTree.getTreeNode(scopeRef);
553
- while(treeNode){
554
- if (treeNode.scopeRef && treeNode.scopeRef.current && $a7a032acae3ddda9$export$d06fae2ee68b101e.getTreeNode(treeNode.scopeRef)) {
555
- let node = $a7a032acae3ddda9$var$getFirstInScope(treeNode.scopeRef.current, true);
556
- $a7a032acae3ddda9$var$restoreFocusToElement(node);
557
- return;
558
- }
559
- treeNode = treeNode.parent;
560
- }
561
- }
562
- });
563
- }
564
- };
565
- }, [
566
- scopeRef,
567
- restoreFocus
568
- ]);
569
- }
570
- function $a7a032acae3ddda9$var$restoreFocusToElement(node) {
571
- // Dispatch a custom event that parent elements can intercept to customize focus restoration.
572
- // For example, virtualized collection components reuse DOM elements, so the original element
573
- // might still exist in the DOM but representing a different item.
574
- if (node.dispatchEvent(new CustomEvent($a7a032acae3ddda9$var$RESTORE_FOCUS_EVENT, {
575
- bubbles: true,
576
- cancelable: true
577
- }))) $a7a032acae3ddda9$var$focusElement(node);
578
- }
579
- function $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, opts, scope) {
580
- let filter = (opts === null || opts === void 0 ? void 0 : opts.tabbable) ? (0, $euGna$reactariautils.isTabbable) : (0, $euGna$reactariautils.isFocusable);
581
- // Ensure that root is an Element or fall back appropriately
582
- let rootElement = (root === null || root === void 0 ? void 0 : root.nodeType) === Node.ELEMENT_NODE ? root : null;
583
- // Determine the document to use
584
- let doc = (0, $euGna$reactariautils.getOwnerDocument)(rootElement);
585
- // Create a TreeWalker, ensuring the root is an Element or Document
586
- let walker = (0, $euGna$reactariautils.createShadowTreeWalker)(doc, root || doc, NodeFilter.SHOW_ELEMENT, {
587
- acceptNode (node) {
588
- // Skip nodes inside the starting node.
589
- if ((0, $euGna$reactariautils.nodeContains)(opts === null || opts === void 0 ? void 0 : opts.from, node)) return NodeFilter.FILTER_REJECT;
590
- if ((opts === null || opts === void 0 ? void 0 : opts.tabbable) && node.tagName === 'INPUT' && node.getAttribute('type') === 'radio') {
591
- // If the radio is in a form, we can get all the other radios by name
592
- if (!$a7a032acae3ddda9$var$isTabbableRadio(node)) return NodeFilter.FILTER_REJECT;
593
- // If the radio is in the same group as the current node and none are selected, we can skip it
594
- if (walker.currentNode.tagName === 'INPUT' && walker.currentNode.type === 'radio' && walker.currentNode.name === node.name) return NodeFilter.FILTER_REJECT;
595
- }
596
- if (filter(node) && (!scope || $a7a032acae3ddda9$var$isElementInScope(node, scope)) && (!(opts === null || opts === void 0 ? void 0 : opts.accept) || opts.accept(node))) return NodeFilter.FILTER_ACCEPT;
597
- return NodeFilter.FILTER_SKIP;
598
- }
599
- });
600
- if (opts === null || opts === void 0 ? void 0 : opts.from) walker.currentNode = opts.from;
601
- return walker;
602
- }
603
- function $a7a032acae3ddda9$export$c5251b9e124bf29(ref, defaultOptions = {}) {
604
- return {
605
- focusNext (opts = {}) {
606
- let root = ref.current;
607
- if (!root) return null;
608
- let { from: from, tabbable: tabbable = defaultOptions.tabbable, wrap: wrap = defaultOptions.wrap, accept: accept = defaultOptions.accept } = opts;
609
- let node = from || (0, $euGna$reactariautils.getActiveElement)((0, $euGna$reactariautils.getOwnerDocument)(root));
610
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
611
- tabbable: tabbable,
612
- accept: accept
613
- });
614
- if ((0, $euGna$reactariautils.nodeContains)(root, node)) walker.currentNode = node;
615
- let nextNode = walker.nextNode();
616
- if (!nextNode && wrap) {
617
- walker.currentNode = root;
618
- nextNode = walker.nextNode();
619
- }
620
- if (nextNode) $a7a032acae3ddda9$var$focusElement(nextNode, true);
621
- return nextNode;
622
- },
623
- focusPrevious (opts = defaultOptions) {
624
- let root = ref.current;
625
- if (!root) return null;
626
- let { from: from, tabbable: tabbable = defaultOptions.tabbable, wrap: wrap = defaultOptions.wrap, accept: accept = defaultOptions.accept } = opts;
627
- let node = from || (0, $euGna$reactariautils.getActiveElement)((0, $euGna$reactariautils.getOwnerDocument)(root));
628
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
629
- tabbable: tabbable,
630
- accept: accept
631
- });
632
- if ((0, $euGna$reactariautils.nodeContains)(root, node)) walker.currentNode = node;
633
- else {
634
- let next = $a7a032acae3ddda9$var$last(walker);
635
- if (next) $a7a032acae3ddda9$var$focusElement(next, true);
636
- return next !== null && next !== void 0 ? next : null;
637
- }
638
- let previousNode = walker.previousNode();
639
- if (!previousNode && wrap) {
640
- walker.currentNode = root;
641
- let lastNode = $a7a032acae3ddda9$var$last(walker);
642
- if (!lastNode) // couldn't wrap
643
- return null;
644
- previousNode = lastNode;
645
- }
646
- if (previousNode) $a7a032acae3ddda9$var$focusElement(previousNode, true);
647
- return previousNode !== null && previousNode !== void 0 ? previousNode : null;
648
- },
649
- focusFirst (opts = defaultOptions) {
650
- let root = ref.current;
651
- if (!root) return null;
652
- let { tabbable: tabbable = defaultOptions.tabbable, accept: accept = defaultOptions.accept } = opts;
653
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
654
- tabbable: tabbable,
655
- accept: accept
656
- });
657
- let nextNode = walker.nextNode();
658
- if (nextNode) $a7a032acae3ddda9$var$focusElement(nextNode, true);
659
- return nextNode;
660
- },
661
- focusLast (opts = defaultOptions) {
662
- let root = ref.current;
663
- if (!root) return null;
664
- let { tabbable: tabbable = defaultOptions.tabbable, accept: accept = defaultOptions.accept } = opts;
665
- let walker = $a7a032acae3ddda9$export$2d6ec8fc375ceafa(root, {
666
- tabbable: tabbable,
667
- accept: accept
668
- });
669
- let next = $a7a032acae3ddda9$var$last(walker);
670
- if (next) $a7a032acae3ddda9$var$focusElement(next, true);
671
- return next !== null && next !== void 0 ? next : null;
672
- }
673
- };
674
- }
675
- function $a7a032acae3ddda9$var$last(walker) {
676
- let next = undefined;
677
- let last;
678
- do {
679
- last = walker.lastChild();
680
- if (last) next = last;
681
- }while (last);
682
- return next;
683
- }
684
- class $a7a032acae3ddda9$var$Tree {
685
- get size() {
686
- return this.fastMap.size;
687
- }
688
- getTreeNode(data) {
689
- return this.fastMap.get(data);
690
- }
691
- addTreeNode(scopeRef, parent, nodeToRestore) {
692
- let parentNode = this.fastMap.get(parent !== null && parent !== void 0 ? parent : null);
693
- if (!parentNode) return;
694
- let node = new $a7a032acae3ddda9$var$TreeNode({
695
- scopeRef: scopeRef
696
- });
697
- parentNode.addChild(node);
698
- node.parent = parentNode;
699
- this.fastMap.set(scopeRef, node);
700
- if (nodeToRestore) node.nodeToRestore = nodeToRestore;
701
- }
702
- addNode(node) {
703
- this.fastMap.set(node.scopeRef, node);
704
- }
705
- removeTreeNode(scopeRef) {
706
- // never remove the root
707
- if (scopeRef === null) return;
708
- let node = this.fastMap.get(scopeRef);
709
- if (!node) return;
710
- let parentNode = node.parent;
711
- // when we remove a scope, check if any sibling scopes are trying to restore focus to something inside the scope we're removing
712
- // if we are, then replace the siblings restore with the restore from the scope we're removing
713
- 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;
714
- let children = node.children;
715
- if (parentNode) {
716
- parentNode.removeChild(node);
717
- if (children.size > 0) children.forEach((child)=>parentNode && parentNode.addChild(child));
718
- }
719
- this.fastMap.delete(node.scopeRef);
720
- }
721
- // Pre Order Depth First
722
- *traverse(node = this.root) {
723
- if (node.scopeRef != null) yield node;
724
- if (node.children.size > 0) for (let child of node.children)yield* this.traverse(child);
725
- }
726
- clone() {
727
- var _node_parent;
728
- let newTree = new $a7a032acae3ddda9$var$Tree();
729
- var _node_parent_scopeRef;
730
- 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);
731
- return newTree;
732
- }
733
- constructor(){
734
- this.fastMap = new Map();
735
- this.root = new $a7a032acae3ddda9$var$TreeNode({
736
- scopeRef: null
737
- });
738
- this.fastMap.set(null, this.root);
739
- }
740
- }
741
- class $a7a032acae3ddda9$var$TreeNode {
742
- addChild(node) {
743
- this.children.add(node);
744
- node.parent = this;
745
- }
746
- removeChild(node) {
747
- this.children.delete(node);
748
- node.parent = undefined;
749
- }
750
- constructor(props){
751
- this.children = new Set();
752
- this.contain = false;
753
- this.scopeRef = props.scopeRef;
754
- }
755
- }
756
- let $a7a032acae3ddda9$export$d06fae2ee68b101e = new $a7a032acae3ddda9$var$Tree();
757
-
758
-
759
- //# sourceMappingURL=FocusScope.main.js.map