Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

use-breakpoint

iiroj114.8kMIT4.0.6TypeScript support: included

A React hook for getting the current responsive media breakpoint

breakpoint, matchMedia, react, responsive, typescript, viewport, window.matchMedia

readme

use-breakpoint

GitHub Actions version code size

A React hook (>=18) for getting the current responsive media breakpoint.

useBreakpoint

Initialize useBreakpoint with a configuration object, and optionally a default breakpoint name (in non-window environments like SSR). The return value will be an object with the breakpoint's name (string), min-width, and max-width values (number):

import useBreakpoint from 'use-breakpoint'

/**
 * It is important to bind the object of breakpoints to a variable for memoization to work correctly.
 * If they are created dynamically, try using the `useMemo` hook.
 */
const BREAKPOINTS = { mobile: 0, tablet: 768, desktop: 1280 }

const CurrentBreakpoint = () => {
  const { breakpoint, maxWidth, minWidth } = useBreakpoint(
    BREAKPOINTS,
    'desktop',
  )
  return <p>The current breakpoint is {breakpoint}!</p>
}

Return values

Given a configuration BREAKPOINTS = { mobile: 0, tablet: 768, desktop: 1280 } and a window size of 1280x960, the hook will return as the breakpoint:

  1. const { breakpoint } = useBreakpoint(BREAKPOINTS)
    • undefined when rendered server-side
    • 'desktop' when rendered client-side
  2. const { breakpoint } = useBreakpoint(BREAKPOINTS, 'mobile')
    • 'mobile' when rendered server-side
    • 'mobile' on the first client-side render
    • 'desktop' on subsequent client-side renders

Hydration

If supplied a default breakpoint, the hook will always return that value when rendered server-side. To not cause inconsistencies during the first client-side render, the default value is also used client-side for the first render, instead of the (possibly different) real breakpoint.

For example, given a breakpoint config:

const { breakpoint } = useBreakpoint(BREAKPOINTS, 'mobile')

When rendered server-side, breakpoint === 'mobile' always, because there is no window. On client-side with a desktop-sized window, on the first render breakpoint === 'mobile', and then on following renders breakpoint === 'desktop'. This is to ensure ReactDOM.hydrate behaves correctly. The implementation relies on the useSyncExternalStore React hook with the getServerSnapshot callback.

Functionality

This hook uses the window.matchMedia functionality to calculate the current breakpoint. For a list of breakpoints, we generate some css media queries in the form of (min-width: XXXpx) and (max-width: YYYpx) and then add listeners for the changes. useBreakpoint will then update its return value when the breakpoint changes from one rule to another.

getCSSMediaQueries

To use the same breakpoint configuration in CSS-in-JS, you can use the getCSSMediaQueries helper:

import { getCSSMediaQueries } from 'use-breakpoint'

const BREAKPOINTS = { mobile: -1, tablet: 768, desktop: 1280 }

const cssQueries = getCSSMediaQueries(BREAKPOINTS, 'screen')
// {
//    desktop: "@media only screen and (min-width: 1280px)",
//    mobile: "@media only screen and (max-width: 767px)",
//    tablet: "@media only screen and (min-width: 768px) and (max-width: 1279px)",
//  }

The second option can be omitted to leave out the media type condition.

Developing

This project is built with Typescript. A Storybook is included for local previewing. The easiest way to get started is cloning the repo and starting the storybook server locally via npm start.

changelog

4.0.6 (2025-02-02)

Bug Fixes

  • deps: update dev dependencies (4660750)

4.0.5 (2024-11-18)

Bug Fixes

  • set correct write permissions for npm provenance (8c55862)

4.0.4 (2024-11-18)

Bug Fixes

  • set correct write permissions for npm provenance (088bcec)

4.0.3 (2024-11-18)

Bug Fixes

  • only match default breakpoint, if none of the queries match (91bad17)

4.0.2 (2024-11-13)

Bug Fixes

  • npm: link to correct repository (7fa6217)

4.0.1 (2023-10-07)

Bug Fixes

  • docs: update README with current required React version (6dbd397)

4.0.0 (2023-10-06)

⚠ BREAKING CHANGES

  • Because of the new implementation relying on useSyncExternalStore(), React version 18 is required to use useBreakpoint().

Features

  • use useSyncExternalStore() hook, require React 18 (20a93bc)

3.1.1 (2023-06-01)

