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: add min and max viewport width configurable options #53081

Merged
merged 5 commits into from
Aug 4, 2023
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
20 changes: 14 additions & 6 deletions lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,21 @@ function gutenberg_get_typography_font_size_value( $preset, $should_use_fluid_ty
$fluid_settings = isset( $typography_settings['fluid'] ) && is_array( $typography_settings['fluid'] ) ? $typography_settings['fluid'] : array();

// Defaults.
$default_maximum_viewport_width = isset( $layout_settings['wideSize'] ) ? $layout_settings['wideSize'] : '1600px';
$default_maximum_viewport_width = '1600px';
$default_minimum_viewport_width = '320px';
$default_minimum_font_size_factor_max = 0.75;
$default_minimum_font_size_factor_min = 0.25;
$default_scale_factor = 1;
$has_min_font_size = isset( $fluid_settings['minFontSize'] ) && ! empty( gutenberg_get_typography_value_and_unit( $fluid_settings['minFontSize'] ) );
$default_minimum_font_size_limit = $has_min_font_size ? $fluid_settings['minFontSize'] : '14px';
$default_minimum_font_size_limit = '14px';

// Defaults overrides.
$minimum_viewport_width = isset( $fluid_settings['minViewportWidth'] ) ? $fluid_settings['minViewportWidth'] : $default_minimum_viewport_width;
$maximum_viewport_width = isset( $layout_settings['wideSize'] ) ? $layout_settings['wideSize'] : $default_maximum_viewport_width;
if ( isset( $fluid_settings['maxViewportWidth'] ) ) {
$maximum_viewport_width = $fluid_settings['maxViewportWidth'];
}
$has_min_font_size = isset( $fluid_settings['minFontSize'] ) && ! empty( gutenberg_get_typography_value_and_unit( $fluid_settings['minFontSize'] ) );
$minimum_font_size_limit = $has_min_font_size ? $fluid_settings['minFontSize'] : $default_minimum_font_size_limit;

// Font sizes.
$fluid_font_size_settings = isset( $preset['fluid'] ) ? $preset['fluid'] : null;
Expand All @@ -489,7 +497,7 @@ function gutenberg_get_typography_font_size_value( $preset, $should_use_fluid_ty

// Parses the minimum font size limit, so we can perform checks using it.
$minimum_font_size_limit = gutenberg_get_typography_value_and_unit(
$default_minimum_font_size_limit,
$minimum_font_size_limit,
array(
'coerce_to' => $preferred_size['unit'],
)
Expand Down Expand Up @@ -538,8 +546,8 @@ function gutenberg_get_typography_font_size_value( $preset, $should_use_fluid_ty

$fluid_font_size_value = gutenberg_get_computed_fluid_typography_value(
array(
'minimum_viewport_width' => $default_minimum_viewport_width,
'maximum_viewport_width' => $default_maximum_viewport_width,
'minimum_viewport_width' => $minimum_viewport_width,
'maximum_viewport_width' => $maximum_viewport_width,
'minimum_font_size' => $minimum_font_size_raw,
'maximum_font_size' => $maximum_font_size_raw,
'scale_factor' => $default_scale_factor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,36 @@ describe( 'typography utils', () => {
'clamp(1.119rem, 1.119rem + ((1vw - 0.2rem) * 0.789), 1.75rem)',
},

{
message:
'should override default max viewport width fluid typography settings',
preset: {
size: '1.75rem',
},
typographySettings: {
fluid: {
maxViewportWidth: '1200px',
},
},
expected:
'clamp(1.119rem, 1.119rem + ((1vw - 0.2rem) * 1.147), 1.75rem)',
},

{
message:
'should override default min viewport width fluid typography settings',
preset: {
size: '1.75rem',
},
typographySettings: {
fluid: {
minViewportWidth: '800px',
},
},
expected:
'clamp(1.119rem, 1.119rem + ((1vw - 0.5rem) * 1.262), 1.75rem)',
},

{
message: 'should return clamp value with em min and max units',
preset: {
Expand Down Expand Up @@ -478,6 +508,21 @@ describe( 'typography utils', () => {
expected:
'clamp(100px, 6.25rem + ((1vw - 3.2px) * 7.813), 200px)',
},

{
message: 'should apply all custom fluid typography settings',
preset: {
size: '17px',
},
typographySettings: {
fluid: {
minFontSize: '16px',
maxViewportWidth: '1200px',
minViewportWidth: '640px',
},
},
expected: 'clamp(16px, 1rem + ((1vw - 6.4px) * 0.179), 17px)',
},
].forEach( ( { message, preset, typographySettings, expected } ) => {
it( `${ message }`, () => {
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function getTypographyFontSizeValue( preset, typographyOptions ) {
fontSize: defaultSize,
minimumFontSizeLimit: fluidTypographySettings?.minFontSize,
maximumViewportWidth: fluidTypographySettings?.maxViewportWidth,
minimumViewportWidth: fluidTypographySettings?.minViewportWidth,
} );

if ( !! fluidFontSizeValue ) {
Expand Down
2 changes: 1 addition & 1 deletion phpunit/block-supports/typography-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ public function data_generate_block_supports_font_size_fixtures() {
'returns clamp value using custom fluid config' => array(
'font_size_value' => '17px',
'theme_slug' => 'block-theme-child-with-fluid-typography-config',
'expected_output' => 'font-size:clamp(16px, 1rem + ((1vw - 3.2px) * 0.147), 17px);',
'expected_output' => 'font-size:clamp(16px, 1rem + ((1vw - 6.4px) * 0.179), 17px);',
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this expected value different now because of the maxViewportWidth and minViewportWidth values included in the test theme's theme.json?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry for the late reply.

Yes that's right.

),
'returns value when font size <= custom min font size bound' => array(
'font_size_value' => '15px',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
},
"typography": {
"fluid": {
"minFontSize": "16px"
"minFontSize": "16px",
"maxViewportWidth": "1200px",
"minViewportWidth": "640px"
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion schemas/json/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,15 @@
"type": "object",
"properties": {
"minFontSize": {
"description": "Allow users to set a global minimum font size boundary in px, rem or em. Custom font sizes below this value will not be clamped, and all calculated minimum font sizes will be, a at minimum, this value.",
"description": "Allow users to set a global minimum font size boundary in px, rem or em. Custom font sizes below this value will not be clamped, and all calculated minimum font sizes will be, at a minimum, this value.",
"type": "string"
},
"maxViewportWidth": {
"description": "Allow users to set custom a max viewport width in px, rem or em, used to set the maximum size boundary of a fluid font size.",
"type": "string"
},
"minViewportWidth": {
"description": "Allow users to set a custom min viewport width in px, rem or em, used to set the minimum size boundary of a fluid font size.",
"type": "string"
}
},
Expand Down