# react-responsiveness

> React Responsiveness

Latest version **0.1.5** (published 2023-08-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-responsiveness
pnpm add react-responsiveness
yarn add react-responsiveness
bun add react-responsiveness
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.5 |
| Published | 2023-08-24 |
| First published | 2023-03-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 20.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | andrei-gheorghiu |
| Keywords | react, react responsiveness plugin, responsiveness, responsive design, responsiveness breakpoints, responsiveness interval, responsive intervals, breakpoints, custom breakpoints, matchmedia, window matchmedia, match media, media query, media queries, @media, css, bootstrap, bulma, chakra, foundation, ionic, material design, materialize, material ui, quasar, semantic ui, Skeleton, vuetify, tailwind, tailwindcss, windi, windicss, ssr, ssr compatible |

## Links

- npm: https://www.npmjs.com/package/react-responsiveness
- Repository: https://github.com/codemonk-digital/react-responsiveness
- Homepage: https://github.com/codemonk-digital/react-responsiveness#readme
- Issues: https://github.com/codemonk-digital/react-responsiveness/issues
- npm.io page: https://npm.io/package/react-responsiveness

## Alternatives

- [@sveltejs/kit](https://npm.io/package/@sveltejs/kit.md) — 2.2M weekly downloads
- [@tangle-network/brand](https://npm.io/package/@tangle-network/brand.md) — 10.0K weekly downloads
- [seneca](https://npm.io/package/seneca.md) — 7.4K weekly downloads
- [@bsb/base](https://npm.io/package/@bsb/base.md) — 7.2K weekly downloads
- [@modern-js/plugin-swc](https://npm.io/package/@modern-js/plugin-swc.md) — 3.4K weekly downloads

## Recent versions

- 0.1.5 (latest) — 2023-08-24
- 0.1.4 — 2023-08-24
- 0.1.3 — 2023-08-24
- 0.1.2 — 2023-03-12
- 0.1.1 — 2023-03-10
- 0.1.0 — 2023-03-09
- 0.0.5 — 2023-03-07
- 0.0.4 — 2023-03-07
- 0.0.3 — 2023-03-06
- 0.0.2 — 2023-03-06
- 0.0.1 — 2023-03-06
- 0.0.1-alpha.1 — 2023-03-06
- 0.0.1-alpha.0 — 2023-03-06

## README

<div class="center">
<h1>React Responsiveness</h1>
</div>

**What** - Tiny plugin for working with responsiveness intervals, developed with a focus on ease of use (DX) and runtime performance.  
**Why** - I am a bit obsessed with both performance and ease of use. See [how it works](#how-it-works).

<p>
<a href="https://www.npmjs.com/package/react-responsiveness"><img src="https://img.shields.io/npm/dt/react-responsiveness.svg?color=f9d342&style=flat-square" alt="Total Downloads"></a>
<a href="https://www.npmjs.com/package/react-responsiveness"><img src="https://img.shields.io/npm/v/react-responsiveness.svg?color=f9d342&style=flat-square" alt="Latest Release"></a>
<a href="https://github.com/codemonk-digital/react-responsiveness/blob/main/LICENSE.MD"><img src="https://img.shields.io/npm/l/react-responsiveness.svg?color=f9d342&style=flat-square" alt="License"></a>
<a href="https://github.com/codemonk-digital/react-responsiveness/blob/main/package.json#L27"><img src="https://img.shields.io/badge/dependencies-0-f9d342.svg?style=flat-square" alt="Dependencies" /></a>
<a href="https://unpkg.com/react-responsiveness"><img src="https://img.badgesize.io/https://unpkg.com/react-responsiveness.svg?compression=gzip&label=umd:minzip&color=f9d342&style=flat-square" alt="unpkg umd min:gzip size" /></a>
<img src="https://img.shields.io/badge/SSR-ready-f9d342?style=flat-square" alt="SSR compatibility status"/>
<a href="https://makeapullrequest.com"><img src="https://img.shields.io/badge/PRs-welcome-f9d342.svg?style=flat-square" alt="PRs Welcome"/></a>
</p>

## Installation

#### yarn

```terminal
yarn add react-responsiveness
```

#### npm

```terminal
npm i react-responsiveness
```

## Demo

[codesandbox](https://codesandbox.io/p/sandbox/react-responsiveness-lx3789?file=/src/App.tsx).

## Usage

#### A) Add provider
<details>
    <summary>Show example</summary>

```tsx
import { ResponsivenessProvider, Presets } from "react-responsiveness";

function App() {
    // ...
}

const WithResponsiveness = () => (
  <ResponsivenessProvider breakpoints={Presets.Bootstrap_5}>
    <App />
  </ResponsivenessProvider>
);
export default WithResponsiveness;
```

</details>

#### B) Use in any component
<details>
    <summary>Show example</summary>

```tsx
import { useResponsiveness } from "react-responsiveness";

const { isMin, isMax, isOnly, currentInterval } = useResponsiveness()

return (<>
   <div>Current interval {currentInterval}</div>
   {isMin('md') && (
       // @media (min-width: 768px)
       <div>content...</div>
   )}
   {isMax('md') && (
       // @media (max-width: 991.9px)
       <div>content...</div>
   )}
   {isOnly('md') && (
       // @media (min-width: 768px) and (max-width: 991.9px)
       <div>content...</div>
   )}
</>)
```
</details>

## Available presets:

`Bootstrap_3`, `Bootstrap_4`, `Bootstrap_5`, `Bulma`, `Chakra`, `Foundation`, `Ionic`, `Material_Design`, `Materialize`, `Material_UI`, `Quasar`, `Semantic_UI`, `Skeleton`, `Tailwind_CSS`, `Windi_CSS`

_Notes:_ 
 - Default breakpoints value is **_already set_** to Bootstrap 5's [responsiveness breakpoints](https://getbootstrap.com/docs/5.3/layout/breakpoints/#available-breakpoints) preset.
<details>
    <summary>Show preset details</summary>

```tsx
Presets.Bootstrap_5 = {
  xs: 0,
  sm: 576,
  md: 768,
  lg: 992,
  xl: 1200,
  xxl: 1400,
};
```
</details>

- If you maintain a CSS framework (or use one often) and want its preset added, [open an issue](https://github.com/codemonk-digital/react-responsiveness/issues) or a PR.
- If you spot any inaccuracy in [presets](https://github.com/codemonk-digital/react-responsiveness/blob/main/lib/presets.ts) (either typo or due to library update), please, let me know, I'll correct it.

## Can I add my own intervals? Of course:

```tsx
<ResponsivenessProvider
  breakpoints={{
    small: 0,
    medium: 777,
    large: 1234,
  }}
>
  // ...
</ResponsivenessProvider>
```
<details>
  <summary>Show usage example</summary>

```tsx
import { useResponsiveness } from "react-responsiveness";

const { isOnly } = useResponsiveness()

return (<>
    {isOnly('medium') && (
        // @media (min-width: 777px) and (max-width: 1233.9px)
        <div>content...</div>
    )}
</>)
```
</details>

## F.A.Q.

#### My IDE's TypeScript service does not pick up the types for the package. Is there a way to fix it?
<details>
  <summary>Show answer</summary>

I've noticed this weird problem in some codesandbox.io instances. 

This seems to fix it:
```ts
// src/react-responsiveness.d.ts
declare module "react-responsiveness" {
  export * from "react-responsiveness/dist/index" 
}
```
Re-exporting types from where they are declared in `package.json` doesn't make much sense, but it keeps TS happy.  
Another way to fix it is to restart the TS service, but it doesn't always work.
</details>

## How it works:

- uses the native [`window.matchMedia(queryString)`](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia) and only reacts to changes in the query's `matches` value. It's the same API powering CSS media queries
- listeners are placed on the `MediaQueryList` instances, meaning they are garbage collected as soon as the app is unmounted, without leaving bound events behind on `<body>` or `window` object
- no global pollution
- in terms of memory and/or CPU consumption, listening to `window.matchMadia` 'change' events is a few hundred times lighter than using the _"traditional"_ `resize` event listener method

## Got issues?

[Let me know!](https://github.com/codemonk-digital/react-responsiveness/issues)

Happy coding!
:: }<(((*> ::

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