/
use-dropdown-menu.ts
258 lines (217 loc) · 7.32 KB
/
use-dropdown-menu.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Imports
import React, { useState, useRef, createRef, useEffect, useMemo } from 'react';
// Create interface for button properties
export interface ButtonProps<ButtonElement extends HTMLElement>
extends Pick<
React.DetailedHTMLProps<React.ButtonHTMLAttributes<ButtonElement>, ButtonElement>,
'onKeyDown' | 'onClick' | 'tabIndex' | 'role' | 'aria-haspopup' | 'aria-expanded'
> {
ref: React.RefObject<ButtonElement>;
}
// Create interface for item properties
export interface ItemProps {
onKeyDown: (e: React.KeyboardEvent<HTMLAnchorElement>) => void;
tabIndex: number;
role: string;
ref: React.RefObject<HTMLAnchorElement>;
}
// A custom Hook that abstracts away the listeners/controls for dropdown menus
export interface DropdownMenuOptions {
focusFirstItemOnClick?: boolean;
}
interface DropdownMenuResponse<ButtonElement extends HTMLElement> {
readonly buttonProps: ButtonProps<ButtonElement>;
readonly itemProps: ItemProps[];
readonly isOpen: boolean;
readonly setIsOpen: React.Dispatch<React.SetStateAction<boolean>>;
readonly moveFocus: (itemIndex: number) => void;
}
export default function useDropdownMenu<ButtonElement extends HTMLElement = HTMLButtonElement>(
itemCount: number,
options?: DropdownMenuOptions
): DropdownMenuResponse<ButtonElement> {
// Use state
const [isOpen, setIsOpen] = useState<boolean>(false);
const currentFocusIndex = useRef<number | null>(null);
const firstRun = useRef(true);
const clickedOpen = useRef(false);
// Create refs
const buttonRef = useRef<ButtonElement>(null);
const itemRefs = useMemo<React.RefObject<HTMLAnchorElement>[]>(
() => Array.from({ length: itemCount }, () => createRef<HTMLAnchorElement>()),
[itemCount]
);
// Create type guard
const isKeyboardEvent = (e: React.KeyboardEvent | React.MouseEvent): e is React.KeyboardEvent =>
(e as React.KeyboardEvent).key !== undefined;
// Handles moving the focus between menu items
const moveFocus = (itemIndex: number): void => {
currentFocusIndex.current = itemIndex;
itemRefs[itemIndex].current?.focus();
};
// Focus the first item when the menu opens
useEffect(() => {
// Stop if this is the first fire of the Hook, and update the ref
if (firstRun.current) {
firstRun.current = false;
return;
}
// If the menu is currently open focus on the first item in the menu
if (isOpen && (!clickedOpen.current || options?.focusFirstItemOnClick)) {
moveFocus(0);
} else if (!isOpen) {
clickedOpen.current = false;
}
}, [isOpen]);
// Handle listening for clicks and auto-hiding the menu
useEffect(() => {
// Ignore if the menu isn't open
if (!isOpen) {
return;
}
// Initialize object to track if the removal happens before the addition of the event listener
// -> We're using an object here so that arrow functions below capture the reference and not the value
const removalTracker = {
removed: false,
};
// This function is designed to handle every click
const handleEveryClick = (event: MouseEvent): void => {
// Make this happen asynchronously
setTimeout(() => {
// Type guard
if (!(event.target instanceof Element)) {
return;
}
// Ignore if we're clicking inside the menu
if (event.target.closest('[role="menu"]') instanceof Element) {
return;
}
// Hide dropdown
setIsOpen(false);
}, 10);
};
// Add listener
// -> Force it to be async to fix: https://github.com/facebook/react/issues/20074
setTimeout(() => {
if (removalTracker.removed) {
return;
}
document.addEventListener('click', handleEveryClick);
}, 1);
// Return function to remove listener
return (): void => {
removalTracker.removed = true;
document.removeEventListener('click', handleEveryClick);
};
}, [isOpen]);
// Disable scroll when the menu is opened, and revert back when the menu is closed
useEffect(() => {
const disableArrowScroll = (event: KeyboardEvent): void => {
if (isOpen && (event.key === 'ArrowDown' || event.key === 'ArrowUp')) {
event.preventDefault();
}
};
document.addEventListener('keydown', disableArrowScroll);
return (): void => document.removeEventListener('keydown', disableArrowScroll);
}, [isOpen]);
// Create a handler function for the button's clicks and keyboard events
const buttonListener = (e: React.KeyboardEvent | React.MouseEvent): void => {
// Detect if event was a keyboard event or a mouse event
if (isKeyboardEvent(e)) {
const { key } = e;
if (!['Enter', ' ', 'Tab', 'ArrowDown', 'Escape'].includes(key)) {
return;
}
if ((key === 'Tab' || key === 'ArrowDown') && clickedOpen.current && isOpen) {
e.preventDefault();
moveFocus(0);
}
if (key === 'Enter' || key === ' ') {
e.preventDefault();
setIsOpen(true);
}
if (key === 'Escape') {
e.preventDefault();
setIsOpen(false);
}
} else {
if (!options?.focusFirstItemOnClick) {
clickedOpen.current = !isOpen;
}
setIsOpen(!isOpen);
}
};
// Create a function that handles menu logic based on keyboard events that occur on menu items
const itemListener = (e: React.KeyboardEvent<HTMLAnchorElement>): void => {
// Destructure the key property from the event object
const { key } = e;
// Handle keyboard controls
if (['Tab', 'Shift', 'Enter', 'Escape', 'ArrowUp', 'ArrowDown', ' '].includes(key)) {
// Create mutable value that initializes as the currentFocusIndex value
let newFocusIndex = currentFocusIndex.current;
// Controls whether the menu is open or closed, if the button should regain focus on close, and if a handler function should be called
if (key === 'Escape') {
setIsOpen(false);
buttonRef.current?.focus();
return;
} else if (key === 'Tab') {
setIsOpen(false);
return;
} else if (key === 'Enter' || key === ' ') {
if (!e.currentTarget.href) {
e.currentTarget.click();
}
setIsOpen(false);
return;
}
// Controls the current index to focus
if (newFocusIndex !== null) {
if (key === 'ArrowUp') {
newFocusIndex -= 1;
} else if (key === 'ArrowDown') {
newFocusIndex += 1;
}
if (newFocusIndex > itemRefs.length - 1) {
newFocusIndex = 0;
} else if (newFocusIndex < 0) {
newFocusIndex = itemRefs.length - 1;
}
}
// After any modification set state to the modified value
if (newFocusIndex !== null) {
moveFocus(newFocusIndex);
}
return;
}
// Handle printable keys
if (/[a-zA-Z0-9./<>?;:"'`!@#$%^&*()\\[\]{}_+=|\\-~,]/.test(key)) {
const index = itemRefs.findIndex(
(ref) =>
ref.current?.innerText?.toLowerCase().startsWith(key.toLowerCase()) ||
ref.current?.textContent?.toLowerCase().startsWith(key.toLowerCase()) ||
ref.current?.getAttribute('aria-label')?.toLowerCase().startsWith(key.toLowerCase())
);
if (index !== -1) {
moveFocus(index);
}
}
};
// Define spreadable props for button and items
const buttonProps: ButtonProps<ButtonElement> = {
onKeyDown: buttonListener,
onClick: buttonListener,
tabIndex: 0,
ref: buttonRef,
role: 'button',
'aria-haspopup': true,
'aria-expanded': isOpen,
};
const itemProps = Array.from({ length: itemCount }, (_ignore, index) => ({
onKeyDown: itemListener,
tabIndex: -1,
role: 'menuitem',
ref: itemRefs[index],
}));
// Return a listener for the button, individual list items, and the state of the menu
return { buttonProps, itemProps, isOpen, setIsOpen, moveFocus } as const;
}