@rws-air/utils
Version:
Utilities for rws-air libraries and applications
37 lines • 1.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWindowSize = void 0;
const react_1 = require("react");
/**
* React hook to get the current window size
* Triggers on window resize.
*
* Use with {@link https://github.com/xnimorz/use-debounce | use-debounce} to ensure it is not triggered instantly!!
*
* @example
* ```typescript
* import { useDebounce } from 'use-debounce';
*
* const [ width, height ] = useDebounce(useWindowSize(), 1000);
*
* useEffect(() => {
* // Do something here
* }, [ width, height ]);
* ```
*/
function useWindowSize() {
const getSize = () => {
return [window.innerWidth, window.innerHeight];
};
const [windowSize, setWindowSize] = (0, react_1.useState)(getSize);
(0, react_1.useEffect)(() => {
function handleResize() {
setWindowSize(getSize());
}
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, []);
return windowSize;
}
exports.useWindowSize = useWindowSize;
//# sourceMappingURL=UseWindowSize.js.map