@react-hook/window-size
Version:
React hooks for updating components when the size of the `window` changes.
1 lines • 2.42 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["../../src/index.tsx"],"sourcesContent":["/* eslint-disable import/no-extraneous-dependencies */\nimport {useThrottle} from '@react-hook/throttle'\nimport useEvent from '@react-hook/event'\n\nconst emptyObj = {}\n\nexport interface ThrottledWindowSizeOptions {\n initialWidth?: number\n initialHeight?: number\n fps?: 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: ThrottledWindowSizeOptions = emptyObj\n): readonly [number, number] => {\n const {fps, leading, initialWidth = 0, initialHeight = 0} = options\n const [size, setThrottledSize] = useThrottle<readonly [number, number]>(\n /* istanbul ignore next */\n typeof document === 'undefined' ? [initialWidth, initialHeight] : getSize,\n fps,\n leading\n )\n const setSize = (): void => setThrottledSize(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<ThrottledWindowSizeOptions, 'initialWidth'>\n): number => useWindowSize(options)[1]\n\nexport const useWindowWidth = (\n options?: Omit<ThrottledWindowSizeOptions, 'initialHeight'>\n): number => useWindowSize(options)[0]\n"],"names":["emptyObj","win","window","wv","visualViewport","getSize","document","documentElement","clientWidth","clientHeight","useWindowSize","options","fps","leading","initialWidth","initialHeight","size","setThrottledSize","useThrottle","setSize","useEvent","useWindowHeight","useWindowWidth"],"mappings":"oFAIA,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,IAACA,EAADC,QAAMA,EAANC,aAAeA,EAAe,EAA9BC,cAAiCA,EAAgB,GAAKJ,GACrDK,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"}