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

Heading: Fix per-level element style overrides #49189

Closed
wants to merge 1 commit into from

Conversation

aaronrobertshaw
Copy link
Contributor

Fixes: #49070

What?

Corrects the CSS selector used for generating h1-h6 element style overrides for the Heading block.

Why?

Without the correct selectors a theme author can't override the Heading block styles per level.

How?

As the heading elements are a special edge case, this quick fix adds a conditional to the generation of element selectors during the theme.json block metadata processing. If the element in question is h1-h6 it will prepend the element selector to the block selector instead of appending the element as a child to the block selector.

Testing Instructions

  1. Using the example theme.json snippet below, update your theme to have a generic h1-h6 element style, then update the core/heading block styles to include an override for a specific h1-h6 element style.
  2. Load the editor and paste in the example block markup from the snippet below
  3. Note that the Heading block's per-level element style override isn't applied. Save and confirm the same on the frontend
  4. Checkout this PR, reload the editor and note that the Heading block's per-level element styles are now applied correctly
  5. Refresh the post on the frontend and confirm the correct styles there.
  6. Tweak your theme's element styles or set them via Global Styles for other elements and ensure those element styles still work correctly.
Theme.json Snippet
{
	"styles": {
		"elements": {
			"h1": {
				"color": {
					"background": "blue"
				}
			}
	        },
		"blocks": {
			"core/heading": {
				"elements": {
					"h1": {
						"color": {
							"background": "pink"
						}
					},
					"h3": {
						"color": {
							"background": "aliceblue"
						}
					},
					"h5": {
						"color": {
							"background": "darkseagreen"
						}
					}
				}
			}
		}
	}
}
Example block markup
<!-- wp:paragraph -->
<p>This is a quick demo for the different levels of heading in the Heading block.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading">Top level heading - h1</h1>
<!-- /wp:heading -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Second level heading - h2</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Third level heading - h3</h3>
<!-- /wp:heading -->

<!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading">Fourth level heading - h4</h4>
<!-- /wp:heading -->

<!-- wp:heading {"level":5} -->
<h5 class="wp-block-heading">Fifth level heading - h5</h5>
<!-- /wp:heading -->

<!-- wp:heading {"level":6} -->
<h6 class="wp-block-heading">Sixth level heading - h6</h6>
<!-- /wp:heading -->

<!-- wp:html -->
<p>This paragraph and the headings below have been added via a custom HTML block to pick up generic element styles</p>
<h1>Generic h1</h1>
<h2>Generic h2</h2>
<h3>Generic h3</h3>
<h4>Generic h4</h4>
<h5>Generic h5</h5>
<h6>Generic h6</h6>
<!-- /wp:html -->

Screenshots or screencast

Before After
Screenshot 2023-03-20 at 1 42 21 pm Screenshot 2023-03-20 at 1 43 12 pm

@aaronrobertshaw aaronrobertshaw added [Type] Bug An existing feature does not function as intended [Block] Heading Affects the Headings Block [Feature] Design Tools Tools that impact the appearance of blocks both to expand the number of tools and improve the experi labels Mar 20, 2023
@aaronrobertshaw aaronrobertshaw self-assigned this Mar 20, 2023
@github-actions
Copy link

Flaky tests detected in 6a92764.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4464511878
📝 Reported issues:

Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

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

Thank you, @aaronrobertshaw!

Can you add a simple PHPUnit test for this particular case? We can confirm that the snippet from the DevNote produces desired CSS.

@Mamaduka Mamaduka requested a review from oandregal March 20, 2023 04:37
@aaronrobertshaw
Copy link
Contributor Author

Closing this PR as per #49070 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Heading Affects the Headings Block [Feature] Design Tools Tools that impact the appearance of blocks both to expand the number of tools and improve the experi [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Heading block] Per-level styling of heading blocks does not work
2 participants