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

use-media-query-lite-react

v1.0.0

Published

Production-ready React 18 & 19 hook for responsive CSS media queries with SSR hydration safety and instant event cleanup.

Readme

use-media-query-lite-react

Lightweight, responsive CSS media query hook for React 18 & 19 with SSR safety and zero dependencies.

npm version npm downloads license bundle size sponsor


💡 Why use-media-query-lite-react?

Detecting viewport breakpoints and responsive state in modern React often requires either cumbersome CSS-in-JS libraries or bloated responsive utility packages:

  • Zero Dependencies: Pure React hook with no runtime overhead (<600 bytes minified & gzipped).
  • SSR Safe: Configurable defaultMatches fallback ensures zero hydration mismatch warnings in Next.js, Remix, Astro, or Gatsby.
  • Modern & Legacy Event Listeners: Gracefully handles both modern matchMedia.addEventListener('change', ...) and legacy matchMedia.addListener(...) for broad browser compatibility.
  • Dynamic Queries: Automatically re-subscribes if the query string prop changes dynamically during runtime.
  • React 18 & 19 Ready: Built and verified to work seamlessly with React concurrent rendering and StrictMode.

📦 Installation

# npm
npm install use-media-query-lite-react

# pnpm
pnpm add use-media-query-lite-react

# yarn
yarn add use-media-query-lite-react

# bun
bun add use-media-query-lite-react

🚀 Quick Start

import React from 'react';
import { useMediaQuery } from 'use-media-query-lite-react';

export function ResponsiveNavbar() {
  const isMobile = useMediaQuery('(max-width: 768px)');
  const isDarkMode = useMediaQuery('(prefers-color-scheme: dark)');

  return (
    <nav className={isDarkMode ? 'nav-dark' : 'nav-light'}>
      <h1>My App</h1>
      {isMobile ? (
        <button aria-label="Open Mobile Menu">☰</button>
      ) : (
        <ul>
          <li><a href="/home">Home</a></li>
          <li><a href="/about">About</a></li>
          <li><a href="/contact">Contact</a></li>
        </ul>
      )}
    </nav>
  );
}

🛠️ Real-World Examples

1. Next.js / SSR Hydration Safety with defaultMatches

When pre-rendering HTML on the server (SSR or SSG), window.matchMedia is not available. Provide a defaultMatches value to prevent layout flicker and hydration mismatches:

import React from 'react';
import { useMediaQuery } from 'use-media-query-lite-react';

export function HeroBanner() {
  // Assume desktop as SSR default
  const isDesktop = useMediaQuery('(min-width: 1024px)', {
    defaultMatches: true,
  });

  return (
    <header className="hero">
      <h2>Transform Your Workflow</h2>
      <img
        src={isDesktop ? '/images/banner-desktop.png' : '/images/banner-mobile.png'}
        alt="Hero illustration"
      />
    </header>
  );
}

2. Device Orientation & Aspect Ratio Detection

import React from 'react';
import { useMediaQuery } from 'use-media-query-lite-react';

export function VideoPlayer() {
  const isPortrait = useMediaQuery('(orientation: portrait)');
  const isRetina = useMediaQuery('(-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi)');

  return (
    <div className={`video-container ${isPortrait ? 'portrait-view' : 'landscape-view'}`}>
      <video controls poster={isRetina ? '/[email protected]' : '/poster.jpg'}>
        <source src="/stream.mp4" type="video/mp4" />
      </video>
    </div>
  );
}

3. Accessible Reduced Motion Preferences

Respect user operating system accessibility settings:

import React from 'react';
import { useMediaQuery } from 'use-media-query-lite-react';

export function AnimatedCarousel() {
  const prefersReducedMotion = useMediaQuery('(prefers-reduced-motion: reduce)');

  return (
    <div
      className="carousel"
      style={{
        transitionDuration: prefersReducedMotion ? '0s' : '0.5s',
      }}
    >
      {/* Slides */}
    </div>
  );
}

4. Reusable Custom Responsive Breakpoint Hooks

Create ergonomic design system helpers for your application:

import { useMediaQuery } from 'use-media-query-lite-react';

export const useIsMobile = () => useMediaQuery('(max-width: 639px)');
export const useIsTablet = () => useMediaQuery('(min-width: 640px) and (max-width: 1023px)');
export const useIsDesktop = () => useMediaQuery('(min-width: 1024px)');
export const useIsDarkMode = () => useMediaQuery('(prefers-color-scheme: dark)');

📖 API Reference

useMediaQuery(query, options?)

Listens to changes in a CSS media query and returns whether it matches.

  • Parameters:
    • query (string): Valid CSS media query string (e.g., '(max-width: 768px)').
    • options (UseMediaQueryOptions, optional): Configuration options.
  • Returns: boolean (true if matching, false otherwise).

Options (UseMediaQueryOptions):

| Option | Type | Default | Description | |---|---|---|---| | defaultMatches | boolean | false | Initial fallback value during SSR or before hydration |


🏷️ TypeScript Interfaces

export interface UseMediaQueryOptions {
  /**
   * Fallback value used when rendering in SSR environments where window is undefined.
   * @default false
   */
  defaultMatches?: boolean;
}

🧪 Testing

Every release is tested with 100% test coverage using Vitest:

npm test

💖 Support the Project

If use-media-query-lite-react made responsive React development cleaner, please consider sponsoring:

👉 Sponsor @Piratha97 on GitHub Sponsors


📄 License

MIT © 2026 Pirapaakaran Pirathapan