# use-breakpoint-matches

> Simple hook to detect changes on the width of a container when matches a given breakpoint and also for the global breakpoints

Latest version **1.2.14** (published 2024-09-15) · MIT license · 0 weekly downloads

## Install

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

## 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.2.14 |
| Published | 2024-09-15 |
| First published | 2023-08-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 73.6 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | royriojas |
| Maintainers | royriojas |
| Keywords | responsive helpers, breakpoints, css responsive design |

## Links

- npm: https://www.npmjs.com/package/use-breakpoint-matches
- npm.io page: https://npm.io/package/use-breakpoint-matches

## Dependencies (1)

- [@babel/runtime](https://npm.io/package/@babel/runtime.md) 7.16.3

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.2.14 (latest) — 2024-09-15
- 1.2.13 — 2024-09-15
- 1.2.12 — 2024-09-15
- 1.2.11 — 2023-09-29
- 1.2.10 — 2023-08-30
- 1.2.9 — 2023-08-30
- 1.2.7 — 2023-08-30
- 1.2.6 — 2023-08-30
- 1.2.5 — 2023-08-30
- 1.2.4 — 2023-08-30
- 1.2.3 — 2023-08-30
- 1.2.2 — 2023-08-30
- 1.2.1 — 2023-08-30
- 1.2.0 — 2023-08-30
- 1.1.0 — 2023-08-30
- … 1 more at https://npm.io/package/use-breakpoint-matches/versions

## README

# useBreakpointMatches

Small collection of utilties to make responsive designs simpler relying of a set of breakpoints.

This module uses `container queries` and `IntersectionObserver` to detect the changes in breakpoints in a given container and uses `MatchMedia` to detect screenBreakpoint changes

## installation

npm i use-breakpoint-matches

## Usage

### BreakpointsAware

The `BreakpointsAware` component is a wrapper around the `useBreakpointMatches` hook.

```tsx
import { BreakpointsAware } from 'use-breakpoint-matches';

// The BreakpointsAware component receives a `breakpoints` prop that is an object with the breakpoints to be used in the component.
// for example { xs: 320, sm: 480, md: 768, lg: 1024 }
// this means we will have 4 breakpoints: xs, sm, md, lg
// xs starts at 0 (this will always be true, as at the very least the width of the container will be 0)
// sm starts at 320
// md starts at 480
// lg starts at 1024
// 
// the main idea is that if the container width is 485px, the matches object will be 
// { 
//   xs: true,
//   sm: true,
//   md: true,
//   lg: false
// }

<BreakpointsAware breakpoints={{ xs: 320, sm: 480, md: 768, lg: 1024 }}>
  {({ matches, cn }) => (
    <>
      <div className={cn({ xs: 'px-2 py-1', sm: 'px-4 py-2', md: 'px-6 py-3', lg: 'px-8 py-4' })}>
        The content here will have different classes as the breakpoints matche
      </div>
      <div>
        /* The content here will be displayed only when the breakpoints matches */
        {matches.xs && <div>xs</div>}
        {matches.sm && <div>sm</div>}
        {matches.md && <div>md</div>}
        {matches.lg && <div>lg</div>}
      </div>
    </>
  )}
</BreakpointsAware>;
```

`BreakpointsAware` receives a `breakpoints` prop that is an object with the breakpoints to be used in the component.

It also receives a `children` prop that is a function that receives an object with the current breakpoints matches and a function to generate a className based on the current breakpoints matches. 

The `cn` helper function is curried with the current breakpoints that match and return the className for the last breakpoint that matches.
So if the current breakpoints matches are `{ xs: true, sm: true, md: true, lg: false }` the `cn` function will return the className for the `md` breakpoint only.

### BreakpointsAware props

| Prop name | Type | Default | Description |
| --- | --- | --- | --- |
| breakpoints | `Record<string, number>` | `{ xss: 320, xs: 480, sm: 690, md: 850, lg: 1124, xl: 1380, xxl: 1920, xxl2: 2160 }` | The breakpoints to be used in the component. |
| width | `number` or `string` | `100%` | The width of the container to be used in the `BreakpointsAware` component. |
| className | `string` | `''` | The className to be used in the `BreakpointsAware` component. |
| children | `(args: { matches: { [P in keyof T]?: boolean }, cn: (args: { [P in keyof T]?: string }) => string }) => React.ReactNode` | `undefined` | The children function to be used in the `BreakpointsAware` component. |

### useBreakpointMatches

The main export from this module is the `useBreakpointMatches` hook.

```tsx
import { useBreakpointMatches } from 'use-breakpoint-matches';

const { setRef, widthMap } = useBreakpointMatches({ breakpoints });
```

`useBreakpointMatches` returns an object with two properties:

- `setRef`: a function that receives a `HTMLElement` and sets the `ref` of the container to be used in the `BreakpointsAware` component.
- `widthMap`: an object with the current breakpoints matches.

This is a low level hook that is not meant to be used directly. It is meant to be used in the `BreakpointsAware` component.

### useScreenBreakpointMatches

This is a higher level hook that uses `useBreakpointMatches` and `useHorizontalBreakpoints` to detect the screen breakpoints.

```tsx
import { useScreenBreakpoints } from 'use-breakpoint-matches';

const { hMatches, vMatches } = useScreenBreakpoints();
```

`useScreenBreakpointMatches` returns an object with four properties:

- `hMatches`: an object with the current horizontal breakpoints matches.
- `vMatches`: an object with the current vertical breakpoints matches.
- `cnByMatchesH`: a function that receives an object with the current horizontal breakpoints matches and returns the className for the last breakpoint that matches.
- `cnByMatchesV`: a function that receives an object with the current vertical breakpoints matches and returns the className for the last breakpoint that matches.

In order to use this hook you need to call `initScreenBreakpoints` before using it.

```tsx
import { initScreenBreakpoints } from 'use-breakpoint-matches';

// Do this before rendering your app
initScreenBreakpoints({ hBreakpoints: { xs: 320, sm: 480, md: 768, lg: 1024 }, vBreakpoints: { xs: 320, sm: 480, md: 768, lg: 1024 } });

// Then you can use it
const { hMatches, vMatches, cnByMatchesH, cnByMatchesV } = useScreenBreakpoints();

// Example usage for horizontal breakpoints
<div className={cnByMatchesH({ xs: 'px-2 py-1', sm: 'px-4 py-2', md: 'px-6 py-3', lg: 'px-8 py-4' })}>
  The content here will have different classes as the breakpoints matche
</div>
<div>
  /* The content here will be displayed only when the breakpoints matches */
  {hMatches.xs && <div>xs</div>}
  {hMatches.sm && <div>sm</div>}
  {hMatches.md && <div>md</div>}
  {hMatches.lg && <div>lg</div>}
</div>

```
## License

MIT

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