/
TetherComponent.jsx
274 lines (228 loc) · 6.13 KB
/
TetherComponent.jsx
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
import { Component, Children } from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import Tether from 'tether';
if (!Tether) {
console.error(
'It looks like Tether has not been included. Please load this dependency first https://github.com/HubSpot/tether'
);
}
const hasCreatePortal = ReactDOM.createPortal !== undefined;
const renderElementToPropTypes = [
PropTypes.string,
PropTypes.shape({
appendChild: PropTypes.func.isRequired,
}),
];
const childrenPropType = ({ children }, propName, componentName) => {
const childCount = Children.count(children);
if (childCount <= 0) {
return new Error(
`${componentName} expects at least one child to use as the target element.`
);
}
if (childCount > 2) {
return new Error(`Only a max of two children allowed in ${componentName}.`);
}
};
const attachmentPositions = [
'auto auto',
'top left',
'top center',
'top right',
'middle left',
'middle center',
'middle right',
'bottom left',
'bottom center',
'bottom right',
];
class TetherComponent extends Component {
static propTypes = {
renderElementTag: PropTypes.string,
renderElementTo: PropTypes.oneOfType(renderElementToPropTypes),
attachment: PropTypes.oneOf(attachmentPositions).isRequired,
targetAttachment: PropTypes.oneOf(attachmentPositions),
offset: PropTypes.string,
targetOffset: PropTypes.string,
targetModifier: PropTypes.string,
enabled: PropTypes.bool,
classes: PropTypes.object,
classPrefix: PropTypes.string,
optimizations: PropTypes.object,
constraints: PropTypes.array,
id: PropTypes.string,
className: PropTypes.string,
style: PropTypes.object,
onUpdate: PropTypes.func,
onRepositioned: PropTypes.func,
children: childrenPropType,
};
static defaultProps = {
renderElementTag: 'div',
renderElementTo: null,
};
_targetNode = null;
_elementParentNode = null;
_tether = null;
constructor(props) {
super(props);
const elementComponent = Children.toArray(props.children)[1];
if (elementComponent) {
this._createContainer();
}
}
componentWillUpdate({ children }) {
const elementComponent = Children.toArray(children)[1];
if (elementComponent) {
this._createContainer();
}
}
componentDidMount() {
this._targetNode = ReactDOM.findDOMNode(this);
this._update();
}
componentDidUpdate() {
this._targetNode = ReactDOM.findDOMNode(this);
this._update();
}
componentWillUnmount() {
this._destroy();
}
getTetherInstance() {
return this._tether;
}
disable() {
this._tether.disable();
}
enable() {
this._tether.enable();
}
on(event, handler, ctx) {
this._tether.on(event, handler, ctx);
}
once(event, handler, ctx) {
this._tether.once(event, handler, ctx);
}
off(event, handler) {
this._tether.off(event, handler);
}
position() {
this._tether.position();
}
_registerEventListeners() {
this.on('update', () => {
return this.props.onUpdate && this.props.onUpdate.apply(this, arguments);
});
this.on('repositioned', () => {
return (
this.props.onRepositioned &&
this.props.onRepositioned.apply(this, arguments)
);
});
}
get _renderNode() {
const { renderElementTo } = this.props;
if (typeof renderElementTo === 'string') {
return document.querySelector(renderElementTo);
}
return renderElementTo || document.body;
}
_destroy() {
if (this._elementParentNode) {
if (!hasCreatePortal) {
ReactDOM.unmountComponentAtNode(this._elementParentNode);
}
this._elementParentNode.parentNode.removeChild(this._elementParentNode);
}
if (this._tether) {
this._tether.destroy();
}
this._elementParentNode = null;
this._tether = null;
}
_createContainer() {
const { renderElementTag } = this.props;
// Create element node container if it hasn't been yet
if (!this._elementParentNode) {
// Create a node that we can stick our content Component in
this._elementParentNode = document.createElement(renderElementTag);
// Append node to the render node
this._renderNode.appendChild(this._elementParentNode);
}
}
_update() {
const { children } = this.props;
const elementComponent = Children.toArray(children)[1];
// If no element component provided, bail out
if (!elementComponent) {
// Destroy Tether element if it has been created
if (this._tether) {
this._destroy();
}
return;
}
if (hasCreatePortal) {
this._updateTether();
} else {
// Render element component into the DOM
ReactDOM.unstable_renderSubtreeIntoContainer(
this,
elementComponent,
this._elementParentNode,
() => {
// If we're not destroyed, update Tether once the subtree has finished rendering
if (this._elementParentNode) {
this._updateTether();
}
}
);
}
}
_updateTether() {
const {
children,
renderElementTag,
renderElementTo,
id,
className,
style,
...options
} = this.props;
const tetherOptions = {
target: this._targetNode,
element: this._elementParentNode,
...options,
};
if (id) {
this._elementParentNode.id = id;
}
if (className) {
this._elementParentNode.className = className;
}
if (style) {
Object.keys(style).forEach(key => {
this._elementParentNode.style[key] = style[key];
});
}
if (this._tether) {
this._tether.setOptions(tetherOptions);
} else {
this._tether = new Tether(tetherOptions);
this._registerEventListeners();
}
this._tether.position();
}
render() {
const { children } = this.props;
const elementComponent = Children.toArray(children)[1];
if (!hasCreatePortal || !elementComponent) {
return Children.toArray(children)[0];
}
return [
Children.toArray(children)[0],
ReactDOM.createPortal(elementComponent, this._elementParentNode),
];
}
}
export default TetherComponent;