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

Fix bump in specificity for layout styles in non-iframed editor #7145

Closed

Conversation

talldan
Copy link
Contributor

@talldan talldan commented Aug 6, 2024

Trac ticket: https://core.trac.wordpress.org/ticket/61829
Github PR: WordPress/gutenberg#64076

Backports the changes from WordPress/gutenberg#64076

What?

Fixes the issue mentioned in WordPress/gutenberg#53468 (comment) to restore theme.json spacing rules taking precedence over the implicit spacing rules in a non-iframed editor.

How?

In trunk the code generates a selector like .is-layout-flow > * (specificity 0,1,0) which is fine normally. When in a non-iframed editor, the transformStyles function prefixes selectors with .editor-styles-wrapper to scope them to the editor canvas, so it becomes .editor-styles-wrapper .is-layout-flow > * (specificity 0,2,0), a bump in specificity that causes the precedence of styles to change.

In this PR I've changed the selector to :root :where(.is-layout-flow) > * (still specificity 0,1,0). transformStyles handles 'root' selectors a little differently, it'll instead replace the :root part so it becomes .editor-styles-wrapper where(.is-layout-flow) > * (keeping the specificity at 0,1,0).

The other layout selector that this affects is the :first-child :last-child selectors that are responsible for resetting margin at the start and end of a block list. They traditionally have a 0,2,0 specificity so that they can override both the above rule and any rules in the theme.json. Those selectors are also maintained at 0,2,0 with this change, they become something like :root :where(.is-layout-flow) > :first-child.

Here's a list of selectors that will be updated (according to our php tests)
-.is-layout-flow  > :first-child {
+:root :where(.is-layout-flow) > :first-child {

-.is-layout-flow  > :last-child {
+:root :where(.is-layout-flow) > :last-child {

-.is-layout-flow  > * {
+:root :where(.is-layout-flow) > * {

-.is-layout-constrained  > :first-child {
+:root :where(.is-layout-constrained) > :first-child {

-.is-layout-constrained  > :last-child {
+:root :where(.is-layout-constrained) > :last-child {

-.is-layout-constrained  > * {
+:root :where(.is-layout-constrained) > * {

-.is-layout-flex  {
+:root :where(.is-layout-flex) {

-.is-layout-grid  {
+:root :where(.is-layout-grid) {

-.wp-block-social-links-is-layout-flow  > :first-child {
+:root :where(.wp-block-social-links-is-layout-flow) > :first-child {

-.wp-block-social-links-is-layout-flow  > :last-child {
+:root :where(.wp-block-social-links-is-layout-flow) > :last-child {

-.wp-block-social-links-is-layout-flow  > * {
+:root :where(.wp-block-social-links-is-layout-flow) > * {

-.wp-block-social-links-is-layout-constrained  > :first-child {
+:root :where(.wp-block-social-links-is-layout-constrained) > :first-child {

-.wp-block-social-links-is-layout-constrained  > :last-child {
+:root :where(.wp-block-social-links-is-layout-constrained) > :last-child {

-.wp-block-social-links-is-layout-constrained  > * {
+:root :where(.wp-block-social-links-is-layout-constrained) > * {

-.wp-block-social-links-is-layout-flex  {
+:root :where(.wp-block-social-links-is-layout-flex) {

-.wp-block-social-links-is-layout-grid  {
+:root :where(.wp-block-social-links-is-layout-grid) {

-.wp-block-buttons-is-layout-flow  > :first-child {
+:root :where(.wp-block-buttons-is-layout-flow) > :first-child {

-.wp-block-buttons-is-layout-flow  > :last-child {
+:root :where(.wp-block-buttons-is-layout-flow) > :last-child {

-.wp-block-buttons-is-layout-flow  > * {
+:root :where(.wp-block-buttons-is-layout-flow) > * {

-.wp-block-buttons-is-layout-constrained  > :first-child {
+:root :where(.wp-block-buttons-is-layout-constrained) > :first-child {

-.wp-block-buttons-is-layout-constrained  > :last-child {
+:root :where(.wp-block-buttons-is-layout-constrained) > :last-child {

-.wp-block-buttons-is-layout-constrained  > * {
+:root :where(.wp-block-buttons-is-layout-constrained) > * {

-.wp-block-buttons-is-layout-flex  {
+:root :where(.wp-block-buttons-is-layout-flex) {

-.wp-block-buttons-is-layout-grid  {
+:root :where(.wp-block-buttons-is-layout-grid) {

Testing Instructions

  1. Add some very obvious margin rules to your theme.json like this for paragraphs (under styles.blocks):
"core/paragraph": {
	"spacing": {
		"margin": {
			"top": "4rem",
			"bottom": "4rem"
		}
	}
},
  1. Create a new post. Ensure you don't have custom fields active.
  2. Insert 3 paragraphs with text, check that the margin looks right. The first paragraph should have no top margin, the last no bottom margin, but in all other cases there should be 4rem top/bottom margin.
  3. Activate Custom Fields in the editor preferences to 'un-iframe' the editor
  4. Check that the styles are the same without the iframe

Also worth checking out trunk and comparing the styles. You should see that in trunk the iframed editor looks correct, but when you activate custom fields the non-iframed editor styles are broken.

Screenshots or screencast

Before

Screenshot 2024-07-31 at 4 33 10 PM

After

Screenshot 2024-07-31 at 4 33 30 PM


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@talldan talldan added the bug label Aug 6, 2024
@talldan talldan self-assigned this Aug 6, 2024
Copy link

github-actions bot commented Aug 6, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props talldanwp.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

github-actions bot commented Aug 6, 2024

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link

@aaronrobertshaw aaronrobertshaw 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 putting this together @talldan 👍

I've tested it in the post editor, following the instructions on this PR and the additional steps outlined over on the Gutenberg PR in WordPress/gutenberg#64076 (comment).

✅ Block-level margin styles in global styles overrides base spacing styles
✅ Block-level margin styles in global styles overrides block level spacing in global styles (e.g. Group block set to 0 block spacing will be overridden by paragraphs within the Group if paragraphs have margin set)
✅ Block instance level block spacing overrides global styles margin rules
✅ Block instance level margins override block instance level block spacing
✅ Block spacing for buttons, social icons, row, stack, and grid all appear to be working

I didn't spot any issues. This LGTM 🚢

Before (iframed) Before (non-iframed)
Screenshot 2024-08-13 at 7 27 48 PM Screenshot 2024-08-13 at 7 27 32 PM
After (iframed) After (non-iframed)
Screenshot 2024-08-13 at 7 24 06 PM Screenshot 2024-08-13 at 7 24 06 PM

Copy link
Contributor

@hellofromtonya hellofromtonya left a comment

Choose a reason for hiding this comment

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

LGTM and ready for commit.

@hellofromtonya
Copy link
Contributor

Committed to trunk via https://core.trac.wordpress.org/changeset/58890.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants