-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathuseFocusable.ts
More file actions
235 lines (209 loc) · 5.85 KB
/
useFocusable.ts
File metadata and controls
235 lines (209 loc) · 5.85 KB
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
229
230
231
232
233
234
235
import {
RefObject,
useCallback,
useMemo,
useRef,
useEffect,
useState
} from 'react';
import noop from 'lodash/noop';
import uniqueId from 'lodash/uniqueId';
import {
SpatialNavigation,
FocusableComponentLayout,
FocusDetails,
KeyPressDetails,
Direction
} from './SpatialNavigation';
import { useFocusContext } from './useFocusContext';
export type EnterPressHandler<P = object> = (
props: P,
details: KeyPressDetails
) => void;
export type EnterReleaseHandler<P = object> = (props: P) => void;
export type ArrowPressHandler<P = object> = (
direction: string,
props: P,
details: KeyPressDetails
) => boolean;
export type ArrowReleaseHandler<P = object> = (
direction: string,
props: P,
) => void;
export type FocusHandler<P = object> = (
layout: FocusableComponentLayout,
props: P,
details: FocusDetails
) => void;
export type BlurHandler<P = object> = (
layout: FocusableComponentLayout,
props: P,
details: FocusDetails
) => void;
export interface UseFocusableConfig<P = object> {
focusable?: boolean;
saveLastFocusedChild?: boolean;
trackChildren?: boolean;
autoRestoreFocus?: boolean;
forceFocus?: boolean;
isFocusBoundary?: boolean;
focusBoundaryDirections?: Direction[];
focusKey?: string;
preferredChildFocusKey?: string;
onEnterPress?: EnterPressHandler<P>;
onEnterRelease?: EnterReleaseHandler<P>;
onArrowPress?: ArrowPressHandler<P>;
onArrowRelease?: ArrowReleaseHandler<P>;
onFocus?: FocusHandler<P>;
onBlur?: BlurHandler<P>;
extraProps?: P;
/**
* Accessibility label for this focusable component.
* When focus lands on a leaf node, the labels of all newly-entered parent
* regions (in tree order) are concatenated with the leaf's own label and
* passed to the `onUtterText` callback provided during `init()`.
*/
accessibilityLabel?: string;
}
export interface UseFocusableResult<E = any> {
ref: RefObject<E>;
focusSelf: (focusDetails?: FocusDetails) => void;
focused: boolean;
hasFocusedChild: boolean;
focusKey: string;
}
const useFocusableHook = <P, E = any>({
focusable = true,
saveLastFocusedChild = true,
trackChildren = false,
autoRestoreFocus = true,
forceFocus = false,
isFocusBoundary = false,
focusBoundaryDirections,
focusKey: propFocusKey,
preferredChildFocusKey,
onEnterPress = noop,
onEnterRelease = noop,
onArrowPress = () => true,
onArrowRelease = noop,
onFocus = noop,
onBlur = noop,
extraProps,
accessibilityLabel
}: UseFocusableConfig<P> = {}): UseFocusableResult<E> => {
const onEnterPressHandler = useCallback(
(details: KeyPressDetails) => {
onEnterPress(extraProps, details);
},
[onEnterPress, extraProps]
);
const onEnterReleaseHandler = useCallback(() => {
onEnterRelease(extraProps);
}, [onEnterRelease, extraProps]);
const onArrowPressHandler = useCallback(
(direction: string, details: KeyPressDetails) =>
onArrowPress(direction, extraProps, details),
[extraProps, onArrowPress]
);
const onArrowReleaseHandler = useCallback((direction: string) => {
onArrowRelease(direction, extraProps);
}, [onArrowRelease, extraProps])
const onFocusHandler = useCallback(
(layout: FocusableComponentLayout, details: FocusDetails) => {
onFocus(layout, extraProps, details);
},
[extraProps, onFocus]
);
const onBlurHandler = useCallback(
(layout: FocusableComponentLayout, details: FocusDetails) => {
onBlur(layout, extraProps, details);
},
[extraProps, onBlur]
);
const ref = useRef<E>(null);
const [focused, setFocused] = useState(false);
const [hasFocusedChild, setHasFocusedChild] = useState(false);
const parentFocusKey = useFocusContext();
/**
* Either using the propFocusKey passed in, or generating a random one
*/
const focusKey = useMemo(
() => propFocusKey || uniqueId('sn:focusable-item-'),
[propFocusKey]
);
const focusSelf = useCallback(
(focusDetails: FocusDetails = {}) => {
SpatialNavigation.setFocus(focusKey, focusDetails);
},
[focusKey]
);
useEffect(() => {
const node: any = ref.current;
SpatialNavigation.addFocusable({
focusKey,
node,
parentFocusKey,
preferredChildFocusKey,
onEnterPress: onEnterPressHandler,
onEnterRelease: onEnterReleaseHandler,
onArrowPress: onArrowPressHandler,
onArrowRelease: onArrowReleaseHandler,
onFocus: onFocusHandler,
onBlur: onBlurHandler,
onUpdateFocus: (isFocused = false) => setFocused(isFocused),
onUpdateHasFocusedChild: (isFocused = false) =>
setHasFocusedChild(isFocused),
saveLastFocusedChild,
trackChildren,
isFocusBoundary,
focusBoundaryDirections,
autoRestoreFocus,
forceFocus,
focusable,
accessibilityLabel
});
return () => {
SpatialNavigation.removeFocusable({
focusKey
});
};
}, []); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
const node: any = ref.current;
SpatialNavigation.updateFocusable(focusKey, {
node,
preferredChildFocusKey,
focusable,
isFocusBoundary,
focusBoundaryDirections,
onEnterPress: onEnterPressHandler,
onEnterRelease: onEnterReleaseHandler,
onArrowPress: onArrowPressHandler,
onArrowRelease: onArrowReleaseHandler,
onFocus: onFocusHandler,
onBlur: onBlurHandler,
accessibilityLabel
});
}, [
focusKey,
preferredChildFocusKey,
focusable,
isFocusBoundary,
focusBoundaryDirections,
onEnterPressHandler,
onEnterReleaseHandler,
onArrowPressHandler,
onArrowReleaseHandler,
onFocusHandler,
onBlurHandler,
accessibilityLabel
]);
return {
ref,
focusSelf,
focused,
hasFocusedChild,
focusKey // returns either the same focusKey as passed in, or generated one
};
};
export const useFocusable = useFocusableHook;