1.1.22 • Published 4 years ago

use-window-size-hook v1.1.22

Weekly downloads
1,379
License
MIT
Repository
github
Last release
4 years ago

use-window-size-hook

React Hook to monitor window size & layout according to Bootstrap 4 breakpoints.

NPM JavaScript Style Guide

Install

npm install --save use-window-size-hook

or

yarn add use-window-size-hook

Usage

import React from 'react';

import { useWindowSize } from 'use-window-size-hook';

const App = () => {
  const {
    width,
    height,
    screenLayout,
  } = useWindowSize();

  return (
    <>
      <p>
        {`Window width: ${width}`}
      </p>

      <p>
        {`Window height: ${height}`}
      </p>

      <p>
        {`Screen layout according to Bootstrap 4: ${screenLayout.layout}`}
      </p>

      <p>
        {`Is md layout: ${screenLayout.isMd}`}
      </p>

      <p>
        {`Is xs layout or below: ${screenLayout.isXsOrBelow}`}
      </p>

      <p>
        {`Is lg layout or above: ${screenLayout.isLgOrAbove}`}
      </p>
    </>
  );
};

Compare layout breakpoints

import React from 'react';

import { useWindowSize, layout } from "use-window-size-hook";

const App = () => {
  const { screenLayout } = useWindowSize();

  const isBiggerThanMd = screenLayout > layout.md;

  return (
    <>
      <p>
        {isBiggerThanMd ? "Layout is bigger than md" : "Layout is md or smaller"}
      </p>
    </>
  );
};

Props

NameTypeRequiredDefault valueDescription
useDebouncebooleanoptionaltrueDefines if the callback is going to be executed when the user finishes resizing the screen or not
debounceTimeMsnumberoptional200Debounce time to check when the user finishes resizing the screen
breakpointsScreenBreakpointsoptionalCheck types belowDefines the breakpoints to be used, you can override and choose your own

Types

useWindowSize result

{
  width?: number;
  height?: number;
  screenLayout: {
    layout: xs | sm | md | lg | xl | undefined;

    isXs: boolean;
    isSm: boolean;
    isMd: boolean;
    isLg: boolean;
    isXl: boolean;

    isXsOrBelow: boolean;
    isSmOrBelow: boolean;
    isMdOrBelow: boolean;
    isLgOrBelow: boolean;
    isXlOrBelow: boolean;

    isXsOrAbove: boolean;
    isSmOrAbove: boolean;
    isMdOrAbove: boolean;
    isLgOrAbove: boolean;
    isXlOrAbove: boolean;
  };
}

Screen Breakpoints

{
  xs: number; // defaults to 0px
  sm: number; // defaults to 576px
  md: number; // defaults to 768px
  lg: number; // defaults to 992px
  xl: number; // defaults to 1200px
}

License

MIT © pedro-lb


This hook is created using create-react-hook.

1.1.22

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.1.2

4 years ago

1.1.21

4 years ago

1.0.61

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago