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

Block Support: Separate opt in for font style and weight options #26844

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Change font style or weight disabling in control
  • Loading branch information
aaronrobertshaw committed Nov 23, 2020
commit 779d1203b73521ad18fc2b90e214ab6da35babee
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import { __, sprintf } from '@wordpress/i18n';
*/
export default function FontAppearanceControl( props ) {
const {
disableFontStyles,
disableFontWeights,
onChange,
options: { fontStyles = [], fontWeights = [] },
value: { fontStyle, fontWeight },
} = props;
const hasStylesOrWeights = fontStyles.length > 0 || fontWeights.length > 0;
const hasStyles = !! fontStyles.length;
const hasWeights = !! fontWeights.length;
const hasStylesOrWeights = hasStyles || hasWeights;
const defaultOption = {
key: 'default',
name: __( 'Default' ),
Expand Down Expand Up @@ -81,18 +81,13 @@ export default function FontAppearanceControl( props ) {

// Map font styles and weights to select options.
const selectOptions = useMemo( () => {
if ( ! disableFontStyles && ! disableFontWeights ) {
if ( hasStyles && hasWeights ) {
return combineOptions();
}

return ! disableFontStyles ? styleOptions() : weightOptions();
return hasStyles ? styleOptions() : weightOptions();
}, [ props.options ] );

// Do not render control if both styles and weights were disabled.
if ( disableFontStyles && disableFontWeights ) {
return null;
}

// Find current selection by comparing font style & weight against options.
const currentSelection = selectOptions.find(
( option ) =>
Expand All @@ -102,11 +97,11 @@ export default function FontAppearanceControl( props ) {

// Adjusts field label in case either styles or weights are disabled.
const getLabel = () => {
if ( disableFontStyles ) {
if ( ! hasStyles ) {
return __( 'Font weight' );
}

if ( disableFontWeights ) {
if ( ! hasWeights ) {
return __( 'Font style' );
}

Expand Down
7 changes: 4 additions & 3 deletions packages/block-editor/src/hooks/font-appearance.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ export function FontAppearanceEdit( props ) {

return (
<FontAppearanceControl
disableFontStyles={ isFontStyleDisabled }
disableFontWeights={ isFontWeightDisabled }
onChange={ onChange }
options={ { fontStyles, fontWeights } }
options={ {
fontStyles: isFontStyleDisabled ? undefined : fontStyles,
fontWeights: isFontWeightDisabled ? undefined : fontWeights,
} }
value={ { fontStyle, fontWeight } }
/>
);
Expand Down