Skip to content

Commit 5ff2803

Browse files
Editor: Add font size constraints for fluid typography.
This commit: * Adds default minimum font size limits so that min font size, where provided, does not become smaller than `14px`/`0.875rem`/`0.875em`. * For font sizes of `< 14px` that have no defined minimum sizes, uses the font size to set the floor of the `clamp()` value. This bugfix prevents converting existing small font sizes to clamp values that will reduce their font size even further in narrow widths. It therefore improves backward compatibility and accessibility. Original PR from Gutenberg repository: * [WordPress/gutenberg#44993 #44993 Fluid typography: add font size constraints] Follow-up to [54260], [54360], [54497], [54500]. Props ramonopoly, andrewserong, isabel_brison, Joen, bernhard-reiter. See #56467. Built from https://develop.svn.wordpress.org/trunk@54646 git-svn-id: https://core.svn.wordpress.org/trunk@54198 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent c2911ae commit 5ff2803

File tree

2 files changed

+67
-18
lines changed

2 files changed

+67
-18
lines changed

wp-includes/block-supports/typography.php

+66-17
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ function wp_apply_typography_support( $block_type, $block_attributes ) {
119119
$custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] )
120120
? $block_attributes['style']['typography']['fontSize']
121121
: null;
122-
$typography_block_styles['fontSize'] = $preset_font_size ? $preset_font_size : wp_get_typography_font_size_value(
123-
array(
124-
'size' => $custom_font_size,
125-
)
126-
);
122+
$typography_block_styles['fontSize'] = $preset_font_size ? $preset_font_size : wp_get_typography_font_size_value(
123+
array(
124+
'size' => $custom_font_size,
125+
)
126+
);
127127
}
128128

