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