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