-
Notifications
You must be signed in to change notification settings - Fork 21
/
DropdownMenu.tsx
228 lines (195 loc) · 6.46 KB
/
DropdownMenu.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
import { useContext, useRef } from 'react';
import * as React from 'react';
import useCallbackRef from '@restart/hooks/useCallbackRef';
import DropdownContext, { DropdownContextValue } from './DropdownContext';
import usePopper, {
UsePopperOptions,
Placement,
Offset,
UsePopperState,
} from './usePopper';
import useClickOutside, { ClickOutsideOptions } from './useClickOutside';
import mergeOptionsWithPopperConfig from './mergeOptionsWithPopperConfig';
export interface UseDropdownMenuOptions {
/**
* Enables the Popper.js `flip` modifier, allowing the Dropdown to
* automatically adjust it's placement in case of overlap with the viewport or
* toggle. See the [flip docs](https://popper.js.org/docs/v2/modifiers/flip/)
* for more info.
*/
flip?: boolean;
/**
* Controls the visible state of the menu, generally this is provided by the
* parent `Dropdown` component, but may also be specified as a prop directly.
*/
show?: boolean;
/**
* Use the `fixed` positioning strategy in Popper. This is used if the
* dropdown toggle is in a fixed container.
*/
fixed?: boolean;
/**
* The PopperJS placement for positioning the Dropdown menu in relation to it's Toggle.
* Generally this is provided by the parent `Dropdown` component,
* but may also be specified as a prop directly.
*/
placement?: Placement;
/**
* Whether or not to use Popper for positioning the menu.
*/
usePopper?: boolean;
/**
* Whether or not to add scroll and resize listeners to update menu position.
*
* See the [event listeners docs](https://popper.js.org/docs/v2/modifiers/event-listeners/)
* for more info.
*/
enableEventListeners?: boolean;
/**
* Offset of the dropdown menu from the dropdown toggle. See the
* [offset docs](https://popper.js.org/docs/v2/modifiers/offset/) for more info.
*/
offset?: Offset;
/**
* Override the default event used by RootCloseWrapper.
*/
rootCloseEvent?: ClickOutsideOptions['clickTrigger'];
/**
* A set of popper options and props passed directly to react-popper's Popper component.
*/
popperConfig?: Omit<UsePopperOptions, 'enabled' | 'placement'>;
}
export type UserDropdownMenuProps = Record<string, any> & {
ref: React.RefCallback<HTMLElement>;
style?: React.CSSProperties;
'aria-labelledby'?: string;
};
export type UserDropdownMenuArrowProps = Record<string, any> & {
ref: React.RefCallback<HTMLElement>;
style: React.CSSProperties;
};
export interface UseDropdownMenuMetadata {
show: boolean;
placement?: Placement;
hasShown: boolean;
toggle?: DropdownContextValue['toggle'];
popper: UsePopperState | null;
arrowProps: Partial<UserDropdownMenuArrowProps>;
}
const noop: any = () => {};
/**
* @memberOf Dropdown
* @param {object} options
* @param {boolean} options.flip Automatically adjust the menu `drop` position based on viewport edge detection
* @param {[number, number]} options.offset Define an offset distance between the Menu and the Toggle
* @param {boolean} options.show Display the menu manually, ignored in the context of a `Dropdown`
* @param {boolean} options.usePopper opt in/out of using PopperJS to position menus. When disabled you must position it yourself.
* @param {string} options.rootCloseEvent The pointer event to listen for when determining "clicks outside" the menu for triggering a close.
* @param {object} options.popperConfig Options passed to the [`usePopper`](/api/usePopper) hook.
*/
export function useDropdownMenu(options: UseDropdownMenuOptions = {}) {
const context = useContext(DropdownContext);
const [arrowElement, attachArrowRef] = useCallbackRef<Element>();
const hasShownRef = useRef(false);
const {
flip,
offset,
rootCloseEvent,
fixed = false,
placement: placementOverride,
popperConfig = {},
enableEventListeners = true,
usePopper: shouldUsePopper = !!context,
} = options;
const show = context?.show == null ? !!options.show : context.show;
if (show && !hasShownRef.current) {
hasShownRef.current = true;
}
const handleClose = (e: React.SyntheticEvent | Event) => {
context?.toggle(false, e);
};
const { placement, setMenu, menuElement, toggleElement } = context || {};
const popper = usePopper(
toggleElement,
menuElement,
mergeOptionsWithPopperConfig({
placement: placementOverride || placement || 'bottom-start',
enabled: shouldUsePopper,
enableEvents: enableEventListeners == null ? show : enableEventListeners,
offset,
flip,
fixed,
arrowElement,
popperConfig,
}),
);
const menuProps: UserDropdownMenuProps = {
ref: setMenu || noop,
'aria-labelledby': toggleElement?.id,
...popper.attributes.popper,
style: popper.styles.popper as any,
};
const metadata: UseDropdownMenuMetadata = {
show,
placement,
hasShown: hasShownRef.current,
toggle: context?.toggle,
popper: shouldUsePopper ? popper : null,
arrowProps: shouldUsePopper
? {
ref: attachArrowRef,
...popper.attributes.arrow,
style: popper.styles.arrow as any,
}
: {},
};
useClickOutside(menuElement, handleClose, {
clickTrigger: rootCloseEvent,
disabled: !show,
});
return [menuProps, metadata] as const;
}
const defaultProps = {
usePopper: true,
};
export interface DropdownMenuProps extends UseDropdownMenuOptions {
/**
* A render prop that returns a Menu element. The `props`
* argument should be spread through to **a component that can accept a ref**.
*
* @type {Function ({
* show: boolean,
* close: (?SyntheticEvent) => void,
* placement: Placement,
* update: () => void,
* forceUpdate: () => void,
* props: {
* ref: (?HTMLElement) => void,
* style: { [string]: string | number },
* aria-labelledby: ?string
* },
* arrowProps: {
* ref: (?HTMLElement) => void,
* style: { [string]: string | number },
* },
* }) => React.Element}
*/
children: (
props: UserDropdownMenuProps,
meta: UseDropdownMenuMetadata,
) => React.ReactNode;
}
/**
* Also exported as `<Dropdown.Menu>` from `Dropdown`.
*
* @displayName DropdownMenu
* @memberOf Dropdown
*/
function DropdownMenu({ children, ...options }: DropdownMenuProps) {
const [props, meta] = useDropdownMenu(options);
return <>{children(props, meta)}</>;
}
DropdownMenu.displayName = 'DropdownMenu';
DropdownMenu.defaultProps = defaultProps;
/** @component */
export default DropdownMenu;