-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e30bc2
commit 935bdbf
Showing
6 changed files
with
80 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
/** | ||
* | ||
* A custom hook which takes a media query as the argument and returns a state | ||
* which will be true when the media query is active and false when not | ||
* | ||
* @example | ||
* const isMobile = useMediaQuery("(max-width:600px)"); | ||
* | ||
* @returns {boolean} Is the media query active | ||
*/ | ||
const useMediaQuery = (mediaQuery) => { | ||
const [doesQueryMatch, setDoesQueryMatch] = useState(() => window.matchMedia(mediaQuery).matches); | ||
|
||
useEffect(() => { | ||
const mediaQueryChangeHandler = (e) => { | ||
setDoesQueryMatch(e.matches); | ||
}; | ||
|
||
// Adding event listener for media query change on component mount | ||
window.matchMedia(mediaQuery).addEventListener('change', mediaQueryChangeHandler); | ||
|
||
// Removing event listener component unmount | ||
return () => { | ||
window.matchMedia(mediaQuery).removeEventListener('change', mediaQueryChangeHandler); | ||
}; | ||
}, []); | ||
|
||
return doesQueryMatch; | ||
}; | ||
|
||
export default useMediaQuery; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters