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