UNPKG

@workday/canvas-kit-react

Version:

The parent module that contains all Workday Canvas Kit React components

25 lines (24 loc) 806 B
import React from 'react'; import { useMount } from './useMount'; const getFromWindow = (property, defaultValue) => { var _a; if (typeof window !== undefined) { return (_a = window[property]) !== null && _a !== void 0 ? _a : defaultValue; } return defaultValue; }; export const useWindowSize = () => { const [width, setWidth] = React.useState(getFromWindow('innerWidth', 0)); const [height, setHeight] = React.useState(getFromWindow('innerHeight', 0)); const onResize = () => { setWidth(window.innerWidth); setHeight(window.innerHeight); }; useMount(() => { window.addEventListener('resize', onResize); return () => { window.removeEventListener('resize', onResize); }; }); return { width, height }; };