Editor: order viewport breakpoints only against a comparable base - #12948
Editor: order viewport breakpoints only against a comparable base#12948jigneshbhavani wants to merge 1 commit into
Conversation
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Media queries resolve em and rem against the initial font size, so a px breakpoint cannot be ordered against a font-relative one. Keep tablet only when both breakpoints share a base, matching how it is already dropped when it is not larger than mobile. See #65833.
68578bd to
75d766d
Compare
irozum
left a comment
There was a problem hiding this comment.
This correctly fixes the ordering bug from #65833: sanitize_viewport_settings() was proving tablet > mobile at a hardcoded 16px base while get_viewport_media_queries() emitted the authored units, so a px/em mix could pass the guard but produce a media query that never matches (or overlaps) in the browser. Keeping tablet only when both breakpoints share a base (px vs. font-relative) is exactly the approach agreed with wildworks in ticket comment:3, and correctly treats em/rem as comparable to each other since both resolve against the initial font size.
I checked out the branch, ran the full Tests_Theme_wpThemeJson suite (295 tests, all green), PHPStan, and PHPCS against both touched files — all clean. Traced the single-breakpoint and default-breakpoint (480px/782px) paths by hand; neither goes through the new base check, so existing behavior for themes using only one breakpoint or the defaults is unaffected. str_ends_with() is polyfilled in compat.php for the PHP 7.4 floor, so no compat concern there. Since settings.viewport is unreleased in 7.1, there's no BC surface to worry about, and the docblock updates on both changed methods are accurate.
No blocking issues. Nice, tightly-scoped fix with good test coverage across the px/em, px/rem, and em/rem-mixed cases plus a settings-level integration test.
WP_Theme_JSON::sanitize_viewport_settings()proves thattabletis larger thanmobileusing values normalized at a hardcoded 16px, butget_viewport_media_queries()emits the authored units. A media query resolvesemandremagainst the initial font size rather than anything set on the page, so the ordering proven at 16px does not transfer to the browser when the two breakpoints are measured against different bases.With
mobile: 30em, tablet: 500pxthe guard passes because 30 x 16 = 480 < 500, and core emits@media (30em < width <= 500px). Above a 16px base that range is empty, and@media (width <= 30em)and@media (width > 500px)begin to overlap instead, so two viewport states apply at once.emandremshare a base in a media query, so they can be ordered against each other. Apxlength and a font-relative one cannot, at any base. This addsget_viewport_breakpoint_base()and keepstabletonly when both breakpoints are measured against the same base, dropping it in the mixed case exactly as it is already dropped when it is not larger thanmobile.No new string, so it is available after hard string freeze.
settings.viewportis new in 7.1 and unreleased, so no existing theme depends on the current behavior.Approach agreed with wildworks in comment:3. The warning message requested there is a follow-up rather than part of this change, since it needs a translatable string.
Gutenberg mirrors this logic in
packages/global-styles-engine/src/utils/viewport.tsandlib/class-wp-theme-json-gutenberg.php, so the editor preview needs the same change upstream. Not part of this PR.Testing instructions
In a theme's
theme.json:{ "version": 3, "settings": { "viewport": { "mobile": "30em", "tablet": "500px" } } }Add a paragraph with block visibility set to hide on tablet, then view the post. Before this change the page emits
@media (30em < width <= 500px). After it,tabletis not a configured breakpoint and only the mobile query is emitted.mobile: 30em, tablet: 40remkeeps both breakpoints, since those share a base.Trac ticket: https://core.trac.wordpress.org/ticket/65833
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Investigation, browser measurements, drafting the change and the tests. I
reviewed, tested and take responsibility for everything in this pull request.