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 1 commit
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
10 changes: 9 additions & 1 deletion lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,22 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
'space-between' => 'space-between',
);

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

ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
$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
56 changes: 49 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,11 @@ import {
justifyRight,
justifySpaceBetween,
} from '@wordpress/icons';
import { Button } from '@wordpress/components';
import {
Button,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
} from '@wordpress/components';

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

const flexWrapMap = {
wrap: 'wrap',
nowrap: 'nowrap',
};

export default {
name: 'flex',
label: __( 'Flex' ),
Expand All @@ -32,10 +41,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 +72,9 @@ 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 = flexWrapMap[ layout.flexWrap ] || flexWrapMap.wrap;
return (
<style>{ `
${ appendSelectors( selector ) } {
Expand All @@ -70,7 +84,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 +176,31 @@ function FlexLayoutJustifyContentControl( {
</fieldset>
);
}

function FlexWrapControl( { layout, onChange } ) {
const { flexWrap = flexWrapMap.wrap } = layout;
const helpTexts = {
wrap: __( 'Items wrap onto multiple lines.' ),
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
nowrap: __( 'Items are forced onto one line.' ),
};
return (
<ToggleGroupControl
label={ __( 'Flex wrap' ) }
value={ flexWrap }
help={ helpTexts[ flexWrap ] }
onChange={ ( value ) => {
onChange( {
...layout,
flexWrap: value,
} );
} }
isBlock
>
<ToggleGroupControlOption value="wrap" label={ __( 'Wrap' ) } />
<ToggleGroupControlOption
value="nowrap"
label={ __( 'No Wrap' ) }
/>
</ToggleGroupControl>
);
}