-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathLock.js
228 lines (193 loc) Β· 6.23 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
221
222
223
224
225
226
227
228
import React, {
forwardRef, useRef, useState, useCallback, useEffect, useMemo, Fragment,
} from 'react';
import {
node, bool, string, any, arrayOf, oneOfType, object, func,
} from 'prop-types';
import { FOCUS_DISABLED, FOCUS_GROUP } from 'focus-lock/constants';
import { useMergeRefs } from 'use-callback-ref';
import { hiddenGuard } from './FocusGuard';
import {
mediumFocus, mediumBlur, mediumSidecar,
} from './medium';
import { focusScope } from './scope';
const emptyArray = [];
const FocusLock = forwardRef(function FocusLockUI(props, parentRef) {
const [realObserved, setObserved] = useState();
const observed = useRef();
const isActive = useRef(false);
const originalFocusedElement = useRef(null);
const [, update] = useState({});
const {
children,
disabled = false,
noFocusGuards = false,
persistentFocus = false,
crossFrame = true,
autoFocus = true,
allowTextSelection,
group,
className,
whiteList,
hasPositiveIndices,
shards = emptyArray,
as: Container = 'div',
lockProps: containerProps = {},
sideCar: SideCar,
returnFocus: shouldReturnFocus = false,
focusOptions,
onActivation: onActivationCallback,
onDeactivation: onDeactivationCallback,
} = props;
const [id] = useState({});
// SIDE EFFECT CALLBACKS
const onActivation = useCallback(({ captureFocusRestore }) => {
if (!originalFocusedElement.current) {
const activeElement = document?.activeElement;
originalFocusedElement.current = activeElement;
// store stack reference
if (activeElement !== document.body) {
originalFocusedElement.current = captureFocusRestore(activeElement);
}
}
if (observed.current && onActivationCallback) {
onActivationCallback(observed.current);
}
isActive.current = true;
update();
}, [onActivationCallback]);
const onDeactivation = useCallback(() => {
isActive.current = false;
if (onDeactivationCallback) {
onDeactivationCallback(observed.current);
}
update();
}, [onDeactivationCallback]);
const returnFocus = useCallback((allowDefer) => {
const { current: focusRestore } = originalFocusedElement;
if (focusRestore) {
const returnFocusTo = (typeof focusRestore === 'function' ? focusRestore() : focusRestore) || document.body;
const howToReturnFocus = typeof shouldReturnFocus === 'function' ? shouldReturnFocus(returnFocusTo) : shouldReturnFocus;
if (howToReturnFocus) {
const returnFocusOptions = 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(returnFocusOptions));
} else {
returnFocusTo.focus(returnFocusOptions);
}
}
}
}, [shouldReturnFocus]);
// MEDIUM CALLBACKS
const onFocus = useCallback((event) => {
if (isActive.current) {
mediumFocus.useMedium(event);
}
}, []);
const onBlur = mediumBlur.useMedium;
// REF PROPAGATION
// not using real refs due to race conditions
const setObserveNode = 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');
}
useEffect(() => {
// report incorrect integration - https://github.com/theKashey/react-focus-lock/issues/123
if (!observed.current && typeof Container !== 'string') {
// eslint-disable-next-line no-console
console.error('FocusLock: could not obtain ref to internal node');
}
}, []);
}
const lockProps = {
[FOCUS_DISABLED]: disabled && 'disabled',
[FOCUS_GROUP]: group,
...containerProps,
};
const hasLeadingGuards = noFocusGuards !== true;
const hasTailingGuards = hasLeadingGuards && (noFocusGuards !== 'tail');
const mergedRef = useMergeRefs([parentRef, setObserveNode]);
const focusScopeValue = useMemo(() => ({
observed,
shards,
enabled: !disabled,
active: isActive.current,
}), [disabled, isActive.current, shards, realObserved]);
return (
<Fragment>
{hasLeadingGuards && [
// nearest focus guard
<div key="guard-first" data-focus-guard tabIndex={disabled ? -1 : 0} style={hiddenGuard} />,
// first tabbed element guard
hasPositiveIndices
? <div key="guard-nearest" data-focus-guard tabIndex={disabled ? -1 : 1} style={hiddenGuard} />
: null,
]}
{!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}
focusOptions={focusOptions}
noFocusGuards={noFocusGuards}
/>
)}
<Container
ref={mergedRef}
{...lockProps}
className={className}
onBlur={onBlur}
onFocus={onFocus}
>
<focusScope.Provider value={focusScopeValue}>
{children}
</focusScope.Provider>
</Container>
{
hasTailingGuards
&& <div data-focus-guard tabIndex={disabled ? -1 : 0} style={hiddenGuard} />
}
</Fragment>
);
});
FocusLock.propTypes = {
children: node,
disabled: bool,
returnFocus: oneOfType([bool, object, func]),
focusOptions: object,
noFocusGuards: bool,
hasPositiveIndices: 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,
};
export default FocusLock;