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