An open API service indexing awesome lists of open source software.

https://github.com/alejandrorm-dev/react-media-queries

react-media-queries is a React hooks library that provides useful custom hooks for responsive design and media queries.
https://github.com/alejandrorm-dev/react-media-queries

react

Last synced: 7 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# React Media Queries

![Build Status](https://img.shields.io/github/actions/workflow/status/AlejandroRM-DEV/react-media-queries/release.yml?branch=main)
![npm version](https://img.shields.io/npm/v/@alejandrorm-dev/react-media-queries)
![npm](https://img.shields.io/npm/dw/@alejandrorm-dev/react-media-queries)
![License](https://img.shields.io/badge/license-MIT-blue)
![GitHub issues](https://img.shields.io/github/issues/AlejandroRM-DEV/react-media-queries)
![GitHub forks](https://img.shields.io/github/forks/AlejandroRM-DEV/react-media-queries)
![GitHub stars](https://img.shields.io/github/stars/AlejandroRM-DEV/react-media-queries)
[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#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](#features)
- [Installation](#installation)
- [Demo](#demo)
- [Usage](#usage)
- [API Reference](#api-reference)
- [SSR Support](#ssr-support)
- [License](#license)
- [Contributing](#contributing)
- [Contributors](#contributors-)

## 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

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

## Demo

https://github.com/user-attachments/assets/dd7719a8-a3bd-4da5-93c2-126bdfc8ceb7

## Usage

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

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

return (

{isLargeScreen ? "Large screen layout" : "Small screen layout"}

);
}
```

## 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:**

```typescript
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](https://allcontributors.org/docs/en/emoji-key)):



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

💻 ⚠️

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!