2022-01-28 20:03:06 +05:00
|
|
|
import { useEffect } from 'react';
|
2022-01-28 20:42:02 +05:00
|
|
|
import debounce from 'util/debounce';
|
2022-01-28 20:03:06 +05:00
|
|
|
|
|
|
|
export function useOnResize(cb) {
|
2022-01-31 17:34:44 +05:00
|
|
|
const isWindowClient = typeof window === 'object';
|
|
|
|
|
2022-01-28 20:03:06 +05:00
|
|
|
useEffect(() => {
|
2022-01-28 20:42:02 +05:00
|
|
|
const handleResize = debounce(cb, 100);
|
2022-01-28 20:03:06 +05:00
|
|
|
|
2022-01-31 17:34:44 +05:00
|
|
|
if (isWindowClient) {
|
|
|
|
window.addEventListener('resize', handleResize);
|
2022-01-28 20:03:06 +05:00
|
|
|
|
2022-01-31 17:34:44 +05:00
|
|
|
handleResize();
|
2022-01-28 20:03:06 +05:00
|
|
|
|
2022-01-31 17:34:44 +05:00
|
|
|
return () => window.removeEventListener('resize', handleResize);
|
|
|
|
}
|
|
|
|
}, [isWindowClient]);
|
2022-01-28 20:03:06 +05:00
|
|
|
}
|