-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from vnvyvu/init
Responsive
- Loading branch information
Showing
4 changed files
with
41 additions
and
4 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
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,23 @@ | ||
import { useState, useCallback, useEffect } from 'react'; | ||
|
||
/** | ||
* isMatches is undefined before hook effect | ||
* @param width to make max-width media query. Default is 767 | ||
* @returns {undefined | boolean} Is media query matches. Undefined if there is no window reference yet | ||
*/ | ||
export const useMediaQuery = (width = 767): undefined | boolean => { | ||
const [isMatches, setIsMatches] = useState<undefined | boolean>(false); | ||
const onChange = useCallback( | ||
() => | ||
setIsMatches(window.matchMedia(`(max-width: ${width}px)`).matches), | ||
[width], | ||
); | ||
|
||
useEffect(() => { | ||
onChange(); | ||
window.addEventListener('resize', onChange); | ||
return () => window.removeEventListener('resize', onChange); | ||
}, [onChange]); | ||
|
||
return isMatches; | ||
}; |