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