1 |
|
2 | import * as React from 'react';
|
3 | import * as ReactDOM from 'react-dom';
|
4 | import PropTypes from 'prop-types';
|
5 | import ownerDocument from '../utils/ownerDocument';
|
6 | import useForkRef from '../utils/useForkRef';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function TrapFocus(props) {
|
12 | var children = props.children,
|
13 | _props$disableAutoFoc = props.disableAutoFocus,
|
14 | disableAutoFocus = _props$disableAutoFoc === void 0 ? false : _props$disableAutoFoc,
|
15 | _props$disableEnforce = props.disableEnforceFocus,
|
16 | disableEnforceFocus = _props$disableEnforce === void 0 ? false : _props$disableEnforce,
|
17 | _props$disableRestore = props.disableRestoreFocus,
|
18 | disableRestoreFocus = _props$disableRestore === void 0 ? false : _props$disableRestore,
|
19 | getDoc = props.getDoc,
|
20 | isEnabled = props.isEnabled,
|
21 | open = props.open;
|
22 | var ignoreNextEnforceFocus = React.useRef();
|
23 | var sentinelStart = React.useRef(null);
|
24 | var sentinelEnd = React.useRef(null);
|
25 | var nodeToRestore = React.useRef();
|
26 | var rootRef = React.useRef(null);
|
27 |
|
28 | var handleOwnRef = React.useCallback(function (instance) {
|
29 |
|
30 | rootRef.current = ReactDOM.findDOMNode(instance);
|
31 | }, []);
|
32 | var handleRef = useForkRef(children.ref, handleOwnRef);
|
33 |
|
34 |
|
35 | React.useMemo(function () {
|
36 | if (!open || typeof window === 'undefined') {
|
37 | return;
|
38 | }
|
39 |
|
40 | nodeToRestore.current = getDoc().activeElement;
|
41 | }, [open]);
|
42 |
|
43 | React.useEffect(function () {
|
44 | if (!open) {
|
45 | return;
|
46 | }
|
47 |
|
48 | var doc = ownerDocument(rootRef.current);
|
49 |
|
50 | if (!disableAutoFocus && rootRef.current && !rootRef.current.contains(doc.activeElement)) {
|
51 | if (!rootRef.current.hasAttribute('tabIndex')) {
|
52 | if (process.env.NODE_ENV !== 'production') {
|
53 | console.error(['Material-UI: 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'));
|
54 | }
|
55 |
|
56 | rootRef.current.setAttribute('tabIndex', -1);
|
57 | }
|
58 |
|
59 | rootRef.current.focus();
|
60 | }
|
61 |
|
62 | var contain = function contain() {
|
63 | if (disableEnforceFocus || !isEnabled() || ignoreNextEnforceFocus.current) {
|
64 | ignoreNextEnforceFocus.current = false;
|
65 | return;
|
66 | }
|
67 |
|
68 | if (rootRef.current && !rootRef.current.contains(doc.activeElement)) {
|
69 | rootRef.current.focus();
|
70 | }
|
71 | };
|
72 |
|
73 | var loopFocus = function loopFocus(event) {
|
74 |
|
75 | if (disableEnforceFocus || !isEnabled() || event.keyCode !== 9) {
|
76 | return;
|
77 | }
|
78 |
|
79 |
|
80 | if (doc.activeElement === rootRef.current) {
|
81 |
|
82 |
|
83 | ignoreNextEnforceFocus.current = true;
|
84 |
|
85 | if (event.shiftKey) {
|
86 | sentinelEnd.current.focus();
|
87 | } else {
|
88 | sentinelStart.current.focus();
|
89 | }
|
90 | }
|
91 | };
|
92 |
|
93 | doc.addEventListener('focus', contain, true);
|
94 | doc.addEventListener('keydown', loopFocus, true);
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | var interval = setInterval(function () {
|
101 | contain();
|
102 | }, 50);
|
103 | return function () {
|
104 | clearInterval(interval);
|
105 | doc.removeEventListener('focus', contain, true);
|
106 | doc.removeEventListener('keydown', loopFocus, true);
|
107 |
|
108 | if (!disableRestoreFocus) {
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | if (nodeToRestore.current && nodeToRestore.current.focus) {
|
114 | nodeToRestore.current.focus();
|
115 | }
|
116 |
|
117 | nodeToRestore.current = null;
|
118 | }
|
119 | };
|
120 | }, [disableAutoFocus, disableEnforceFocus, disableRestoreFocus, isEnabled, open]);
|
121 | return React.createElement(React.Fragment, null, React.createElement("div", {
|
122 | tabIndex: 0,
|
123 | ref: sentinelStart,
|
124 | "data-test": "sentinelStart"
|
125 | }), React.cloneElement(children, {
|
126 | ref: handleRef
|
127 | }), React.createElement("div", {
|
128 | tabIndex: 0,
|
129 | ref: sentinelEnd,
|
130 | "data-test": "sentinelEnd"
|
131 | }));
|
132 | }
|
133 |
|
134 | process.env.NODE_ENV !== "production" ? TrapFocus.propTypes = {
|
135 | |
136 |
|
137 |
|
138 | children: PropTypes.element.isRequired,
|
139 |
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | disableAutoFocus: PropTypes.bool,
|
149 |
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | disableEnforceFocus: PropTypes.bool,
|
157 |
|
158 | |
159 |
|
160 |
|
161 |
|
162 | disableRestoreFocus: PropTypes.bool,
|
163 |
|
164 | |
165 |
|
166 |
|
167 |
|
168 | getDoc: PropTypes.func.isRequired,
|
169 |
|
170 | |
171 |
|
172 |
|
173 |
|
174 | isEnabled: PropTypes.func.isRequired,
|
175 |
|
176 | |
177 |
|
178 |
|
179 | open: PropTypes.bool.isRequired
|
180 | } : void 0;
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | export default TrapFocus; |
\ | No newline at end of file |