Skip to content

Commit 935bdbf

Browse files
committed
📱 Add useMediaQuery hook
1 parent 8e30bc2 commit 935bdbf

File tree

6 files changed

+80
-12
lines changed

6 files changed

+80
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ yarn add use-custom-hooks
5151
- [usePrevious](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-useprevious)
5252
- [useDebounce](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usedebounce)
5353
- [useDarkMode](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usedarkmode)
54+
- [useMediaQuery](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usemediaquery)
5455
- [useGeoLocation](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usegeolocation)
5556
- [useLocalStorage](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-uselocalstorage)
5657
- [useMousePosition](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usemouseposition)
@@ -129,4 +130,4 @@ SOFTWARE.
129130

130131
## ✍🏻 Creator
131132

132-
[Aromal Anil](https://aromalanil.me)
133+
[Aromal Anil](https://aromalanil.tech)

docs/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 📗 Index
44

55
- [useLocalStorage](#-uselocalstorage)
6+
- [useMediaQuery](#-usemediaquery)
67
- [useForm](#-useform)
78
- [useStack](#-usestack)
89
- [useQueue](#-usequeue)
@@ -53,6 +54,38 @@ const LocalValue = () => {
5354

5455
</br>
5556

57+
## 📱 useMediaQuery
58+
59+
Custom hook which listens for a media query and updates the state when the query is active/inactive
60+
61+
### Usage
62+
63+
```jsx
64+
import React from "react";
65+
import { useMediaQuery } from "use-custom-hooks";
66+
67+
const BottomNav = () => {
68+
const isMobileDevice = useMediaQuery("(max-width:600px)");
69+
/*
70+
If isMobileDevice will be true when the screen size is less than
71+
600px, and false otherwise
72+
*/
73+
74+
// Component will only be rendered in mobile devices
75+
return isMobileDevice ? <div className="bottom-nav"></div> : null;
76+
};
77+
```
78+
79+
### Parameters
80+
81+
1. `mediaQuery` (_String_) : The media query to listen to.
82+
83+
### Return value
84+
85+
1. `isMediaQueryActive` (_any_) : A boolean state denoting if the media query is active or not
86+
87+
</br>
88+
5689
## 📃 useForm
5790

5891
Custom hook to create controlled form component.

hooks/useMediaQuery.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { useEffect, useState } from 'react';
2+
3+
/**
4+
*
5+
* A custom hook which takes a media query as the argument and returns a state
6+
* which will be true when the media query is active and false when not
7+
*
8+
* @example
9+
* const isMobile = useMediaQuery("(max-width:600px)");
10+
*
11+
* @returns {boolean} Is the media query active
12+
*/
13+
const useMediaQuery = (mediaQuery) => {
14+
const [doesQueryMatch, setDoesQueryMatch] = useState(() => window.matchMedia(mediaQuery).matches);
15+
16+
useEffect(() => {
17+
const mediaQueryChangeHandler = (e) => {
18+
setDoesQueryMatch(e.matches);
19+
};
20+
21+
// Adding event listener for media query change on component mount
22+
window.matchMedia(mediaQuery).addEventListener('change', mediaQueryChangeHandler);
23+
24+
// Removing event listener component unmount
25+
return () => {
26+
window.matchMedia(mediaQuery).removeEventListener('change', mediaQueryChangeHandler);
27+
};
28+
}, []);
29+
30+
return doesQueryMatch;
31+
};
32+
33+
export default useMediaQuery;

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export { default as useToggle } from './hooks/useToggle';
55
export { default as useDarkMode } from './hooks/useDarkMode';
66
export { default as usePrevious } from './hooks/usePrevious';
77
export { default as useDebounce } from './hooks/useDebounce';
8+
export { default as useMediaQuery } from './hooks/useMediaQuery';
89
export { default as useGeoLocation } from './hooks/useGeoLocation';
910
export { default as useLocalStorage } from './hooks/useLocalStorage';
1011
export { default as useMousePosition } from './hooks/useMousePosition';

package-lock.json

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-custom-hooks",
3-
"version": "1.9.0",
3+
"version": "1.10.0",
44
"description": "A collection of Custom Hooks for React.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)