# All files / src/_hooks useIsMobile.ts

0% Statements 0/18
0% Branches 0/6
0% Functions 0/5
0% Lines 0/16

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

     import * as React from 'react';

    declare const window: any;

    export const useIsMobile = (enableDrawer: boolean | number) => {
      const [isMobile, setIsMobile] = React.useState(checkMobile(enableDrawer));

      const updateLayout = React.useCallback(() => {
        setIsMobile(checkMobile(enableDrawer));
      }, []);

      React.useEffect(() => {
        window.addEventListener('resize', updateLayout);
        return () => {
          window.removeEventListener('resize', updateLayout);
        };
      }, [updateLayout]);

      return isMobile;
    };

    export const checkMobile = (enableDrawer: boolean | number) => {
      if (enableDrawer === true) {
        enableDrawer = 768;
      } else if (enableDrawer === false) {
        return false;
      }

      return typeof window !== 'undefined' && window.innerWidth < enableDrawer;
    };