2020-05-05 08:10:02 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useResizeObserver } from 'wordpress-compose';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a resizeListener element and a class name based on its width.
|
|
|
|
* Class names are based on the smaller of the breakpoints:
|
|
|
|
* https://github.com/WordPress/gutenberg/tree/master/packages/viewport#usage
|
2020-08-20 14:14:12 +00:00
|
|
|
* Values are also based on those breakpoints minus ~80px which is approximately
|
2020-05-05 08:10:02 +00:00
|
|
|
* the left + right margin in Storefront with a font-size of 16px.
|
|
|
|
* _Note: `useContainerQueries` will return an empty class name `` until after
|
|
|
|
* first render_
|
|
|
|
*
|
|
|
|
* @return {Array} An array of {Element} `resizeListener` and {string} `className`.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* const App = () => {
|
2020-05-11 16:07:22 +00:00
|
|
|
* const [ resizeListener, containerClassName ] = useContainerQueries();
|
2020-05-05 08:10:02 +00:00
|
|
|
*
|
|
|
|
* return (
|
2020-05-11 16:07:22 +00:00
|
|
|
* <div className={ containerClassName }>
|
2020-05-05 08:10:02 +00:00
|
|
|
* { resizeListener }
|
|
|
|
* Your content here
|
|
|
|
* </div>
|
|
|
|
* );
|
|
|
|
* };
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export const useContainerQueries = () => {
|
|
|
|
const [ resizeListener, { width } ] = useResizeObserver();
|
|
|
|
|
|
|
|
let className = '';
|
|
|
|
if ( width > 700 ) {
|
|
|
|
className = 'is-large';
|
|
|
|
} else if ( width > 520 ) {
|
|
|
|
className = 'is-medium';
|
|
|
|
} else if ( width > 400 ) {
|
|
|
|
className = 'is-small';
|
|
|
|
} else if ( width ) {
|
|
|
|
className = 'is-mobile';
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ resizeListener, className ];
|
|
|
|
};
|