Download hooks/useWindowSize.ts from sandy-try/blog: direct link, hf CLI and curl.
- Browser
- Download file 672 Bytes
-
https://huggingface.co/spaces/sandy-try/blog/resolve/main/hooks/useWindowSize.ts
- Command line
-
hf download hf://spaces/sandy-try/blog/hooks/useWindowSize.ts
-
curl -L -o useWindowSize.ts https://huggingface.co/spaces/sandy-try/blog/resolve/main/hooks/useWindowSize.ts
672 Bytes
| import { useEffect, useState } from 'react' | |
| interface WindowSize { | |
| width: number, | |
| height: number | |
| } | |
| const useWindowSize = () => { | |
| const [size, setSize] = useState<WindowSize>({ | |
| width: document.documentElement.clientWidth, | |
| height: document.documentElement.clientHeight | |
| }) | |
| useEffect(() => { | |
| const onResize = () => { | |
| setSize({ | |
| width: document.documentElement.clientWidth, | |
| height: document.documentElement.clientHeight | |
| }) | |
| } | |
| onResize() | |
| window.addEventListener('resize', onResize) | |
| return () => { | |
| window.removeEventListener('resize', onResize) | |
| } | |
| }, []) | |
| return size | |
| } | |
| export default useWindowSize | |