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 Styles: Ensure unique classname generation for variations #64511

Merged
merged 2 commits into from
Aug 29, 2024

Conversation

aaronrobertshaw
Copy link
Contributor

Fixes: #64422

What?

Simplifies block style variation class name generation to ensure unique class.

Why?

Avoids potential for non-unique class names and conflicting styles when exact copies of a block are inserted via a repeated pattern.

Background

Originally the block style variation block support followed the same approach as styles generated by the elements block support. This entailed needing to generate a predictable classname in two separate filters ruling out the prior use of wp_unique_id.

The recent changes to these supports, such that their styles and class are generated in a single filter, mean the generated classname no longer needs to be a hash based off the block's attributes. Not only does this make the classname simpler and shorter but it also avoids the issue flagged in #64422 where exact copies of a block inserted via patterns result in the same ID and potentially conflicting styles.

How?

Replace the hashing of block attributes in the block style variation classnames with a call to wp_unique_id.

Testing Instructions

Confirm unique classnames on frontend

  1. Create a pattern
  2. Add a button with the Outline block styles to the pattern
  3. Save the pattern and edit a post
  4. Insert the above pattern 3 times in the post
  5. Save and view the post on the frontend
  6. Inspect a button and search the markup for is-style-outline-. You should find CSS with three styles matching that pattern however they should now all be unique.

Ensure no regressions for block style variations on the frontend

  1. Add some custom block style variations to your theme. See #57908 for detailed examples or the snippet below for a quick option.
  2. Add suitable content to a page, making sure there is enough that nested applications of block style variations can occur
  3. In the site editor, apply the custom block style variations to various content, including in a nested fashion
  4. Save and confirm the correct styling on the frontend
Example block style variation theme.json partial (place under `/styles`)
{
	"$schema": "https://schemas.wp.org/trunk/theme.json",
	"version": 3,
	"title": "Section A",
	"slug": "section-a",
	"blockTypes": [ "core/group" ],
	"styles": {
		"color": {
			"background": "slategrey",
			"text": "snow"
		},
		"blocks": {
			"core/group": {
				"color": {
					"background": "darkslategrey",
					"text": "whitesmoke"
				}
			}
		}
	}
}

Screenshots or screencast

Simple unique classnames for exact copies of blocks

Screenshot 2024-08-14 at 9 27 11 PM

Block Style Variations still working

Screen.Recording.2024-08-14.at.9.24.28.PM.mp4

@aaronrobertshaw aaronrobertshaw added [Type] Bug An existing feature does not function as intended [Feature] Block Style Variations Issues or PRs that are related to the style variations for blocks labels Aug 14, 2024
@aaronrobertshaw aaronrobertshaw self-assigned this Aug 14, 2024
Copy link

github-actions bot commented Aug 14, 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.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: carolinan <poena@git.wordpress.org>
Co-authored-by: asafm7 <asafm7@git.wordpress.org>

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

Copy link

Flaky tests detected in 9065b90.
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/10389961399
📝 Reported issues:

Copy link
Member

@ramonjd ramonjd left a comment

Choose a reason for hiding this comment

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

✅ Unique ids appear for style variations inside multiple patterns on the frontend
✅ Unique style variations rules are being generated for global styles on the frontend, e.g., :root :where(.wp-block-group.is-style-section-a--17)

@ramonjd ramonjd merged commit 47bf56a into trunk Aug 29, 2024
62 checks passed
@ramonjd ramonjd deleted the update/frontend-block-style-variation-class-generation branch August 29, 2024 04:43
@github-actions github-actions bot added this to the Gutenberg 19.2 milestone Aug 29, 2024
bph pushed a commit to bph/gutenberg that referenced this pull request Aug 31, 2024
…Press#64511)

* Block Styles: Ensure unique classname generation for variations

* Add backport changelog

Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: carolinan <poena@git.wordpress.org>
Co-authored-by: asafm7 <asafm7@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block Style Variations Issues or PRs that are related to the style variations for blocks [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Redundant duplicate CSS for outline buttons inside a pattern
2 participants