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

WIP: [Block Library - Social Icons]: Make the block use the new flex layout #33987

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 4 additions & 1 deletion lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ function gutenberg_get_layout_style( $selector, $layout ) {
$style .= "$selector .alignleft { float: left; margin-right: 2em; }";
$style .= "$selector .alignright { float: right; margin-left: 2em; }";
} elseif ( 'flex' === $layout_type ) {
$justify_content = isset( $layout['justifyContent'] ) ? $layout['justifyContent'] : 'flex-start';

$style = "$selector {";
$style .= 'display: flex;';
$style .= 'flex-direction: row;';
$style .= 'column-gap: 0.5em;';
$style .= 'align-items: center;';
$style .= "justify-content: $justify_content;";
$style .= '}';
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was expecting more changes here to retrieve the "default layout" from the block support config and apply it when the "layout" attribute is empty.

Expand Down
66 changes: 34 additions & 32 deletions packages/block-editor/src/hooks/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import { addFilter } from '@wordpress/hooks';
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import {
Button,
ButtonGroup,
BaseControl,
ToggleControl,
PanelBody,
__experimentalSegmentedControl as SegmentedControl,
__experimentalSegmentedControlOption as SegmentedControlOption,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useContext, createPortal } from '@wordpress/element';
Expand All @@ -32,40 +33,40 @@ import { getLayoutType, getLayoutTypes } from '../layouts';

const layoutBlockSupportKey = '__experimentalLayout';

const canBlockSwitchLayout = ( blockTypeOrName ) => {
const layoutBlockSupportConfig = getBlockSupport(
blockTypeOrName,
layoutBlockSupportKey
);

return layoutBlockSupportConfig?.allowSwitching;
};

function LayoutPanel( { setAttributes, attributes, name: blockName } ) {
const { layout = {} } = attributes;
const defaultLayout = useSetting( 'layout' );
const { layout } = attributes;
// TODO: check if a theme should provide default values per `layoutType`.
// Fow now we use the values from `flow` (content, width).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now the default layout value in theme.json has not a type prop. It assumes it is the default/flow layout and this is how it is used for example in Group block. With the expansion of new layouts wouldn't we want themes to provide some defaults for other layouts as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my mind, the theme provides a single default layout and chooses its type and doesn't provide defaults for all layouts. The default layout is automatically used in the post editor canvas.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the provided by the theme default should be only about flow layout, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a theme would want to use a "grid" layout by default or some other layout we didn't build yet but yeah in most cases, it will be a "flow" one I think.

const defaultThemeLayout = useSetting( 'layout' );
const themeSupportsLayout = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
return getSettings().supportsLayout;
}, [] );

// TODO: If we implement layout for blocks and replace
// current display of them like this PR explores, shouldn't this check change?
if ( ! themeSupportsLayout ) {
return null;
}

const allowLayoutSwitching = canBlockSwitchLayout( blockName );
const { inherit = false, type = 'default' } = layout;
const {
allowSwitching: canBlockSwitchLayout,
default: defaultBlockLayout,
} = getBlockSupport( blockName, layoutBlockSupportKey ) || {};

const usedLayout = layout ? layout : defaultBlockLayout || {};
const { inherit = false, type = 'default' } = usedLayout;
const layoutType = getLayoutType( type );

const onChangeType = ( newType ) =>
setAttributes( { layout: { type: newType } } );
setAttributes( { layout: { type: newType } } ); // TODO needs checking with block defaults.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify this comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure yet 😄 . I'm adding some comments to check it more when the time comes. I just thought I should check if we would need to add the defaultBlockLayout props if exist as well, because this line will trigger a rerender and the logic is if layout is truthy. Therefore I'm not sure it will behave properly.

const onChangeLayout = ( newLayout ) =>
setAttributes( { layout: newLayout } );

