-
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.
Viewport: Split withViewportMatch from ifViewportMatches
- Loading branch information
Showing
8 changed files
with
89 additions
and
29 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
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,29 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { mount } from 'enzyme'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { dispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import '../store'; | ||
import withViewportMatch from '../with-viewport-match'; | ||
|
||
describe( 'withViewportMatch()', () => { | ||
const Component = () => <div>Hello</div>; | ||
|
||
it( 'should render with result of query as custom prop name', () => { | ||
dispatch( 'core/viewport' ).setIsMatching( { '> wide': true } ); | ||
const EnhancedComponent = withViewportMatch( { isWide: '> wide' } )( Component ); | ||
const wrapper = mount( <EnhancedComponent /> ); | ||
|
||
expect( wrapper.find( Component ).props() ).toEqual( { isWide: true } ); | ||
|
||
wrapper.unmount(); | ||
} ); | ||
} ); |
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,35 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { mapValues } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { compose, getWrapperDisplayName } from '@wordpress/element'; | ||
import { withSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Higher-order component creator, creating a new component which renders if | ||
* the viewport query is satisfied or with the given optional prop name. | ||
* | ||
* @param {Object} queries Object of prop name to viewport query. | ||
* @param {string} propName Optional prop name to which result is assigned. | ||
* | ||
* @see isViewportMatch | ||
* | ||
* @return {Function} Higher-order component. | ||
*/ | ||
const withViewportMatch = ( queries ) => ( WrappedComponent ) => { | ||
const EnhancedComponent = compose( [ | ||
withSelect( ( select ) => mapValues( queries, ( query ) => { | ||
return select( 'core/viewport' ).isViewportMatch( query ); | ||
} ) ), | ||
] )( WrappedComponent ); | ||
|
||
EnhancedComponent.displayName = getWrapperDisplayName( WrappedComponent, 'withViewportMatch' ); | ||
|
||
return EnhancedComponent; | ||
}; | ||
|
||
export default withViewportMatch; |