Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RNMobile] Correct isMobile condition in nested Media&Text #19778

Merged
merged 3 commits into from
Jan 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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