From 87d964a2319ca791d822f02b465242ca76f7870e Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Wed, 3 Aug 2022 14:52:30 +1000 Subject: [PATCH] Fix buggy align behaviour --- packages/block-editor/src/layouts/flow.js | 25 ++++++++++++++-- packages/block-editor/src/layouts/utils.js | 34 ++++++++++++++++++++++ packages/block-library/src/group/edit.js | 7 ++--- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/packages/block-editor/src/layouts/flow.js b/packages/block-editor/src/layouts/flow.js index 6f6ab4d51db436..205faa95785fcc 100644 --- a/packages/block-editor/src/layouts/flow.js +++ b/packages/block-editor/src/layouts/flow.js @@ -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'; @@ -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; }, }; diff --git a/packages/block-editor/src/layouts/utils.js b/packages/block-editor/src/layouts/utils.js index e2a27f7d7b121a..11996d4715312d 100644 --- a/packages/block-editor/src/layouts/utils.js +++ b/packages/block-editor/src/layouts/utils.js @@ -1,3 +1,8 @@ +/** + * WordPress dependencies + */ +import { __, sprintf } from '@wordpress/i18n'; + /** * Utility to generate the proper CSS selector for layout styles. * @@ -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; +} diff --git a/packages/block-library/src/group/edit.js b/packages/block-library/src/group/edit.js index e31642796db343..c192b085a3f3c7 100644 --- a/packages/block-library/src/group/edit.js +++ b/packages/block-library/src/group/edit.js @@ -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';