From 73893a247dcb87f9928183f81b3221b7bcf0d16c Mon Sep 17 00:00:00 2001 From: Lior Belinsky Date: Fri, 1 Jun 2018 16:49:35 +0300 Subject: [PATCH] [fixed] Safety check for SSR (#668) - add missing test for string appElement - add safety check for using setElement on ssr - fix minor typo --- specs/Modal.spec.js | 22 ++++++++++++++++++++++ src/helpers/ariaAppHider.js | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/specs/Modal.spec.js b/specs/Modal.spec.js index d96196f1..ff8d7056 100644 --- a/specs/Modal.spec.js +++ b/specs/Modal.spec.js @@ -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 ( +
+ + hello + +
+ ); + } + } + const appElement = "body"; + Modal.setAppElement(appElement); + ReactDOM.render(, 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); diff --git a/src/helpers/ariaAppHider.js b/src/helpers/ariaAppHider.js index 36d02e77..017b17db 100644 --- a/src/helpers/ariaAppHider.js +++ b/src/helpers/ariaAppHider.js @@ -1,4 +1,5 @@ import warning from "warning"; +import { canUseDOM } from "./safeHTMLElement"; let globalElement = null; @@ -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;