Skip to content

Commit

Permalink
Fix buggy align behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
tellthemachines committed Aug 4, 2022
1 parent fe65860 commit 87d964a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
25 changes: 22 additions & 3 deletions packages/block-editor/src/layouts/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/

import { getBlockGapCSS } from './utils';
import { getBlockGapCSS, getAlignmentsInfo } from './utils';
import { getGapBoxControlValueFromStyle } from '../hooks/gap';
import { shouldSkipSerialization } from '../hooks/utils';

Expand Down Expand Up @@ -54,7 +54,26 @@ export default {
getOrientation() {
return 'vertical';
},
getAlignments() {
return [];
getAlignments( layout ) {
const alignmentInfo = getAlignmentsInfo( layout );
if ( layout.alignments !== undefined ) {
if ( ! layout.alignments.includes( 'none' ) ) {
layout.alignments.unshift( 'none' );
}
return layout.alignments.map( ( alignment ) => ( {
name: alignment,
info: alignmentInfo[ alignment ],
} ) );
}

const alignments = [
{ name: 'left' },
{ name: 'center' },
{ name: 'right' },
];

alignments.unshift( { name: 'none', info: alignmentInfo.none } );

return alignments;
},
};
34 changes: 34 additions & 0 deletions packages/block-editor/src/layouts/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';

/**
* Utility to generate the proper CSS selector for layout styles.
*
Expand Down Expand Up @@ -62,3 +67,32 @@ export function getBlockGapCSS(
}
return output;
}

/**
* Helper method to assign contextual info to clarify
* alignment settings.
*
* Besides checking if `contentSize` and `wideSize` have a
* value, we now show this information only if their values
* are not a `css var`. This needs to change when parsing
* css variables land.
*
* @see https://github.com/WordPress/gutenberg/pull/34710#issuecomment-918000752
*
* @param {Object} layout The layout object.
* @return {Object} An object with contextual info per alignment.
*/
export function getAlignmentsInfo( layout ) {
const { contentSize, wideSize } = layout;
const alignmentInfo = {};
const sizeRegex = /^(?!0)\d+(px|em|rem|vw|vh|%)?$/i;
if ( sizeRegex.test( contentSize ) ) {
// translators: %s: container size (i.e. 600px etc)
alignmentInfo.none = sprintf( __( 'Max %s wide' ), contentSize );
}
if ( sizeRegex.test( wideSize ) ) {
// translators: %s: container size (i.e. 600px etc)
alignmentInfo.wide = sprintf( __( 'Max %s wide' ), wideSize );
}
return alignmentInfo;
}
7 changes: 3 additions & 4 deletions packages/block-library/src/group/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ function GroupEdit( { attributes, setAttributes, clientId } ) {
);
const defaultLayout = useSetting( 'layout' ) || {};
const { tagName: TagName = 'div', templateLock, layout = {} } = attributes;
const usedLayout =
! layout?.type || layout?.type === 'column'
? { ...defaultLayout, ...layout, type: 'column' }
: layout;
const usedLayout = ! layout?.type
? { ...defaultLayout, ...layout, type: 'default' }
: { ...defaultLayout, ...layout };
const { type = 'default' } = usedLayout;
const layoutSupportEnabled = themeSupportsLayout || type !== 'default';

Expand Down

0 comments on commit 87d964a

Please sign in to comment.