Return

All tests / hooks useWindowResize.ts

100% Statements 13/13
100% Branches 4/4
100% Functions 4/4
100% Lines 12/12

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 511x 1x                       6x               6x 3x 3x               3x   3x   3x 3x       6x                 6x  
import React from 'react';
import { mobileMax, tabletMax, tabletMin, desktopMin } from '@/constants/devices';
 
type WindowTpe = {
    isMobile: boolean;
    isTablet: boolean;
    isMobileOrTablet: boolean;
    isDesktop: boolean;
    width: number;
    height: number;
};
 
function useWindowResize(): WindowTpe {
    const [widowSize, setWindowSize] = React.useState({
        isMobile: false,
        isTablet: false,
        isDesktop: false,
        width: 0,
        height: 0,
    });
 
    React.useEffect(() => {
        const handleResize = () => {
            setWindowSize({
                isMobile: window.innerWidth <= mobileMax,
                isTablet: window.innerWidth <= tabletMax && window.innerWidth >= tabletMin,
                isDesktop: window.innerWidth >= desktopMin,
                width: window.innerWidth,
                height: window.innerHeight,
            });
        };
        window.addEventListener('resize', handleResize);
 
        handleResize();
 
        return () => {
            window.removeEventListener('resize', handleResize);
        };
    }, []);
 
    return {
        isMobile: widowSize.isMobile,
        isTablet: widowSize.isTablet,
        isMobileOrTablet: widowSize.isMobile || widowSize.isTablet,
        isDesktop: widowSize.isDesktop,
        width: widowSize.width,
        height: widowSize.height,
    };
}
export default useWindowResize;