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 configurable settings for minimum font size to theme.json #42489

Merged
merged 15 commits into from Dec 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
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
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
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
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
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
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
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