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