Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import {useState, useEffect} from 'react';
function throttle( callback ) {
let timeout;
if (timeout) {
cancelAnimationFrame(timeout);
}
timeout = requestAnimationFrame(function () {
callback(this.args)
});
}
export const useBreakpoint = ({breakpoints}) => {
const getBreakpoint = (width) => {
if ( window.innerWidth > breakpoints.xxl) {
return 'xxxl'
}
if ( window.innerWidth > breakpoints.xl) {
return 'xxl'
}
if ( window.innerWidth > breakpoints.lg) {
return 'xl'
}
if ( window.innerWidth > breakpoints.md) {
return 'lg'
}
if ( window.innerWidth > breakpoints.sm) {
return 'md'
}
if ( window.innerWidth > breakpoints.xs) {
return 'sm'
}
if ( window.innerWidth > breakpoints.xxs) {
return 'xs'
}
return 'xxs'
};
const [ breakpoint, setBreakpoint ] = useState(getBreakpoint(window.innerWidth));
const handleResize = () => {
setBreakpoint(getBreakpoint(window.innerWidth))
}
const throttledHander = throttle(handleResize);
useEffect(() => {
window.addEventListener('resize', throttledHander);
return () => window.removeEventListener('resize', throttledHander);
});
return { breakpoint };
};
export default useBreakpoint; |