1 |
|
2 | import * as React from 'react';
|
3 | import PropTypes from 'prop-types';
|
4 | import { exactProp, elementAcceptingRef, unstable_useForkRef as useForkRef, unstable_ownerDocument as ownerDocument } from '@mui/utils';
|
5 | import { jsx as _jsx } from "react/jsx-runtime";
|
6 | import { jsxs as _jsxs } from "react/jsx-runtime";
|
7 |
|
8 | const candidatesSelector = ['input', 'select', 'textarea', 'a[href]', 'button', '[tabindex]', 'audio[controls]', 'video[controls]', '[contenteditable]:not([contenteditable="false"])'].join(',');
|
9 | function getTabIndex(node) {
|
10 | const tabindexAttr = parseInt(node.getAttribute('tabindex') || '', 10);
|
11 | if (!Number.isNaN(tabindexAttr)) {
|
12 | return tabindexAttr;
|
13 | }
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | if (node.contentEditable === 'true' || (node.nodeName === 'AUDIO' || node.nodeName === 'VIDEO' || node.nodeName === 'DETAILS') && node.getAttribute('tabindex') === null) {
|
24 | return 0;
|
25 | }
|
26 | return node.tabIndex;
|
27 | }
|
28 | function isNonTabbableRadio(node) {
|
29 | if (node.tagName !== 'INPUT' || node.type !== 'radio') {
|
30 | return false;
|
31 | }
|
32 | if (!node.name) {
|
33 | return false;
|
34 | }
|
35 | const getRadio = selector => node.ownerDocument.querySelector(`input[type="radio"]${selector}`);
|
36 | let roving = getRadio(`[name="${node.name}"]:checked`);
|
37 | if (!roving) {
|
38 | roving = getRadio(`[name="${node.name}"]`);
|
39 | }
|
40 | return roving !== node;
|
41 | }
|
42 | function isNodeMatchingSelectorFocusable(node) {
|
43 | if (node.disabled || node.tagName === 'INPUT' && node.type === 'hidden' || isNonTabbableRadio(node)) {
|
44 | return false;
|
45 | }
|
46 | return true;
|
47 | }
|
48 | function defaultGetTabbable(root) {
|
49 | const regularTabNodes = [];
|
50 | const orderedTabNodes = [];
|
51 | Array.from(root.querySelectorAll(candidatesSelector)).forEach((node, i) => {
|
52 | const nodeTabIndex = getTabIndex(node);
|
53 | if (nodeTabIndex === -1 || !isNodeMatchingSelectorFocusable(node)) {
|
54 | return;
|
55 | }
|
56 | if (nodeTabIndex === 0) {
|
57 | regularTabNodes.push(node);
|
58 | } else {
|
59 | orderedTabNodes.push({
|
60 | documentOrder: i,
|
61 | tabIndex: nodeTabIndex,
|
62 | node: node
|
63 | });
|
64 | }
|
65 | });
|
66 | return orderedTabNodes.sort((a, b) => a.tabIndex === b.tabIndex ? a.documentOrder - b.documentOrder : a.tabIndex - b.tabIndex).map(a => a.node).concat(regularTabNodes);
|
67 | }
|
68 | function defaultIsEnabled() {
|
69 | return true;
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | function FocusTrap(props) {
|
84 | const {
|
85 | children,
|
86 | disableAutoFocus = false,
|
87 | disableEnforceFocus = false,
|
88 | disableRestoreFocus = false,
|
89 | getTabbable = defaultGetTabbable,
|
90 | isEnabled = defaultIsEnabled,
|
91 | open
|
92 | } = props;
|
93 | const ignoreNextEnforceFocus = React.useRef(false);
|
94 | const sentinelStart = React.useRef(null);
|
95 | const sentinelEnd = React.useRef(null);
|
96 | const nodeToRestore = React.useRef(null);
|
97 | const reactFocusEventTarget = React.useRef(null);
|
98 |
|
99 |
|
100 | const activated = React.useRef(false);
|
101 | const rootRef = React.useRef(null);
|
102 |
|
103 | const handleRef = useForkRef(children.ref, rootRef);
|
104 | const lastKeydown = React.useRef(null);
|
105 | React.useEffect(() => {
|
106 |
|
107 | if (!open || !rootRef.current) {
|
108 | return;
|
109 | }
|
110 | activated.current = !disableAutoFocus;
|
111 | }, [disableAutoFocus, open]);
|
112 | React.useEffect(() => {
|
113 |
|
114 | if (!open || !rootRef.current) {
|
115 | return;
|
116 | }
|
117 | const doc = ownerDocument(rootRef.current);
|
118 | if (!rootRef.current.contains(doc.activeElement)) {
|
119 | if (!rootRef.current.hasAttribute('tabIndex')) {
|
120 | if (process.env.NODE_ENV !== 'production') {
|
121 | console.error(['MUI: The modal content node does not accept focus.', 'For the benefit of assistive technologies, ' + 'the tabIndex of the node is being set to "-1".'].join('\n'));
|
122 | }
|
123 | rootRef.current.setAttribute('tabIndex', '-1');
|
124 | }
|
125 | if (activated.current) {
|
126 | rootRef.current.focus();
|
127 | }
|
128 | }
|
129 | return () => {
|
130 |
|
131 | if (!disableRestoreFocus) {
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | if (nodeToRestore.current && nodeToRestore.current.focus) {
|
137 | ignoreNextEnforceFocus.current = true;
|
138 | nodeToRestore.current.focus();
|
139 | }
|
140 | nodeToRestore.current = null;
|
141 | }
|
142 | };
|
143 |
|
144 |
|
145 |
|
146 | }, [open]);
|
147 | React.useEffect(() => {
|
148 |
|
149 | if (!open || !rootRef.current) {
|
150 | return;
|
151 | }
|
152 | const doc = ownerDocument(rootRef.current);
|
153 | const contain = nativeEvent => {
|
154 | const {
|
155 | current: rootElement
|
156 | } = rootRef;
|
157 |
|
158 |
|
159 |
|
160 | if (rootElement === null) {
|
161 | return;
|
162 | }
|
163 | if (!doc.hasFocus() || disableEnforceFocus || !isEnabled() || ignoreNextEnforceFocus.current) {
|
164 | ignoreNextEnforceFocus.current = false;
|
165 | return;
|
166 | }
|
167 | if (!rootElement.contains(doc.activeElement)) {
|
168 |
|
169 | if (nativeEvent && reactFocusEventTarget.current !== nativeEvent.target || doc.activeElement !== reactFocusEventTarget.current) {
|
170 | reactFocusEventTarget.current = null;
|
171 | } else if (reactFocusEventTarget.current !== null) {
|
172 | return;
|
173 | }
|
174 | if (!activated.current) {
|
175 | return;
|
176 | }
|
177 | let tabbable = [];
|
178 | if (doc.activeElement === sentinelStart.current || doc.activeElement === sentinelEnd.current) {
|
179 | tabbable = getTabbable(rootRef.current);
|
180 | }
|
181 | if (tabbable.length > 0) {
|
182 | const isShiftTab = Boolean(lastKeydown.current?.shiftKey && lastKeydown.current?.key === 'Tab');
|
183 | const focusNext = tabbable[0];
|
184 | const focusPrevious = tabbable[tabbable.length - 1];
|
185 | if (typeof focusNext !== 'string' && typeof focusPrevious !== 'string') {
|
186 | if (isShiftTab) {
|
187 | focusPrevious.focus();
|
188 | } else {
|
189 | focusNext.focus();
|
190 | }
|
191 | }
|
192 | } else {
|
193 | rootElement.focus();
|
194 | }
|
195 | }
|
196 | };
|
197 | const loopFocus = nativeEvent => {
|
198 | lastKeydown.current = nativeEvent;
|
199 | if (disableEnforceFocus || !isEnabled() || nativeEvent.key !== 'Tab') {
|
200 | return;
|
201 | }
|
202 |
|
203 |
|
204 |
|
205 | if (doc.activeElement === rootRef.current && nativeEvent.shiftKey) {
|
206 |
|
207 |
|
208 | ignoreNextEnforceFocus.current = true;
|
209 | if (sentinelEnd.current) {
|
210 | sentinelEnd.current.focus();
|
211 | }
|
212 | }
|
213 | };
|
214 | doc.addEventListener('focusin', contain);
|
215 | doc.addEventListener('keydown', loopFocus, true);
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | const interval = setInterval(() => {
|
224 | if (doc.activeElement && doc.activeElement.tagName === 'BODY') {
|
225 | contain(null);
|
226 | }
|
227 | }, 50);
|
228 | return () => {
|
229 | clearInterval(interval);
|
230 | doc.removeEventListener('focusin', contain);
|
231 | doc.removeEventListener('keydown', loopFocus, true);
|
232 | };
|
233 | }, [disableAutoFocus, disableEnforceFocus, disableRestoreFocus, isEnabled, open, getTabbable]);
|
234 | const onFocus = event => {
|
235 | if (nodeToRestore.current === null) {
|
236 | nodeToRestore.current = event.relatedTarget;
|
237 | }
|
238 | activated.current = true;
|
239 | reactFocusEventTarget.current = event.target;
|
240 | const childrenPropsHandler = children.props.onFocus;
|
241 | if (childrenPropsHandler) {
|
242 | childrenPropsHandler(event);
|
243 | }
|
244 | };
|
245 | const handleFocusSentinel = event => {
|
246 | if (nodeToRestore.current === null) {
|
247 | nodeToRestore.current = event.relatedTarget;
|
248 | }
|
249 | activated.current = true;
|
250 | };
|
251 | return _jsxs(React.Fragment, {
|
252 | children: [_jsx("div", {
|
253 | tabIndex: open ? 0 : -1,
|
254 | onFocus: handleFocusSentinel,
|
255 | ref: sentinelStart,
|
256 | "data-testid": "sentinelStart"
|
257 | }), React.cloneElement(children, {
|
258 | ref: handleRef,
|
259 | onFocus
|
260 | }), _jsx("div", {
|
261 | tabIndex: open ? 0 : -1,
|
262 | onFocus: handleFocusSentinel,
|
263 | ref: sentinelEnd,
|
264 | "data-testid": "sentinelEnd"
|
265 | })]
|
266 | });
|
267 | }
|
268 | process.env.NODE_ENV !== "production" ? FocusTrap.propTypes = {
|
269 |
|
270 |
|
271 |
|
272 |
|
273 | |
274 |
|
275 |
|
276 | children: elementAcceptingRef,
|
277 | |
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | disableAutoFocus: PropTypes.bool,
|
287 | |
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 | disableEnforceFocus: PropTypes.bool,
|
295 | |
296 |
|
297 |
|
298 |
|
299 |
|
300 | disableRestoreFocus: PropTypes.bool,
|
301 | |
302 |
|
303 |
|
304 |
|
305 |
|
306 | getTabbable: PropTypes.func,
|
307 | |
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 | isEnabled: PropTypes.func,
|
317 | |
318 |
|
319 |
|
320 | open: PropTypes.bool.isRequired
|
321 | } : void 0;
|
322 | if (process.env.NODE_ENV !== 'production') {
|
323 |
|
324 | FocusTrap['propTypes' + ''] = exactProp(FocusTrap.propTypes);
|
325 | }
|
326 | export default FocusTrap; |
\ | No newline at end of file |