/
Dropdown.tsx
295 lines (252 loc) · 7.7 KB
/
Dropdown.tsx
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import matches from 'dom-helpers/matches';
import qsa from 'dom-helpers/querySelectorAll';
import React, { useCallback, useRef, useEffect, useMemo } from 'react';
import PropTypes from 'prop-types';
import { useUncontrolledProp } from 'uncontrollable';
import usePrevious from '@restart/hooks/usePrevious';
import useForceUpdate from '@restart/hooks/useForceUpdate';
import useGlobalListener from '@restart/hooks/useGlobalListener';
import useEventCallback from '@restart/hooks/useEventCallback';
import DropdownContext, { DropDirection } from './DropdownContext';
import DropdownMenu from './DropdownMenu';
import DropdownToggle from './DropdownToggle';
const propTypes = {
/**
* A render prop that returns the root dropdown element. The `props`
* argument should spread through to an element containing _both_ the
* menu and toggle in order to handle keyboard events for focus management.
*
* @type {Function ({
* props: {
* onKeyDown: (SyntheticEvent) => void,
* },
* }) => React.Element}
*/
children: PropTypes.node,
/**
* Determines the direction and location of the Menu in relation to it's Toggle.
*/
drop: PropTypes.oneOf(['up', 'left', 'right', 'down']),
/**
* Controls the focus behavior for when the Dropdown is opened. Set to
* `true` to always focus the first menu item, `keyboard` to focus only when
* navigating via the keyboard, or `false` to disable completely
*
* The Default behavior is `false` **unless** the Menu has a `role="menu"`
* where it will default to `keyboard` to match the recommended [ARIA Authoring practices](https://www.w3.org/TR/wai-aria-practices-1.1/#menubutton).
*/
focusFirstItemOnShow: PropTypes.oneOf([false, true, 'keyboard']),
/**
* A css slector string that will return __focusable__ menu items.
* Selectors should be relative to the menu component:
* e.g. ` > li:not('.disabled')`
*/
itemSelector: PropTypes.string,
/**
* Align the menu to the 'end' side of the placement side of the Dropdown toggle. The default placement is `top-start` or `bottom-start`.
*/
alignEnd: PropTypes.bool,
/**
* Whether or not the Dropdown is visible.
*
* @controllable onToggle
*/
show: PropTypes.bool,
/**
* Sets the initial show position of the Dropdown.
*/
defaultShow: PropTypes.bool,
/**
* A callback fired when the Dropdown wishes to change visibility. Called with the requested
* `show` value, the DOM event, and the source that fired it: `'click'`,`'keydown'`,`'rootClose'`, or `'select'`.
*
* ```ts static
* function(
* isOpen: boolean,
* event: SyntheticEvent,
* ): void
* ```
*
* @controllable show
*/
onToggle: PropTypes.func,
};
export interface DropdownInjectedProps {
onKeyDown: React.KeyboardEventHandler;
}
export interface DropdownProps {
drop?: DropDirection;
alignEnd?: boolean;
defaultShow?: boolean;
show?: boolean;
onToggle: (nextShow: boolean, event?: React.SyntheticEvent | Event) => void;
itemSelector?: string;
focusFirstItemOnShow?: false | true | 'keyboard';
children: React.ReactNode;
}
function useRefWithUpdate() {
const forceUpdate = useForceUpdate();
const ref = useRef<HTMLElement | null>(null);
const attachRef = useCallback(
(element: null | HTMLElement) => {
ref.current = element;
// ensure that a menu set triggers an update for consumers
forceUpdate();
},
[forceUpdate],
);
return [ref, attachRef] as const;
}
/**
* @displayName Dropdown
*/
function Dropdown({
drop,
alignEnd,
defaultShow,
show: rawShow,
onToggle: rawOnToggle,
itemSelector = '* > *',
focusFirstItemOnShow,
children,
}: DropdownProps) {
const [show, onToggle] = useUncontrolledProp(
rawShow,
defaultShow!,
rawOnToggle,
);
// We use normal refs instead of useCallbackRef in order to populate the
// the value as quickly as possible, otherwise the effect to focus the element
// may run before the state value is set
const [menuRef, setMenu] = useRefWithUpdate();
const menuElement = menuRef.current;
const [toggleRef, setToggle] = useRefWithUpdate();
const toggleElement = toggleRef.current;
const lastShow = usePrevious(show);
const lastSourceEvent = useRef<string | null>(null);
const focusInDropdown = useRef(false);
const toggle = useCallback(
(nextShow: boolean, event?: Event | React.SyntheticEvent) => {
onToggle(nextShow, event);
},
[onToggle],
);
const context = useMemo(
() => ({
toggle,
drop,
show,
alignEnd,
menuElement,
toggleElement,
setMenu,
setToggle,
}),
[
toggle,
drop,
show,
alignEnd,
menuElement,
toggleElement,
setMenu,
setToggle,
],
);
if (menuElement && lastShow && !show) {
focusInDropdown.current = menuElement.contains(document.activeElement);
}
const focusToggle = useEventCallback(() => {
if (toggleElement && toggleElement.focus) {
toggleElement.focus();
}
});
const maybeFocusFirst = useEventCallback(() => {
const type = lastSourceEvent.current;
let focusType = focusFirstItemOnShow;
if (focusType == null) {
focusType =
menuRef.current && matches(menuRef.current, '[role=menu]')
? 'keyboard'
: false;
}
if (
focusType === false ||
(focusType === 'keyboard' && !/^key.+$/.test(type!))
) {
return;
}
const first = qsa(menuRef.current!, itemSelector)[0];
if (first && first.focus) first.focus();
});
useEffect(() => {
if (show) maybeFocusFirst();
else if (focusInDropdown.current) {
focusInDropdown.current = false;
focusToggle();
}
// only `show` should be changing
}, [show, focusInDropdown, focusToggle, maybeFocusFirst]);
useEffect(() => {
lastSourceEvent.current = null;
});
const getNextFocusedChild = (current: HTMLElement, offset: number) => {
if (!menuRef.current) return null;
const items = qsa(menuRef.current, itemSelector);
let index = items.indexOf(current) + offset;
index = Math.max(0, Math.min(index, items.length));
return items[index];
};
useGlobalListener('keydown', (event: KeyboardEvent) => {
const { key } = event;
const target = event.target as HTMLElement;
const fromMenu = menuRef.current?.contains(target);
const fromToggle = toggleRef.current?.contains(target);
// Second only to https://github.com/twbs/bootstrap/blob/8cfbf6933b8a0146ac3fbc369f19e520bd1ebdac/js/src/dropdown.js#L400
// in inscrutability
const isInput = /input|textarea/i.test(target.tagName);
if (isInput && (key === ' ' || (key !== 'Escape' && fromMenu))) {
return;
}
if (!fromMenu && !fromToggle) {
return;
}
lastSourceEvent.current = event.type;
switch (key) {
case 'ArrowUp': {
const next = getNextFocusedChild(target, -1);
if (next && next.focus) next.focus();
event.preventDefault();
return;
}
case 'ArrowDown':
event.preventDefault();
if (!show) {
onToggle(true, event);
} else {
const next = getNextFocusedChild(target, 1);
if (next && next.focus) next.focus();
}
return;
case 'Escape':
case 'Tab':
if (key === 'Escape') {
event.preventDefault();
event.stopPropagation();
}
onToggle(false, event);
break;
default:
}
});
return (
<DropdownContext.Provider value={context}>
{children}
</DropdownContext.Provider>
);
}
Dropdown.displayName = 'ReactOverlaysDropdown';
Dropdown.propTypes = propTypes;
Dropdown.Menu = DropdownMenu;
Dropdown.Toggle = DropdownToggle;
export default Dropdown;