Block Supports: Add opt-in semantic CSS class generation for style properties#80103
Block Supports: Add opt-in semantic CSS class generation for style properties#80103sarthaknagoshe2002 wants to merge 2 commits into
Conversation
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
|
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 Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @itsViney. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
What?
Closes #71693
This PR introduces a opt-in architecture that translates Gutenberg block style attributes (like padding, margin, typography, and borders) into semantic, standardized CSS classes (e.g.,
has-padding,has-large-font-size) on both the server (PHP) and client (React).Why?
Currently, Gutenberg hardcodes user styling choices via inline styles (
style="padding: 100px;") or scoped style blocks. This creates a nightmare for theme developers trying to enforce responsive design or global brand consistency (e.g., updating a primary brand color across hundreds of old posts).By translating these stubborn inline styles into predictable CSS classes, we give control back to theme authors, allowing them to target and override editor styles easily via CSS.
How?
wp_enabled_style_propertiesfilter, defaulting to off so it doesn't break backwards compatibility for existing themes.WP_HTML_Tag_Processor.__experimentalStyleClassesEnabledkey in theWP_REST_Block_Editor_Settings_Controllerschema and added it to theBLOCK_EDITOR_SETTINGSJS allowlist to securely pass the active filters from the PHP server to the React client.wp_enabled_style_properties_{$block_name}andwp_enabled_style_properties_{$slug}, allowing developers to target or exclude specific blocks.Testing Instructions
functions.phpand add the global opt-in filter:Screenshots or screencast
Goal
If a developer wants to add custom badges, specific borders, or design adjustments to a block based on user layout choices (like specific padding alignments or mixed border-radii), they can simply write clean class selectors. The video shows this in action across both the editor and the frontend once the opt-in filter is active.
Setup
Theme Opt-In (functions.php):
Custom Theme Styles (style.css):
Screen.Recording.2026-07-10.at.5.10.58.PM.mov
When the block receives 80px padding and unlinked corner values, the engine injects
has-80-paddingandhas-mixed-border-radiusalongside side specific classes, allowing the theme's custom CSS to match those precise state combinations and apply the custom red borders, badges, and rotational effects instantly.Use of AI Tools
I utilized AI to assist with scaffolding the PHP and JavaScript parsers, and drafting the PHPUnit/Jest test suites. I directed the architecture, reviewed all generated code against Gutenberg coding standards, executed fault-injection tests.