-
Notifications
You must be signed in to change notification settings - Fork 67
/
Lock.js
176 lines (150 loc) Β· 4.31 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
import React, { useState, useRef, useCallback } from 'react';
import {
node, bool, string, any, arrayOf, oneOfType, object, func,
} from 'prop-types';
import * as constants from 'focus-lock/constants';
import { hiddenGuard } from './FocusGuard';
import { mediumFocus, mediumBlur, mediumSidecar } from './medium';
const emptyArray = [];
function FocusLock(props) {
const [realObserved, setObserved] = useState();
const observed = useRef();
const isActive = useRef(false);
const originalFocusedElement = useRef(null);
// SIDE EFFECT CALLBACKS
const onActivation = useCallback(() => {
originalFocusedElement.current = (
originalFocusedElement.current || (document && document.activeElement)
);
if (observed.current && props.onActivation) {
props.onActivation(observed.current);
}
isActive.current = true;
}, []);
const onDeactivation = useCallback(() => {
isActive.current = false;
if (props.onDeactivation) {
props.onDeactivation(observed.current);
}
}, []);
const returnFocus = useCallback(() => {
const { current } = originalFocusedElement;
if (props.returnFocus && current && current.focus) {
current.focus();
originalFocusedElement.current = null;
}
}, []);
// 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 = realObserved;
setObserved(newObserved);
}
}, []);
const {
children,
disabled,
noFocusGuards,
persistentFocus,
autoFocus,
allowTextSelection,
group,
className,
whiteList,
shards = emptyArray,
as: Container = 'div',
lockProps: containerProps = {},
sideCar: SideCar,
} = props;
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');
}
}
const lockProps = {
[constants.FOCUS_DISABLED]: disabled && 'disabled',
[constants.FOCUS_GROUP]: group,
...containerProps,
};
const hasLeadingGuards = noFocusGuards !== true;
const hasTailingGuards = hasLeadingGuards && (noFocusGuards !== 'tail');
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
]}
<Container
ref={setObserveNode}
{...lockProps}
className={className}
onBlur={onBlur}
onFocus={onFocus}
>
{!disabled && (
<SideCar
sideCar={mediumSidecar}
observed={realObserved}
disabled={disabled}
persistentFocus={persistentFocus}
autoFocus={autoFocus}
whiteList={whiteList}
shards={shards}
onActivation={onActivation}
onDeactivation={onDeactivation}
returnFocus={returnFocus}
/>
)}
{children}
</Container>
{
hasTailingGuards
&& <div data-focus-guard tabIndex={disabled ? -1 : 0} style={hiddenGuard} />
}
</React.Fragment>
);
}
FocusLock.propTypes = {
children: node.isRequired,
disabled: bool,
returnFocus: bool,
noFocusGuards: bool,
allowTextSelection: bool,
autoFocus: bool,
persistentFocus: 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 = {
disabled: false,
returnFocus: false,
noFocusGuards: false,
autoFocus: true,
persistentFocus: false,
allowTextSelection: undefined,
group: undefined,
className: undefined,
whiteList: undefined,
shards: undefined,
as: 'div',
lockProps: {},
onActivation: undefined,
onDeactivation: undefined,
};
export default FocusLock;