Skip to content

Commit

Permalink
[RNMobile] Correct isMobile condition in nested Media&Text (#19778)
Browse files Browse the repository at this point in the history
* Correct isMobile condition in nested Media&Text

* Do not export BREAKPOINTS
  • Loading branch information
lukewalczak authored Jan 28, 2020
1 parent ba2495f commit 08182bf
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions packages/block-library/src/media-text/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
} from '@wordpress/components';
import { withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { withViewportMatch } from '@wordpress/viewport';

/**
* Internal dependencies
Expand All @@ -37,6 +36,9 @@ const TEMPLATE = [
];
// this limits the resize to a safe zone to avoid making broken layouts
const WIDTH_CONSTRAINT_PERCENTAGE = 15;
const BREAKPOINTS = {
mobile: 480,
};
const applyWidthConstraints = ( width ) => Math.max( WIDTH_CONSTRAINT_PERCENTAGE, Math.min( width, 100 - WIDTH_CONSTRAINT_PERCENTAGE ) );

class MediaTextEdit extends Component {
Expand All @@ -47,8 +49,11 @@ class MediaTextEdit extends Component {
this.onMediaUpdate = this.onMediaUpdate.bind( this );
this.onWidthChange = this.onWidthChange.bind( this );
this.commitWidthChange = this.commitWidthChange.bind( this );
this.onLayoutChange = this.onLayoutChange.bind( this );

this.state = {
mediaWidth: null,
containerWidth: 0,
};
}

Expand Down Expand Up @@ -111,6 +116,19 @@ class MediaTextEdit extends Component {
} );
}

onLayoutChange( { nativeEvent } ) {
const { width } = nativeEvent.layout;
const { containerWidth } = this.state;

if ( containerWidth === width ) {
return null;
}

this.setState( {
containerWidth: width,
} );
}

renderMediaArea() {
const { attributes, isSelected } = this.props;
const { mediaAlt, mediaId, mediaPosition, mediaType, mediaUrl, mediaWidth, imageFill, focalPoint } = attributes;
Expand All @@ -132,7 +150,6 @@ class MediaTextEdit extends Component {
attributes,
backgroundColor,
setAttributes,
isMobile,
isSelected,
isParentSelected,
isAncestorSelected,
Expand All @@ -143,6 +160,11 @@ class MediaTextEdit extends Component {
mediaWidth,
verticalAlignment,
} = attributes;
const {
containerWidth,
} = this.state;

const isMobile = containerWidth < BREAKPOINTS.mobile;
const shouldStack = isStackedOnMobile && isMobile;
const temporaryMediaWidth = shouldStack ? 100 : ( this.state.mediaWidth || mediaWidth );
const widthString = `${ temporaryMediaWidth }%`;
Expand Down Expand Up @@ -195,7 +217,7 @@ class MediaTextEdit extends Component {
isCollapsed={ false }
/>
</BlockControls>
<View style={ containerStyles }>
<View style={ containerStyles } onLayout={ this.onLayoutChange }>
<View style={ { width: widthString, ...mediaContainerStyle } } >
{ this.renderMediaArea() }
</View>
Expand All @@ -214,7 +236,6 @@ class MediaTextEdit extends Component {

export default compose(
withColors( 'backgroundColor' ),
withViewportMatch( { isMobile: '< small' } ),
withSelect( ( select, { clientId } ) => {
const {
getSelectedBlockClientId,
Expand Down

0 comments on commit 08182bf

Please sign in to comment.