/
index.ts
40 lines (34 loc) · 832 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { useState } from 'react'
import { useEventListener } from '../useEventListener'
import { useMount } from '../useMount'
export interface WindowSizeOptions {
initialWidth?: number
initialHeight?: number
}
/**
* Reactive window size.
*
* @param options
*
* @see https://react-hooks-library.vercel.app/core/useWindowSize
*/
export function useWindowSize({
initialWidth = Infinity,
initialHeight = Infinity
}: WindowSizeOptions = {}) {
const [width, setWidth] = useState(initialWidth)
const [height, setHeight] = useState(initialHeight)
useMount(() => {
setWidth(window.innerWidth)
setHeight(window.innerHeight)
})
useEventListener(
'resize',
() => {
setWidth(window.innerWidth)
setHeight(window.innerHeight)
},
{ passive: true }
)
return { width, height }
}