-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unstable_TrapFocus.js
208 lines (178 loc) · 7.2 KB
/
Unstable_TrapFocus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
/* eslint-disable consistent-return, jsx-a11y/no-noninteractive-tabindex, camelcase */
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import ownerDocument from '../utils/ownerDocument';
import useForkRef from '../utils/useForkRef';
import { exactProp } from '@material-ui/utils';
/**
* Utility component that locks focus inside the component.
*/
function Unstable_TrapFocus(props) {
const {
children,
disableAutoFocus = false,
disableEnforceFocus = false,
disableRestoreFocus = false,
getDoc,
isEnabled,
open
} = props;
const ignoreNextEnforceFocus = React.useRef();
const sentinelStart = React.useRef(null);
const sentinelEnd = React.useRef(null);
const nodeToRestore = React.useRef();
const rootRef = React.useRef(null); // can be removed once we drop support for non ref forwarding class components
const handleOwnRef = React.useCallback(instance => {
// #StrictMode ready
rootRef.current = ReactDOM.findDOMNode(instance);
}, []);
const handleRef = useForkRef(children.ref, handleOwnRef);
const prevOpenRef = React.useRef();
React.useEffect(() => {
prevOpenRef.current = open;
}, [open]);
if (!prevOpenRef.current && open && typeof window !== 'undefined') {
// WARNING: Potentially unsafe in concurrent mode.
// The way the read on `nodeToRestore` is setup could make this actually safe.
// Say we render `open={false}` -> `open={true}` but never commit.
// We have now written a state that wasn't committed. But no committed effect
// will read this wrong value. We only read from `nodeToRestore` in effects
// that were committed on `open={true}`
// WARNING: Prevents the instance from being garbage collected. Should only
// hold a weak ref.
nodeToRestore.current = getDoc().activeElement;
}
React.useEffect(() => {
if (!open) {
return;
}
const doc = ownerDocument(rootRef.current); // We might render an empty child.
if (!disableAutoFocus && rootRef.current && !rootRef.current.contains(doc.activeElement)) {
if (!rootRef.current.hasAttribute('tabIndex')) {
if (process.env.NODE_ENV !== 'production') {
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'));
}
rootRef.current.setAttribute('tabIndex', -1);
}
rootRef.current.focus();
}
const contain = () => {
const {
current: rootElement
} = rootRef; // Cleanup functions are executed lazily in React 17.
// Contain can be called between the component being unmounted and its cleanup function being run.
if (rootElement === null) {
return;
}
if (!doc.hasFocus() || disableEnforceFocus || !isEnabled() || ignoreNextEnforceFocus.current) {
ignoreNextEnforceFocus.current = false;
return;
}
if (rootRef.current && !rootRef.current.contains(doc.activeElement)) {
rootRef.current.focus();
}
};
const loopFocus = event => {
// 9 = Tab
if (disableEnforceFocus || !isEnabled() || event.keyCode !== 9) {
return;
} // Make sure the next tab starts from the right place.
if (doc.activeElement === rootRef.current) {
// We need to ignore the next contain as
// it will try to move the focus back to the rootRef element.
ignoreNextEnforceFocus.current = true;
if (event.shiftKey) {
sentinelEnd.current.focus();
} else {
sentinelStart.current.focus();
}
}
};
doc.addEventListener('focus', contain, true);
doc.addEventListener('keydown', loopFocus, true); // With Edge, Safari and Firefox, no focus related events are fired when the focused area stops being a focused area
// e.g. https://bugzilla.mozilla.org/show_bug.cgi?id=559561.
//
// The whatwg spec defines how the browser should behave but does not explicitly mention any events:
// https://html.spec.whatwg.org/multipage/interaction.html#focus-fixup-rule.
const interval = setInterval(() => {
contain();
}, 50);
return () => {
clearInterval(interval);
doc.removeEventListener('focus', contain, true);
doc.removeEventListener('keydown', loopFocus, true); // restoreLastFocus()
if (!disableRestoreFocus) {
// In IE 11 it is possible for document.activeElement to be null resulting
// in nodeToRestore.current being null.
// Not all elements in IE 11 have a focus method.
// Once IE 11 support is dropped the focus() call can be unconditional.
if (nodeToRestore.current && nodeToRestore.current.focus) {
nodeToRestore.current.focus();
}
nodeToRestore.current = null;
}
};
}, [disableAutoFocus, disableEnforceFocus, disableRestoreFocus, isEnabled, open]);
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
tabIndex: 0,
ref: sentinelStart,
"data-test": "sentinelStart"
}), /*#__PURE__*/React.cloneElement(children, {
ref: handleRef
}), /*#__PURE__*/React.createElement("div", {
tabIndex: 0,
ref: sentinelEnd,
"data-test": "sentinelEnd"
}));
}
process.env.NODE_ENV !== "production" ? Unstable_TrapFocus.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* A single child content element.
*/
children: PropTypes.node,
/**
* If `true`, the trap focus will not automatically shift focus to itself when it opens, and
* replace it to the last focused element when it closes.
* This also works correctly with any trap focus children that have the `disableAutoFocus` prop.
*
* Generally this should never be set to `true` as it makes the trap focus less
* accessible to assistive technologies, like screen readers.
*/
disableAutoFocus: PropTypes.bool,
/**
* If `true`, the trap focus will not prevent focus from leaving the trap focus while open.
*
* Generally this should never be set to `true` as it makes the trap focus less
* accessible to assistive technologies, like screen readers.
*/
disableEnforceFocus: PropTypes.bool,
/**
* If `true`, the trap focus will not restore focus to previously focused element once
* trap focus is hidden.
*/
disableRestoreFocus: PropTypes.bool,
/**
* Return the document to consider.
* We use it to implement the restore focus between different browser documents.
*/
getDoc: PropTypes.func.isRequired,
/**
* Do we still want to enforce the focus?
* This prop helps nesting TrapFocus elements.
*/
isEnabled: PropTypes.func.isRequired,
/**
* If `true`, focus will be locked.
*/
open: PropTypes.bool.isRequired
} : void 0;
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line
Unstable_TrapFocus['propTypes' + ''] = exactProp(Unstable_TrapFocus.propTypes);
}
export default Unstable_TrapFocus;