npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ehsaneha/react-media-query

v1.0.1

Published

A custom React hook that returns the current responsive breakpoint index or evaluates media query rules based on the window's width.

Readme

useMediaQuery

A lightweight React hook that provides responsive breakpoint detection and media query evaluation based on the current window width.

✨ Features

  • Determine the current breakpoint index (xs, sm, md, lg, xl)
  • Evaluate string, number, or object-based media query rules
  • Fully customizable via context
  • Optional live updates on window resize

🚀 Installation

Install the package and its peer dependencies:

npm install @ehsaneha/react-media-query

Or

yarn add @ehsaneha/react-media-query

🔧 Usage

Basic Example

import useMediaQuery, {
  MediaQueryProvider,
  BREAKPOINTS,
  MEDIA_QUERRY_RULES,
} from "./useMediaQuery";

function Component1() {
  const breakpoint = useMediaQuery(); // returns 0–5 based on window width
  return <p>Current breakpoint index: {breakpoint}</p>;
}

function Component2() {
  const isTabletOrLower = useMediaQuery({ md: true }); // returns boolean on window width
  return (
    <p>
      Window Size is now:
      {isTabletOrLower ? "Tablet Or Phone" : "Larger Than Tablet"}
    </p>
  );
}

function App() {
  return (
    <>
      <Component1 />
      <Component2 />
    </>
  );
}

Using Rules

const isTabletOrLower = useMediaQuery(MEDIA_QUERRY_RULES.MD); // true if width <= 'sm' breakpoint
const isExactlyTablet = useMediaQuery(MEDIA_QUERRY_RULES.EMD); // true if width === 'sm' breakpoint
const breakPointIndex = useMediaQuery({ sm: true, lg: true }); // closest matching index if width matches either
const isTabletOrLower = useMediaQuery(BREAKPOINTS.MD); // true if index <= 2 (e.g. md or below)

📡 useMediaQueryOnChange

A companion hook to trigger a callback whenever the viewport matches a new media query state.

import { useMediaQueryOnChange } from "./useMediaQuery";

function App() {
  useMediaQueryOnChange((result) => {
    console.log("Breakpoint changed:", result);
  }, "md");

  return <div>Listening to breakpoint changes…</div>;
}
  • Supports same rule types as useMediaQuery
  • Callback result type is boolean or index (0–5)
  • Debounced internally (default delay: 1000ms)

📐 Breakpoints

By default, the breakpoints used are:

| Key | Width (px) | | --- | ---------- | | xs | 576 | | sm | 768 | | md | 992 | | lg | 1200 | | xl | 1700 |

You can override these via the MediaQueryProvider:

<MediaQueryProvider sizes={{ sm: 700, md: 900 }}>
  <App />
</MediaQueryProvider>

⏱ Live Updates

useMediaQuery responds to window resize events and automatically updates its return value.

🧪 Testing

If testing in Jest, make sure to mock window.innerWidth and debounce logic. Use jsdom for DOM environment simulation.


License

This package is licensed under the MIT License. See LICENSE for more information.


Feel free to modify or contribute to this package!