diff --git a/packages/block-editor/CHANGELOG.md b/packages/block-editor/CHANGELOG.md index e2230b0c3ff1e..cc14eb7d86789 100644 --- a/packages/block-editor/CHANGELOG.md +++ b/packages/block-editor/CHANGELOG.md @@ -4,7 +4,8 @@ ### Bug Fix -- `FontSizePicker`: Update fluid utils so that only string, floats and integers are treated as valid font sizes for the purposes of fluid typography.([#44847](https://github.com/WordPress/gutenberg/pull/44847)) +- `FontSizePicker`: Update fluid utils so that only string, floats and integers are treated as valid font sizes for the purposes of fluid typography ([#44847](https://github.com/WordPress/gutenberg/pull/44847)) +- `getTypographyClassesAndStyles()`: Ensure that font sizes are transformed into fluid values if fluid typography is activated ([#44852](https://github.com/WordPress/gutenberg/pull/44852)) ## 10.2.0 (2022-10-05) diff --git a/packages/block-editor/README.md b/packages/block-editor/README.md index d30f527997028..2ecacfd4b959e 100644 --- a/packages/block-editor/README.md +++ b/packages/block-editor/README.md @@ -521,6 +521,7 @@ attributes. _Parameters_ - _attributes_ `Object`: Block attributes. +- _isFluidFontSizeActive_ `boolean`: Whether the function should try to convert font sizes to fluid values. _Returns_ diff --git a/packages/block-editor/src/hooks/test/use-typography-props.js b/packages/block-editor/src/hooks/test/use-typography-props.js index 93e4de29bc734..52f7bf97328ee 100644 --- a/packages/block-editor/src/hooks/test/use-typography-props.js +++ b/packages/block-editor/src/hooks/test/use-typography-props.js @@ -25,4 +25,26 @@ describe( 'getTypographyClassesAndStyles', () => { }, } ); } ); + + it( 'should return fluid font size styles', () => { + const attributes = { + fontFamily: 'tofu', + style: { + typography: { + letterSpacing: '22px', + fontSize: '2rem', + textTransform: 'uppercase', + }, + }, + }; + expect( getTypographyClassesAndStyles( attributes, true ) ).toEqual( { + className: 'has-tofu-font-family', + style: { + letterSpacing: '22px', + fontSize: + 'clamp(1.5rem, 1.5rem + ((1vw - 0.48rem) * 2.885), 3rem)', + textTransform: 'uppercase', + }, + } ); + } ); } ); diff --git a/packages/block-editor/src/hooks/use-typography-props.js b/packages/block-editor/src/hooks/use-typography-props.js index d08105d8d90c1..d70ae08aafc59 100644 --- a/packages/block-editor/src/hooks/use-typography-props.js +++ b/packages/block-editor/src/hooks/use-typography-props.js @@ -9,6 +9,7 @@ import classnames from 'classnames'; */ import { getInlineStyles } from './style'; import { getFontSizeClass } from '../components/font-sizes'; +import { getComputedFluidTypographyValue } from '../components/font-sizes/fluid-utils'; // This utility is intended to assist where the serialization of the typography // block support is being skipped for a block but the typography related CSS @@ -18,12 +19,26 @@ import { getFontSizeClass } from '../components/font-sizes'; * Provides the CSS class names and inline styles for a block's typography support * attributes. * - * @param {Object} attributes Block attributes. + * @param {Object} attributes Block attributes. + * @param {boolean} isFluidFontSizeActive Whether the function should try to convert font sizes to fluid values. * * @return {Object} Typography block support derived CSS classes & styles. */ -export function getTypographyClassesAndStyles( attributes ) { - const typographyStyles = attributes?.style?.typography || {}; +export function getTypographyClassesAndStyles( + attributes, + isFluidFontSizeActive +) { + let typographyStyles = attributes?.style?.typography || {}; + + if ( isFluidFontSizeActive ) { + typographyStyles = { + ...typographyStyles, + fontSize: getComputedFluidTypographyValue( { + fontSize: attributes?.style?.typography?.fontSize, + } ), + }; + } + const style = getInlineStyles( { typography: typographyStyles } ); const fontFamilyClassName = !! attributes?.fontFamily ? `has-${ kebabCase( attributes.fontFamily ) }-font-family` diff --git a/packages/block-library/src/search/edit.js b/packages/block-library/src/search/edit.js index 807dda04e743e..377788d281d3d 100644 --- a/packages/block-library/src/search/edit.js +++ b/packages/block-library/src/search/edit.js @@ -16,6 +16,7 @@ import { getTypographyClassesAndStyles as useTypographyProps, store as blockEditorStore, __experimentalGetElementClassName, + useSetting, } from '@wordpress/block-editor'; import { useDispatch, useSelect } from '@wordpress/data'; import { useEffect } from '@wordpress/element'; @@ -113,7 +114,11 @@ export default function SearchEdit( { } const colorProps = useColorProps( attributes ); - const typographyProps = useTypographyProps( attributes ); + const fluidTypographyEnabled = useSetting( 'typography.fluid' ); + const typographyProps = useTypographyProps( + attributes, + fluidTypographyEnabled + ); const unitControlInstanceId = useInstanceId( UnitControl ); const unitControlInputId = `wp-block-search__width-${ unitControlInstanceId }`; const isButtonPositionInside = 'button-inside' === buttonPosition;