Skip to content

Commit

Permalink
Fluid Typography: ensure global styles preset fluid sizes are calcula…
Browse files Browse the repository at this point in the history
…ted correctly (#44791)

* Forked #44761

* Ensuring the font size picker select box shows the right labels

* update comment. Typescript has beaten me. It's much more convenient to use getFontSizeOptions(), but I guess we'll have to refactor.

* Adding comment about Typescript bug (YAY, it wasn't me being dumb with TS for once)

* Added tests yo

* Changeo loggo
  • Loading branch information
ramonjd authored and andrewserong committed Oct 10, 2022
1 parent 3fbec39 commit d2561a0
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 23 deletions.
9 changes: 8 additions & 1 deletion lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php
Original file line number Diff line number Diff line change
Expand Up @@ -1047,8 +1047,15 @@ protected static function compute_style_properties( $styles, $settings = array()
continue;
}

// Calculate fluid typography rules where available.
// Calculates fluid typography rules where available.
if ( 'font-size' === $css_property ) {
/*
* gutenberg_get_typography_font_size_value() will check
* if fluid typography has been activated and also
* whether the incoming value can be converted to a fluid value.
* Values that already have a "clamp()" function will not pass the test,
* and therefore the original $value will be returned.
*/
$value = gutenberg_get_typography_font_size_value( array( 'size' => $value ) );
}

Expand Down
3 changes: 3 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Bug Fix
- `FontSizePicker`: Ensure that fluid font size presets appear correctly in the UI controls ([#44791](https://github.com/WordPress/gutenberg/pull/44791))

## 21.2.0 (2022-10-05)

### Enhancements
Expand Down
8 changes: 7 additions & 1 deletion packages/components/src/font-size-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ const UnforwardedFontSizePicker = (
),
[ shouldUseSelectControl, fontSizes, disableCustomFontSizes ]
);
const selectedOption = getSelectedOption( fontSizes, value );
const selectedOption = getSelectedOption(
fontSizes,
value,
shouldUseSelectControl,
disableCustomFontSizes
);

const isCustomValue = selectedOption.slug === CUSTOM_FONT_SIZE;
const [ showCustomValueControl, setShowCustomValueControl ] = useState(
! disableCustomFontSizes && isCustomValue
Expand Down
15 changes: 10 additions & 5 deletions packages/components/src/font-size-picker/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,33 +120,38 @@ describe( 'getToggleGroupOptions', () => {
).toEqual( [
{
key: '1',
value: '1',
label: 'S',
name: '1',
size: '1',
value: '1',
},
{
key: '2',
value: '2',
label: 'M',
name: '2',
size: '2',
value: '2',
},
{
key: '3',
value: '3',
label: 'L',
name: '3',
size: '3',
value: '3',
},
{
key: '4',
value: '4',
label: 'XL',
name: '4',
size: '4',
value: '4',
},
{
key: '5',
value: '5',
label: 'XXL',
name: 'XXL',
value: '5',
size: '5',
},
] );
} );
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/font-size-picker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,5 @@ export type FontSizeToggleGroupOption = {
value: number | string;
label: string;
name: string;
size?: number | string;
};
27 changes: 22 additions & 5 deletions packages/components/src/font-size-picker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ function getSelectOptions(
];
return options.map( ( { slug, name, size } ) => ( {
key: slug,
name,
name: name || slug,
label: name || slug,
slug,
size,
__experimentalHint:
size && isSimpleCssValue( size ) && parseFloat( String( size ) ),
Expand All @@ -134,6 +136,7 @@ export function getToggleGroupOptions(
return {
key: slug,
value: size,
size,
label: labelAliases[ index ],
name: name || labelAliases[ index ],
};
Expand All @@ -142,13 +145,27 @@ export function getToggleGroupOptions(

export function getSelectedOption(
fontSizes: FontSize[],
value: FontSizePickerProps[ 'value' ]
value: FontSizePickerProps[ 'value' ],
useSelectControl: boolean,
disableCustomFontSizes: boolean = false
): FontSizeOption {
if ( ! value ) {
return DEFAULT_FONT_SIZE_OPTION;
}
return (
fontSizes.find( ( font ) => font.size === value ) ||
CUSTOM_FONT_SIZE_OPTION

const fontSizeOptions = getFontSizeOptions(
useSelectControl,
fontSizes,
disableCustomFontSizes
);

const selectedOption = fontSizeOptions
? // @TODO Array.find() triggers error on array types unions. It's a bug. See: https://github.com/microsoft/TypeScript/issues/44373.
// @ts-ignore
fontSizeOptions.find(
( option: FontSizeSelectOption ) => option.size === value
) // @ts-ignore
: null;

return selectedOption || CUSTOM_FONT_SIZE_OPTION;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,41 @@ describe( 'typography utils', () => {
},
expected: '28px',
},
// Should coerce number to `px` and return non-fluid value.
// Should coerce number to `px` and return fluid value.
{
preset: {
size: 33,
fluid: true,
},
typographySettings: {
fluid: true,
},
expected:
'clamp(24.75px, 1.546875rem + ((1vw - 7.68px) * 2.975), 49.5px)',
},
// Should return incoming value when already clamped.
{
preset: {
size: 'clamp(21px, 1.3125rem + ((1vw - 7.68px) * 2.524), 42px)',
fluid: false,
},
typographySettings: {
fluid: true,
},
expected:
'clamp(21px, 1.3125rem + ((1vw - 7.68px) * 2.524), 42px)',
},
// Should return incoming value with unsupported unit.
{
preset: {
size: '1000%',
fluid: false,
},
typographySettings: {
fluid: true,
},
expected: '1000%',
},
// Should return fluid value.
{
preset: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import { getSupportedGlobalStylesPanels, useSetting, useStyle } from './hooks';
import { getTypographyFontSizeValue } from './typography-utils';

export function useHasTypographyPanel( name ) {
const hasFontFamily = useHasFontFamilyControl( name );
Expand Down Expand Up @@ -151,7 +152,19 @@ export default function TypographyPanel( { name, element, headingLevel } ) {
} else if ( element && element !== 'text' ) {
prefix = `elements.${ element }.`;
}
const [ fluidTypography ] = useSetting( 'typography.fluid', name );
const [ fontSizes ] = useSetting( 'typography.fontSizes', name );

// Convert static font size values to fluid font sizes if fluidTypography is activated.
const fontSizesWithFluidValues = fontSizes.map( ( font ) => {
if ( !! fluidTypography ) {
font.size = getTypographyFontSizeValue( font, {
fluid: fluidTypography,
} );
}
return font;
} );

const disableCustomFontSizes = ! useSetting(
'typography.customFontSize',
name
Expand Down Expand Up @@ -240,7 +253,7 @@ export default function TypographyPanel( { name, element, headingLevel } ) {
<FontSizePicker
value={ fontSize }
onChange={ setFontSize }
fontSizes={ fontSizes }
fontSizes={ fontSizesWithFluidValues }
disableCustomFontSizes={ disableCustomFontSizes }
withReset={ false }
size="__unstable-large"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,27 @@
*/
import { getComputedFluidTypographyValue } from '@wordpress/block-editor';

/**
* @typedef {Object} FluidPreset
* @property {string|undefined} max A maximum font size value.
* @property {string|undefined} min A minimum font size value.
*/

/**
* @typedef {Object} Preset
* @property {string} size A default font size.
* @property {string} name A font size name, displayed in the UI.
* @property {string} slug A font size slug
* @property {boolean|FluidPreset|undefined} fluid A font size slug
*/

/**
* Returns a font-size value based on a given font-size preset.
* Takes into account fluid typography parameters and attempts to return a css formula depending on available, valid values.
*
* @param {Object} preset
* @param {string} preset.size A default font size.
* @param {string} preset.name A font size name, displayed in the UI.
* @param {string} preset.slug A font size slug.
* @param {Object} preset.fluid
* @param {string|undefined} preset.fluid.max A maximum font size value.
* @param {string|undefined} preset.fluid.min A minimum font size value.
* @param {Object} typographySettings
* @param {boolean} typographySettings.fluid Whether fluid typography is enabled.
* @param {Preset} preset
* @param {Object} typographySettings
* @param {boolean} typographySettings.fluid Whether fluid typography is enabled.
*
* @return {string} An font-size value
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ export function getStylesDeclarations(

// Calculate fluid typography rules where available.
if ( cssProperty === 'font-size' ) {
/*
* getTypographyFontSizeValue() will check
* if fluid typography has been activated and also
* whether the incoming value can be converted to a fluid value.
* Values that already have a "clamp()" function will not pass the test,
* and therefore the original $value will be returned.
*/
ruleValue = getTypographyFontSizeValue(
{ size: ruleValue },
tree?.settings?.typography
Expand Down

0 comments on commit d2561a0

Please sign in to comment.