Bug Fixes

  • add missing file extension to imports (#21) (d3f0c21), closes #20

3.1.0 (2023-05-30)

Features

  • add getCSSMediaQueries helper (86fabb8)
  • build and package cjs version (d5b6d29)

3.0.7 (2023-02-16)

Bug Fixes

  • deps: update dependencies (c04d4d8)
  • disable Storybook telemetry (4b9320f)

3.0.6 (2022-12-09)

Bug Fixes

  • add main field to package.json for better backwards-compatibility (2af567e)

3.0.5 (2022-12-04)

Bug Fixes

  • deps: update all dependencies (2d66006)

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

3.0.4 (2022-11-18)

Bug Fixes

  • use MediaQueryList change event when supported (1b7d7f8)

3.0.3 (2022-06-10)

Bug Fixes

  • deps: update react@18 dev dependency (94f6b48)

3.0.2 (2022-03-27)

3.0.1 (2022-01-07)

Bug Fixes

  • use explicit extensions in import paths (185bc75)

3.0.0 (2021-11-27)

⚠ BREAKING CHANGES

  • use-breakpoint is now a pure ESM module, and might need transpilation (eg. with babel) when used in older CommonJS-based projects.

build

  • use only TSC to build ESM distribution (2f5e381)

2.0.3 (2021-11-27)

2.0.2 (2021-08-30)

Bug Fixes

  • use useEffect without DOM to suppress unnecessary warning (05311fd)

2.0.1 (2021-05-27)

Bug Fixes

  • useLayoutEffect instead of useEffect (#9) (e35d66e)

2.0.0 (2021-04-15)

⚠ BREAKING CHANGES

  • Previously this hook returned undefined when the default value was not set and the real breakpoint couldn't be found (like during SSR).

Now it returns an object { breakpoint: undefined, minWidth: undefined, maxWidth: undefined } to make isomorphic usage easier.

This might require changes to existing code.

Features

  • allow returning the real breakpoint instead of default on the first client render (972c5bc)
  • directly return correct initial breakpoint when default not set (2f25717)

Bug Fixes

  • always return an object for easier usage (76b4e4a)
  • bump package-lock.json version (f80b97c)
  • correct typeof check in useDebugValue (0c4114c)
  • return null instead of undefined (2136384)
  • return null instead of undefined maxWidth where appropriate (f5d5543)
  • returned maxWidth does not overlap next breakpoint's minWidth (75c0d85)
  • use matchMedia listener callback argument (a01c890)

1.1.7 (2021-04-10)

1.1.6 (2021-02-19)

1.1.5 (2020-12-24)

Bug Fixes

  • deps: update dependencies (2d50781)

1.1.4 (2020-10-26)

Bug Fixes

  • actually update the breakpoint (5c6ae5c)
  • tsc: work around tsc errors after update (de6cfae)
  • no need to set useState setter as dependency (80117f0)

1.1.3 (2020-10-26)

Bug Fixes

  • tsc: work around tsc errors after update (de6cfae)
  • no need to set useState setter as dependency (80117f0)

1.1.2 (2020-05-04)

Bug Fixes

  • do not use .mjs extension for module field (995c2ca)
  • replace tslint with eslint and fix errors (5b9d1bf)
  • unsubscribe from window.matchMedia when unmounting (b0ce1b0)
  • update storybook config (f71801b)

1.1.1 (2020-03-14)

1.1.0 (2019-12-30)

Features

  • use mjs extension for esm module (31bc908)

1.0.31 (2019-12-01)

1.0.30 (2019-10-26)

1.0.29 (2019-09-23)

1.0.28 (2019-09-23)

1.0.27 (2019-09-22)

1.0.26 (2019-09-22)

1.0.25 (2019-09-22)

1.0.24 (2019-09-22)

1.0.23 (2019-09-22)

1.0.22 (2019-09-22)

1.0.21 (2019-09-22)

1.0.20 (2019-09-22)

Bug Fixes

1.0.19 (2019-07-23)

1.0.18 (2019-07-11)

1.0.17 (2019-07-07)

Build System

  • add husky, commitlint and lint-staged (263d94c)

1.0.16 (2019-07-06)

1.0.15 (2019-06-02)

1.0.14 (2019-05-13)

1.0.13 (2019-04-28)

1.0.12 (2019-04-28)

1.0.11 (2019-04-17)

1.0.10 (2019-03-27)

1.0.9 (2019-03-17)

1.0.8 (2019-03-02)

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.0.7 (2019-02-28)

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.0.6 (2019-02-28)

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.0.5 (2019-02-28)

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.0.4 (2019-02-28)

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.0.3 (2019-02-28)

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.0.2 (2019-02-27)

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.0.1 (2019-02-27)

Bug Fixes

  • storybook: resolve TypeScript files before JavaScript, install missing devDependency (5cf9739)
  • recreate media query listeners if config changes (1c03a6a)

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.0.0 (2019-02-27)

Features