# use-window-size-hook

> React Hook to monitor window size & layout.

Latest version **1.1.22** (published 2020-06-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-window-size-hook
pnpm add use-window-size-hook
yarn add use-window-size-hook
bun add use-window-size-hook
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.22 |
| Published | 2020-06-26 |
| First published | 2019-10-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=8 |
| Dependencies | 0 |
| Unpacked size | 81.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | pedro-lb |
| Maintainers | pedrobini |
| Keywords | hooks, react, react-hook, window-size, size |

## Links

- npm: https://www.npmjs.com/package/use-window-size-hook
- Repository: https://github.com/pedro-lb/use-window-size-hook
- Homepage: https://github.com/pedro-lb/use-window-size-hook#readme
- Issues: https://github.com/pedro-lb/use-window-size-hook/issues
- npm.io page: https://npm.io/package/use-window-size-hook

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.22 (latest) — 2020-06-26
- 1.1.21 — 2020-06-16
- 1.1.2 — 2020-06-16
- 1.1.1 — 2020-06-16
- 1.1.0 — 2020-06-16
- 1.0.61 — 2020-06-08
- 1.0.6 — 2020-02-19
- 1.0.5 — 2019-11-18
- 1.0.4 — 2019-11-18
- 1.0.3 — 2019-10-04
- 1.0.2 — 2019-10-04
- 1.0.1 — 2019-10-04
- 1.0.0 — 2019-10-04

## README

# use-window-size-hook

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

[![NPM](https://img.shields.io/npm/v/use-window-size-hook.svg)](https://www.npmjs.com/package/use-window-size-hook) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

## Install

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

or

```bash
yarn add use-window-size-hook
```

## Usage

```jsx
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

```js
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

Name | Type | Required | Default value | Description
:--- | :--- | :--- | :--- | :---
`useDebounce` | `boolean` | _optional_ | `true` | Defines if the callback is going to be executed when the user finishes resizing the screen or not
`debounceTimeMs` | `number` | _optional_ | `200` | Debounce time to check when the user finishes resizing the screen
`breakpoints` | `ScreenBreakpoints` | _optional_ | Check types below | Defines the breakpoints to be used, you can override and choose your own

## Types

### useWindowSize result

```ts
{
  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

```ts
{
  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](https://github.com/pedro-lb)

---

This hook is created using [create-react-hook](https://github.com/hermanya/create-react-hook).

---
_Source: https://npm.io/package/use-window-size-hook · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
