UNPKG

@appbuckets/react-ui

Version:
40 lines (37 loc) 1.1 kB
import * as React from 'react'; /* -------- * Hook Definition * -------- */ function useWindowResize(config) { var disabled = config.disabled, onResize = config.onResize; // ---- // Hook is limited to a useEffect with event listener attached // ---- React.useEffect( function () { /** If hook has been disabled, return a noop */ if (disabled) { return function () { return null; }; } /** Build a well know function to be removed on effect clear */ function handleWindowResize() { if (typeof onResize === 'function') { onResize({ height: window.innerHeight, width: window.innerWidth }); } } /** Attach the event */ window.addEventListener('resize', handleWindowResize); /** Call the handler once to simulate first load */ handleWindowResize(); /** Return a function to clear event */ return function () { return window.removeEventListener('resize', handleWindowResize); }; }, [disabled, onResize] ); } export { useWindowResize };