Skip to content

Commit

Permalink
Switch typography block support to use ToolsPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Aug 11, 2021
1 parent 5a610d4 commit d4149ae
Show file tree
Hide file tree
Showing 21 changed files with 604 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
.components-font-appearance-control__select {
margin-bottom: 24px;

ul {
li {
color: $gray-900;
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 { default as __experimentalImageSizeControl } from './image-size-control';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
.block-editor-line-height-control {
margin-bottom: 24px;

input {
display: block;
max-width: 60px;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

.block-editor-text-decoration-control__buttons {
display: inline-flex;
margin-bottom: 24px;

.components-button.has-icon {
min-width: 24px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

.block-editor-text-transform-control__buttons {
display: inline-flex;
margin-bottom: 24px;

.components-button.has-icon {
min-width: 24px;
Expand Down
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,
},
} ),
} );
}
39 changes: 33 additions & 6 deletions packages/block-editor/src/hooks/font-family.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,10 @@ const getFontFamilyFromAttributeValue = ( fontFamilies, value ) => {
};

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

if ( isDisable ) {
return null;
}

const value = getFontFamilyFromAttributeValue(
fontFamilies,
Expand Down Expand Up @@ -89,3 +83,36 @@ export function useIsFontFamilyDisabled( { name } ) {
! hasBlockSupport( name, FONT_FAMILY_SUPPORT_KEY )
);
}

/**
* 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.style?.typography?.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.attributes Block's attributes.
* @param {Object} props.setAttributes Function to set block's attributes.
*/
export function resetFontFamily( { attributes = {}, setAttributes } ) {
const { style } = attributes;

setAttributes( {
style: cleanEmptyObject( {
...style,
typography: {
...style?.typography,
fontFamily: undefined,
},
} ),
} );
}
40 changes: 35 additions & 5 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 @@ -148,6 +143,41 @@ export function FontSizeEdit( props ) {
return <FontSizePicker onChange={ onChange } value={ fontSizeValue } />;
}

/**
* 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,
},
} ),
} );
}

/**
* Custom hook that checks if font-size settings have been disabled.
*
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 @@ -28,12 +28,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 @@ -69,3 +63,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 d4149ae

Please sign in to comment.