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

Font Appearance: Improve consistency of label in Typography panel #35860

Merged
merged 1 commit into from
Oct 25, 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 @@ -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.
*
Expand All @@ -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' ),
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -198,7 +206,7 @@ export default function FontAppearanceControl( props ) {
hasStylesOrWeights && (
<CustomSelectControl
className="components-font-appearance-control"
label={ getLabel() }
label={ label }
describedBy={ getDescribedBy() }
options={ selectOptions }
value={ currentSelection }
Expand Down
11 changes: 10 additions & 1 deletion packages/block-editor/src/hooks/typography.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import InspectorControls from '../components/inspector-controls';
import { getFontAppearanceLabel } from '../components/font-appearance-control';

import {
LINE_HEIGHT_SUPPORT_KEY,
Expand All @@ -24,6 +25,8 @@ import {
hasFontAppearanceValue,
resetFontAppearance,
useIsFontAppearanceDisabled,
useIsFontStyleDisabled,
useIsFontWeightDisabled,
} from './font-appearance';
import {
FONT_FAMILY_SUPPORT_KEY,
Expand Down Expand Up @@ -83,6 +86,9 @@ export function TypographyPanel( props ) {
const isTextTransformDisabled = useIsTextTransformDisabled( props );
const isLetterSpacingDisabled = useIsLetterSpacingDisabled( props );

const hasFontStyles = ! useIsFontStyleDisabled( props );
const hasFontWeights = ! useIsFontWeightDisabled( props );

const isDisabled = useIsTypographyDisabled( props );
const isSupported = hasTypographySupport( props.name );

Expand Down Expand Up @@ -147,7 +153,10 @@ export function TypographyPanel( props ) {
<ToolsPanelItem
className="single-column"
hasValue={ () => hasFontAppearanceValue( props ) }
label={ __( 'Appearance' ) }
label={ getFontAppearanceLabel(
hasFontStyles,
hasFontWeights
) }
onDeselect={ () => resetFontAppearance( props ) }
isShownByDefault={ defaultControls?.fontAppearance }
resetAllFilter={ ( newAttributes ) => ( {
Expand Down