From 8825883e9346a93b1e9b83e552b10b84ab0ec299 Mon Sep 17 00:00:00 2001 From: ramonjd Date: Thu, 19 May 2022 12:12:02 +1000 Subject: [PATCH] Testing out an approach similar to the gallery block #38164 --- lib/blocks.php | 2 +- packages/block-library/src/columns/edit.js | 8 ++ .../block-library/src/columns/gap-styles.js | 32 ++++++++ packages/block-library/src/columns/index.php | 73 +++++++++++++++++++ packages/block-library/src/columns/style.scss | 5 +- 5 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 packages/block-library/src/columns/gap-styles.js create mode 100644 packages/block-library/src/columns/index.php diff --git a/lib/blocks.php b/lib/blocks.php index 3f0e8a547a868..fa611f49a859e 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -20,7 +20,6 @@ function gutenberg_reregister_core_block_types() { 'freeform', 'code', 'column', - 'columns', 'comments', 'group', 'heading', @@ -51,6 +50,7 @@ function gutenberg_reregister_core_block_types() { 'calendar.php' => 'core/calendar', 'categories.php' => 'core/categories', 'cover.php' => 'core/cover', + 'columns.php' => 'core/columns', 'comment-author-avatar.php' => 'core/comment-author-avatar', 'comment-author-name.php' => 'core/comment-author-name', 'comment-content.php' => 'core/comment-content', diff --git a/packages/block-library/src/columns/edit.js b/packages/block-library/src/columns/edit.js index 432fc4d320445..277634578945e 100644 --- a/packages/block-library/src/columns/edit.js +++ b/packages/block-library/src/columns/edit.js @@ -30,6 +30,7 @@ import { createBlocksFromInnerBlocksTemplate, store as blocksStore, } from '@wordpress/blocks'; +import { Platform } from '@wordpress/element'; /** * Internal dependencies @@ -40,6 +41,7 @@ import { getRedistributedColumnWidths, toWidthPrecision, } from './utils'; +import GapStyles from './gap-styles'; /** * Allowed blocks constant is passed to InnerBlocks precisely as specified here. @@ -92,6 +94,12 @@ function ColumnsEditContainer( { value={ verticalAlignment } /> + { Platform.isWeb && ( + + ) } { + return ; + }; + + return gap && styleElement + ? createPortal( , styleElement ) + : null; +} diff --git a/packages/block-library/src/columns/index.php b/packages/block-library/src/columns/index.php new file mode 100644 index 0000000000000..6b5c623f59c84 --- /dev/null +++ b/packages/block-library/src/columns/index.php @@ -0,0 +1,73 @@ + $value ) { + $gap_value[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value; + } + } else { + $gap_value = preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value; + } + + // The gap value can be a string value or a split top/left value. For the columns we want `left` which equates to gap-column. + // See: https://github.com/WordPress/gutenberg/pull/37736. + if ( is_array( $gap_value ) ) { + $gap_value = isset( $gap_value['left'] ) ? $gap_value['left'] : null; + } + + if ( ! $gap_value ) { + return $content; + } + + $class = wp_unique_id( 'wp-block-columns-' ); + $content = preg_replace( + '/' . preg_quote( 'class="', '/' ) . '/', + 'class="' . $class . ' ', + $content, + 1 + ); + + $style = '.' . $class . '{ --wp--style--unstable-columns-gap: ' . $gap_value . ';}'; + // Ideally styles should be loaded in the head, but blocks may be parsed + // after that, so loading in the footer for now. + // See https://core.trac.wordpress.org/ticket/53494. + add_action( + 'wp_footer', + function () use ( $style ) { + echo ''; + } + ); + return $content; +} + +/** + * Registers the `core/columns` block on server. + */ +function register_block_core_columns() { + register_block_type_from_metadata( + __DIR__ . '/columns', + array( + 'render_callback' => 'block_core_columns_render', + ) + ); +} + +add_action( 'init', 'register_block_core_columns' ); diff --git a/packages/block-library/src/columns/style.scss b/packages/block-library/src/columns/style.scss index 3ad29fb3086d4..8971a0aa53584 100644 --- a/packages/block-library/src/columns/style.scss +++ b/packages/block-library/src/columns/style.scss @@ -43,9 +43,8 @@ // As with mobile styles, this must be important since the Column // assigns its own width as an inline style, which should take effect // starting at `break-medium`. - // Note: if the columns block has a style.spacing.blockGap value of > var(--wp--style--block-gap, 2em) this effect will not show. - // What would make this particular definition more robust is if we could do the following dynamically: ( var(--wp--style--block-gap, 2em) + style.spacing.blockGap ) - flex-basis: calc(50% - calc(var(--wp--style--block-gap, 2em) / 2)) !important; + // Note: to calculate the offset we have to add the root block gap value with any block support spacing value (stored in --wp--style--unstable-columns-gap). + flex-basis: calc(50% - calc(var(--wp--style--block-gap, 2em) + var(--wp--style--unstable-columns-gap, 0em) / 2)) !important; } }