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

Section Styles not rendering "spacing.blockGap" values #63273

Open
bacoords opened this issue Jul 8, 2024 · 2 comments
Open

Section Styles not rendering "spacing.blockGap" values #63273

bacoords opened this issue Jul 8, 2024 · 2 comments
Labels
[Type] Bug An existing feature does not function as intended

Comments

@bacoords
Copy link
Contributor

bacoords commented Jul 8, 2024

Description

When registering a block style variation (i.e. section style), the "blockGap" value isn't being output in the editor or on the front end. Other spacing values are working ("spacing.padding", "spacing.margin").

This is particularly useful in a number of blocks with flex layout options (column, group, navigation) and works when added as a value to the block in theme.json (styles.blocks) but not when added to a section style.

Step-by-step reproduction instructions

Add a new section style JSON file to your theme's /styles/ directory:

{
	"$schema": "https://schemas.wp.org/trunk/theme.json",
	"version": 3,
	"title": "Footer Nav 1",
	"slug": "navigation-footer-1",
	"blockTypes": ["core/navigation"],
	"styles": {
		"spacing": {
			"blockGap": "var:preset|spacing|60"
		}
	}
}

This doesn't work.

However, adding this to your theme.json:

	"core/navigation": {
		"spacing": {
			"blockGap": "var:preset|spacing|80"
		}
	}

works fine.

Screenshots, screen recording, code snippet

No response

Environment info

  • WordPress 6.6-RC2
  • Gutenberg 18.7.1

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

@bacoords bacoords added the [Type] Bug An existing feature does not function as intended label Jul 8, 2024
@ramonjd
Copy link
Member

ramonjd commented Jul 9, 2024

I think maybe the styles need to be added in toStyles where variations rulesets are built.

Example diff
diff --git a/packages/block-editor/src/components/global-styles/use-global-styles-output.js b/packages/block-editor/src/components/global-styles/use-global-styles-output.js
index dba86b1db7..3cac006419 100644
--- a/packages/block-editor/src/components/global-styles/use-global-styles-output.js
+++ b/packages/block-editor/src/components/global-styles/use-global-styles-output.js
@@ -1077,6 +1077,22 @@ export const toStyles = (
 										`:root :where(${ styleVariationSelector })`
 									);
 								}
+
+								if ( options.blockGap && hasBlockGapSupport ) {
+									const gapValue =
+										getGapCSSValue(
+											styleVariations?.spacing?.blockGap
+										) || '0.5em';
+									ruleset =
+										ruleset +
+										`:root :where(${ styleVariationSelector }) > * { margin-block-start: ${ gapValue }; margin-block-end: 0; }`;
+									ruleset =
+										ruleset +
+										`:root :where(${ styleVariationSelector }) > :first-child { margin-block-start: 0; }`;
+									ruleset =
+										ruleset +
+										`:root :where(${ styleVariationSelector }) > :last-child { margin-block-end: 0; }`;
+								}
 							}
 						}
 					);
diff --git a/packages/block-editor/src/hooks/block-style-variation.js b/packages/block-editor/src/hooks/block-style-variation.js
index f302cf2aa3..6df023de75 100644
--- a/packages/block-editor/src/hooks/block-style-variation.js
+++ b/packages/block-editor/src/hooks/block-style-variation.js
@@ -327,7 +327,7 @@ function useBlockProps( { name, className, clientId } ) {
 			getBlockStyles,
 			clientId
 		);
-		const hasBlockGapSupport = false;
+		const hasBlockGapSupport = true;
 		const hasFallbackGapSupport = true;
 		const disableLayoutStyles = true;
 		const disableRootPadding = true;
@@ -340,7 +340,7 @@ function useBlockProps( { name, className, clientId } ) {
 			disableLayoutStyles,
 			disableRootPadding,
 			{
-				blockGap: false,
+				blockGap: true,
 				blockStyles: true,
 				layoutStyles: false,
 				marginReset: false,

Which leads me to another question: do variations support layout styles? Or, in other words, does getLayoutStyles() need to be called inside the loop as well?

cc @aaronrobertshaw for tips

@aaronrobertshaw
Copy link
Contributor

Which leads me to another question: do variations support layout styles? Or, in other words, does getLayoutStyles() need to be called inside the loop as well?

To date they don't. The general direction was to focus on the more common color/typography styling. Then expand out to things like spacing, border etc.

This one is on my list to look at today. Appreciate the diff and head start @ramonjd 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
3 participants