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

Allow selector ordering to ensure theme.json root selector margin takes precedence #36960

Merged
merged 7 commits into from
Dec 9, 2021

Conversation

getdave
Copy link
Contributor

@getdave getdave commented Nov 29, 2021

Description

Companion to Core patch https://core.trac.wordpress.org/ticket/54550.

(originally an alternative approach to #36958).

Note that as with #36958 this PR also includes changes from #36957 which make it possible for you to test this PR. They will be removed prior to merge requires you to test against WordPress 5.8 (that's five point EIGHT not NINE). See #36957 (comment) for reasons.

To use WP 5.8 add this to your .wp-env.override.json file:

{
    "core": "WordPress/WordPress#5.8.2"
}

Closes #36147

How has this been tested?

Follow same instructions as #36958.

But this time you should see that the body { margin: 0; } declaration is still output but is overridden by values from theme.json if they are found.

Basically the default margin: 0 is still applied but anything in theme.json will win out.

I believe this is the best approach because

  1. It is limited in scope.
  2. It retains the original margin: 0 on body which Themes have come to expect thus avoiding backwards compat issues.
  3. It caters for both types of margin notation (i.e. string or object) that can be applied in theme.json.

Screenshots

Types of changes

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.
  • 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).

@getdave getdave added [Type] Bug An existing feature does not function as intended Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels Nov 29, 2021
@getdave getdave self-assigned this Nov 29, 2021
@@ -1059,7 +1076,7 @@ private function get_block_classes( $style_nodes ) {
}

if ( self::ROOT_BLOCK_SELECTOR === $selector ) {
$block_rules .= 'body { margin: 0; }';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Avoid applying rule here otherwise it will end up being applied last in the selector order thereby ensuring it takes precedence over rules applied via theme.json.

// `margin` in its `spacing` declaration for the `body` element then these
// user-generated values take precedence in the CSS cascade.
// See: https://github.com/WordPress/gutenberg/issues/36147.
if ( self::ROOT_BLOCK_SELECTOR === $selector ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

By applying the margin: 0 rule early we ensure that any values provided from theme.json will be later in the selector order and thus take precedence over the default.

@getdave getdave marked this pull request as ready for review November 29, 2021 13:27
@getdave
Copy link
Contributor Author

getdave commented Nov 29, 2021

cc @overclokk as you raised the original Issue.

@getdave getdave added the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Nov 29, 2021
Copy link
Contributor

@ntsekouras ntsekouras left a comment

Choose a reason for hiding this comment

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

Thanks for the PRs Dave! I tested and work as expected.

I think I prefer this PR from the conditional zeroing of margin - also in the other approach we should also take into account the format of margin:{top: '10px'} etc.

I'm just wondering if it worths to call the functions again, instead of just

if ( self::ROOT_BLOCK_SELECTOR === $selector ) {
	$block_rules .= 'body { margin: 0; }';
}

before the generation of the styles.

@getdave
Copy link
Contributor Author

getdave commented Nov 29, 2021

I'm just wondering if it worths to call the functions again, instead of just

if ( self::ROOT_BLOCK_SELECTOR === $selector ) {
	$block_rules .= 'body { margin: 0; }';
}

before the generation of the styles.

I did this because it was putting the selectors on the same line within the outputted code so I thought perhaps it was safer to reuse the function.

@overclokk
Copy link

overclokk commented Nov 29, 2021

I was thinking why not adding that value as default in theme.json in core, so the theme can override that vales without an if statement

@getdave getdave force-pushed the fix/alt-default-theme-json-body-margin branch from ad82e53 to b3b02dc Compare November 30, 2021 09:19
@getdave
Copy link
Contributor Author

getdave commented Nov 30, 2021

In light of #36957 (comment) I've removed the fix I cherrypicked from #36957.

You now need to test this PR against WordPress 5.8.

@getdave getdave force-pushed the fix/alt-default-theme-json-body-margin branch from b3b02dc to a66fb10 Compare November 30, 2021 09:33
@getdave
Copy link
Contributor Author

getdave commented Nov 30, 2021

I was thinking why not adding that value as default in theme.json in core, so the theme can override that vales without an if statement

Responded in #36958 (comment).

Copy link
Contributor

@ntsekouras ntsekouras left a comment

Choose a reason for hiding this comment

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

Thanks Dave for your work here! IMO this fix can be landed and discuss afterwards a different approach that would potentially change where we apply all the root styles from theme.json, given the WP release phase we are right now.

I'd give it a bit more time before merging if anyone else wants to chime in - cc @youknowriad

lib/class-wp-theme-json-gutenberg.php Outdated Show resolved Hide resolved
@@ -724,7 +734,6 @@ private function get_block_classes( $style_nodes ) {
}

if ( self::ROOT_BLOCK_SELECTOR === $selector ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Makes me wonder if all the content of this condition should be above but it's good regardless.

@getdave
Copy link
Contributor Author

getdave commented Dec 1, 2021

Ok then folks, it seems the majority are agreed that this PR is the approach we want to pursue.

I will close out #36996 and then make a Core patch for the changes in this PR.

@getdave
Copy link
Contributor Author

getdave commented Dec 1, 2021

Companion Core patch in https://core.trac.wordpress.org/ticket/54550 cc @ntsekouras

@getdave
Copy link
Contributor Author

getdave commented Dec 9, 2021

Just fixing the PHP unit tests prior to merging.

@getdave getdave merged commit 30aab8a into trunk Dec 9, 2021
@getdave getdave deleted the fix/alt-default-theme-json-body-margin branch December 9, 2021 15:50
@github-actions github-actions bot added this to the Gutenberg 12.2 milestone Dec 9, 2021
@noisysocks noisysocks removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Dec 13, 2021
noisysocks pushed a commit that referenced this pull request Dec 13, 2021
…es precedence (#36960)

* Set default margin on body prior to generating styles from theme.json

* Add additional context to comment

Addresses #36960 (comment)

* Restore simple version of body rule

Also avoids creating failing unit tests due to a single space character changing in a snaphshot!

* Update with fixes made in Core patch

See WordPress/wordpress-develop#1989

* Fix output now includes new line.

* Fix tests relating to spacing

* Fix test to put body CSS reset first
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] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The margin added at the top level styles (for body) in theme.json is override by a margin added from gutenberg
5 participants