-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathOverlay.tsx
287 lines (250 loc) · 7.52 KB
/
Overlay.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
import PropTypes from 'prop-types';
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import useCallbackRef from '@restart/hooks/useCallbackRef';
import useMergedRefs from '@restart/hooks/useMergedRefs';
import { placements } from './popper';
import usePopper, {
Placement,
UsePopperOptions,
Offset,
State,
} from './usePopper';
import useRootClose, { RootCloseOptions } from './useRootClose';
import useWaitForDOMRef, { DOMContainer } from './useWaitForDOMRef';
import { TransitionCallbacks } from './types';
import mergeOptionsWithPopperConfig from './mergeOptionsWithPopperConfig';
export interface OverlayProps extends TransitionCallbacks {
flip?: boolean;
placement?: Placement;
offset?: Offset;
containerPadding?: number;
popperConfig?: Omit<UsePopperOptions, 'placement'>;
container?: DOMContainer;
target: DOMContainer;
show?: boolean;
transition?: React.ComponentType<
{ in?: boolean; appear?: boolean } & TransitionCallbacks
>;
onHide?: (e: Event) => void;
rootClose?: boolean;
rootCloseDisabled?: boolean;
rootCloseEvent?: RootCloseOptions['clickTrigger'];
children: (value: {
show: boolean;
placement: Placement;
update: () => void;
forceUpdate: () => void;
state?: State;
props: Record<string, any> & {
ref: React.RefCallback<HTMLElement>;
style: React.CSSProperties;
'aria-labelledby'?: string;
};
arrowProps: Record<string, any> & {
ref: React.RefCallback<HTMLElement>;
style: React.CSSProperties;
};
}) => React.ReactNode;
}
/**
* Built on top of `Popper.js`, the overlay component is
* great for custom tooltip overlays.
*/
const Overlay = React.forwardRef<HTMLElement, OverlayProps>(
(props, outerRef) => {
const {
flip,
offset,
placement,
containerPadding = 5,
popperConfig = {},
transition: Transition,
} = props;
const [rootElement, attachRef] = useCallbackRef<HTMLElement>();
const [arrowElement, attachArrowRef] = useCallbackRef<Element>();
const mergedRef = useMergedRefs<HTMLElement | null>(attachRef, outerRef);
const container = useWaitForDOMRef(props.container);
const target = useWaitForDOMRef(props.target);
const [exited, setExited] = useState(!props.show);
const { styles, attributes, ...popper } = usePopper(
target,
rootElement,
mergeOptionsWithPopperConfig({
placement,
enableEvents: !!props.show,
containerPadding: containerPadding || 5,
flip,
offset,
arrowElement,
popperConfig,
}),
);
if (props.show) {
if (exited) setExited(false);
} else if (!props.transition && !exited) {
setExited(true);
}
const handleHidden: TransitionCallbacks['onExited'] = (...args) => {
setExited(true);
if (props.onExited) {
props.onExited(...args);
}
};
// Don't un-render the overlay while it's transitioning out.
const mountOverlay = props.show || (Transition && !exited);
useRootClose(rootElement, props.onHide!, {
disabled: !props.rootClose || props.rootCloseDisabled,
clickTrigger: props.rootCloseEvent,
});
if (!mountOverlay) {
// Don't bother showing anything if we don't have to.
return null;
}
let child = props.children({
...popper,
show: !!props.show,
props: {
...attributes.popper,
style: styles.popper as any,
ref: mergedRef,
},
arrowProps: {
...attributes.arrow,
style: styles.arrow as any,
ref: attachArrowRef,
},
});
if (Transition) {
const { onExit, onExiting, onEnter, onEntering, onEntered } = props;
child = (
<Transition
in={props.show}
appear
onExit={onExit}
onExiting={onExiting}
onExited={handleHidden}
onEnter={onEnter}
onEntering={onEntering}
onEntered={onEntered}
>
{child}
</Transition>
);
}
return container ? ReactDOM.createPortal(child, container) : null;
},
);
Overlay.displayName = 'Overlay';
Overlay.propTypes = {
/**
* Set the visibility of the Overlay
*/
show: PropTypes.bool,
/** Specify where the overlay element is positioned in relation to the target element */
placement: PropTypes.oneOf(placements),
/**
* A DOM Element, Ref to an element, or function that returns either. The `target` element is where
* the overlay is positioned relative to.
*/
target: PropTypes.any,
/**
* A DOM Element, Ref to an element, or function that returns either. The `container` will have the Portal children
* appended to it.
*/
container: PropTypes.any,
/**
* Enables the Popper.js `flip` modifier, allowing the Overlay 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,
/**
* A render prop that returns an element to overlay and position. See
* the [react-popper documentation](https://github.com/FezVrasta/react-popper#children) for more info.
*
* @type {Function ({
* show: boolean,
* placement: Placement,
* update: () => void,
* forceUpdate: () => void,
* props: {
* ref: (?HTMLElement) => void,
* style: { [string]: string | number },
* aria-labelledby: ?string
* [string]: string | number,
* },
* arrowProps: {
* ref: (?HTMLElement) => void,
* style: { [string]: string | number },
* [string]: string | number,
* },
* }) => React.Element}
*/
children: PropTypes.func.isRequired,
/**
* Control how much space there is between the edge of the boundary element and overlay.
* A convenience shortcut to setting `popperConfig.modfiers.preventOverflow.padding`
*/
containerPadding: PropTypes.number,
/**
* A set of popper options and props passed directly to react-popper's Popper component.
*/
popperConfig: PropTypes.object,
/**
* Specify whether the overlay should trigger `onHide` when the user clicks outside the overlay
*/
rootClose: PropTypes.bool,
/**
* Specify event for toggling overlay
*/
rootCloseEvent: PropTypes.oneOf(['click', 'mousedown']),
/**
* Specify disabled for disable RootCloseWrapper
*/
rootCloseDisabled: PropTypes.bool,
/**
* A Callback fired by the Overlay when it wishes to be hidden.
*
* __required__ when `rootClose` is `true`.
*
* @type func
*/
onHide(props, ...args) {
if (props.rootClose) {
return PropTypes.func.isRequired(props, ...args);
}
return PropTypes.func(props, ...args);
},
/**
* A `[email protected]` `<Transition/>` component
* used to animate the overlay as it changes visibility.
*/
// @ts-ignore
transition: PropTypes.elementType,
/**
* Callback fired before the Overlay transitions in
*/
onEnter: PropTypes.func,
/**
* Callback fired as the Overlay begins to transition in
*/
onEntering: PropTypes.func,
/**
* Callback fired after the Overlay finishes transitioning in
*/
onEntered: PropTypes.func,
/**
* Callback fired right before the Overlay transitions out
*/
onExit: PropTypes.func,
/**
* Callback fired as the Overlay begins to transition out
*/
onExiting: PropTypes.func,
/**
* Callback fired after the Overlay finishes transitioning out
*/
onExited: PropTypes.func,
};
export default Overlay;