-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContent.js
291 lines (261 loc) · 8.11 KB
/
Content.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
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
288
289
290
291
import React from 'react';
import PropTypes from 'prop-types';
import { createPopper } from '@popperjs/core';
import _uniqueId from 'lodash/uniqueId';
import _isEqual from 'lodash/isEqual';
import Stack from './Stack';
import Portal from './Portal';
export default class Content extends React.Component {
static className = 'react-nested-popper_Content';
popperEl = null;
resizerEl = null;
popperInstance = null;
determinedStack = null;
resizerEventAdded = false;
positionPoller = null;
lastTargetPos = [0, 0];
targetParents = null;
// used in stack so we don't duplicate call destroy
isDestroying = false;
constructor(props) {
super(props);
// we add a unique id so the stack can easily find instances as needed
this.id = _uniqueId();
}
componentDidMount() {
// if show was initially enabled, init popperjs immediately
if (this.props._show) {
this.initPopperInstance();
this.initResizer();
}
if (this.props._targetRef) {
this.targetParents = Stack._getTargetParents(this.props._targetRef);
}
}
componentDidUpdate(prevProps) {
// watch prop changes to see if we need to init or update the popperjs instance
// if not show, we kill the instance and clean up refs
if (this.props._show) {
if (!this.popperInstance) {
this.initPopperInstance();
this.initResizer();
} else if (!_isEqual(this.props.popperOptions, prevProps.popperOptions)) {
this.popperInstance.setOptions(this.props.popperOptions);
this.forcePopperUpdate();
}
} else {
this.destroyResizer();
this.destroyPopperInstance();
}
if (prevProps._targetRef !== this.props._targetRef) {
this.targetParents = Stack._getTargetParents(this.props._targetRef);
}
}
componentWillUnmount() {
this.destroyResizer();
this.destroyPopperInstance();
}
/* called if stack wants to hide this popper */
componentWillDestroy() {
this.isDestroying = true;
this.props._onComponentWillDestroy();
}
/* init popperjs */
initPopperInstance() {
// only init if we actually have a target and popper element to bind to
if (this.popperEl && this.props._targetRef && !this.popperInstance) {
this.popperInstance = createPopper(this.props._targetRef, this.popperEl, this.props.popperOptions);
// push this instance to the stack so the stack can manage it
let stack = this.props._stack;
if (stack === 'auto') {
const myTargetParents = Stack._getTargetParents(this.props._targetRef);
if (!myTargetParents) {
stack = ['_content' + this.id];
} else {
stack = Stack._reflow(this.props._targetRef, myTargetParents);
}
}
this.determinedStack = stack;
Stack._push(this, stack);
// use capture so child re-renders happen after. Otherwise the original clicked target can disappear, causing false outside clicks
document.addEventListener('click', this.onOutsideClick, true);
this.forcePopperUpdate();
}
}
/* destroy popperjs */
destroyPopperInstance() {
if (this.popperInstance) {
document.removeEventListener('click', this.onOutsideClick, true);
this.popperInstance.destroy();
this.popperInstance = null;
// remove this instance from the stack
Stack._removeBy(this);
}
this.popperEl = null;
this.isDestroying = false;
}
forcePopperUpdate() {
setTimeout(() => {
// tell popper to relcalculate based on options in case it didn't do it properly the first time
if (this.popperInstance) {
this.popperInstance.update();
}
});
}
initResizer() {
if (this.resizerEl && !this.resizerEventAdded) {
if (!this.resizerEl.contentDocument) {
this.resizerEl.addEventListener('load', this.initResizer);
this.resizerEventAdded = null;
} else {
if (this.resizerEventAdded === null) {
this.resizerEl.removeEventListener('load', this.initResizer);
}
if (this.props._targetRef) {
this.lastTargetPos = this.props._targetRef.getBoundingClientRect();
}
this.positionPoller = setInterval(this.onCheckPosition, 50);
this.resizerEl.contentDocument.defaultView.addEventListener('resize', this.onResize);
this.resizerEventAdded = true;
}
}
}
destroyResizer() {
if (this.resizerEl && this.resizerEl.contentDocument && this.resizerEventAdded) {
this.resizerEl.contentDocument.defaultView.removeEventListener('resize', this.onResize);
}
if (this.positionPoller) {
clearInterval(this.positionPoller);
this.positionPoller = null;
}
this.resizerEventAdded = false;
}
/* sets the ref for the popperjs content so it can be bound. Also provide to user if they want it */
setPopperRef = (el) => {
this.popperEl = el;
if (this.props.innerRef) {
this.props.innerRef(el);
}
}
setResizerRef = (el) => {
this.resizerEl = el;
}
onCheckPosition = () => {
if (this.props._targetRef) {
let { left: lastLeft, top: lastTop } = this.lastTargetPos;
const thisTargetPos = this.props._targetRef.getBoundingClientRect();
const { left, top } = thisTargetPos;
if (lastLeft === 0 && lastTop === 0) {
lastLeft = left;
lastTop = top;
}
if (lastLeft !== left || lastTop !== top) {
if (this.targetParents && this.popperInstance) { // only update for nested poppers
this.popperInstance.update();
}
}
this.lastTargetPos = thisTargetPos;
}
}
/* any click outside the portal or popper should trigger an outside click */
onOutsideClick = (e) => {
if (!this.popperEl || !this.popperEl.contains(e.target)) {
this.props._onOutsideClick(this, e);
}
}
/* update popper position on content resize */
onResize = () => {
if (this.popperInstance) {
this.popperInstance.update();
}
}
render() {
if (!this.props._targetRef || !this.props._show) {
return null;
}
const popper = (
<div
className={this.props.className}
ref={this.setPopperRef}
onClick={this.props.onClick}
style={{ position: 'relative' }}
>
{this.renderResizerIframe()}
{this.props.children}
{this.props.includeArrow && (
<div className={this.props.arrowClassName} data-popper-arrow />
)}
</div>
);
if (this.props._usePortal) {
return (
<Portal
portalRoot={this.props._portalRoot}
className={this.props._portalClassName}
>
{popper}
</Portal>
);
}
return popper;
}
renderResizerIframe() {
return (
<iframe
src="about:blank"
ref={this.setResizerRef}
aria-hidden
tabIndex={-1}
frameBorder={0}
title="resizer"
style={{
display: 'block',
opacity: 0,
position: 'absolute',
top: 0,
left: 0,
height: '100%',
width: '100%',
overflow: 'hidden',
pointerEvents: 'none',
zIndex: -1,
}}
/>
);
}
}
// see docs for definitions of each external prop here
Content.propTypes = {
// external
arrowClassName: PropTypes.string,
className: PropTypes.string,
includeArrow: PropTypes.bool,
innerRef: PropTypes.func,
onClick: PropTypes.func,
popperOptions: PropTypes.object,
// internal - these can't be required otherwise will show error to end user
_onComponentWillDestroy: PropTypes.func,
_onOutsideClick: PropTypes.func,
_portalClassName: PropTypes.string,
_portalRoot: PropTypes.element,
_stack: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
_show: PropTypes.bool,
_targetRef: PropTypes.any,
_usePortal: PropTypes.bool,
};
Content.defaultProps = {
arrowClassName: '',
className: '',
includeArrow: false,
innerRef: (el) => {},
onClick: (e) => {},
popperOptions: {},
_onComponentWillDestroy: () => {},
_onOutsideClick: (instance, e) => {},
_portalClassName: '',
_portalRoot: null,
_stack: 'auto',
_show: false,
_targetRef: null,
_usePortal: true,
};