-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
useArrowKeyFocusManager.ts
211 lines (178 loc) · 8.39 KB
/
useArrowKeyFocusManager.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
import {useCallback, useEffect, useMemo, useState} from 'react';
import CONST from '@src/CONST';
import useKeyboardShortcut from './useKeyboardShortcut';
type Config = {
maxIndex: number;
onFocusedIndexChange?: (index: number) => void;
initialFocusedIndex?: number;
disabledIndexes?: readonly number[];
shouldExcludeTextAreaNodes?: boolean;
isActive?: boolean;
itemsPerRow?: number;
disableCyclicTraversal?: boolean;
disableHorizontalKeys?: boolean;
allowNegativeIndexes?: boolean;
};
type UseArrowKeyFocusManager = [number, (index: number) => void];
/**
* A hook that makes it easy to use the arrow keys to manage focus of items in a list
*
* Recommendation: To ensure stability, wrap the `onFocusedIndexChange` function with the useCallback hook before using it with this hook.
*
* @param config.maxIndex – typically the number of items in your list
* @param [config.onFocusedIndexChange] – optional callback to execute when focusedIndex changes
* @param [config.initialFocusedIndex] – where to start in the list
* @param [config.disabledIndexes] – An array of indexes to disable + skip over
* @param [config.shouldExcludeTextAreaNodes] – Whether arrow keys should have any effect when a TextArea node is focused
* @param [config.isActive] – Whether the component is ready and should subscribe to KeyboardShortcut
* @param [config.itemsPerRow] – The number of items per row. If provided, the arrow keys will move focus horizontally as well as vertically
* @param [config.disableCyclicTraversal] – Whether to disable cyclic traversal of the list. If true, the arrow keys will have no effect when the first or last item is focused
* @param [config.disableHorizontalKeys] – Whether to disable the right/left keys
*/
export default function useArrowKeyFocusManager({
maxIndex,
onFocusedIndexChange = () => {},
initialFocusedIndex = 0,
// The "disabledIndexes" array needs to be stable to prevent the "useCallback" hook from being recreated unnecessarily.
// Hence the use of CONST.EMPTY_ARRAY.
disabledIndexes = CONST.EMPTY_ARRAY,
shouldExcludeTextAreaNodes = true,
isActive,
itemsPerRow,
disableCyclicTraversal = false,
disableHorizontalKeys = false,
allowNegativeIndexes = false,
}: Config): UseArrowKeyFocusManager {
const allowHorizontalArrowKeys = !!itemsPerRow;
const [focusedIndex, setFocusedIndex] = useState(initialFocusedIndex);
const arrowConfig = useMemo(
() => ({
excludedNodes: shouldExcludeTextAreaNodes ? ['TEXTAREA'] : [],
isActive,
}),
[isActive, shouldExcludeTextAreaNodes],
);
const horizontalArrowConfig = useMemo(
() => ({
excludedNodes: shouldExcludeTextAreaNodes ? ['TEXTAREA'] : [],
isActive: isActive && !disableHorizontalKeys,
}),
[isActive, shouldExcludeTextAreaNodes, disableHorizontalKeys],
);
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => onFocusedIndexChange(focusedIndex), [focusedIndex]);
const arrowUpCallback = useCallback(() => {
if (maxIndex < 0) {
return;
}
const nextIndex = disableCyclicTraversal ? -1 : maxIndex;
setFocusedIndex((actualIndex) => {
let currentFocusedIndex = -1;
if (allowHorizontalArrowKeys) {
currentFocusedIndex = actualIndex > 0 ? actualIndex - itemsPerRow : nextIndex;
} else {
currentFocusedIndex = actualIndex > 0 ? actualIndex - 1 : nextIndex;
}
let newFocusedIndex = currentFocusedIndex;
while (disabledIndexes.includes(newFocusedIndex)) {
newFocusedIndex -= allowHorizontalArrowKeys ? itemsPerRow : 1;
if (newFocusedIndex < 0) {
if (disableCyclicTraversal) {
if (!allowNegativeIndexes) {
return actualIndex;
}
break;
}
newFocusedIndex = maxIndex;
}
if (newFocusedIndex === currentFocusedIndex) {
// all indexes are disabled
return actualIndex; // no-op
}
}
return newFocusedIndex;
});
}, [allowHorizontalArrowKeys, disableCyclicTraversal, disabledIndexes, itemsPerRow, maxIndex, allowNegativeIndexes]);
useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ARROW_UP, arrowUpCallback, arrowConfig);
const arrowDownCallback = useCallback(() => {
if (maxIndex < 0) {
return;
}
const nextIndex = disableCyclicTraversal ? maxIndex : 0;
setFocusedIndex((actualIndex) => {
let currentFocusedIndex = -1;
if (actualIndex === -1) {
currentFocusedIndex = 0;
} else if (allowHorizontalArrowKeys) {
currentFocusedIndex = actualIndex < maxIndex ? actualIndex + itemsPerRow : nextIndex;
} else {
currentFocusedIndex = actualIndex < maxIndex ? actualIndex + 1 : nextIndex;
}
if (disableCyclicTraversal && currentFocusedIndex > maxIndex) {
return actualIndex;
}
let newFocusedIndex = currentFocusedIndex;
while (disabledIndexes.includes(newFocusedIndex)) {
if (actualIndex < 0) {
newFocusedIndex += 1;
} else {
newFocusedIndex += allowHorizontalArrowKeys ? itemsPerRow : 1;
}
if (newFocusedIndex > maxIndex) {
if (disableCyclicTraversal) {
return actualIndex;
}
newFocusedIndex = 0;
}
if (newFocusedIndex === currentFocusedIndex) {
// all indexes are disabled
return actualIndex;
}
}
return newFocusedIndex;
});
}, [allowHorizontalArrowKeys, disableCyclicTraversal, disabledIndexes, itemsPerRow, maxIndex]);
useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ARROW_DOWN, arrowDownCallback, arrowConfig);
const arrowLeftCallback = useCallback(() => {
if (maxIndex < 0 || !allowHorizontalArrowKeys) {
return;
}
const nextIndex = disableCyclicTraversal ? -1 : maxIndex;
setFocusedIndex((actualIndex) => {
let currentFocusedIndex = -1;
currentFocusedIndex = actualIndex > 0 ? actualIndex - 1 : nextIndex;
let newFocusedIndex = currentFocusedIndex;
while (disabledIndexes.includes(newFocusedIndex)) {
newFocusedIndex = newFocusedIndex > 0 ? newFocusedIndex - 1 : nextIndex;
if (newFocusedIndex === currentFocusedIndex) {
// all indexes are disabled
return actualIndex; // no-op
}
}
return newFocusedIndex;
});
}, [allowHorizontalArrowKeys, disableCyclicTraversal, disabledIndexes, maxIndex]);
useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ARROW_LEFT, arrowLeftCallback, horizontalArrowConfig);
const arrowRightCallback = useCallback(() => {
if (maxIndex < 0 || !allowHorizontalArrowKeys) {
return;
}
const nextIndex = disableCyclicTraversal ? maxIndex : 0;
setFocusedIndex((actualIndex) => {
let currentFocusedIndex = -1;
currentFocusedIndex = actualIndex < maxIndex ? actualIndex + 1 : nextIndex;
let newFocusedIndex = currentFocusedIndex;
while (disabledIndexes.includes(newFocusedIndex)) {
newFocusedIndex = newFocusedIndex < maxIndex ? newFocusedIndex + 1 : nextIndex;
if (newFocusedIndex === currentFocusedIndex) {
// all indexes are disabled
return actualIndex;
}
}
return newFocusedIndex;
});
}, [allowHorizontalArrowKeys, disableCyclicTraversal, disabledIndexes, maxIndex]);
useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ARROW_RIGHT, arrowRightCallback, horizontalArrowConfig);
// Note: you don't need to manually manage focusedIndex in the parent. setFocusedIndex is only exposed in case you want to reset focusedIndex or focus a specific item
return [focusedIndex, setFocusedIndex];
}