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.
- 🚀 Performant: Uses
useSyncExternalStorefor 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
npm install @alejandrorm-dev/react-media-queriesDemo.mov
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>
);
}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)");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.
This project is licensed under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request.
Thanks goes to these wonderful people (emoji key):
Alejandro Ramírez Muñoz 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!