manifold/web/hooks/use-window-size.ts

21 lines
508 B
TypeScript
Raw Normal View History

import { useEffect, useState } from 'react'
export const useWindowSize = () => {
2022-01-18 23:10:21 +00:00
const [size, setSize] = useState<{
width: number | undefined
height: number | undefined
}>({ width: undefined, height: undefined })
useEffect(() => {
const onResize = () => {
setSize({ width: window.innerWidth, height: window.innerHeight })
}
2022-01-18 23:10:21 +00:00
onResize()
window.addEventListener('resize', onResize)
return () => window.removeEventListener('resize', onResize)
}, [])
return size
}