Skip to content
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: covert font size number values to pixels #44807

Merged
merged 2 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ function gutenberg_typography_get_css_variable_inline_style( $attributes, $featu

/**
* Internal method that checks a string for a unit and value and returns an array consisting of `'value'` and `'unit'`, e.g., [ '42', 'rem' ].
* A raw font size of `value + unit` is expected. If the value is a number, it will convert to `value + 'px'`.
*
* @access private
*
Expand All @@ -248,6 +249,11 @@ function gutenberg_get_typography_value_and_unit( $raw_value, $options = array()
return null;
}

// Converts numbers to pixel values by default.
if ( is_numeric( $raw_value ) ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the docblock should be updated for this function to indicated that an int is possible for the $raw_value.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since wp_get_typography_value_and_unit will be parsing values from theme.json, I think this function should gaurd against anything other than a number and a string for the $raw_value. Also, the tests should add some unhappy paths to test for these unacceptable values, like {} or [].

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes reflected in the backport PR WordPress/wordpress-develop#3428. @dream-encode I added you as a reviewer to that PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks folks! 🙏 I'll make sure these changes make it back to Gutenberg.

$raw_value = $raw_value . 'px';
}

$defaults = array(
'coerce_to' => '',
'root_size_value' => 16,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ export function getComputedFluidTypographyValue( {
}

/**
* Internal method that checks a string for a unit and value and returns an array consisting of `'value'` and `'unit'`, e.g., [ '42', 'rem' ].
* A raw font size of `value + unit` is expected. If the value is a number, it will convert to `value + 'px'`.
*
* @param {string} rawValue Raw size value from theme.json.
* @param {string|number} rawValue Raw size value from theme.json.
* @param {Object|undefined} options Calculation options.
*
* @return {{ unit: string, value: number }|null} An object consisting of `'value'` and `'unit'` properties.
Expand All @@ -158,7 +160,8 @@ export function getTypographyValueAndUnit( rawValue, options = {} ) {
return null;
}

if ( typeof rawValue === 'number' && ! Number.isNaN( rawValue ) ) {
// Converts numbers to pixel values by default.
if ( typeof rawValue === 'number' ) {
rawValue = `${ rawValue }px`;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe( 'typography utils', () => {
typographySettings: undefined,
expected: '28px',
},
// Default return non-fluid value where `size` is not a number | string.
// Default return non-fluid value where `size` is undefined.
{
preset: {
size: undefined,
Expand Down
28 changes: 22 additions & 6 deletions phpunit/block-supports/typography-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,23 @@ public function test_gutenberg_get_typography_font_size_value( $font_size_preset
*/
public function data_generate_font_size_preset_fixtures() {
return array(
'default_return_value' => array(
'default_return_value' => array(
'font_size_preset' => array(
'size' => '28px',
),
'should_use_fluid_typography' => false,
'expected_output' => '28px',
),

'default_return_value_when_fluid_is_false' => array(
'default_return_value_when_size_is_undefined' => array(
'font_size_preset' => array(
'size' => null,
),
'should_use_fluid_typography' => false,
'expected_output' => null,
),

'default_return_value_when_fluid_is_false' => array(
'font_size_preset' => array(
'size' => '28px',
'fluid' => false,
Expand All @@ -293,14 +301,22 @@ public function data_generate_font_size_preset_fixtures() {
'expected_output' => '28px',
),

'return_fluid_value' => array(
'return_fluid_value' => array(
'font_size_preset' => array(
'size' => '1.75rem',
),
'should_use_fluid_typography' => true,
'expected_output' => 'clamp(1.3125rem, 1.3125rem + ((1vw - 0.48rem) * 2.524), 2.625rem)',
),

'return_fluid_value_with_number_coerced_to_px' => array(
'font_size_preset' => array(
'size' => 33,
),
'should_use_fluid_typography' => true,
'expected_output' => 'clamp(24.75px, 1.546875rem + ((1vw - 7.68px) * 2.975), 49.5px)',
),

'return_default_fluid_values_with_empty_fluid_array' => array(
'font_size_preset' => array(
'size' => '28px',
Expand All @@ -310,7 +326,7 @@ public function data_generate_font_size_preset_fixtures() {
'expected_output' => 'clamp(21px, 1.3125rem + ((1vw - 7.68px) * 2.524), 42px)',
),

'return_default_fluid_values_with_null_value' => array(
'return_default_fluid_values_with_null_value' => array(
'font_size_preset' => array(
'size' => '28px',
'fluid' => null,
Expand All @@ -319,7 +335,7 @@ public function data_generate_font_size_preset_fixtures() {
'expected_output' => 'clamp(21px, 1.3125rem + ((1vw - 7.68px) * 2.524), 42px)',
),

'return_size_with_invalid_fluid_units' => array(
'return_size_with_invalid_fluid_units' => array(
'font_size_preset' => array(
'size' => '10em',
'fluid' => array(
Expand All @@ -331,7 +347,7 @@ public function data_generate_font_size_preset_fixtures() {
'expected_output' => '10em',
),

'return_fluid_clamp_value' => array(
'return_fluid_clamp_value' => array(
'font_size_preset' => array(
'size' => '28px',
'fluid' => array(
Expand Down