Opened 21 months ago
Last modified 9 months ago
#61265 new enhancement
Add class name for block variations
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Future Release | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Editor | Keywords: | has-patch has-screenshots has-test-info has-unit-tests |
| Focuses: | Cc: |
Description
In order to allow block authors to add custom styling for individual block variations, it would be convenient to have a class name automatically added for the variation.
There's some precedent in the Social Icon block, which e.g. for its WordPress variation adds the wp-social-link-wordpress class name (in addition to wp-social-link).
Attachments (2)
Change History (19)
This ticket was mentioned in PR #6602 on WordPress/wordpress-develop by @Bernhard Reiter.
21 months ago
#1
- Keywords has-patch added
@tomjcafferkey commented on PR #6602:
21 months ago
#2
I'm assuming we're going to need to open a GB counterpart to deal with the same logic for adding variation class names in the editor too?
@Bernhard Reiter commented on PR #6602:
21 months ago
#3
I'm assuming we're going to need to open a GB counterpart to deal with the same logic for adding variation class names in the editor too?
Oh, good point! I was originally just focused on dynamic blocks, but you're right, we'll probably need something somewhere around addGeneratedClassName and/or getBlockDefaultClassName.
@Bernhard Reiter commented on PR #6602:
21 months ago
#4
I'm assuming we're going to need to open a GB counterpart to deal with the same logic for adding variation class names in the editor too?
Oh, good point! I was originally just focused on dynamic blocks, but you're right, we'll probably need something somewhere around
addGeneratedClassNameand/orgetBlockDefaultClassName.
@tjcafferkey Mind giving that a stab? 😄
@tomjcafferkey commented on PR #6602:
21 months ago
#5
Mind giving that a stab? 😄
I'd love to!
This ticket was mentioned in Slack in #core by oglekler. View the logs.
21 months ago
#7
@
21 months ago
- Keywords needs-testing-info added
Hi @bernhard-reiter It looks quite well-defined, but we have just a few days before Beta 1, it can possibly go into 6.6 if we hurry up. Can you please provide testing instructions? Thank you!
#8
@
21 months ago
== Test Report
Please ignore, it was meant for https://core.trac.wordpress.org/ticket/60236
This ticket was mentioned in Slack in #core by chaion07. View the logs.
17 months ago
#12
@
17 months ago
Test Report
This report validates that the indicated patch addresses the issue.
Patch tested: https://github.com/WordPress/wordpress-develop/pull/6602
Environment
- WordPress: 6.7-alpha-58576-src
- PHP: 8.2.14
- Server: nginx/1.25.3
- Database: mysqli (Server: 8.0.39 / Client: mysqlnd 8.2.14)
- Browser: Safari 17.6
- OS: macOS
- Theme: Twenty Twenty-Four 1.2
- MU Plugins: None activated
- Plugins:
- Test Reports 1.1.0
Steps to Test
- Open a Post post in edit mode
- Add a 'Categories' block (or any other block variation) to the page content. Settings for the block can be the default
- Assign the 'Uncategorized' category to the post and publish the post
- View the front end of the site
- Inspect the 'Uncategorized' text (the Categories block) and ensure that the markup contains a new class showing the active variation
Expected Results
- ✅ An additional class should be added to the block, using the format
wp-block-<block name>-<variation name>
Actual Results
- ✅ The class
wp-block-post-terms-categoryis added to the block
Additional Notes
- I tested using the default WP develop repo following the Readme's instructions, and used
gh pr checkout 6602to check out the specific PR
Supplemental Artifacts
This ticket was mentioned in Slack in #core-test by jessedyck. View the logs.
17 months ago
#14
@
17 months ago
- Milestone changed from 6.7 to Future Release
It seems like there are still some unanswered questions on the PR. With beta 1 deadline in less than 24 hours, I'm going to punt this one.
If a committer has the time today to review and commit, it can be moved back.
11 months ago
#15
It looks like https://github.com/WordPress/wordpress-develop/commit/07e3b258b5f111f2d3d5d093f929bbd040ac19b9 referenced a wrong ticket.
#16
@
11 months ago
- Keywords has-screenshots has-testing-info has-unit-tests added; needs-testing-info removed
Test Report
Description
This report validates whether the indicated patch works as expected.
Patch tested: https://github.com/WordPress/wordpress-develop/pull/6602
Environment
- WordPress: 6.8-beta2-59971-src
- PHP: 8.2.27
- Server: nginx/1.27.4
- Database: mysqli (Server: 8.0.41 / Client: mysqlnd 8.2.27)
- Browser: Chrome 133.0.0.0
- OS: macOS
- Theme: Twenty Sixteen 3.4
- MU Plugins: None activated
- Plugins:
- Test Reports 1.2.0
Actual Results
- ✅ Issue resolved with patch.
- Block variations now receive a unique class name automatically.
- Verified the expected class names are applied based on block variation.
Additional Notes
- No unexpected side effects observed in block editor rendering or frontend output.


For a given block
my/blockwith a variationmyvariationthat's registered on the server side (via `variation_callback` or `get_block_type_variations`), add a classwp-block-my-block-mynavigationto the block wrapper.### Design choiced
This PR introduces a new function called
get_active_block_variation(), which is modelled after its client-side counterpart, `getActiveBlockVariation()`. The signatures are _almost_ identical, the major difference being that the client-side version accepts a block type _name_, whereas the server side one expects aWP_Block_Typeobject.Inference. Note that while
get_active_block_variationrespects theisActivefield to determine the variation of a block given its attributes, it also has a fallback mechanism, which is to compare the variation's set of definingattributesto the block's.I believe that this is a reasonable technique to determine a block variation, especially in the absence of an
isActivefield (which on the server side is furthermore limited to an array of attributes that need to be considered in determining a variation, as opposed to the client side, where arbitrary callbacks are allowed forisActive).If we agree with this fallback mechanism, I'm inclined to add it to the client side's
getActiveBlockVariationas well.### TODO
scopearguments### Testing Instructions
Test with a Block Theme (such as TT4) that uses Template Part block (and ideally also Navigation Link blocks), as those are among the few blocks with server-side registered variations. Inspect the page source and verify that the aforementioned blocks have indeed a variation-specific classname added, e.g.
wp-block-template-part-area_footerorwp-block-navigation-link-page.### Screenshots
---
Trac ticket: https://core.trac.wordpress.org/ticket/61265