return (
<InspectorControls>
<PanelBody title={ __( 'Layout' ) }>
{ !! defaultLayout && (
{ inherit && !! defaultThemeLayout && (
<ToggleControl
label={ __( 'Inherit default layout' ) }
checked={ !! inherit }
Expand All @@ -74,8 +75,7 @@ function LayoutPanel( { setAttributes, attributes, name: blockName } ) {
}
/>
) }

{ ! inherit && allowLayoutSwitching && (
{ ! inherit && canBlockSwitchLayout && (
<LayoutTypeSwitcher
type={ type }
onChange={ onChangeType }
Expand All @@ -84,7 +84,7 @@ function LayoutPanel( { setAttributes, attributes, name: blockName } ) {

{ ! inherit && layoutType && (
<layoutType.edit
layout={ layout }
layout={ usedLayout }
onChange={ onChangeLayout }
/>
) }
Expand All @@ -95,19 +95,21 @@ function LayoutPanel( { setAttributes, attributes, name: blockName } ) {

function LayoutTypeSwitcher( { type, onChange } ) {
return (
<ButtonGroup>
{ getLayoutTypes().map( ( { name, label } ) => {
return (
<Button
<BaseControl>
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
<SegmentedControl
value={ type }
onChange={ onChange }
isBlock={ true }
>
{ getLayoutTypes().map( ( { name, label } ) => (
<SegmentedControlOption
key={ name }
isPressed={ type === name }
onClick={ () => onChange( name ) }
>
{ label }
</Button>
);
} ) }
</ButtonGroup>
value={ name }
label={ label }
></SegmentedControlOption>
) ) }
</SegmentedControl>
</BaseControl>
);
}

Expand Down
30 changes: 26 additions & 4 deletions packages/block-editor/src/layouts/flex.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,44 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import { appendSelectors } from './utils';
import { JustifyContentControl } from '../components/justify-content-control';

const HORIZONTAL_JUSTIFY_CONTROLS = {
left: 'flex-start',
center: 'center',
right: 'flex-end',
'space-between': 'space-between',
};
youknowriad marked this conversation as resolved.
Show resolved Hide resolved

export default {
name: 'flex',

label: __( 'Flex' ),

edit() {
return null;
edit: function LayoutFlexEdit( { layout = {}, onChange } ) {
const { justifyContent = 'flex-start' } = layout;
return (
<JustifyContentControl
allowedControls={ Object.keys( HORIZONTAL_JUSTIFY_CONTROLS ) }
value={ justifyContent }
onChange={ ( value ) => {
onChange( {
...layout,
justifyContent: HORIZONTAL_JUSTIFY_CONTROLS[ value ],
} );
} }
/>
);
},

save: function FlexLayoutStyle( { selector } ) {
save: function FlexLayoutStyle( { selector, layout = {} } ) {
const { justifyContent = 'flex-start' } = layout;
return (
<style>{ `${ appendSelectors( selector ) } {
display: flex;
column-gap: 0.5em;
align-items: center;
flex-direction: row;
justify-content: ${ justifyContent };
}` }</style>
);
},
Expand Down
3 changes: 0 additions & 3 deletions packages/block-editor/src/layouts/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,9 @@ export default {
margin-left: auto !important;
margin-right: auto !important;
}

${ appendSelectors( selector, '> [data-align="wide"]' ) } {
max-width: ${ wideSize ?? contentSize };
}

${ appendSelectors( selector, '> [data-align="full"]' ) } {
max-width: none;
}
Expand All @@ -129,7 +127,6 @@ export default {
float: left;
margin-right: 2em;
}

${ appendSelectors( selector, '> [data-align="right"]' ) } {
float: right;
margin-left: 2em;
Expand Down
11 changes: 10 additions & 1 deletion packages/block-library/src/social-links/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@
},
"supports": {
"align": [ "left", "center", "right" ],
"anchor": true
"anchor": true,
"__experimentalLayout": {
"allowSwitching": false,
"inherit": false,
"default": {
"type": "flex",
"justifyContent": "flex-start",
"column-gap": "normal"
}
}
},
"styles": [
{ "name": "default", "label": "Default", "isDefault": true },
Expand Down
95 changes: 95 additions & 0 deletions packages/block-library/src/social-links/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,103 @@ import classNames from 'classnames';
*/
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';

const justifyContentMap = {
left: 'flex-start',
right: 'flex-end',
center: 'center',
'space-between': 'space-between',
};

// TODO: this is super temp implementation to test quickly.
// This will need to be applied to the others migrations.
// The problem exists because `itemsJustification` was introduced in https://github.com/WordPress/gutenberg/pull/28980/files
// and wasn't declared in block.json. I'll create a separate issue for this as well.
const migrateWithLayout = ( attributes ) => {
if ( !! attributes.layout ) {
return attributes;
}

let justifyContent = 'flex-start';
let className = attributes.className;
const cssClasses = className?.split( ' ' );
if ( cssClasses ) {
const prefix = 'item-justified-';
className = cssClasses.reduce( ( accumulator, cssClass ) => {
if ( ! cssClass.startsWith( prefix ) ) {
justifyContent =
justifyContentMap[ cssClass.slice( prefix.length + 1 ) ];
return accumulator;
}
return `${ accumulator } ${ cssClass }`;
}, '' );
}
return {
...attributes,
className,
layout: {
type: 'flex',
justifyContent,
'column-gap': 'normal',
},
};
};

// Social Links block deprecations.
const deprecated = [
// Implement `flex` layout.
{
attributes: {
iconColor: {
type: 'string',
},
customIconColor: {
type: 'string',
},
iconColorValue: {
type: 'string',
},
iconBackgroundColor: {
type: 'string',
},
customIconBackgroundColor: {
type: 'string',
},
iconBackgroundColorValue: {
type: 'string',
},
openInNewTab: {
type: 'boolean',
default: false,
},
size: {
type: 'string',
},
},
isEligible: ( { layout } ) => ! layout,
migrate: migrateWithLayout,
save( props ) {
const {
attributes: {
iconBackgroundColorValue,
iconColorValue,
itemsJustification,
size,
},
} = props;

const className = classNames( size, {
'has-icon-color': iconColorValue,
'has-icon-background-color': iconBackgroundColorValue,
[ `items-justified-${ itemsJustification }` ]: itemsJustification,
} );

return (
<ul { ...useBlockProps.save( { className } ) }>
<InnerBlocks.Content />
</ul>
);
},
},
// V1. Remove CSS variable use for colors.
{
attributes: {
Expand Down
Loading