129129
if ( $has_font_family_support && ! $should_skip_font_family ) {
@@ -348,8 +348,17 @@ function wp_get_typography_value_and_unit( $raw_value, $options = array() ) {
348348
$unit = $options['coerce_to'];
349349
}
350350

351+
/*
352+
* No calculation is required if swapping between em and rem yet,
353+
* since we assume a root size value. Later we might like to differentiate between
354+
* :root font size (rem) and parent element font size (em) relativity.
355+
*/
356+
if ( ( 'em' === $options['coerce_to'] || 'rem' === $options['coerce_to'] ) && ( 'em' === $unit || 'rem' === $unit ) ) {
357+
$unit = $options['coerce_to'];
358+
}
359+
351360
return array(
352-
'value' => $value,
361+
'value' => round( $value, 3 ),
353362
'unit' => $unit,
354363
);
355364
}
@@ -380,26 +389,29 @@ function wp_get_computed_fluid_typography_value( $args = array() ) {
380389
$minimum_font_size_raw = isset( $args['minimum_font_size'] ) ? $args['minimum_font_size'] : null;
381390
$scale_factor = isset( $args['scale_factor'] ) ? $args['scale_factor'] : null;
382391

383-
// Grab the minimum font size and normalize it in order to use the value for calculations.
392+
// Normalizes the minimum font size in order to use the value for calculations.
384393
$minimum_font_size = wp_get_typography_value_and_unit( $minimum_font_size_raw );
385394

386-
// We get a 'preferred' unit to keep units consistent when calculating, otherwise the result will not be accurate.
395+
/*
396+
* We get a 'preferred' unit to keep units consistent when calculating,
397+
* otherwise the result will not be accurate.
398+
*/
387399
$font_size_unit = isset( $minimum_font_size['unit'] ) ? $minimum_font_size['unit'] : 'rem';
388400

389-
// Grab the maximum font size and normalize it in order to use the value for calculations.
401+
// Normalizes the maximum font size in order to use the value for calculations.
390402
$maximum_font_size = wp_get_typography_value_and_unit(
391403
$maximum_font_size_raw,
392404
array(
393405
'coerce_to' => $font_size_unit,
394406
)
395407
);
396408

397-
// Protect against unsupported units.
409+
// Checks for mandatory min and max sizes, and protects against unsupported units.
398410
if ( ! $maximum_font_size || ! $minimum_font_size ) {
399411
return null;
400412
}
401413

402-
// Use rem for accessible fluid target font scaling.
414+
// Uses rem for accessible fluid target font scaling.
403415
$minimum_font_size_rem = wp_get_typography_value_and_unit(
404416
$minimum_font_size_raw,
405417
array(
@@ -427,8 +439,9 @@ function wp_get_computed_fluid_typography_value( $args = array() ) {
427439
*/
428440
$view_port_width_offset = round( $minimum_viewport_width['value'] / 100, 3 ) . $font_size_unit;
429441
$linear_factor = 100 * ( ( $maximum_font_size['value'] - $minimum_font_size['value'] ) / ( $maximum_viewport_width['value'] - $minimum_viewport_width['value'] ) );
430-
$linear_factor = round( $linear_factor, 3 ) * $scale_factor;
431-
$fluid_target_font_size = implode( '', $minimum_font_size_rem ) . " + ((1vw - $view_port_width_offset) * $linear_factor)";
442+
$linear_factor_scaled = round( $linear_factor * $scale_factor, 3 );
443+
$linear_factor_scaled = empty( $linear_factor_scaled ) ? 1 : $linear_factor_scaled;
444+
$fluid_target_font_size = implode( '', $minimum_font_size_rem ) . " + ((1vw - $view_port_width_offset) * $linear_factor_scaled)";
432445

433446
return "clamp($minimum_font_size_raw, $fluid_target_font_size, $maximum_font_size_raw)";
434447
}
@@ -478,6 +491,7 @@ function wp_get_typography_font_size_value( $preset, $should_use_fluid_typograph
478491
$default_minimum_font_size_factor = 0.75;
479492
$default_maximum_font_size_factor = 1.5;
480493
$default_scale_factor = 1;
494+
$default_minimum_font_size_limit = '14px';
481495

482496
// Font sizes.
483497
$fluid_font_size_settings = isset( $preset['fluid'] ) ? $preset['fluid'] : null;
@@ -499,13 +513,48 @@ function wp_get_typography_font_size_value( $preset, $should_use_fluid_typograph
499513
return $preset['size'];
500514
}
501515

502-
// If no fluid min or max font sizes are available, create some using min/max font size factors.
516+
// If no fluid max font size is available, create one using max font size factor.
517+
if ( ! $maximum_font_size_raw ) {
518+
$maximum_font_size_raw = round( $preferred_size['value'] * $default_maximum_font_size_factor, 3 ) . $preferred_size['unit'];
519+
}
520+
521+
// If no fluid min font size is available, create one using min font size factor.
503522
if ( ! $minimum_font_size_raw ) {
504-
$minimum_font_size_raw = ( $preferred_size['value'] * $default_minimum_font_size_factor ) . $preferred_size['unit'];
523+
$minimum_font_size_raw = round( $preferred_size['value'] * $default_minimum_font_size_factor, 3 ) . $preferred_size['unit'];
505524
}
506525

507-
if ( ! $maximum_font_size_raw ) {
508-
$maximum_font_size_raw = ( $preferred_size['value'] * $default_maximum_font_size_factor ) . $preferred_size['unit'];
526+
// Normalizes the minimum font size limit according to the incoming unit, so we can perform checks using it.
527+
$minimum_font_size_limit = wp_get_typography_value_and_unit(
528+
$default_minimum_font_size_limit,
529+
array(
530+
'coerce_to' => $preferred_size['unit'],
531+
)
532+
);
533+
534+
if ( ! empty( $minimum_font_size_limit ) ) {
535+
/*
536+
* If a minimum size was not passed to this function
537+
* and the user-defined font size is lower than $minimum_font_size_limit,
538+
* then use the user-defined font size as the minimum font-size.
539+
*/
540+
if ( ! isset( $fluid_font_size_settings['min'] ) && $preferred_size['value'] < $minimum_font_size_limit['value'] ) {
541+
$minimum_font_size_raw = implode( '', $preferred_size );
542+
} else {
543+
$minimum_font_size_parsed = wp_get_typography_value_and_unit(
544+
$minimum_font_size_raw,
545+
array(
546+
'coerce_to' => $preferred_size['unit'],
547+
)
548+
);
549+
550+
/*
551+
* If the passed or calculated minimum font size is lower than $minimum_font_size_limit
552+
* use $minimum_font_size_limit instead.
553+
*/
554+
if ( ! empty( $minimum_font_size_parsed ) && $minimum_font_size_parsed['value'] < $minimum_font_size_limit['value'] ) {
555+
$minimum_font_size_raw = implode( '', $minimum_font_size_limit );
556+
}
557+
}
509558
}
510559

511560
$fluid_font_size_value = wp_get_computed_fluid_typography_value(

wp-includes/version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '6.2-alpha-54645';
19+
$wp_version = '6.2-alpha-54646';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)