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 for custom font values: scratch pad #44759

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function gutenberg_register_typography_support( $block_type ) {
* @return array Typography CSS classes and inline styles.
*/
function gutenberg_apply_typography_support( $block_type, $block_attributes ) {

if ( ! property_exists( $block_type, 'supports' ) ) {
return array();
}
Expand Down Expand Up @@ -108,7 +109,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
if ( $has_font_size_support && ! $should_skip_font_size ) {
$preset_font_size = array_key_exists( 'fontSize', $block_attributes ) ? "var:preset|font-size|{$block_attributes['fontSize']}" : null;
$custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] ) ? $block_attributes['style']['typography']['fontSize'] : null;
$typography_block_styles['fontSize'] = $preset_font_size ? $preset_font_size : $custom_font_size;
$typography_block_styles['fontSize'] = $preset_font_size ? $preset_font_size : gutenberg_get_typography_font_size_value( array( 'size' => $custom_font_size ) );
}

if ( $has_font_family_support && ! $should_skip_font_family ) {
Expand Down Expand Up @@ -381,7 +382,7 @@ function gutenberg_get_typography_font_size_value( $preset, $should_use_fluid_ty
$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;

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

Expand Down
66 changes: 56 additions & 10 deletions packages/block-editor/src/hooks/font-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
shouldSkipSerialization,
} from './utils';
import useSetting from '../components/use-setting';
// @TODO move this to a common lib and export.
import { getTypographyFontSizeValue } from '../../../edit-site/src/components/global-styles/typography-utils';

export const FONT_SIZE_SUPPORT_KEY = 'typography.fontSize';

Expand Down Expand Up @@ -117,16 +119,27 @@ export function FontSizeEdit( props ) {
setAttributes,
} = props;
const fontSizes = useSetting( 'typography.fontSizes' );
const isFluidTypographyEnabled = useSetting( 'typography.fluid' );

const onChange = ( value ) => {
const fontSizeSlug = getFontSizeObjectByValue( fontSizes, value ).slug;
let fontSizeValue;
if (
isFluidTypographyEnabled &&
typeof style?.typography?.fontSize === 'string'
) {
fontSizeValue = getTypographyFontSizeValue(
{ size: style?.typography?.fontSize },
{ fluid: isFluidTypographyEnabled }
);
}

setAttributes( {
style: cleanEmptyObject( {
...style,
typography: {
...style?.typography,
fontSize: fontSizeSlug ? undefined : value,
fontSize: fontSizeSlug ? undefined : fontSizeValue || value,
},
} ),
fontSize: fontSizeSlug,
Expand Down Expand Up @@ -215,12 +228,42 @@ export function useIsFontSizeDisabled( { name: blockName } = {} ) {
const withFontSizeInlineStyles = createHigherOrderComponent(
( BlockListBlock ) => ( props ) => {
const fontSizes = useSetting( 'typography.fontSizes' );
const isFluidTypographyEnabled = useSetting( 'typography.fluid' );
const {
name: blockName,
attributes: { fontSize, style },
wrapperProps,
} = props;

console.log(
'what? hasBlockSupport',
hasBlockSupport( blockName, FONT_SIZE_SUPPORT_KEY )
);
console.log(
'what? isFluidTypographyEnabled',
isFluidTypographyEnabled
);
let fontSizeValue;
let newProps = props;
if (
isFluidTypographyEnabled &&
typeof style?.typography?.fontSize === 'string'
) {
fontSizeValue = getTypographyFontSizeValue(
{ size: style?.typography?.fontSize },
{ fluid: isFluidTypographyEnabled }
);
newProps = {
...props,
wrapperProps: {
...wrapperProps,
style: {
fontSize: fontSizeValue,
...wrapperProps?.style,
},
},
};
}
console.log( 'fontSizeValue 1', fontSizeValue );
// Only add inline styles if the block supports font sizes,
// doesn't skip serialization of font sizes,
// doesn't already have an inline font size,
Expand All @@ -235,16 +278,19 @@ const withFontSizeInlineStyles = createHigherOrderComponent(
! fontSize ||
style?.typography?.fontSize
) {
return <BlockListBlock { ...props } />;
return <BlockListBlock { ...newProps } />;
}

const fontSizeValue = getFontSize(
fontSizes,
fontSize,
style?.typography?.fontSize
).size;

const newProps = {
// @TODO Should be getFontSize()?
if ( ! isFluidTypographyEnabled ) {
fontSizeValue = getFontSize(
fontSizes,
fontSize,
style?.typography?.fontSize
).size;
}
console.log( 'fontSizeValue 2', fontSizeValue );
newProps = {
...props,
wrapperProps: {
...wrapperProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
export function getTypographyFontSizeValue( preset, typographySettings ) {
const { size: defaultSize } = preset;

if ( true !== typographySettings?.fluid ) {
if ( true !== typographySettings?.fluid || ! defaultSize ) {
return defaultSize;
}

Expand Down
3 changes: 3 additions & 0 deletions test/emptytheme/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"version": 2,
"settings": {
"appearanceTools": true,
"typography": {
"fluid": true
},
"layout": {
"contentSize": "840px",
"wideSize": "1100px"
Expand Down