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

Block gap: Only render CSS variable if corresponding theme setting is enabled #35209

Conversation

andrewserong
Copy link
Contributor

@andrewserong andrewserong commented Sep 29, 2021

Description

Fixes #35197

In #35197 it's described that rendering the --wp--style--block-gap CSS variable globally when the block gap setting hasn't been explicitly opted in-to, could result in styling regressions on classic themes that use add_theme_support( 'experimental-link-color' ); if we're to proceed with updating Button block styling to use the new CSS variable.

In this PR, we're exploring skipping rendering the CSS variable if the corresponding setting in theme.json is set to null. The mechanism is to add a list of protected properties, and for these particular properties, only render them if their corresponding setting in theme.json is not null. This behaviour is consistent with the approach in #34491 to opt out of generating block gap styling. The extension here is to also skip rendering the CSS variable itself.

This is just one particular approach to solving the issue, so please let me know if you think it's a bad idea, or if you can think of a better way to handle it.

How has this been tested?

With TwentyTwentyOne theme enabled, open up the post editor and inspect element, and find the set of four or so style elements that sit underneath the visual editor component. Before this PR is applied, you should see that the global --wp--style--block-gap CSS variable is rendered.

With this PR applied, it should no longer render that variable with that theme active.

With TT1-Blocks active, and settings.spacing.blockGap enabled in the theme.json file, then the CSS variable should be rendered.

With TT1-Blocks active, and settings.spacing.blockGap set to null in the theme.json file, then the CSS variable should not be rendered.

Screenshots

TwentyTwentyOne theme before TwentyTwentyOne theme after
image image

TT1-Blocks (should remain unaffected when the settings.spacing.blockGap setting is enabled in theme.json:

image

Types of changes

Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers. (N/A)
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

@andrewserong andrewserong added [Type] Enhancement A suggestion for improvement. Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels Sep 29, 2021
@andrewserong andrewserong self-assigned this Sep 29, 2021
@andrewserong
Copy link
Contributor Author

Note: this still needs to have tests added, but I thought I'd get the PR up as a proof of concept.

@@ -551,6 +560,12 @@ private static function compute_style_properties( $styles ) {
foreach ( self::PROPERTIES_METADATA as $css_property => $value_path ) {
$value = self::get_property_value( $styles, $value_path );

if ( in_array( $css_property, self::PROTECTED_PROPERTIES ) ) {
if ( ! _wp_array_get( $settings, $value_path, false ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll want to null check this in order to be consistent with #34491, which makes the block gap styles explicitly opt-out rather than opt-in; according to that PR setting the blockGap setting to false should turn off the block support UI but not the global gap styles.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, the drawback to explicitly checking for null is that we'll end up with styling regressions on blocks themes that don't opt in to the block support, but do opt in to the global block gap styling (ie blockGap: false or omitting the setting entirely).

I think that just might be a limitation of the unified variable approach 🤔

@stacimc
Copy link
Contributor

stacimc commented Sep 29, 2021

Thanks so much for this, @andrewserong! Great find -- what a tricky one to track down.

This is looking really good so far for me! I tested on Twenty Twenty One/Seedlet and confirmed that the variable is no longer generated, and on TT1-blocks I confirmed that it's added correctly. I just left one comment about blocks themes that set blockGap: false or omit it; because the block gap styles treat this setting as an opt-out only when set to null, the layout styles using the variable are generated but the variable is not.

I like this approach, although I wonder if needing to explicitly check for null makes it less reusable than it might otherwise be. The unified variable makes this tricky for backwards compatibility, and may mean that we'll just have to accept the tradeoff of styling regressions on blocks themes. I would be very happy to get this through and resolve the issue on classic themes!

@andrewserong andrewserong force-pushed the update/block-gap-css-variable-to-only-render-with-corresponding-setting branch from 3cdcc6f to afb35ef Compare September 30, 2021 03:51
@andrewserong andrewserong changed the title [WIP] Block gap: Only render CSS variable if corresponding theme setting is enabled Block gap: Only render CSS variable if corresponding theme setting is enabled Sep 30, 2021
@andrewserong andrewserong marked this pull request as ready for review September 30, 2021 04:09
@andrewserong
Copy link
Contributor Author

Thanks for testing and for the suggestions @stacimc! I've updated this PR to use an explicit check for null and have added a couple of tests.

@andrewserong
Copy link
Contributor Author

@youknowriad I've just added you as a reviewer since you've done a lot of the work on the layout and gap support. What do you think of this approach for ensuring that we don't render the --wp--style--block-gap CSS variable on classic themes (or theme.json themes that set blockGap to null)?

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

* These properties are only rendered if a corresponding setting in the
* `PROPERTIES_METADATA` path enables it via a value other than `null`.
*/
const PROTECTED_PROPERTIES = array( '--wp--style--block-gap' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small thing: Should we have something like 'spacing.blockGap' => 'spacing.blockGap' instead. Meaning (theme.json style property mapping to its corresponding theme.json setting key). The current logic relies on the output instead of the input and assumes the setting key is the same as the style key (which is of course true now).

@andrewserong
Copy link
Contributor Author

Thanks for reviewing and for the suggestion @youknowriad! That feels better to me explicitly describing the mapping so that we can support a variance between the style key and settings key if we need to for future properties 👍

I went for something close to what you suggested, using 'spacing.blockGap' => array( 'spacing', 'blockGap' ) so that the value is an array that plays nicely with _wp_array_get.

Since it's a fairly minor change, I'll go ahead and merge it in once tests pass, but happy to do any further changes in follow-ups if need be!

@andrewserong andrewserong merged commit 0cdcd47 into trunk Oct 1, 2021
@andrewserong andrewserong deleted the update/block-gap-css-variable-to-only-render-with-corresponding-setting branch October 1, 2021 05:05
@github-actions github-actions bot added this to the Gutenberg 11.7 milestone Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Block Gap css variable is loaded in the editor for some classic themes
3 participants