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

There is no way for the front side to determine that a Group Block is a "Row" variation. #38126

Closed
ddryo opened this issue Jan 21, 2022 · 4 comments
Labels
[Block] Group Affects the Group Block (and row, stack and grid variants) [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.

Comments

@ddryo
Copy link
Contributor

ddryo commented Jan 21, 2022

Description

Since WordPress 5.9, the wp-container-{id} class is given to the Group block, and the flex style of "Row" is written for this selector.

Class names that can be used to determine whether a block is a "Row" (such as is-row) are not output, so the theme cannot adjust the style for "Row" only.

Why do we need classes like is-row?

In some cases, Block Styles are registered and used for Group blocks.

When these block styles are used in "Row "blocks, the design may be broken.

So, we need to make minor adjustments to the "Row" blocks.

Step-by-step reproduction instructions

  1. Place the "Row" block.

Screenshots, screen recording, code snippet

スクリーンショット 2022-01-21 17 54 52

スクリーンショット 2022-01-21 17 54 58

Environment info

  • WordPress 5.9-RC3
    -Twenty Twenty-One

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@ddryo
Copy link
Contributor Author

ddryo commented May 20, 2022

Are there any plans to fix this problem?

I think it is a pretty important issue.

@ddryo
Copy link
Contributor Author

ddryo commented May 20, 2022

I would like to propose a new problem with the absence of the is-row class.

In the classic theme, there are cases where wp-container-{id} output is unnecessary.

Suppose we stop the output of wp-container-{id} with the following code.

remove_filter( 'render_block', 'wp_render_layout_support_flag' );

In this case, the collapsed block needs to be adjusted by applying CSS separately, but there is no identifiable class for the "row" variation, and it is not possible to apply flex to it on your own.

Translated with www.DeepL.com/Translator (free version)

@carolinan carolinan removed their assignment Jun 10, 2022
@skorasaurus skorasaurus added the [Block] Group Affects the Group Block (and row, stack and grid variants) label Jun 30, 2022
@cbirdsong
Copy link

Here is an interim workaround filter that also accounts for the "Stack" variation:

function block_filter_group_add_layout_classes($block_content, $block)
{
	if (empty($block["attrs"]["layout"])) {
		return $block_content;
	}

	$classes = [];

	if (!empty($block["attrs"]["layout"]["type"]) && $block["attrs"]["layout"]["type"] == "flex") {
		if (isset($block["attrs"]["layout"]["orientation"])) {
			$orientation = $block["attrs"]["layout"]["orientation"];
		} else {
			$orientation = "horizontal";
			// orientation not explicitly defined on the "row" variation as of 6.0, so assume horizontal if absent
		}

		switch ($orientation) {
			case "vertical":
				$classes[] = "is-layout-stack";
				break;
			case "horizontal":
				$classes[] = "is-layout-row";
				break;
		}
	}

	if (!empty($block["attrs"]["layout"]["justifyContent"])) {
		$classes[] = "is-content-justification-" . $block["attrs"]["layout"]["justifyContent"];
	}

	if (!empty($block["attrs"]["layout"]["flexWrap"])) {
		$classes[] = "is-" . $block["attrs"]["layout"]["flexWrap"];
	}

	if (!empty($classes)) {
		$classes = implode(" ", $classes);

		$search = 'class="wp-block-group';
		$replace = 'class="wp-block-group ' . $classes;
		$block_content = preg_replace("/" . $search . "/", $replace, $block_content, 1);
	}

	return $block_content;
}
add_filter("render_block_core/group", "block_filter_group_add_layout_classes", 10, 2);

Caveats:

  • You probably need to remove the .wp-container- class for the preg_replace to work.
  • This will stop working entirely if the value of $block["attrs"]["layout"]["type"] changes away from flex.
  • There will be redundant or duplicate classes added to your markup once core adds semantic classes to these blocks.

@ddryo
Copy link
Contributor Author

ddryo commented Nov 4, 2022

I just confirmed that the class was added in 6.1.
thank you!

@ddryo ddryo closed this as completed Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Group Affects the Group Block (and row, stack and grid variants) [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants