-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathLock.js
220 lines (186 loc) Β· 5.77 KB
/
Lock.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
209
210
211
212
213
214
215
216
217
218
219
220
import * as React from 'react';
import {
node, bool, string, any, arrayOf, oneOfType, object, func,
} from 'prop-types';
import * as constants from 'focus-lock/constants';
import {useMergeRefs} from 'use-callback-ref';
import {hiddenGuard} from './FocusGuard';
import {mediumFocus, mediumBlur, mediumSidecar} from './medium';
import {useEffect} from "react";
const emptyArray = [];
const FocusLock = React.forwardRef(function FocusLockUI(props, parentRef) {
const [realObserved, setObserved] = React.useState();
const observed = React.useRef();
const isActive = React.useRef(false);
const originalFocusedElement = React.useRef(null);
const {
children,
disabled,
noFocusGuards,
persistentFocus,
crossFrame,
autoFocus,
allowTextSelection,
group,
className,
whiteList,
shards = emptyArray,
as: Container = 'div',
lockProps: containerProps = {},
sideCar: SideCar,
returnFocus: shouldReturnFocus,
onActivation: onActivationCallback,
onDeactivation: onDeactivationCallback,
} = props;
const [id] = React.useState({});
// SIDE EFFECT CALLBACKS
const onActivation = React.useCallback(() => {
originalFocusedElement.current = (
originalFocusedElement.current || (document && document.activeElement)
);
if (observed.current && onActivationCallback) {
onActivationCallback(observed.current);
}
isActive.current = true;
}, [onActivationCallback]);
const onDeactivation = React.useCallback(() => {
isActive.current = false;
if (onDeactivationCallback) {
onDeactivationCallback(observed.current);
}
}, [onDeactivationCallback]);
useEffect(() => {
if(!disabled) {
// cleanup return focus on trap deactivation
// sideEffect/returnFocus should happen by this time
originalFocusedElement.current = null;
}
}, []);
const returnFocus = React.useCallback((allowDefer) => {
const {current: returnFocusTo} = originalFocusedElement;
if (returnFocusTo && returnFocusTo.focus) {
const howToReturnFocus = typeof shouldReturnFocus === 'function' ? shouldReturnFocus(returnFocusTo) : shouldReturnFocus;
if (Boolean(howToReturnFocus)) {
const focusOptions = typeof howToReturnFocus === 'object' ? howToReturnFocus : undefined;
originalFocusedElement.current = null;
if (allowDefer) {
// React might return focus after update
// it's safer to defer the action
Promise.resolve().then(() => returnFocusTo.focus(focusOptions));
} else {
returnFocusTo.focus(focusOptions);
}
}
}
}, [shouldReturnFocus]);
// MEDIUM CALLBACKS
const onFocus = React.useCallback((event) => {
if (isActive.current) {
mediumFocus.useMedium(event);
}
}, []);
const onBlur = mediumBlur.useMedium;
// REF PROPAGATION
// not using real refs due to race conditions
const setObserveNode = React.useCallback((newObserved) => {
if (observed.current !== newObserved) {
observed.current = newObserved;
setObserved(newObserved);
}
}, []);
if (process.env.NODE_ENV !== 'production') {
if (typeof allowTextSelection !== 'undefined') {
// eslint-disable-next-line no-console
console.warn('React-Focus-Lock: allowTextSelection is deprecated and enabled by default');
}
React.useEffect(() => {
if (!observed.current) {
// eslint-disable-next-line no-console
console.error('FocusLock: could not obtain ref to internal node');
}
}, []);
}
const lockProps = {
[constants.FOCUS_DISABLED]: disabled && 'disabled',
[constants.FOCUS_GROUP]: group,
...containerProps,
};
const hasLeadingGuards = noFocusGuards !== true;
const hasTailingGuards = hasLeadingGuards && (noFocusGuards !== 'tail');
const mergedRef = useMergeRefs([parentRef, setObserveNode]);
return (
<React.Fragment>
{hasLeadingGuards && [
<div key="guard-first" data-focus-guard tabIndex={disabled ? -1 : 0} style={hiddenGuard}/>, // nearest focus guard
<div key="guard-nearest" data-focus-guard tabIndex={disabled ? -1 : 1} style={hiddenGuard}/>, // first tabbed element guard
]}
{!disabled && (
<SideCar
id={id}
sideCar={mediumSidecar}
observed={realObserved}
disabled={disabled}
persistentFocus={persistentFocus}
crossFrame={crossFrame}
autoFocus={autoFocus}
whiteList={whiteList}
shards={shards}
onActivation={onActivation}
onDeactivation={onDeactivation}
returnFocus={returnFocus}
/>
)}
<Container
ref={mergedRef}
{...lockProps}
className={className}
onBlur={onBlur}
onFocus={onFocus}
>
{children}
</Container>
{
hasTailingGuards
&& <div data-focus-guard tabIndex={disabled ? -1 : 0} style={hiddenGuard}/>
}
</React.Fragment>
);
});
FocusLock.propTypes = {
children: node,
disabled: bool,
returnFocus: oneOfType([bool, object]),
noFocusGuards: bool,
allowTextSelection: bool,
autoFocus: bool,
persistentFocus: bool,
crossFrame: bool,
group: string,
className: string,
whiteList: func,
shards: arrayOf(any),
as: oneOfType([string, func, object]),
lockProps: object,
onActivation: func,
onDeactivation: func,
sideCar: any.isRequired,
};
FocusLock.defaultProps = {
children: undefined,
disabled: false,
returnFocus: false,
noFocusGuards: false,
autoFocus: true,
persistentFocus: false,
crossFrame: true,
allowTextSelection: undefined,
group: undefined,
className: undefined,
whiteList: undefined,
shards: undefined,
as: 'div',
lockProps: {},
onActivation: undefined,
onDeactivation: undefined,
};
export default FocusLock;