-
Notifications
You must be signed in to change notification settings - Fork 37
/
Popper.js
183 lines (156 loc) · 4.37 KB
/
Popper.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
181
182
183
import React, { useContext, forwardRef } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import PopperJS from 'popper.js';
import css from './Popper.css';
import { OVERLAY_CONTAINER_ID } from '../../util/consts';
import useOverlayContainer from '../../hooks/useOverlayContainer';
import StripesOverlayContext from '../../util/StripesOverlayContext';
export const OVERLAY_MODIFIERS = {
flip: { boundariesElement: 'viewport', padding: 5 },
preventOverflow: { boundariesElement: 'viewport', padding: 5 },
}
export const AVAILABLE_PLACEMENTS = [
'bottom',
'top',
'left',
'right',
'top-start',
'top-end',
'bottom-start',
'bottom-end',
'left-start',
'left-end',
'right-start',
'right-end',
'auto',
'auto-start',
'auto-end',
];
const [DEFAULT_PLACEMENT] = AVAILABLE_PLACEMENTS;
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}
export function withOverlayContext(WrappedComponent) {
const WithOverlayContext = forwardRef(({ portal, ...props }, ref) => { // eslint-disable-line react/prop-types
const { usePortal } = useContext(StripesOverlayContext);
const portalRef = useOverlayContainer(document.getElementById(OVERLAY_CONTAINER_ID));
return <WrappedComponent ref={ref} portal={usePortal ? portalRef.element : portal} {...props} />;
});
WithOverlayContext.displayName = `WithOverlayContext(${getDisplayName(WrappedComponent)})`;
return WithOverlayContext;
}
class Popper extends React.Component {
static propTypes = {
anchorRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.instanceOf(Element),
})
]).isRequired,
children: PropTypes.node.isRequired,
hideIfClosed: PropTypes.bool,
isOpen: PropTypes.bool,
modifiers: PropTypes.object,
onUpdate: PropTypes.func,
overlayProps: PropTypes.object,
overlayRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
placement: PropTypes.oneOf(AVAILABLE_PLACEMENTS),
portal: PropTypes.instanceOf(Element),
}
static defaultProps = {
placement: DEFAULT_PLACEMENT,
onUpdate: () => {},
overlayProps: {}
}
constructor(props) {
super(props);
// typecheck for ref callbacks...
if (typeof this.props.overlayRef === 'function') {
this.overlayRef = (ref) => {
props.overlayRef(ref);
this.overlayRef.current = ref;
};
} else {
this.overlayRef = props.overlayRef || React.createRef();
}
}
componentDidMount() {
if (!this.props.isOpen) return;
this.createPopperInstance();
}
componentDidUpdate() {
this.updatePopperInstance();
}
componentWillUnmount() {
this.destroyPopperInstance();
}
renderOverlay() {
const {
portal,
children,
overlayProps,
hideIfClosed,
isOpen
} = this.props;
let displayProp = {};
if (!isOpen && hideIfClosed) {
displayProp = { hidden: true };
}
const overlay = (
<div className={css.overlay} ref={this.overlayRef} {...displayProp} {...overlayProps}>
{children}
</div>
);
return portal ? ReactDOM.createPortal(overlay, portal) : overlay;
}
getOptions() {
const {
modifiers,
placement,
onUpdate,
} = this.props;
let calculatedPlacement = placement;
if (document.dir === 'rtl') {
const placementRE = new RegExp(/start|end|left|right/g);
const hash = {
end: 'start',
start: 'end',
left: 'right',
right: 'left'
};
calculatedPlacement = calculatedPlacement.replace(placementRE, matched => hash[matched]);
}
return {
modifiers,
placement: calculatedPlacement,
onCreate: onUpdate,
};
}
updatePopperInstance = () => {
this.destroyPopperInstance();
this.createPopperInstance();
};
createPopperInstance() {
this.popperInstance = this.props.isOpen
? new PopperJS(
this.props.anchorRef.current,
this.overlayRef.current,
this.getOptions(),
)
: null;
}
destroyPopperInstance() {
if (this.popperInstance) {
this.popperInstance.destroy();
}
}
render() {
const { hideIfClosed, isOpen } = this.props;
if (hideIfClosed || isOpen) {
return this.renderOverlay();
}
return null;
}
}
export default withOverlayContext(Popper);