Skip to content

Commit

Permalink
Initial commit (#44852)
Browse files Browse the repository at this point in the history
- ensure that we convert fluid font sizes to fluid values in the editor for search block block supports
- we do this by passing the getTypographyClassesAndStyles hook a flag to tell it whether to convert

Updated CHANGELOG.md
Added tests
  • Loading branch information
ramonjd committed Oct 11, 2022
1 parent fe22cda commit edf3e4a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/block-editor/CHANGELOG.md
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/README.md
Expand Up @@ -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_

Expand Down
22 changes: 22 additions & 0 deletions packages/block-editor/src/hooks/test/use-typography-props.js
Expand Up @@ -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',
},
} );
} );
} );
21 changes: 18 additions & 3 deletions packages/block-editor/src/hooks/use-typography-props.js
Expand Up @@ -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
Expand All @@ -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`
Expand Down
7 changes: 6 additions & 1 deletion packages/block-library/src/search/edit.js
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit edf3e4a

Please sign in to comment.