Skip to content

Commit

Permalink
Typography: Switch to ToolsPanel for block support UI (#33744)
Browse files Browse the repository at this point in the history
* Switch typography block support to use ToolsPanel
* Update font size picker reset e2e tests for ToolsPanel
  • Loading branch information
aaronrobertshaw authored Oct 22, 2021
1 parent ff468f2 commit 0d5bd30
Show file tree
Hide file tree
Showing 17 changed files with 444 additions and 119 deletions.
5 changes: 5 additions & 0 deletions packages/block-editor/src/components/block-inspector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ const BlockInspectorSingleBlock = ( {
</div>
) }
<InspectorControls.Slot bubblesVirtually={ bubblesVirtually } />
<InspectorControls.Slot
__experimentalGroup="typography"
bubblesVirtually={ bubblesVirtually }
label={ __( 'Typography' ) }
/>
<InspectorControls.Slot
__experimentalGroup="dimensions"
bubblesVirtually={ bubblesVirtually }
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export { default as __experimentalDuotoneControl } from './duotone-control';
export { default as __experimentalFontAppearanceControl } from './font-appearance-control';
export { default as __experimentalFontFamilyControl } from './font-family';
export { default as __experimentalLetterSpacingControl } from './letter-spacing-control';
export { default as __experimentalTextDecorationControl } from './text-decoration-control';
export { default as __experimentalTextTransformControl } from './text-transform-control';
export { default as __experimentalColorGradientControl } from './colors-gradients/control';
export { default as __experimentalPanelColorGradientSettings } from './colors-gradients/panel-color-gradient-settings';
export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ const InspectorControlsAdvanced = createSlotFill( 'InspectorAdvancedControls' );
const InspectorControlsDimensions = createSlotFill(
'InspectorControlsDimensions'
);
const InspectorControlsTypography = createSlotFill(
'InspectorControlsTypography'
);

const groups = {
default: InspectorControlsDefault,
advanced: InspectorControlsAdvanced,
dimensions: InspectorControlsDimensions,
typography: InspectorControlsTypography,
};

export default groups;

This file was deleted.

This file was deleted.

41 changes: 36 additions & 5 deletions packages/block-editor/src/hooks/font-appearance.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ export function FontAppearanceEdit( props ) {
const hasFontStyles = ! useIsFontStyleDisabled( props );
const hasFontWeights = ! useIsFontWeightDisabled( props );

if ( ! hasFontStyles && ! hasFontWeights ) {
return null;
}

const onChange = ( newStyles ) => {
setAttributes( {
style: cleanEmptyObject( {
Expand All @@ -54,7 +50,6 @@ export function FontAppearanceEdit( props ) {
};

const fontStyle = style?.typography?.fontStyle;

const fontWeight = style?.typography?.fontWeight;

return (
Expand Down Expand Up @@ -112,3 +107,39 @@ export function useIsFontAppearanceDisabled( props ) {

return stylesDisabled && weightsDisabled;
}

/**
* Checks if there is either a font style or weight value set within the
* typography styles.
*
* @param {Object} props Block props.
* @return {boolean} Whether or not the block has a font style or weight.
*/
export function hasFontAppearanceValue( props ) {
const { fontStyle, fontWeight } = props.attributes.style?.typography || {};
return !! fontStyle || !! fontWeight;
}

/**
* Resets the font style and weight block support attributes. This can be used
* when disabling the font appearance support controls for a block via a
* progressive discovery panel.
*
* @param {Object} props Block props.
* @param {Object} props.attributes Block's attributes.
* @param {Object} props.setAttributes Function to set block's attributes.
*/
export function resetFontAppearance( { attributes = {}, setAttributes } ) {
const { style } = attributes;

setAttributes( {
style: cleanEmptyObject( {
...style,
typography: {
...style?.typography,
fontStyle: undefined,
fontWeight: undefined,
},
} ),
} );
}
40 changes: 28 additions & 12 deletions packages/block-editor/src/hooks/font-family.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export const FONT_FAMILY_SUPPORT_KEY = 'typography.__experimentalFontFamily';
* Filters registered block settings, extending attributes to include
* the `fontFamily` attribute.
*
* @param {Object} settings Original block settings
* @return {Object} Filtered block settings
* @param {Object} settings Original block settings
* @return {Object} Filtered block settings
*/
function addAttributes( settings ) {
if ( ! hasBlockSupport( settings, FONT_FAMILY_SUPPORT_KEY ) ) {
Expand All @@ -45,10 +45,10 @@ function addAttributes( settings ) {
/**
* Override props assigned to save component to inject font family.
*
* @param {Object} props Additional props applied to save element
* @param {Object} blockType Block type
* @param {Object} attributes Block attributes
* @return {Object} Filtered props applied to save element
* @param {Object} props Additional props applied to save element
* @param {Object} blockType Block type
* @param {Object} attributes Block attributes
* @return {Object} Filtered props applied to save element
*/
function addSaveProps( props, blockType, attributes ) {
if ( ! hasBlockSupport( blockType, FONT_FAMILY_SUPPORT_KEY ) ) {
Expand Down Expand Up @@ -103,16 +103,10 @@ function addEditProps( settings ) {
}

export function FontFamilyEdit( {
name,
setAttributes,
attributes: { fontFamily },
} ) {
const fontFamilies = useSetting( 'typography.fontFamilies' );
const isDisable = useIsFontFamilyDisabled( { name } );

if ( isDisable ) {
return null;
}

const value = find( fontFamilies, ( { slug } ) => fontFamily === slug )
?.fontFamily;
Expand Down Expand Up @@ -152,6 +146,28 @@ export function useIsFontFamilyDisabled( { name } ) {
);
}

/**
* Checks if there is a current value set for the font family block support.
*
* @param {Object} props Block props.
* @return {boolean} Whether or not the block has a font family value set.
*/
export function hasFontFamilyValue( props ) {
return !! props.attributes.fontFamily;
}

/**
* Resets the font family block support attribute. This can be used when
* disabling the font family support controls for a block via a progressive
* discovery panel.
*
* @param {Object} props Block props.
* @param {Object} props.setAttributes Function to set block's attributes.
*/
export function resetFontFamily( { setAttributes } ) {
setAttributes( { fontFamily: undefined } );
}

addFilter(
'blocks.registerBlockType',
'core/fontFamily/addAttribute',
Expand Down
48 changes: 42 additions & 6 deletions packages/block-editor/src/hooks/font-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export function FontSizeEdit( props ) {
attributes: { fontSize, style },
setAttributes,
} = props;
const isDisabled = useIsFontSizeDisabled( props );
const fontSizes = useSetting( 'typography.fontSizes' );

const onChange = ( value ) => {
Expand All @@ -132,10 +131,6 @@ export function FontSizeEdit( props ) {
} );
};

if ( isDisabled ) {
return null;
}

const fontSizeObject = getFontSize(
fontSizes,
fontSize,
Expand All @@ -145,7 +140,48 @@ export function FontSizeEdit( props ) {
const fontSizeValue =
fontSizeObject?.size || style?.typography?.fontSize || fontSize;

return <FontSizePicker onChange={ onChange } value={ fontSizeValue } />;
return (
<FontSizePicker
onChange={ onChange }
value={ fontSizeValue }
withReset={ false }
/>
);
}

/**
* Checks if there is a current value set for the font size block support.
*
* @param {Object} props Block props.
* @return {boolean} Whether or not the block has a font size value set.
*/
export function hasFontSizeValue( props ) {
const { fontSize, style } = props.attributes;
return !! fontSize || !! style?.typography?.fontSize;
}

/**
* Resets the font size block support attribute. This can be used when
* disabling the font size support controls for a block via a progressive
* discovery panel.
*
* @param {Object} props Block props.
* @param {Object} props.attributes Block's attributes.
* @param {Object} props.setAttributes Function to set block's attributes.
*/
export function resetFontSize( { attributes = {}, setAttributes } ) {
const { style } = attributes;

setAttributes( {
fontSize: undefined,
style: cleanEmptyObject( {
...style,
typography: {
...style?.typography,
fontSize: undefined,
},
} ),
} );
}

/**
Expand Down
39 changes: 33 additions & 6 deletions packages/block-editor/src/hooks/letter-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ export function LetterSpacingEdit( props ) {
setAttributes,
} = props;

const isDisabled = useIsLetterSpacingDisabled( props );

if ( isDisabled ) {
return null;
}

function onChange( newSpacing ) {
setAttributes( {
style: cleanEmptyObject( {
Expand Down Expand Up @@ -70,3 +64,36 @@ export function useIsLetterSpacingDisabled( { name: blockName } = {} ) {

return notSupported || ! hasLetterSpacing;
}

/**
* Checks if there is a current value set for the letter spacing block support.
*
* @param {Object} props Block props.
* @return {boolean} Whether or not the block has a letter spacing set.
*/
export function hasLetterSpacingValue( props ) {
return !! props.attributes.style?.typography?.letterSpacing;
}

/**
* Resets the letter spacing block support attribute. This can be used when
* disabling the letter spacing support controls for a block via a progressive
* discovery panel.
*
* @param {Object} props Block props.
* @param {Object} props.attributes Block's attributes.
* @param {Object} props.setAttributes Function to set block's attributes.
*/
export function resetLetterSpacing( { attributes = {}, setAttributes } ) {
const { style } = attributes;

setAttributes( {
style: cleanEmptyObject( {
...style,
typography: {
...style?.typography,
letterSpacing: undefined,
},
} ),
} );
}
44 changes: 36 additions & 8 deletions packages/block-editor/src/hooks/line-height.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ export const LINE_HEIGHT_SUPPORT_KEY = 'typography.lineHeight';
export function LineHeightEdit( props ) {
const {
attributes: { style },
setAttributes,
} = props;
const isDisabled = useIsLineHeightDisabled( props );

if ( isDisabled ) {
return null;
}

const onChange = ( newLineHeightValue ) => {
const newStyle = {
Expand All @@ -37,9 +33,8 @@ export function LineHeightEdit( props ) {
lineHeight: newLineHeightValue,
},
};
props.setAttributes( {
style: cleanEmptyObject( newStyle ),
} );

setAttributes( { style: cleanEmptyObject( newStyle ) } );
};
return (
<LineHeightControl
Expand All @@ -62,3 +57,36 @@ export function useIsLineHeightDisabled( { name: blockName } = {} ) {
! hasBlockSupport( blockName, LINE_HEIGHT_SUPPORT_KEY ) || isDisabled
);
}

/**
* Checks if there is a current value set for the line height block support.
*
* @param {Object} props Block props.
* @return {boolean} Whether or not the block has a line height value set.
*/
export function hasLineHeightValue( props ) {
return !! props.attributes.style?.typography?.lineHeight;
}

/**
* Resets the line height block support attribute. This can be used when
* disabling the line height support controls for a block via a progressive
* discovery panel.
*
* @param {Object} props Block props.
* @param {Object} props.attributes Block's attributes.
* @param {Object} props.setAttributes Function to set block's attributes.
*/
export function resetLineHeight( { attributes = {}, setAttributes } ) {
const { style } = attributes;

setAttributes( {
style: cleanEmptyObject( {
...style,
typography: {
...style?.typography,
lineHeight: undefined,
},
} ),
} );
}
Loading

0 comments on commit 0d5bd30

Please sign in to comment.