digitalfabrik/integreat-app

View on GitHub
web/src/hooks/useWindowDimensions.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { useState, useEffect } from 'react'

import dimensions from '../constants/dimensions'

type WindowDimensionsType = { width: number; height: number; viewportSmall: boolean }

const getWindowDimensions = (): WindowDimensionsType => {
  const { innerWidth: width, innerHeight: height } = window
  return {
    width,
    height,
    viewportSmall: width <= dimensions.maxWidthViewportSmall,
  }
}

const useWindowDimensions = (): WindowDimensionsType => {
  const [windowDimensions, setWindowDimensions] = useState(getWindowDimensions())

  useEffect(() => {
    const handleResize = () => {
      setWindowDimensions(getWindowDimensions())
    }

    window.addEventListener('resize', handleResize)
    return () => window.removeEventListener('resize', handleResize)
  }, [])

  return windowDimensions
}

export default useWindowDimensions