-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Fluid typography: do not calculate fluid font size when min and max viewport widths are equal #57866
Fluid typography: do not calculate fluid font size when min and max viewport widths are equal #57866
Conversation
…e fluid typography config, do not calculate fluid typography values and return null. We could provide a fallback from the default values in gutenberg_get_typography_font_size_value() but I think it might be better to bypass returning clamp values altogether because fundamentally, the input is wrong.
This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress. If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged. If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack. Thank you! ❤️ View changed files❔ lib/block-supports/typography.php ❔ phpunit/block-supports/typography-test.php |
Size Change: +5 B (0%) Total Size: 1.69 MB
ℹ️ View Unchanged
|
lib/block-supports/typography.php
Outdated
$linear_factor = ! empty( $linear_factor_denominator ) ? 100 * ( ( $maximum_font_size['value'] - $minimum_font_size['value'] ) / ( $linear_factor_denominator ) ) : 0; | ||
$linear_factor_scaled = round( $linear_factor * $scale_factor, 3 ); | ||
$linear_factor_scaled = empty( $linear_factor_scaled ) ? 1 : $linear_factor_scaled; |
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.
Would this be more readable?
if ( ! empty( $linear_factor_denominator ) ) {
$linear_factor = 100 * ( ( $maximum_font_size['value'] - $minimum_font_size['value'] ) / ( $linear_factor_denominator ) );
$linear_factor_scaled = round( $linear_factor * $scale_factor, 3 );
} else {
$linear_factor_scaled = 1;
}
$linear_factor_scaled = empty( $linear_factor_scaled ) ? 1 : $linear_factor_scaled;
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.
Slightly! That looks a bit more readable to me since it reduces a bit of the complexity in a single line 👍
I don't feel strongly about it either way, though!
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.
Thanks for getting a PR up so quickly!
While testing, I found that the fluid typography output felt a bit unexpected to me as the resulting font-size seemed to always be very far away from the one I entered into the block editor. For example, if I set a block's font-size to 48px
, then with * 1
, it never gets close to being 48px despite the viewport size. In practice, it winds up being 27.894px
to 32.458px
:
2024-01-16.16.12.46.mp4
Not calculate fluid typography values and return null.
Because of the above, I think I'd lean toward an early return actually, and skip fluid typography output when the min and max viewport are the same.
Separately to this PR, I noticed that it looks like Fluid typography rules currently aren't working for custom values within the block editor. From a bit of digging, it looks like maybe the fluid typography calls aren't happening within useBlockProps here 🤔 :
function useBlockProps( { name, fontSize, style } ) { |
That last bug isn't a blocker for this PR, but just thought I'd mention it while I notice it.
I've run out of time to keep digging today, but happy to help out with this tomorrow!
Added a comment over on #56912 (review) where I think the issue was introduced. Not quite sure what the right fix is 🤔 |
I should have listened to my first commit 29cf213 I'll revert. Thanks for the quick test! |
23baf65
to
29cf213
Compare
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.
Another one I owe you @andrewserong 🙏 |
✅ I updated this PR with the |
What?
When the same value is provided for min and max viewport widths in the fluid typography config, return early.
Kudos to @josephscott for catching this.
Core patch:
Why?
To avoid dividing by zero values. PHP will throw an error and, besides, the fluid typography clamp rule needs valid max and min viewport constraints.
Alternatives to this approach:
gutenberg_get_typography_font_size_value()
, e.g.,1600 - 320
.Testing Instructions
The error is triggered when the min and max viewport widths are equal.
Here is some test theme.json
Check that font-sizes are not fluid.
Important
In the Editor things will be appear broken because, via
wp-admin/edit-form-blocks.php
Core is running theme.json through the Theme_Json class, which will call Corewp_
typography functions.Here's the expected error:
PHP error in the editor
There's a Core patch you can run to get everything working.
E.g., if using
wp-env
use a.wp-env.override.json
override file to point to the WordPress source