-
Notifications
You must be signed in to change notification settings - Fork 99
/
useFocusWithin.ts
212 lines (184 loc) · 6.22 KB
/
useFocusWithin.ts
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
import React from 'react';
import {SyntheticFocusEvent} from './SyntheticFocusEvent';
import {useSyntheticBlurEvent} from './useSyntheticBlurEvent';
export interface FocusWithinProps {
/** Whether the focus within events should be disabled. */
isDisabled?: boolean;
/** Handler that is called when the target element or a descendant receives focus. */
onFocusWithin?: (e: React.FocusEvent) => void;
/** Handler that is called when the target element and all descendants lose focus. */
onBlurWithin?: (e: React.FocusEvent) => void;
/** Handler that is called when the the focus within state changes. */
onFocusWithinChange?: (isFocusWithin: boolean) => void;
}
export interface UseFocusWithinProps extends FocusWithinProps {}
export interface UseFocusWithinResult {
focusWithinProps: {
onFocus?: (event: React.FocusEvent<Element, Element>) => void;
onBlur?: (event: React.FocusEvent<Element, Element>) => void;
};
}
/**
* Callback on focus outside event.
*
* @callback onFocusEventCallback
* @param {FocusEvent} event
*/
/**
* Callback on focus change event.
*
* @callback onFocusChangeCallback
* @param {boolean} isFocusWithin
*/
/**
* Handles focus events for the target and its descendants.
*
* @param {Object} props
* @param {boolean} [props.isDisabled=false] - whether the focus within events should be disabled.
* @param {onFocusEventCallback} props.onFocusWithin - handler that is called when the target element or a descendant receives focus.
* @param {onFocusEventCallback} props.onBlurWithin - handler that is called when the target element and all descendants lose focus.
* @param {onFocusChangeCallback} props.onFocusChange - handler that is called when the the focus within state changes.
*
* @returns container props
*
* @example
*
* function Select() {
* const [open, setOpen] = React.useState(false);
*
* const handleFocusOutside = React.useCallback(() => {setOpen(false);}, []);
*
* const {focusWithinProps} = useFocusWithin({onBlurWithin: handleFocusOutside});
*
* return (
* <span {...focusWithinProps}>
* <Button onClick={() => {setOpen(true);}}>Select</Button>
* <Popup open={open}>
* ...
* </Popup>
* </span>
* );
* }
* }
*/
export function useFocusWithin(props: UseFocusWithinProps) {
const {onFocusWithin, onBlurWithin, onFocusWithinChange, isDisabled} = props;
const isFocusWithinRef = React.useRef(false);
const onFocus = React.useCallback(
(event: React.FocusEvent) => {
if (!isFocusWithinRef.current && document.activeElement === event.target) {
isFocusWithinRef.current = true;
if (onFocusWithin) {
onFocusWithin(event);
}
if (onFocusWithinChange) {
onFocusWithinChange(true);
}
}
},
[onFocusWithin, onFocusWithinChange],
);
const onBlur = React.useCallback(
(event: React.FocusEvent) => {
if (!isFocusWithinRef.current) {
return;
}
isFocusWithinRef.current = false;
if (onBlurWithin) {
onBlurWithin(event);
}
if (onFocusWithinChange) {
onFocusWithinChange(false);
}
},
[onBlurWithin, onFocusWithinChange],
);
const {onBlur: onBlurHandler, onFocus: onFocusHandler} = useFocusEvents({
onFocus,
onBlur,
isDisabled,
});
if (isDisabled) {
return {
focusWithinProps: {
onFocus: undefined,
onBlur: undefined,
},
};
}
return {
focusWithinProps: {
onFocus: onFocusHandler,
onBlur: onBlurHandler,
},
};
}
function useFocusEvents({
onFocus,
onBlur,
isDisabled,
}: {
onFocus: (event: React.FocusEvent) => void;
onBlur: (event: React.FocusEvent) => void;
isDisabled?: boolean;
}) {
const capturedRef = React.useRef(false);
const targetRef = React.useRef<EventTarget | null>(null);
React.useEffect(() => {
if (isDisabled) {
return undefined;
}
const handleFocus = function () {
capturedRef.current = false;
};
const handleFocusIn = function (event: FocusEvent) {
if (!capturedRef.current && targetRef.current) {
const blurEvent = new FocusEvent('blur', {
...event,
relatedTarget: event.target,
bubbles: false,
cancelable: false,
});
onBlur(
new SyntheticFocusEvent('blur', blurEvent, {
target: targetRef.current,
currentTarget: targetRef.current,
}),
);
targetRef.current = null;
}
};
window.addEventListener('focus', handleFocus, {capture: true});
// use focusin because a focus event does not bubble and current browser
// implementations fire focusin events after fucus event
window.addEventListener('focusin', handleFocusIn);
return () => {
window.removeEventListener('focus', handleFocus, {capture: true});
window.removeEventListener('focusin', handleFocusIn);
};
}, [isDisabled, onBlur]);
const onBlurHandler = React.useCallback(
(event: React.FocusEvent) => {
if (
event.relatedTarget === null ||
event.relatedTarget === document.body ||
event.relatedTarget === (document as EventTarget)
) {
onBlur(event);
targetRef.current = null;
}
},
[onBlur],
);
const onSyntheticFocus = useSyntheticBlurEvent(onBlur);
const onFocusHandler = React.useCallback(
(event: React.FocusEvent) => {
capturedRef.current = true;
targetRef.current = event.target;
onSyntheticFocus(event);
onFocus(event);
},
[onSyntheticFocus, onFocus],
);
return {onBlur: onBlurHandler, onFocus: onFocusHandler};
}