-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Overlay.js
177 lines (152 loc) · 4.21 KB
/
Overlay.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
import React, { useRef } from 'react';
import { findDOMNode } from 'react-dom';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import BaseOverlay from 'react-overlays/Overlay';
import { componentOrElement, elementType } from 'prop-types-extra';
import Fade from './Fade';
const propTypes = {
/**
* A component instance, DOM node, or function that returns either.
* The `container` element will have the Overlay appended to it via a React portal.
*/
container: PropTypes.oneOfType([componentOrElement, PropTypes.func]),
/**
* A component instance, DOM node, or function that returns either.
* The overlay will be positioned in relation to the `target`
*/
target: PropTypes.oneOfType([componentOrElement, PropTypes.func]),
/**
* Set the visibility of the Overlay
*/
show: PropTypes.bool,
/**
* A set of popper options and props passed directly to Popper.
*/
popperConfig: PropTypes.object,
/**
* Specify whether the overlay should trigger onHide when the user clicks outside the overlay
*/
rootClose: PropTypes.bool,
/**
* Specify event for triggering a "root close" toggle.
*/
rootCloseEvent: PropTypes.oneOf(['click', 'mousedown']),
/**
* A callback invoked by the overlay when it wishes to be hidden. Required if
* `rootClose` is specified.
*/
onHide: PropTypes.func,
/**
* Animate the entering and exiting of the Ovelay. `true` will use the `<Fade>` transition,
* or a custom react-transition-group `<Transition>` component can be provided.
*/
transition: PropTypes.oneOfType([PropTypes.bool, 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,
/**
* The placement of the Overlay in relation to it's `target`.
*/
placement: PropTypes.oneOf([
'auto-start',
'auto',
'auto-end',
'top-start',
'top',
'top-end',
'right-start',
'right',
'right-end',
'bottom-end',
'bottom',
'bottom-start',
'left-end',
'left',
'left-start',
]),
};
const defaultProps = {
transition: Fade,
rootClose: false,
show: false,
placement: 'top',
};
function wrapRefs(props, arrowProps) {
const { ref } = props;
const { ref: aRef } = arrowProps;
props.ref = ref.__wrapped || (ref.__wrapped = (r) => ref(findDOMNode(r)));
arrowProps.ref =
aRef.__wrapped || (aRef.__wrapped = (r) => aRef(findDOMNode(r)));
}
function Overlay({ children: overlay, transition, ...outerProps }) {
const popperRef = useRef({});
transition = transition === true ? Fade : transition || null;
return (
<BaseOverlay {...outerProps} transition={transition}>
{({
props: overlayProps,
arrowProps,
show,
state,
scheduleUpdate,
placement,
outOfBoundaries,
...props
}) => {
wrapRefs(overlayProps, arrowProps);
const popper = Object.assign(popperRef.current, {
state,
scheduleUpdate,
placement,
outOfBoundaries,
});
if (typeof overlay === 'function')
return overlay({
...props,
...overlayProps,
placement,
show,
popper,
arrowProps,
});
return React.cloneElement(overlay, {
...props,
...overlayProps,
placement,
arrowProps,
popper,
className: classNames(
overlay.props.className,
!transition && show && 'show',
),
style: { ...overlay.props.style, ...overlayProps.style },
});
}}
</BaseOverlay>
);
}
Overlay.propTypes = propTypes;
Overlay.defaultProps = defaultProps;
export default Overlay;