Skip to content

Commit

Permalink
[fixed] Safety check for SSR (#668)
Browse files Browse the repository at this point in the history
- add missing test for string appElement
- add safety check for using setElement on ssr
- fix minor typo
  • Loading branch information
Lior Belinsky authored and diasbruno committed Jun 1, 2018
1 parent 5f92df7 commit 73893a2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
22 changes: 22 additions & 0 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,28 @@ export default () => {
ReactDOM.unmountComponentAtNode(node);
});

it("allow setting appElement of type string", () => {
const node = document.createElement("div");
class App extends Component {
render() {
return (
<div>
<Modal isOpen>
<span>hello</span>
</Modal>
</div>
);
}
}
const appElement = "body";
Modal.setAppElement(appElement);
ReactDOM.render(<App />, node);
document.body
.querySelector(".ReactModalPortal")
.parentNode.should.be.eql(document.body);
ReactDOM.unmountComponentAtNode(node);
});

it("default parentSelector should be document.body.", () => {
const modal = renderModal({ isOpen: true });
modal.props.parentSelector().should.be.eql(document.body);
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/ariaAppHider.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import warning from "warning";
import { canUseDOM } from "./safeHTMLElement";

let globalElement = null;

Expand All @@ -12,7 +13,7 @@ export function assertNodeList(nodeList, selector) {

export function setElement(element) {
let useElement = element;
if (typeof useElement === "string") {
if (typeof useElement === "string" && canUseDOM) {
const el = document.querySelectorAll(useElement);
assertNodeList(el, useElement);
useElement = "length" in el ? el[0] : el;
Expand Down

0 comments on commit 73893a2

Please sign in to comment.