Skip to content

Commit 7faa32a

Browse files
virgofxTheSharpieOne
authored andcommitted
fix(Modal): Ensure that this._element exists before removing it. (reactstrap#916)
fix(Build): Deprecate `react-portal` which includes extra code and fix portal references to use ReactDOM directly. Update deps and minimum stability now React16. fix(Modal): Ensure that `this._element` exists before removing it. Fixes reactstrap#918. Breaking Change: reactstrap now requires a minimum react version of 16. Please update your version of react to 16 (from 15 to 16 is very seamless).
1 parent 113e6d3 commit 7faa32a

5 files changed

Lines changed: 51 additions & 12 deletions

File tree

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,11 @@
9494
"lodash.tonumber": "^4.0.3",
9595
"prop-types": "^15.5.8",
9696
"react-popper": "^0.8.3",
97-
"react-portal": "^4.1.2",
9897
"react-transition-group": "^2.2.1"
9998
},
10099
"peerDependencies": {
101-
"react": "^0.14.9 || ^15.3.0 || ^16.0.0",
102-
"react-dom": "^0.14.9 || ^15.3.0 || ^16.0.0"
100+
"react": "^16.0.0",
101+
"react-dom": "^16.0.0"
103102
},
104103
"devDependencies": {
105104
"babel-cli": "^6.14.0",

src/Modal.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import { Portal } from 'react-portal';
43
import classNames from 'classnames';
4+
import Portal from './Portal';
55
import Fade from './Fade';
66
import {
77
getOriginalBodyPadding,
@@ -191,8 +191,10 @@ class Modal extends React.Component {
191191
}
192192

193193
destroy() {
194-
document.body.removeChild(this._element);
195-
this._element = null;
194+
if (this._element) {
195+
document.body.removeChild(this._element);
196+
this._element = null;
197+
}
196198

197199
const modalOpenClassName = mapToCssModules('modal-open', this.props.cssModule);
198200
// Use regex to prevent matching `modal-open` as part of a different class, e.g. `my-modal-opened`

src/Portal.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import PropTypes from 'prop-types';
4+
import { canUseDOM } from './utils';
5+
6+
const propTypes = {
7+
children: PropTypes.node.isRequired,
8+
node: PropTypes.any
9+
};
10+
11+
class Portal extends React.Component {
12+
componentWillUnmount() {
13+
if (this.defaultNode) {
14+
document.body.removeChild(this.defaultNode);
15+
}
16+
this.defaultNode = null;
17+
}
18+
19+
render() {
20+
if (!canUseDOM) {
21+
return null;
22+
}
23+
24+
if (!this.props.node && !this.defaultNode) {
25+
this.defaultNode = document.createElement('div');
26+
document.body.appendChild(this.defaultNode);
27+
}
28+
29+
return ReactDOM.createPortal(
30+
this.props.children,
31+
this.props.node || this.defaultNode
32+
);
33+
}
34+
}
35+
36+
Portal.propTypes = propTypes;
37+
38+
export default Portal;

src/utils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,9 @@ export const PopperPlacements = [
199199
'left',
200200
'left-start',
201201
];
202+
203+
export const canUseDOM = !!(
204+
typeof window !== 'undefined' &&
205+
window.document &&
206+
window.document.createElement
207+
);

yarn.lock

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6659,12 +6659,6 @@ react-popper@^0.8.3:
66596659
popper.js "^1.12.9"
66606660
prop-types "^15.6.0"
66616661

6662-
react-portal@^4.1.2:
6663-
version "4.1.2"
6664-
resolved "https://registry.yarnpkg.com/react-portal/-/react-portal-4.1.2.tgz#7f28f3c8c2ed5c541907c0ed0f24e8996acf627f"
6665-
dependencies:
6666-
prop-types "^15.5.8"
6667-
66686662
react-prism@^4.3.2:
66696663
version "4.3.2"
66706664
resolved "https://registry.yarnpkg.com/react-prism/-/react-prism-4.3.2.tgz#5c07609539b3ba6f45eb33e7d6a3588df3270c21"

0 commit comments

Comments
 (0)