@react-hook/window-size
Version:
React hooks for updating components when the size of the `window` changes.
1 lines • 2.37 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["../../src/index.tsx"],"sourcesContent":["import {useDebounce} from '@react-hook/debounce'\nimport useEvent from '@react-hook/event'\n\nconst emptyObj = {}\n\nexport interface DebouncedWindowSizeOptions {\n initialWidth?: number\n initialHeight?: number\n wait?: number\n leading?: boolean\n}\n\nconst win = typeof window === 'undefined' ? null : window\nconst wv =\n win && typeof win.visualViewport !== 'undefined' ? win.visualViewport : null\nconst getSize = () =>\n [\n document.documentElement.clientWidth,\n document.documentElement.clientHeight,\n ] as const\n\nexport const useWindowSize = (\n options: DebouncedWindowSizeOptions = emptyObj\n): readonly [number, number] => {\n const {wait, leading, initialWidth = 0, initialHeight = 0} = options\n const [size, setDebouncedSize] = useDebounce<readonly [number, number]>(\n /* istanbul ignore next */\n typeof document === 'undefined' ? [initialWidth, initialHeight] : getSize,\n wait,\n leading\n )\n const setSize = (): void => setDebouncedSize(getSize)\n\n useEvent(win, 'resize', setSize)\n // @ts-expect-error\n useEvent(wv, 'resize', setSize)\n useEvent(win, 'orientationchange', setSize)\n\n return size\n}\n\nexport const useWindowHeight = (\n options?: Omit<DebouncedWindowSizeOptions, 'initialWidth'>\n): number => useWindowSize(options)[1]\n\nexport const useWindowWidth = (\n options?: Omit<DebouncedWindowSizeOptions, 'initialHeight'>\n): number => useWindowSize(options)[0]\n"],"names":["emptyObj","win","window","wv","visualViewport","getSize","document","documentElement","clientWidth","clientHeight","useWindowSize","options","wait","leading","initialWidth","initialHeight","size","setDebouncedSize","useDebounce","setSize","useEvent","useWindowHeight","useWindowWidth"],"mappings":"oFAGA,IAAMA,EAAW,CAAA,EASXC,EAAwB,oBAAXC,OAAyB,KAAOA,OAC7CC,EACJF,QAAqC,IAAvBA,EAAIG,eAAiCH,EAAIG,eAAiB,KACpEC,EAAU,IACd,CACEC,SAASC,gBAAgBC,YACzBF,SAASC,gBAAgBE,cAGhBC,EAAgB,SAC3BC,QAC8B,IAD9BA,IAAAA,EAAsCX,GAEtC,IAAMY,KAACA,EAADC,QAAOA,EAAPC,aAAgBA,EAAe,EAA/BC,cAAkCA,EAAgB,GAAKJ,GACtDK,EAAMC,GAAoBC,EAEX,oBAAbZ,SAA2B,CAACQ,EAAcC,GAAiBV,EAClEO,EACAC,GAEIM,EAAU,IAAYF,EAAiBZ,GAO7C,OALAe,EAASnB,EAAK,SAAUkB,GAExBC,EAASjB,EAAI,SAAUgB,GACvBC,EAASnB,EAAK,oBAAqBkB,GAE5BH,CACR,EAEYK,EACXV,GACWD,EAAcC,GAAS,GAEvBW,EACXX,GACWD,EAAcC,GAAS"}