Skip to content

react-media-queries is a React hooks library that provides useful custom hooks for responsive design and media queries.

License

Notifications You must be signed in to change notification settings

AlejandroRM-DEV/react-media-queries

Repository files navigation

React Media Queries

Build Status npm version npm License GitHub issues GitHub forks GitHub stars All Contributors

A lightweight, performant React hooks library for responsive design and media query management. Built with TypeScript and optimized for modern React applications, including SSR support.

Table of Contents

Features

  • 🚀 Performant: Uses useSyncExternalStore for optimal performance and React 18+ compatibility
  • 🔄 SSR Compatible: Handles server-side rendering gracefully
  • 🎯 TypeScript: Full TypeScript support with type definitions
  • 🪝 Hooks-Based: Modern React hooks API
  • Efficient: Shared stores prevent duplicate media query listeners

Installation

npm install @alejandrorm-dev/react-media-queries

Demo

Demo.mov

Usage

import { useMediaQuery } from "@alejandrorm-dev/react-media-queries";

function ResponsiveComponent() {
	const isLargeScreen = useMediaQuery("(min-width: 1200px)");

	return (
		<div>{isLargeScreen ? "Large screen layout" : "Small screen layout"}</div>
	);
}

API Reference

useMediaQuery(query: string): boolean

Evaluates a single CSS media query and returns its current match state.

Parameters:

  • query (string): The CSS media query string to evaluate (e.g., "(min-width: 768px)")

Returns: boolean - true if the media query matches, false otherwise. Returns false during SSR.

Example:

const isTablet = useMediaQuery("(min-width: 768px) and (max-width: 1024px)");

SSR Support

The hook is designed to work seamlessly in server-side rendering environments. During SSR, useMediaQuery returns false. On the client, the value updates to reflect the actual media query state.

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Alejandro Ramírez Muñoz
Alejandro Ramírez Muñoz

💻 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

About

react-media-queries is a React hooks library that provides useful custom hooks for responsive design and media queries.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •