Skip to content

Commit

Permalink
fix(Search): avoid window/document in ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
levithomason committed Nov 7, 2016
1 parent 1bce6f2 commit 0d8194c
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/modules/Search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
customPropTypes,
getElementType,
getUnhandledProps,
isBrowser,
keyboardKey,
makeDebugger,
META,
Expand Down Expand Up @@ -191,6 +192,9 @@ export default class Search extends Component {
// TODO objectDiff still runs in prod, stop it
debug('to state:', objectDiff(prevState, this.state))

// Do not access document when server side rendering
if (!isBrowser) return

// focused / blurred
if (!prevState.focus && this.state.focus) {
debug('search focused')
Expand Down Expand Up @@ -232,6 +236,10 @@ export default class Search extends Component {

componentWillUnmount() {
debug('componentWillUnmount()')

// Do not access document when server side rendering
if (!isBrowser) return

document.removeEventListener('keydown', this.moveSelectionOnKeyDown)
document.removeEventListener('keydown', this.selectItemOnEnter)
document.removeEventListener('keydown', this.closeOnEscape)
Expand Down Expand Up @@ -306,12 +314,16 @@ export default class Search extends Component {
const { onMouseDown } = this.props
if (onMouseDown) onMouseDown(e)
this.isMouseDown = true
// Do not access document when server side rendering
if (!isBrowser) return
document.addEventListener('mouseup', this.handleDocumentMouseUp)
}

handleDocumentMouseUp = () => {
debug('handleDocumentMouseUp()')
this.isMouseDown = false
// Do not access document when server side rendering
if (!isBrowser) return
document.removeEventListener('mouseup', this.handleDocumentMouseUp)
}

Expand Down Expand Up @@ -431,6 +443,8 @@ export default class Search extends Component {

scrollSelectedItemIntoView = () => {
debug('scrollSelectedItemIntoView()')
// Do not access document when server side rendering
if (!isBrowser) return
const menu = document.querySelector('.ui.search.active.visible .results.visible')
const item = menu.querySelector('.result.active')
debug(`menu (results): ${menu}`)
Expand Down Expand Up @@ -494,7 +508,7 @@ export default class Search extends Component {
<div className='message empty'>
<div className='header'>{noResultsMessage}</div>
{noResultsDescription &&
<div className='description'>{noResultsDescription}</div>
<div className='description'>{noResultsDescription}</div>
}
</div>
)
Expand Down

0 comments on commit 0d8194c

Please sign in to comment.