Skip to content

Commit

Permalink
feat: add support for server in useDimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jpudysz committed Oct 25, 2023
1 parent 3dbf069 commit 037c95e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/hooks/useDimensions.web.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { useEffect, useRef, useState } from 'react'
import type { ScreenSize } from '../types'
import { isServer } from '../utils'

export const useDimensions = (): ScreenSize => {
if (isServer()) {
return {
width: 0,
height: 0
}
}

const timerRef = useRef<ReturnType<typeof setTimeout>>()
const [screenSize, setScreenSize] = useState<ScreenSize>({
width: window.innerWidth,
Expand Down
1 change: 1 addition & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export const warn = (message: string) => {
}

export const isWeb = () => Platform.OS === 'web'
export const isServer = () => typeof window === 'undefined'
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { normalizeStyles } from './normalizeStyles'
export * from './normalizer'
export { getBreakpointFromScreenWidth, sortAndValidateBreakpoints, getValueForBreakpoint } from './breakpoints'
export { proxifyFunction, parseStyle } from './styles'
export { isServer } from './common'
export {
extractValues,
getKeyForCustomMediaQuery,
Expand Down

0 comments on commit 037c95e

Please sign in to comment.