Skip to content

Commit

Permalink
Fix: ResponsiveWrapper can not handle cases with small dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Mar 16, 2020
1 parent 844b932 commit c8ce275
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/components/src/responsive-wrapper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import useResizeAware from 'react-resize-aware';

/**
* WordPress dependencies
Expand All @@ -14,15 +15,23 @@ function ResponsiveWrapper( {
children,
isInline = false,
} ) {
const [
containerResizeListener,
{ width: containerWidth },
] = useResizeAware();
if ( Children.count( children ) !== 1 ) {
return null;
}
const imageStyle = {
paddingBottom: ( naturalHeight / naturalWidth ) * 100 + '%',
paddingBottom:
naturalWidth < containerWidth
? naturalHeight
: ( naturalHeight / naturalWidth ) * 100 + '%',
};
const TagName = isInline ? 'span' : 'div';
return (
<TagName className="components-responsive-wrapper">
{ containerResizeListener }
<TagName style={ imageStyle } />
{ cloneElement( children, {
className: classnames(
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/responsive-wrapper/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
left: 0;
width: 100%;
height: 100%;
margin: auto;
}

0 comments on commit c8ce275

Please sign in to comment.