Skip to content

Commit

Permalink
Make all HOCs node-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
dpikt committed Jan 2, 2020
1 parent 9256b2c commit 77d41b7
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 5 deletions.
10 changes: 8 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module.exports = {
'extends': '@launchpadlab/eslint-config/react'
}
extends: '@launchpadlab/eslint-config/react',
// Enforce isomorphic code (node and browser)
env: {
browser: false,
node: false,
'shared-node-browser': true,
},
}
3 changes: 3 additions & 0 deletions src/connectQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
noop,
parse,
wrapDisplayName,
isServer,
} from './utils'

/**
Expand Down Expand Up @@ -49,6 +50,8 @@ import {

function connectQuery (mappingConfig = noop, { camelize = true } = {}) {
function modify (props) {
if (isServer()) return
// eslint-disable-next-line no-undef
const query = parse(window.location.search)
const casedQuery = camelize ? camelizeKeys(query) : query
const primitiveQuery = mapValues(casedQuery, coerce)
Expand Down
10 changes: 8 additions & 2 deletions src/modal.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
// import PropTypes from 'prop-types'
import { connectModal, show, hide, destroy } from 'redux-modal'
import { isEvent } from './utils'
import { isEvent, isServer } from './utils'
import Modal from 'react-modal'

/**
Expand Down Expand Up @@ -60,6 +60,12 @@ import Modal from 'react-modal'
* )(Layout)
*/

function getRootElement () {
if (isServer()) return
// eslint-disable-next-line no-undef
return document.querySelector('#root')
}

// Default modal wrapper.
function DefaultModalComponent({ show, handleHide, ...rest }) {
return (
Expand All @@ -70,7 +76,7 @@ function DefaultModalComponent({ show, handleHide, ...rest }) {
className="modal-inner"
overlayClassName="modal-fade-screen"
bodyOpenClassName="modal-open"
appElement={document.querySelector('#root')}
appElement={ getRootElement() }
{...rest}
/>
)
Expand Down
6 changes: 5 additions & 1 deletion src/onOutsideClick.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { callWithProps, wrapDisplayName } from './utils'
import { callWithProps, wrapDisplayName, isServer } from './utils'

/**
* A function that returns a React HOC to handle logic to be run when a click occurs outside of a component.
Expand Down Expand Up @@ -51,13 +51,17 @@ export default function onOutsideClick (handler) {
* Bind click handler to document on mount
*/
componentDidMount () {
if (isServer()) return
// eslint-disable-next-line no-undef
document.addEventListener('click', this.handleClickOutside)
}

/*
* Unbind click handler from document on unmount
*/
componentWillUnmount () {
if (isServer()) return
// eslint-disable-next-line no-undef
document.removeEventListener('click', this.handleClickOutside)
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils/getEnvVar.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { get } from 'lodash/fp'
import isServer from './isServer'

// Attempts to retrieve an environment variable from process.env

function getEnvVar (varName) {
// On the server, retrieve vars from process
// eslint-disable-next-line no-undef
if (isServer()) return get(`env.${ varName }`, process)
// Otherwise, retrieve vars from window.process
// eslint-disable-next-line no-undef
return get(`process.env.${ varName }`, window)
}

Expand Down
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ export createMapperFunc from './createMapperFunc'
export isEvent from './isEvent'
export isImage from './isImage'
export isPdf from './isPdf'
export isServer from './isServer'
export { createRoutesFromReactChildren } from './routeUtils'
7 changes: 7 additions & 0 deletions src/utils/isServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Returns true when code is running in server mode

function isServer() {
return typeof window === 'undefined'
}

export default isServer

0 comments on commit 77d41b7

Please sign in to comment.