Skip to content

Commit

Permalink
fix(docz): add window check to useWindowSize hook (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Morozov authored and pedronauck committed Mar 12, 2019
1 parent 1274b97 commit e4d7cb4
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions core/docz/src/hooks/useWindowSize.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import { useState, useEffect } from 'react'
import { throttle } from 'lodash/fp'

const getSize = () => ({
innerHeight: window.innerHeight,
innerWidth: window.innerWidth,
outerHeight: window.outerHeight,
outerWidth: window.outerWidth,
const isClient = typeof window === 'object'

const getSize = (initialWidth: number, initialHeight: number) => ({
innerHeight: isClient ? window.innerHeight : initialHeight,
innerWidth: isClient ? window.innerWidth : initialWidth,
outerHeight: isClient ? window.outerHeight : initialHeight,
outerWidth: isClient ? window.outerWidth : initialWidth,
})

export const useWindowSize = () => {
const [windowSize, setWindowSize] = useState(getSize())
const tSetWindowResize = throttle(300, () => setWindowSize(getSize()))
export const useWindowSize = (
throttleMs: number = 300,
initialWidth = Infinity,
initialHeight = Infinity
) => {
const [windowSize, setWindowSize] = useState(
getSize(initialHeight, initialHeight)
)
const tSetWindowResize = throttle(throttleMs, () =>
setWindowSize(getSize(initialHeight, initialHeight))
)

useEffect(() => {
window.addEventListener('resize', tSetWindowResize)
Expand Down

0 comments on commit e4d7cb4

Please sign in to comment.