-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Block visibility based on screen size: add backend block support #73994
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
base: trunk
Are you sure you want to change the base?
Block visibility based on screen size: add backend block support #73994
Conversation
…based on screen size. * Introduce a new function to add 'display' to safe CSS properties for responsive styles. * Add comprehensive unit tests to validate block visibility behavior across different scenarios, including responsive breakpoints and experiment toggling.
…s and corresponding unit tests. This streamlines block visibility support by eliminating unused code related to responsive visibility experiments.
- Introduce a new file for KSES compatibility, adding 'display' to the list of safe CSS properties to support responsive visibility. - Update unit tests to verify the correct generation of CSS styles, including 'display:none' for hidden blocks based on screen size.
|
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 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. |
|
WP Core sync to come... |
|
Flaky tests detected in 168804b. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/20252320559
|
PrathameshBhagat365
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sanitize generated class names: use sanitize_html_class() (or sanitize_key()) when building 'wp-block-hidden-...'.
Restrict/validate breakpoints: only honor known breakpoint keys (array_intersect with defined breakpoints) to avoid mismatched counts and unexpected classes.
All-hidden check: compare hidden breakpoints against actual breakpoint keys (use intersection) before returning ''.
WP_HTML_Tag_Processor fallback: add a safe fallback if the class isn't available (careful regex or DOMDocument) so adding the class can't fail.
KSES shim loading: guard or document the compat/kses.php include (WP version check or comment) and add a unit test that toggles the experiment and asserts 'display' is added/removed from safe_style_css.
Style engine / !important: confirm the engine accepts '!important' values; prefer explicit API for importance or ensure the generated CSS is not stripped/altered.
Tests: add test for unknown breakpoint keys being ignored and for the WP_HTML_Tag_Processor fallback (if implemented).
…ts. Update unit tests to validate behavior for responsive visibility scenarios, ensuring only known breakpoints are processed and that valid class names are generated.
- Introduce a new changelog file for pull request #10629. - Update comments in block visibility support to clarify future JSON format centralization. - Refactor unit test to reflect Core
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements backend support for responsive block visibility, enabling blocks to be conditionally hidden at specific viewport breakpoints (mobile, tablet, desktop) using CSS media queries and the Style Engine. The feature is gated behind the gutenberg-hide-blocks-based-on-screen-size experiment flag, while maintaining backward compatibility for the existing boolean blockVisibility: false behavior.
- Generates viewport-specific CSS rules with media queries based on block metadata
- Adds visibility classes to blocks and enqueues styles via the Style Engine
- Includes comprehensive unit tests covering various breakpoint combinations and edge cases
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| phpunit/block-supports/block-visibility-test.php | Adds comprehensive test suite covering boolean visibility, viewport-based visibility with experiment flag, edge cases like empty/unknown breakpoints, and CSS generation verification |
| lib/block-supports/block-visibility.php | Implements core logic for parsing breakpoint visibility metadata, generating media query CSS rules, applying visibility classes, and handling the experiment flag |
| lib/compat/wordpress-7.0/kses.php | Adds 'display' property to safe CSS attributes list when experiment is enabled, required for inline styles |
| lib/load.php | Includes the new kses.php compatibility file in the WordPress 7.0 compatibility layer |
| backport-changelog/7.0/10629.md | Documents the backport reference to WordPress core PR |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…reakpoints. - Simplify CSS generation by removing unnecessary sanitization of breakpoint names in the visibility class. - Remove outdated unit test for CSS generation related to display:none, ensuring tests focus on current functionality.
…idden - Introduce a new test to ensure that block content is empty when all viewport breakpoints (mobile, tablet, desktop) are set to hidden. - Update existing test assertions for clarity regarding empty visibility objects.
tellthemachines
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is working well and generally LGTM, the only thing I'm not convinced about is the need for defining both max and min breakpoints (see below). This is assuming that we're shaping this in a way that will make it easy to build a theme.json setting on top of it.
…sibility experiment.
- Update breakpoint definitions to include names and slugs for better clarity. - Simplify media query generation logic for breakpoints, ensuring correct CSS output. - Modify unit tests to validate the new structure and ensure accurate CSS generation for responsive visibility.
Related to #72502
Backport Core PR:
What?
Implements backend support for responsive block visibility, allowing blocks to be hidden on specific viewport breakpoints (mobile, tablet, desktop) using CSS media queries.
Functionality is gated behind the
gutenberg-hide-blocks-based-on-screen-sizeexperiment flag. The existing boolean blockVisibility: false behavior works without the experiment enabled.Clientside state is happening over in:
How?
This PR borrows approaches from #73735 and #73888 to implement the backend that:
Testing
Unit tests:
npm run test:unit:php:base -- --filter=WP_Block_Supports_Block_Visibility_TestTo test manually, enable the experiment:
Then copy and paste this into your post editor, publish and view the frontend.
Kapture.2025-12-14.at.19.37.29.mp4
Make sure to test with the experiment off as well. The last item should never show with the experiment on and off.