From 39a14030753f32c989142a89bee79b20df20d1b1 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Fri, 22 Oct 2021 15:40:16 +1000 Subject: [PATCH] Reuse util function to keep consistent font appearance label --- .../font-appearance-control/index.js | 36 +++++++++++-------- packages/block-editor/src/hooks/typography.js | 11 +++++- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/packages/block-editor/src/components/font-appearance-control/index.js b/packages/block-editor/src/components/font-appearance-control/index.js index f238cd54d9aa8..d4c9dc85715d2 100644 --- a/packages/block-editor/src/components/font-appearance-control/index.js +++ b/packages/block-editor/src/components/font-appearance-control/index.js @@ -55,6 +55,26 @@ const FONT_WEIGHTS = [ }, ]; +/** + * Adjusts font appearance field label in case either font styles or weights + * are disabled. + * + * @param {boolean} hasFontStyles Whether font styles are enabled and present. + * @param {boolean} hasFontWeights Whether font weights are enabled and present. + * @return {string} A label representing what font appearance is being edited. + */ +export const getFontAppearanceLabel = ( hasFontStyles, hasFontWeights ) => { + if ( ! hasFontStyles ) { + return __( 'Font weight' ); + } + + if ( ! hasFontWeights ) { + return __( 'Font style' ); + } + + return __( 'Appearance' ); +}; + /** * Control to display unified font style and weight options. * @@ -70,6 +90,7 @@ export default function FontAppearanceControl( props ) { value: { fontStyle, fontWeight }, } = props; const hasStylesOrWeights = hasFontStyles || hasFontWeights; + const label = getFontAppearanceLabel( hasFontStyles, hasFontWeights ); const defaultOption = { key: 'default', name: __( 'Default' ), @@ -152,19 +173,6 @@ export default function FontAppearanceControl( props ) { option.style.fontWeight === fontWeight ) || selectOptions[ 0 ]; - // Adjusts field label in case either styles or weights are disabled. - const getLabel = () => { - if ( ! hasFontStyles ) { - return __( 'Font weight' ); - } - - if ( ! hasFontWeights ) { - return __( 'Font style' ); - } - - return __( 'Appearance' ); - }; - // Adjusts screen reader description based on styles or weights. const getDescribedBy = () => { if ( ! currentSelection ) { @@ -198,7 +206,7 @@ export default function FontAppearanceControl( props ) { hasStylesOrWeights && ( hasFontAppearanceValue( props ) } - label={ __( 'Appearance' ) } + label={ getFontAppearanceLabel( + hasFontStyles, + hasFontWeights + ) } onDeselect={ () => resetFontAppearance( props ) } isShownByDefault={ defaultControls?.fontAppearance } resetAllFilter={ ( newAttributes ) => ( {