Skip to content

Commit

Permalink
[RNMobile][FIX] Columns block renders more than two columns in a row (#…
Browse files Browse the repository at this point in the history
…23305)

* fix columnsInRow on init and some performance improvements

* rename getColumnWidth to contentStyle
  • Loading branch information
dratwas committed Jun 19, 2020
1 parent ca30617 commit 64b3ed3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
12 changes: 10 additions & 2 deletions packages/block-editor/src/components/block-list/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class BlockList extends Component {
parentWidth: this.props.parentWidth,
renderFooterAppender: this.props.renderFooterAppender,
onDeleteBlock: this.props.onDeleteBlock,
contentStyle: this.props.contentstyle,
};
this.renderItem = this.renderItem.bind( this );
this.renderBlockListFooter = this.renderBlockListFooter.bind( this );
Expand Down Expand Up @@ -108,16 +109,23 @@ export class BlockList extends Component {
}

getExtraData() {
const { parentWidth, renderFooterAppender, onDeleteBlock } = this.props;
const {
parentWidth,
renderFooterAppender,
onDeleteBlock,
contentStyle,
} = this.props;
if (
this.extraData.parentWidth !== parentWidth ||
this.extraData.renderFooterAppender !== renderFooterAppender ||
this.extraData.onDeleteBlock !== onDeleteBlock
this.extraData.onDeleteBlock !== onDeleteBlock ||
this.extraData.contentStyle !== contentStyle
) {
this.extraData = {
parentWidth,
renderFooterAppender,
onDeleteBlock,
contentStyle,
};
}
return this.extraData;
Expand Down
51 changes: 27 additions & 24 deletions packages/block-library/src/columns/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
BlockVerticalAlignmentToolbar,
} from '@wordpress/block-editor';
import { withDispatch, useSelect } from '@wordpress/data';
import { useEffect, useState } from '@wordpress/element';
import { useEffect, useState, useMemo } from '@wordpress/element';
import { useResizeObserver } from '@wordpress/compose';
import { createBlock } from '@wordpress/blocks';
/**
Expand Down Expand Up @@ -61,33 +61,34 @@ function ColumnsEditContainer( {
const [ resizeListener, sizes ] = useResizeObserver();
const [ columnsInRow, setColumnsInRow ] = useState( MIN_COLUMNS_NUMBER );

const containerMaxWidth = styles.columnsContainer.maxWidth;

const { verticalAlignment } = attributes;
const { width } = sizes || {};

useEffect( () => {
const newColumnCount = ! columnCount ? DEFAULT_COLUMNS : columnCount;
updateColumns( columnCount, newColumnCount );
setColumnsInRow( getColumnsInRow( width, newColumnCount ) );
if ( width ) {
setColumnsInRow( getColumnsInRow( width, newColumnCount ) );
}
}, [ columnCount ] );

useEffect( () => {
setColumnsInRow( getColumnsInRow( width, columnCount ) );
if ( width ) {
setColumnsInRow( getColumnsInRow( width, columnCount ) );
}
}, [ width ] );

const getColumnWidth = ( containerWidth = containerMaxWidth ) => {
const minWidth = Math.min( containerWidth, containerMaxWidth );
const contentStyle = useMemo( () => {
const minWidth = Math.min( width, styles.columnsContainer.maxWidth );
const columnBaseWidth = minWidth / columnsInRow;

let columnWidth = columnBaseWidth;
if ( columnsInRow > 1 ) {
const margins = columnsInRow * 2 * styles.columnMargin.marginLeft;
columnWidth = ( minWidth - margins ) / columnsInRow;
}

return columnWidth;
};
return { width: columnWidth };
}, [ width, columnsInRow ] );

const getColumnsInRow = ( containerWidth, columnsNumber ) => {
if ( containerWidth < BREAKPOINTS.mobile ) {
Expand Down Expand Up @@ -137,20 +138,22 @@ function ColumnsEditContainer( {
</BlockControls>
<View style={ isSelected && styles.innerBlocksSelected }>
{ resizeListener }
<InnerBlocks
renderAppender={ renderAppender }
__experimentalMoverDirection={
columnsInRow > 1 ? 'horizontal' : undefined
}
horizontal={ true }
allowedBlocks={ ALLOWED_BLOCKS }
contentResizeMode="stretch"
onAddBlock={ onAddNextColumn }
onDeleteBlock={
columnCount === 1 ? onDeleteBlock : undefined
}
contentStyle={ { width: getColumnWidth( width ) } }
/>
{ width && (
<InnerBlocks
renderAppender={ renderAppender }
__experimentalMoverDirection={
columnsInRow > 1 ? 'horizontal' : undefined
}
horizontal={ true }
allowedBlocks={ ALLOWED_BLOCKS }
contentResizeMode="stretch"
onAddBlock={ onAddNextColumn }
onDeleteBlock={
columnCount === 1 ? onDeleteBlock : undefined
}
contentStyle={ contentStyle }
/>
) }
</View>
</>
);
Expand Down

0 comments on commit 64b3ed3

Please sign in to comment.