-
Notifications
You must be signed in to change notification settings - Fork 222
/
DropdownMenu.js
180 lines (151 loc) · 4.53 KB
/
DropdownMenu.js
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
import PropTypes from 'prop-types';
import { useContext, useRef } from 'react';
import useCallbackRef from '@restart/hooks/useCallbackRef';
import DropdownContext from './DropdownContext';
import usePopper, { toModifierMap } from './usePopper';
import useRootClose from './useRootClose';
export function useDropdownMenu(options = {}) {
const context = useContext(DropdownContext);
const [arrowElement, attachArrowRef] = useCallbackRef();
const hasShownRef = useRef(false);
const {
flip,
rootCloseEvent,
popperConfig = {},
usePopper: shouldUsePopper = true,
} = options;
const show = context.show == null ? options.show : context.show;
const alignEnd =
context.alignEnd == null ? options.alignEnd : context.alignEnd;
if (show && !hasShownRef.current) {
hasShownRef.current = true;
}
const handleClose = e => {
if (!context.toggle) return;
context.toggle(false, e);
};
const { drop, setMenu, menuElement, toggleElement } = context;
let placement = alignEnd ? 'bottom-end' : 'bottom-start';
if (drop === 'up') placement = alignEnd ? 'top-end' : 'top-start';
else if (drop === 'right') placement = alignEnd ? 'right-end' : 'right-start';
else if (drop === 'left') placement = alignEnd ? 'left-end' : 'left-start';
const modifiers = toModifierMap(popperConfig.modifiers);
const popper = usePopper(toggleElement, menuElement, {
placement,
enabled: !!(shouldUsePopper && show),
modifiers: {
...modifiers,
eventListeners: {
enabled: !!show,
},
arrow: {
...modifiers.arrow,
enabled: !!arrowElement,
options: {
...modifiers.arrow?.options,
element: arrowElement,
},
},
flip: {
enabled: !!flip,
...modifiers.flip,
},
},
});
let menu = null;
const menuProps = {
ref: setMenu,
'aria-labelledby': toggleElement && toggleElement.id,
};
const childArgs = {
show,
alignEnd,
hasShown: hasShownRef.current,
close: handleClose,
};
if (!shouldUsePopper) {
menu = { ...childArgs, props: menuProps };
} else {
menu = {
...popper,
...childArgs,
props: {
...menuProps,
style: popper.styles,
},
arrowProps: {
ref: attachArrowRef,
style: popper.arrowStyles,
},
};
}
useRootClose(menuElement, handleClose, {
clickTrigger: rootCloseEvent,
disabled: !(menu && show),
});
return menu;
}
const propTypes = {
/**
* A render prop that returns a Menu element. The `props`
* argument should spread through to **a component that can accept a ref**.
*
* @type {Function ({
* show: boolean,
* alignEnd: boolean,
* close: (?SyntheticEvent) => void,
* placement: Placement,
* outOfBoundaries: ?boolean,
* scheduleUpdate: () => void,
* props: {
* ref: (?HTMLElement) => void,
* style: { [string]: string | number },
* aria-labelledby: ?string
* },
* arrowProps: {
* ref: (?HTMLElement) => void,
* style: { [string]: string | number },
* },
* }) => React.Element}
*/
children: PropTypes.func.isRequired,
/**
* 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: PropTypes.bool,
/**
* Aligns the dropdown menu to the 'end' of it's placement position.
* Generally this is provided by the parent `Dropdown` component,
* but may also be specified as a prop directly.
*/
alignEnd: PropTypes.bool,
/**
* Enables the Popper.js `flip` modifier, allowing the Dropdown to
* automatically adjust it's placement in case of overlap with the viewport or toggle.
* Refer to the [flip docs](https://popper.js.org/popper-documentation.html#modifiers..flip.enabled) for more info
*/
flip: PropTypes.bool,
usePopper: PropTypes.oneOf([true, false]),
/**
* A set of popper options and props passed directly to react-popper's Popper component.
*/
popperConfig: PropTypes.object,
/**
* Override the default event used by RootCloseWrapper.
*/
rootCloseEvent: PropTypes.string,
};
const defaultProps = {
usePopper: true,
};
function DropdownMenu({ children, ...options }) {
const args = useDropdownMenu(options);
return args.hasShown ? children(args) : null;
}
DropdownMenu.displayName = 'ReactOverlaysDropdownMenu';
DropdownMenu.propTypes = propTypes;
DropdownMenu.defaultProps = defaultProps;
/** @component */
export default DropdownMenu;