forked from react-bootstrap/react-bootstrap-bower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverlayMixin.js
75 lines (62 loc) · 2.03 KB
/
OverlayMixin.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
define(function (require, exports, module) {var React = require('react');
var CustomPropTypes = require('./utils/CustomPropTypes');
module.exports = {
propTypes: {
container: CustomPropTypes.mountable
},
getDefaultProps: function () {
return {
container: {
// Provide `getDOMNode` fn mocking a React component API. The `document.body`
// reference needs to be contained within this function so that it is not accessed
// in environments where it would not be defined, e.g. nodejs. Equally this is needed
// before the body is defined where `document.body === null`, this ensures
// `document.body` is only accessed after componentDidMount.
getDOMNode: function getDOMNode() {
return document.body;
}
}
};
},
componentWillUnmount: function () {
this._unrenderOverlay();
if (this._overlayTarget) {
this.getContainerDOMNode()
.removeChild(this._overlayTarget);
this._overlayTarget = null;
}
},
componentDidUpdate: function () {
this._renderOverlay();
},
componentDidMount: function () {
this._renderOverlay();
},
_mountOverlayTarget: function () {
this._overlayTarget = document.createElement('div');
this.getContainerDOMNode()
.appendChild(this._overlayTarget);
},
_renderOverlay: function () {
if (!this._overlayTarget) {
this._mountOverlayTarget();
}
// Save reference to help testing
this._overlayInstance = React.render(this.renderOverlay(), this._overlayTarget);
},
_unrenderOverlay: function () {
React.unmountComponentAtNode(this._overlayTarget);
this._overlayInstance = null;
},
getOverlayDOMNode: function () {
if (!this.isMounted()) {
throw new Error('getOverlayDOMNode(): A component must be mounted to have a DOM node.');
}
return this._overlayInstance.getDOMNode();
},
getContainerDOMNode: function () {
return this.props.container.getDOMNode ?
this.props.container.getDOMNode() : this.props.container;
}
};
});