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

[Block Editor]: Add flex-wrap control to flex layout #36003

Merged
merged 2 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,19 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
'space-between' => 'space-between',
);

$flex_wrap_options = array( 'wrap', 'nowrap' );
$flex_wrap = ! empty( $layout['flexWrap'] ) && in_array( $layout['flexWrap'], $flex_wrap_options, true ) ?
$layout['flexWrap'] :
'wrap';

$style = "$selector {";
$style .= 'display: flex;';
if ( $has_block_gap_support ) {
$style .= 'gap: var( --wp--style--block-gap, 0.5em );';
} else {
$style .= 'gap: 0.5em;';
}
$style .= 'flex-wrap: wrap;';
$style .= "flex-wrap: $flex_wrap;";
$style .= 'align-items: center;';
/**
* Add this style only if is not empty for backwards compatibility,
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
}

.block-editor-hooks__flex-layout-justification-controls {
margin-bottom: $grid-unit-15;
legend {
margin-bottom: $grid-unit-10;
}
Expand Down
39 changes: 32 additions & 7 deletions packages/block-editor/src/layouts/flex.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
justifyRight,
justifySpaceBetween,
} from '@wordpress/icons';
import { Button } from '@wordpress/components';
import { Button, ToggleControl } from '@wordpress/components';

/**
* Internal dependencies
Expand All @@ -24,6 +24,8 @@ const justifyContentMap = {
'space-between': 'space-between',
};

const flexWrapOptions = [ 'wrap', 'nowrap' ];

export default {
name: 'flex',
label: __( 'Flex' ),
Expand All @@ -32,10 +34,13 @@ export default {
onChange,
} ) {
return (
<FlexLayoutJustifyContentControl
layout={ layout }
onChange={ onChange }
/>
<>
<FlexLayoutJustifyContentControl
layout={ layout }
onChange={ onChange }
/>
<FlexWrapControl layout={ layout } onChange={ onChange } />
</>
);
},
toolBarControls: function FlexLayoutToolbarControls( {
Expand All @@ -60,7 +65,11 @@ export default {
const blockGapSupport = useSetting( 'spacing.blockGap' );
const hasBlockGapStylesSupport = blockGapSupport !== null;
const justifyContent =
justifyContentMap[ layout.justifyContent ] || 'flex-start';
justifyContentMap[ layout.justifyContent ] ||
justifyContentMap.left;
const flexWrap = flexWrapOptions.includes( layout.flexWrap )
? layout.flexWrap
: 'wrap';
return (
<style>{ `
${ appendSelectors( selector ) } {
Expand All @@ -70,7 +79,7 @@ export default {
? 'var( --wp--style--block-gap, 0.5em )'
: '0.5em'
};
flex-wrap: wrap;
flex-wrap: ${ flexWrap };
align-items: center;
flex-direction: row;
justify-content: ${ justifyContent };
Expand Down Expand Up @@ -162,3 +171,19 @@ function FlexLayoutJustifyContentControl( {
</fieldset>
);
}

function FlexWrapControl( { layout, onChange } ) {
const { flexWrap = 'wrap' } = layout;
return (
<ToggleControl
label={ __( 'Allow to wrap to multiple lines' ) }
onChange={ ( value ) => {
onChange( {
...layout,
flexWrap: value ? 'wrap' : 'nowrap',
} );
} }
checked={ flexWrap === 'wrap' }
/>
);
}