# @decisiv/breakpoint-observer

> A collection of utils enabling component level responsiveness.

Latest version **1.8.8** (published 2020-08-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @decisiv/breakpoint-observer
pnpm add @decisiv/breakpoint-observer
yarn add @decisiv/breakpoint-observer
bun add @decisiv/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.8.8 |
| Published | 2020-08-04 |
| First published | 2019-10-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 30.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Decisiv UI Development Team |
| Maintainers | andrewtpoe, ddaniell, decisiv |

## Links

- npm: https://www.npmjs.com/package/@decisiv/breakpoint-observer
- npm.io page: https://npm.io/package/@decisiv/breakpoint-observer

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [react-hook-size](https://npm.io/package/react-hook-size.md) ^1.3.0

## Recent versions

- 1.8.8 (latest) — 2020-08-04
- 1.8.9-alpha.9 (alpha) — 2026-10-20
- 1.8.9-alpha.3 (next) — 2020-08-10
- 1.8.9-alpha.8 — 2026-10-06
- 1.8.9-alpha.7 — 2026-09-24
- 1.8.9-alpha.6 — 2026-09-09
- 1.8.9-alpha.5 — 2026-05-20
- 1.8.9-alpha.4 — 2026-05-20
- 1.8.8-alpha.38 — 2020-05-04
- 1.8.7 — 2019-12-18
- 1.8.6 — 2019-11-22
- 1.8.5 — 2019-10-24
- 1.8.4 — 2019-10-16
- 1.8.3 — 2019-10-10
- 1.8.2 — 2019-10-09
- … 1 more at https://npm.io/package/@decisiv/breakpoint-observer/versions

## README

# BreakpointObserver

- [Introduction](#introduction)
- [Usage](#usage)
  - [Installation](#installation)
  - [React Component](#react-component)
  - [React hook](#react-hook)
- [How it works](#how-it-works)

## Introduction

This package provides both a React hook and React component that observe a DOM
element and enable component based responsiveness (vs. page based). The
underlying technology is the
[react-hook-size](https://github.com/infodusha/react-hook-size) package, a hook
based implementation of the experimental
[ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)
technology. Because browser adoption is still relatively sparse, a polyfill is
included.

## Usage

### Installation

```
yarn add @decisiv/breakpoint-observer
```

### React Component

The `BreakpointObserver` component is built with the function as a child
pattern. The `children` prop is required and must be a function.

```jsx
import BreakpointObserver, { sizes } from '@decisiv/breakpoint-observer';

function ExampleComponent() {
  // The breakpoints object defines the minimum widths for specific breakpoint names.
  const breakpoints = {
    [sizes.SM]: 300,
    [sizes.MD]: 420,
    [sizes.LG]: 560,
  };

  return (
    <BreakpointObserver
      // This prop is optional, but if undefined the returned `breakpoint` will also be undefined.
      breakpoints={breakpoints}
    >
      {({
        breakpoint, // The currently active breakpoint. Defaults to `sizes.XS`.
        dimensions, // An object containing the `height` & `width` of the observed DOM element.
        observedElementRef, // The ref must be assigned to the DOM element you want to observe.
      }) => (
        <div id="the-observed-html-element" ref={observedElementRef}>
          <YourStyledComponentWithModifiers
            modifiers={{
              [sizes.XS]: 'hidden',
              [sizes.SM]: 'small',
              [sizes.MD]: 'medium',
              [sizes.LG]: 'large',
            }}
            size={breakpoint}
          />
        </div>
      )}
    </BreakpointObserver>
  );
}
```

#### Props

| Name          | Type        | Default | Description                                                                                                                                                                                                                                                                                                                                  |
| ------------- | ----------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `breakpoints` | Breakpoints |         | Definition of the breakpoints minimum widths. Should match the following type signature: <br/>`{ [k in Sizes]: number }`<br/> The `number` represents the minimum width of the element for that breakpoint. The breakpoint value `sizes.XS` will be automatically applied if the element's width is below the lowest declared minimum width. |
| `children`    | function    |         | This component uses the function as a child pattern. `children` must be a function and must be defined.                                                                                                                                                                                                                                      |

### React hook

The hook provides a very similar API to the React component. The primary
advantage is that it can be used outside of the render statement of the
component you are building.

```jsx
import { sizes, useBreakpointObserver } from '@decisiv/breakpoint-observer';

function ExampleComponent() {
  // The breakpoints object defines the minimum widths for specific breakpoint names.
  const breakpoints = {
    [sizes.SM]: 300,
    [sizes.MD]: 420,
    [sizes.LG]: 560,
  };

  const [
    observedElementRef, // The ref must be assigned to the DOM element you want to observe.
    {
      breakpoint, // The currently active breakpoint. Defaults to `sizes.XS`.
      dimensions, // An object containing the `height` & `width` of the observed DOM element.
    },
  ] = useBreakpointObserver(
    breakpoints, // Optional, but if undefined the returned `breakpoint` will also be undefined.
  );

  return (
    <div id="the-observed-html-element" ref={observedElementRef}>
      <YourStyledComponentWithModifiers
        modifiers={{
          [sizes.XS]: 'hidden',
          [sizes.SM]: 'small',
          [sizes.MD]: 'medium',
          [sizes.LG]: 'large',
        }}
        size={breakpoint}
      />
    </div>
  );
}
```

## How it works

Both the hook and component utilities will create and listen to a
`ResizeObserver` for an HTML element. You must tell the utilities which HTML
element to observe using the `observedElementRef`. Once registered and rendered,
both utilities give you access to the current dimensions (height and width) and
the currently active breakpoint.

The logic used to select the currently active breakpoint is relatively simple.
The number values in the `breakpoints` prop are considered minimum widths. The
utilities will compare those minimum widths to the HTML element's actual width
and select the breakpoint with the largest minimum width that is less than or
equal to the components actual width.

There is no need to define a breakpoint with a minimum width of zero. The
utilities assume a breakpoint of `sizes.XS` exists with a minimum width of zero.
If the width is less than the smallest declared breakpoint minimum width, the
breakpoint value passed down will be `sizes.XS`.

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