-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add useViewportMatch react hook alternative to withViewportMatch (#18816
- Loading branch information
1 parent
6ec0934
commit f4300ff
Showing
14 changed files
with
128 additions
and
51 deletions.
There are no files selected for viewing
9 changes: 7 additions & 2 deletions
9
packages/block-editor/src/components/block-mobile-toolbar/index.js
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 |
---|---|---|
@@ -1,19 +1,24 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { ifViewportMatches } from '@wordpress/viewport'; | ||
import { useViewportMatch } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BlockMover from '../block-mover'; | ||
|
||
function BlockMobileToolbar( { clientId, moverDirection } ) { | ||
const isMobile = useViewportMatch( 'small', '<' ); | ||
if ( ! isMobile ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="block-editor-block-mobile-toolbar"> | ||
<BlockMover clientIds={ [ clientId ] } __experimentalOrientation={ moverDirection } /> | ||
</div> | ||
); | ||
} | ||
|
||
export default ifViewportMatches( '< small' )( BlockMobileToolbar ); | ||
export default BlockMobileToolbar; |
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,60 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import useMediaQuery from '../use-media-query'; | ||
|
||
/** | ||
* @typedef {"huge"|"wide"|"large"|"medium"|"small"|"mobile"} WPBreakpoint | ||
*/ | ||
|
||
/** | ||
* Hash of breakpoint names with pixel width at which it becomes effective. | ||
* | ||
* @see _breakpoints.scss | ||
* | ||
* @type {Object<WPBreakpoint,number>} | ||
*/ | ||
const BREAKPOINTS = { | ||
huge: 1440, | ||
wide: 1280, | ||
large: 960, | ||
medium: 782, | ||
small: 600, | ||
mobile: 480, | ||
}; | ||
|
||
/** | ||
* @typedef {">="|"<"} WPViewportOperator | ||
*/ | ||
|
||
/** | ||
* Object mapping media query operators to the condition to be used. | ||
* | ||
* @type {Object<WPViewportOperator,string>} | ||
*/ | ||
const CONDITIONS = { | ||
'>=': 'min-width', | ||
'<': 'max-width', | ||
}; | ||
|
||
/** | ||
* Returns true if the viewport matches the given query, or false otherwise. | ||
* | ||
* @param {WPBreakpoint} breakpoint Breakpoint size name. | ||
* @param {WPViewportOperator} [operator=">="] Viewport operator. | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* useViewportMatch( 'huge', <' ); | ||
* useViewportMatch( 'medium' ); | ||
* ``` | ||
* | ||
* @return {boolean} Whether viewport matches query. | ||
*/ | ||
const useViewportMatch = ( breakpoint, operator = '>=' ) => { | ||
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
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
2 changes: 1 addition & 1 deletion
2
packages/editor/src/components/post-saved-state/test/__snapshots__/index.js.snap
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