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

Typography: Switch to ToolsPanel for block support UI #33744

Merged
merged 3 commits into from
Oct 22, 2021
Merged
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
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;
}
ciampo marked this conversation as resolved.
Show resolved Hide resolved

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