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

@utilityjs/use-media-query

v2.0.0

Published

A React hook that helps detect whether media queries individually match.

Readme

UtilityJS | useMediaQuery

A React hook that helps detect whether media queries individually match.

Features

  • Multiple Queries: Support for single or multiple media queries
  • Reactive Updates: Automatically updates when viewport changes
  • SSR Safe: Works correctly with server-side rendering
  • Performance Optimized: Efficient event listener management
  • TypeScript Support: Full type safety with TypeScript definitions
  • Browser Compatibility: Graceful fallback when matchMedia is not supported

Installation

npm install @utilityjs/use-media-query

or

pnpm add @utilityjs/use-media-query

Usage

Single Media Query

import { useMediaQuery } from "@utilityjs/use-media-query";

function ResponsiveComponent() {
  const [isMobile] = useMediaQuery("(max-width: 768px)");

  return <div>{isMobile ? <MobileNavigation /> : <DesktopNavigation />}</div>;
}

Multiple Media Queries

import { useMediaQuery } from "@utilityjs/use-media-query";

function MultiBreakpointComponent() {
  const [isMobile, isTablet, isDesktop] = useMediaQuery([
    "(max-width: 768px)",
    "(min-width: 769px) and (max-width: 1024px)",
    "(min-width: 1025px)",
  ]);

  if (isMobile) return <MobileLayout />;
  if (isTablet) return <TabletLayout />;
  if (isDesktop) return <DesktopLayout />;

  return <DefaultLayout />;
}

Common Breakpoints

import { useMediaQuery } from "@utilityjs/use-media-query";

function BreakpointExample() {
  const [isXs, isSm, isMd, isLg, isXl] = useMediaQuery([
    "(max-width: 575px)", // Extra small devices
    "(min-width: 576px) and (max-width: 767px)", // Small devices
    "(min-width: 768px) and (max-width: 991px)", // Medium devices
    "(min-width: 992px) and (max-width: 1199px)", // Large devices
    "(min-width: 1200px)", // Extra large devices
  ]);

  return (
    <div>
      <p>Current breakpoint:</p>
      {isXs && <span>Extra Small (XS)</span>}
      {isSm && <span>Small (SM)</span>}
      {isMd && <span>Medium (MD)</span>}
      {isLg && <span>Large (LG)</span>}
      {isXl && <span>Extra Large (XL)</span>}
    </div>
  );
}

Dark Mode Detection

import { useMediaQuery } from "@utilityjs/use-media-query";

function ThemeComponent() {
  const [isDarkMode] = useMediaQuery("(prefers-color-scheme: dark)");

  return (
    <div className={isDarkMode ? "dark-theme" : "light-theme"}>
      <h1>Current theme: {isDarkMode ? "Dark" : "Light"}</h1>
    </div>
  );
}

Orientation Detection

import { useMediaQuery } from "@utilityjs/use-media-query";

function OrientationComponent() {
  const [isPortrait, isLandscape] = useMediaQuery([
    "(orientation: portrait)",
    "(orientation: landscape)",
  ]);

  return (
    <div>
      {isPortrait && <p>Device is in portrait mode</p>}
      {isLandscape && <p>Device is in landscape mode</p>}
    </div>
  );
}

Reduced Motion Detection

import { useMediaQuery } from "@utilityjs/use-media-query";

function AccessibilityComponent() {
  const [prefersReducedMotion] = useMediaQuery(
    "(prefers-reduced-motion: reduce)",
  );

  return (
    <div className={prefersReducedMotion ? "no-animations" : "with-animations"}>
      <h1>Respecting user preferences</h1>
    </div>
  );
}

API

useMediaQuery(query)

A React hook that tracks the state of CSS media queries.

Parameters

  • query: string | string[] - A single media query string or an array of media query strings

Returns

  • boolean[] - An array of boolean values indicating whether each query matches

Behavior

  • Initial State: All queries start as false during SSR and initial render
  • Client Hydration: Queries are evaluated and updated after component mounts
  • Reactive Updates: Automatically updates when media query matches change
  • Error Handling: Logs an error and returns false if matchMedia is not supported

Common Media Query Patterns

Viewport Width

(max-width: 768px)           /* Mobile */
(min-width: 769px)           /* Tablet and up */
(min-width: 1024px)          /* Desktop and up */

Device Characteristics

(orientation: portrait)       /* Portrait orientation */
(orientation: landscape)      /* Landscape orientation */
(hover: hover)               /* Device supports hover */
(pointer: coarse)            /* Touch device */

User Preferences

(prefers-color-scheme: dark)     /* Dark mode preference */
(prefers-reduced-motion: reduce) /* Reduced motion preference */
(prefers-contrast: high)         /* High contrast preference */

Display Properties

(resolution: 2dppx)          /* High DPI displays */
(aspect-ratio: 16/9)         /* Specific aspect ratio */

Browser Support

This hook relies on the window.matchMedia API, which is supported in:

  • Chrome 9+
  • Firefox 6+
  • Safari 5.1+
  • Edge 12+
  • Internet Explorer 10+

For unsupported browsers, the hook will log an error and return false for all queries.

Performance Considerations

  • Event Listener Optimization: Uses a single event listener per media query
  • Efficient Updates: Only updates state when query matches actually change
  • Memory Management: Properly cleans up event listeners on unmount
  • Event Listener Optimization: Uses a single event listener per media query
  • Efficient Updates: Only updates state when query matches actually change
  • Memory Management: Properly cleans up event listeners on unmount
  • Memoization: Query strings are memoized to prevent unnecessary re-subscriptions

Contributing

Read the contributing guide to learn about our development process, how to propose bug fixes and improvements, and how to build and test your changes.

License

This project is licensed under the terms of the MIT license.