Skip to content

Commit

Permalink
Fluid typography: add configurable settings for minimum font size to …
Browse files Browse the repository at this point in the history
…theme.json (#42489)

* Reinstating the commit in edeaca7 so we can test fluid configuration

Update docs

* Adding a filter pre-calculations

* $fluid_settings was missing

* Removing filter
Pulling across config changes to JS

* Add config slot for minFontSize

* Adding config to test theme

* Updating args to include `minimumFontSizeLimit`
Fixed `typographySettings` check

* Updating tests and fixing settings logic
Updating schema

Changelogs

* Ensure that a theme's fluid font size settings are used when calculating custom font sizes and search block font sizes.

* Validating incoming viewport widths and min font size against accepted units.

* Ensure we check for fluid settings validity in getTypographyClassesAndStyles

* Rolling back configurable values to the global minimum font size only.
Updating tests.

* Remove config assignment from getTypographyClassesAndStyles and updated tests.

* Adding frontend test for unsupported units in min font size config

* simplifying condition so that it's readable in screen widths < 100000px
  • Loading branch information
ramonjd authored Dec 15, 2022
1 parent da03081 commit 7973d9c
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Settings related to typography.
| customFontSize | boolean | true | |
| fontStyle | boolean | true | |
| fontWeight | boolean | true | |
| fluid | boolean | | |
| fluid | undefined | false | |
| letterSpacing | boolean | true | |
| lineHeight | boolean | false | |
| textDecoration | boolean | true | |
Expand Down
11 changes: 9 additions & 2 deletions lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,18 +451,25 @@ function gutenberg_get_typography_font_size_value( $preset, $should_use_fluid_ty

// Checks if fluid font sizes are activated.
$typography_settings = gutenberg_get_global_settings( array( 'typography' ) );
$should_use_fluid_typography = isset( $typography_settings['fluid'] ) && true === $typography_settings['fluid'] ? true : $should_use_fluid_typography;
$should_use_fluid_typography
= isset( $typography_settings['fluid'] ) &&
( true === $typography_settings['fluid'] || is_array( $typography_settings['fluid'] ) ) ?
true :
$should_use_fluid_typography;

if ( ! $should_use_fluid_typography ) {
return $preset['size'];
}

$fluid_settings = isset( $typography_settings['fluid'] ) && is_array( $typography_settings['fluid'] ) ? $typography_settings['fluid'] : array();

// Defaults.
$default_maximum_viewport_width = '1600px';
$default_minimum_viewport_width = '768px';
$default_minimum_font_size_factor = 0.75;
$default_scale_factor = 1;
$default_minimum_font_size_limit = '14px';
$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';

// Font sizes.
$fluid_font_size_settings = isset( $preset['fluid'] ) ? $preset['fluid'] : null;
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Enhancements

- `URLInput`: the `renderSuggestions` callback prop now receives `currentInputValue` as a new parameter ([45806](https://github.com/WordPress/gutenberg/pull/45806)).
- Fluid typography: add configurable fluid typography settings for minimum font size to theme.json ([#42489](https://github.com/WordPress/gutenberg/pull/42489)).

### Bug Fix

Expand Down
3 changes: 2 additions & 1 deletion packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ _Parameters_
- _args.minimumFontSize_ `?string`: Minimum font size for any clamp() calculation. Optional.
- _args.scaleFactor_ `?number`: A scale factor to determine how fast a font scales within boundaries. Optional.
- _args.minimumFontSizeFactor_ `?number`: How much to scale defaultFontSize by to derive minimumFontSize. Optional.
- _args.minimumFontSizeLimit_ `?string`: The smallest a calculated font size may be. Optional.

_Returns_

Expand Down Expand Up @@ -520,7 +521,7 @@ attributes.
_Parameters_

- _attributes_ `Object`: Block attributes.
- _isFluidFontSizeActive_ `boolean`: Whether the function should try to convert font sizes to fluid values.
- _fluidTypographySettings_ `Object|boolean`: If boolean, whether the function should try to convert font sizes to fluid values, otherwise an object containing theme fluid typography settings.

_Returns_

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const DEFAULT_MINIMUM_FONT_SIZE_LIMIT = '14px';
* @param {?string} args.minimumFontSize Minimum font size for any clamp() calculation. Optional.
* @param {?number} args.scaleFactor A scale factor to determine how fast a font scales within boundaries. Optional.
* @param {?number} args.minimumFontSizeFactor How much to scale defaultFontSize by to derive minimumFontSize. Optional.
* @param {?string} args.minimumFontSizeLimit The smallest a calculated font size may be. Optional.
*
* @return {string|null} A font-size value using clamp().
*/
Expand All @@ -51,8 +52,13 @@ export function getComputedFluidTypographyValue( {
maximumViewPortWidth = DEFAULT_MAXIMUM_VIEWPORT_WIDTH,
scaleFactor = DEFAULT_SCALE_FACTOR,
minimumFontSizeFactor = DEFAULT_MINIMUM_FONT_SIZE_FACTOR,
minimumFontSizeLimit = DEFAULT_MINIMUM_FONT_SIZE_LIMIT,
minimumFontSizeLimit,
} ) {
// Validate incoming settings and set defaults.
minimumFontSizeLimit = !! getTypographyValueAndUnit( minimumFontSizeLimit )
? minimumFontSizeLimit
: DEFAULT_MINIMUM_FONT_SIZE_LIMIT;

/*
* Calculates missing minimumFontSize and maximumFontSize from
* defaultFontSize if provided.
Expand Down
17 changes: 13 additions & 4 deletions packages/block-editor/src/hooks/font-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,22 @@ function addEditPropsForFluidCustomFontSizes( blockType ) {
// BlockListContext.Provider. If we set fontSize using editor.
// BlockListBlock instead of using getEditWrapperProps then the value is
// clobbered when the core/style/addEditProps filter runs.
const isFluidTypographyEnabled =
!! select( blockEditorStore ).getSettings().__experimentalFeatures
const fluidTypographyConfig =
select( blockEditorStore ).getSettings().__experimentalFeatures
?.typography?.fluid;

const fluidTypographySettings =
typeof fluidTypographyConfig === 'object'
? fluidTypographyConfig
: {};

const newFontSize =
fontSize && isFluidTypographyEnabled
? getComputedFluidTypographyValue( { fontSize } )
fontSize && !! fluidTypographyConfig
? getComputedFluidTypographyValue( {
fontSize,
minimumFontSizeLimit:
fluidTypographySettings?.minFontSize,
} )
: null;

if ( newFontSize === null ) {
Expand Down
26 changes: 26 additions & 0 deletions packages/block-editor/src/hooks/test/use-typography-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,30 @@ describe( 'getTypographyClassesAndStyles', () => {
},
} );
} );

it( 'should return configured fluid font size styles', () => {
const attributes = {
fontFamily: 'tofu',
style: {
typography: {
textDecoration: 'underline',
fontSize: '2rem',
textTransform: 'uppercase',
},
},
};
expect(
getTypographyClassesAndStyles( attributes, {
minFontSize: '1rem',
} )
).toEqual( {
className: 'has-tofu-font-family',
style: {
textDecoration: 'underline',
fontSize:
'clamp(1.5rem, 1.5rem + ((1vw - 0.48rem) * 0.962), 2rem)',
textTransform: 'uppercase',
},
} );
} );
} );
22 changes: 15 additions & 7 deletions packages/block-editor/src/hooks/use-typography-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,31 @@ import { getComputedFluidTypographyValue } from '../components/font-sizes/fluid-
* Provides the CSS class names and inline styles for a block's typography support
* attributes.
*
* @param {Object} attributes Block attributes.
* @param {boolean} isFluidFontSizeActive Whether the function should try to convert font sizes to fluid values.
* @param {Object} attributes Block attributes.
* @param {Object|boolean} fluidTypographySettings If boolean, whether the function should try to convert font sizes to fluid values,
* otherwise an object containing theme fluid typography settings.
*
* @return {Object} Typography block support derived CSS classes & styles.
*/
export function getTypographyClassesAndStyles(
attributes,
isFluidFontSizeActive
fluidTypographySettings
) {
let typographyStyles = attributes?.style?.typography || {};

if ( isFluidFontSizeActive ) {
if (
!! fluidTypographySettings &&
( true === fluidTypographySettings ||
Object.keys( fluidTypographySettings ).length !== 0 )
) {
const newFontSize =
getComputedFluidTypographyValue( {
fontSize: attributes?.style?.typography?.fontSize,
minimumFontSizeLimit: fluidTypographySettings?.minFontSize,
} ) || attributes?.style?.typography?.fontSize;
typographyStyles = {
...typographyStyles,
fontSize: getComputedFluidTypographyValue( {
fontSize: attributes?.style?.typography?.fontSize,
} ),
fontSize: newFontSize,
};
}

Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/search/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ export default function SearchEdit( {
}

const colorProps = useColorProps( attributes );
const fluidTypographyEnabled = useSetting( 'typography.fluid' );
const fluidTypographySettings = useSetting( 'typography.fluid' );
const typographyProps = useTypographyProps(
attributes,
fluidTypographyEnabled
fluidTypographySettings
);
const unitControlInstanceId = useInstanceId( UnitControl );
const unitControlInputId = `wp-block-search__width-${ unitControlInstanceId }`;
Expand Down
6 changes: 5 additions & 1 deletion packages/edit-site/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

### Breaking Changes

- Updated dependencies to require React 18 ([45235](https://github.com/WordPress/gutenberg/pull/45235))
- Updated dependencies to require React 18 ([45235](https://github.com/WordPress/gutenberg/pull/45235)).

### Enhancements

- Fluid typography: add configurable fluid typography settings for minimum font size to theme.json ([#42489](https://github.com/WordPress/gutenberg/pull/42489)).

## 4.19.0 (2022-11-16)

Expand Down
Loading

0 comments on commit 7973d9c

Please sign in to comment.