UNPKG

@appbuckets/react-ui

Version:
72 lines (65 loc) 1.82 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var React = require('react'); function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty( n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; }, } ); } }); } n['default'] = e; return Object.freeze(n); } var React__namespace = /*#__PURE__*/ _interopNamespace(React); /* -------- * Hook Definition * -------- */ function useWindowResize(config) { var disabled = config.disabled, onResize = config.onResize; // ---- // Hook is limited to a useEffect with event listener attached // ---- React__namespace.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] ); } exports.useWindowResize = useWindowResize;