# use-breakpoint-observer

> A React hook that allows you to use a ResizeObserver and gives you access to the currently active breakpoint and the width dimension of the targeted html element or body

Latest version **1.0.6** (published 2022-10-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-breakpoint-observer
pnpm add use-breakpoint-observer
yarn add use-breakpoint-observer
bun add use-breakpoint-observer
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2022-10-01 |
| First published | 2022-09-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Paul Vaxevanis |
| Maintainers | paulvaxevanis |
| Keywords | react, hooks, ResizeObserver |

## Links

- npm: https://www.npmjs.com/package/use-breakpoint-observer
- Repository: https://github.com/vaxevanis/useBreakpointsObserver
- npm.io page: https://npm.io/package/use-breakpoint-observer

## Dependencies (1)

- [-](https://npm.io/package/-.md) ^0.0.1

## 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.0.6 (latest) — 2022-10-01
- 1.0.5 — 2022-10-01
- 1.0.4 — 2022-10-01
- 1.0.3 — 2022-10-01
- 1.0.2 — 2022-10-01
- 1.0.1 — 2022-09-30
- 1.0.0 — 2022-09-30

## README

# useBreakpointsObserver()

<img alt="npm bundle size" src="https://img.shields.io/bundlephobia/minzip/use-breakpoint-observer">

🐜 Tiny size
🦄 Written in Typescript
😎 No timeouts or `window.resize`

> A React hook that allows you to use a ResizeObserver and gives you access to the currently active breakpoint and the width dimension of the targeted html element or body

## Install

```bash
npm install use-breakpoint-observer
```

```bash
yarn add use-breakpoint-observer
```

## Basic Usage

```jsx
import React from 'react';
import useBreakpointsObserver from './useBreakpointsObserver';

// Define the minimum width for each specific breakpoint name
const breakPointSizes = {
  xs: 480,
  sm: 576,
  md: 768,
  lg: 992,
  xl: 1200,
  xxl: 1600,
};

const App = () => {
  let observeRef = useRef(null);

  // call the Hook
  const [breakSize, width] = useBreakpointsObserver(
    observeRef,
    breakPointSizes,
  );

  return (
    <div ref={observeRef}>
      <p>
        Breakpoint is: {breakSize}
        <br />
        Width is: {width}
      </p>
    </div>
  );
};
```

#### Note: Pass a `null` htmlRef prop to observe the body element:

```jsx
const [breakSize, width] = useBreakpointsObserver(null, breakPointSizes);
```

## API

### useResizeObserver(htmlRef, breakpoints)

```ts

const useBreakpointsObserver = <T extends HTMLElement>(
  htmlRef: React.RefObject<T> | null,
  breakpoints: BreakSizesType,
): [BreakPointsType, number | null]

```

| Argument    | Type                                        | Required? | Description                                                                 |
| ----------- | ------------------------------------------- | --------- | --------------------------------------------------------------------------- |
| htmlRef     | <code>React.RefObject<T> &#124; null</code> | Yes       | A React ref created by `useRef()` or `null`to observe the HTML body element |
| breakpoints | <code>BreakSizesType</code> (Object)        | Yes       | An Object that defines the minimum width for each specific breakpoint name  |

## Types

### breakpoints

```ts
export type BreakSizesType = {
  [key in BreakPointsType]?: number;
};

export type BreakPointsType = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl';
```

## LICENSE

MIT

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