# @tywmick/use-media-breakpoints

> A React hook for using screen width breakpoints.

Latest version **1.0.2** (published 2020-06-11) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @tywmick/use-media-breakpoints
pnpm add @tywmick/use-media-breakpoints
yarn add @tywmick/use-media-breakpoints
bun add @tywmick/use-media-breakpoints
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-06-11 |
| First published | 2020-03-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Ty Mick |
| Maintainers | tywmick |
| Keywords | react, hook, media breakpoints, media queries, screen width, screen size, responsive, responsive design, typescript |

## Links

- npm: https://www.npmjs.com/package/@tywmick/use-media-breakpoints
- Repository: https://github.com/tywmick/use-media-breakpoints
- Homepage: https://github.com/tywmick/use-media-breakpoints#readme
- Issues: https://github.com/tywmick/use-media-breakpoints/issues
- npm.io page: https://npm.io/package/@tywmick/use-media-breakpoints

## 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.2 (latest) — 2020-06-11
- 1.0.1 — 2020-06-11
- 1.0.0 — 2020-06-10
- 0.1.0 — 2020-03-05

## README

# useMediaBreakpoints

[![npm version](https://img.shields.io/npm/v/@tywmick/use-media-breakpoints.svg)](https://npmjs.org/package/@tywmick/use-media-breakpoints "View this package on npm")
[![npm bundle size](https://img.shields.io/bundlephobia/min/@tywmick/use-media-breakpoints)](https://npmjs.org/package/@tywmick/use-media-breakpoints "View this package on npm")
[![MIT license](https://img.shields.io/npm/l/@tywmick/use-media-breakpoints.svg)](/LICENSE.md)

A React hook for using screen width breakpoints.

I essentially wanted to duplicate the logic of [Bootstrap's `media-breakpoint` Sass mixins](https://getbootstrap.com/docs/4.5/layout/overview/#responsive-breakpoints) within my React code.

## Installation

```sh
npm install @tywmick/use-media-breakpoints
# OR
yarn add @tywmick/use-media-breakpoints
```

## Usage

After importing the hook...

```js
import useMediaBreakpoints from "@tymick/use-media-breakpoints";
```

...call it from the top level of your React function.

```js
const breakpoint = useMediaBreakpoints({
  xs: 0,
  sm: 576,
  md: 768,
  lg: 992,
  xl: 1200,
});
```

The hook has one optional parameter: an object containing the set of screen width breakpoints (in pixels) you wish to use. If this parameter is omitted, [Bootstrap's default breakpoints](https://getbootstrap.com/docs/4.5/layout/overview/#containers) will be used. If you choose to specify your own set of breakpoints, `sm` and `lg` breakpoints are required, while `xs`, `md`, and `xl` breakpoints are optional.

This hook returns an object containing the boolean results of several media queries. For example, if the width of the window is `800px`, the value of `breakpoint` (as defined above) will be

```js
{
  xs: false,
  sm: false,
  md: true,
  lg: false,
  xl: false,
  only: {
    xs: false,
    sm: false,
    md: true,
    lg: false,
    xl: false,
  },
  up: {
    xs: true,
    sm: true,
    md: true,
    lg: false,
    xl: false,
  },
  down: {
    xs: false,
    sm: false,
    md: true,
    lg: true,
    xl: true,
  },
  between: {
    xs: {
      sm: false,
      md: true,
      lg: true,
      xl: true,
    },
    sm: {
      md: true,
      lg: true,
      xl: true,
    },
    md: {
      lg: true,
      xl: true,
    },
    lg: {
      xl: false,
    },
  },
}
```

What's that good for? Say you have a React component you only want to display on `md`-sized screens. Thow this into your JSX:

```jsx
{breakpoint.md && <MyComponent />}
{/* OR */}
{breakpoint.only.md && <MyComponent />}
```

Or maybe you want to use one component on larger screens and a different one on smaller screens:

```jsx
{breakpoint.up.lg ? <LargerVersion /> : <SmallerVersion />}
```

Or maybe you want to describe the size of the screen in paragraph form with an odd sort of precision:

```jsx
<p>
  This screen is {breakpoint.between.sm.lg ? "" : "pretty "}
  {breakpoint.down.sm ? "small" : breakpoint.up.lg ? "big" : "average"}.
</p>
```

But that's up to you.

Have fun!

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