Skip to content

Commit 7f18dc1

Browse files
authored
Merge pull request #13 from mbrookson/bugfix/portal-ssr
Check for window and document to stop SSR crashing
2 parents 9991843 + dfcb429 commit 7f18dc1

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.0.0",
2+
"version": "1.0.1",
33
"license": "MIT",
44
"name": "react-simple-hook-modal",
55
"description": "A simple React modal with hook based API",

src/components/Modal.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@ export interface ModalProps {
1212
transition?: ModalTransition;
1313
}
1414

15+
function hasDOM() {
16+
return !!(
17+
typeof window !== 'undefined' &&
18+
window.document &&
19+
window.document.createElement
20+
);
21+
}
22+
1523
export const Modal: React.FC<ModalProps> = modal => {
16-
const container = document.getElementById('react-simple-modal-container');
1724
const { addOrUpdate, remove, getStaggerPixels } = useModalContext();
1825
const { id, isOpen } = modal;
1926

27+
const container = hasDOM()
28+
? document.getElementById('react-simple-modal-container')
29+
: null;
30+
2031
useEffect(() => {
2132
isOpen ? addOrUpdate(id) : remove(id);
2233
return () => remove(id);

0 commit comments

Comments
 (0)