-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
c65477e
commit 3c85f9e
Showing
11 changed files
with
135 additions
and
44 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
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,56 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import useMediaQuery from '../use-media-query'; | ||
|
||
/** | ||
* Hash of breakpoint names with pixel width at which it becomes effective. | ||
* | ||
* @see _breakpoints.scss | ||
* | ||
* @type {Object} | ||
*/ | ||
const BREAKPOINTS = { | ||
huge: 1440, | ||
wide: 1280, | ||
large: 960, | ||
medium: 782, | ||
small: 600, | ||
mobile: 480, | ||
}; | ||
|
||
/** | ||
* Object mapping media query operators to the condition to be used. | ||
* | ||
* @type {Object} | ||
*/ | ||
const CONDITIONS = { | ||
'>=': 'min-width', | ||
'<': 'max-width', | ||
}; | ||
|
||
/** | ||
* Returns true if the viewport matches the given query, or false otherwise. | ||
* | ||
* @param {string} query Query string. Includes operator and breakpoint name, | ||
* space separated. Operator defaults to >=. | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* useViewportMatch( '< huge' ); | ||
* useViewportMatch( 'medium' ); | ||
* ``` | ||
* | ||
* @return {boolean} Whether viewport matches query. | ||
*/ | ||
const useViewportMatch = ( query ) => { | ||
if ( query.indexOf( ' ' ) === -1 ) { | ||
query = '>= ' + query; | ||
} | ||
const [ operator, breakpoint ] = query.split( ' ' ); | ||
const mediaQuery = `(${ CONDITIONS[ operator ] }: ${ BREAKPOINTS[ breakpoint ] }px)`; | ||
return useMediaQuery( mediaQuery ); | ||
}; | ||
|
||
export default useViewportMatch; |
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
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